From 4f55cb76c6142e1632c4932ebb9fcb0ea967a851 Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Sun, 1 Dec 2024 07:16:18 +0900 Subject: [PATCH] fix: Add conditional export to support --wasm build (#3363) * fix: Add conditional export to support --wasm build * Add stubs for extension --------- Co-authored-by: Simon Binder --- drift/lib/src/web/channel_legacy_dummy.dart | 27 +++++++++++++++++++++ drift/lib/web.dart | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 drift/lib/src/web/channel_legacy_dummy.dart diff --git a/drift/lib/src/web/channel_legacy_dummy.dart b/drift/lib/src/web/channel_legacy_dummy.dart new file mode 100644 index 000000000..56e9fe1c4 --- /dev/null +++ b/drift/lib/src/web/channel_legacy_dummy.dart @@ -0,0 +1,27 @@ +import 'package:stream_channel/stream_channel.dart'; + +/// Extension to transform a raw `MessagePort` from web workers into a Dart +/// [StreamChannel]. +extension PortToChannel on dynamic { + /// Converts this port to a two-way communication channel, exposed as a + /// [StreamChannel]. + /// + /// This can be used to implement a remote database connection over service + /// workers. + /// + /// The [explicitClose] parameter can be used to control whether a close + /// message should be sent through the channel when it is closed. This will + /// cause it to be closed on the other end as well. Note that this is not a + /// reliable way of determining channel closures though, as there is no event + /// for channels being closed due to a tab or worker being closed. + /// Both "ends" of a JS channel calling [channel] on their part must use the + /// value for [explicitClose]. + @Deprecated( + 'Please use MessagePorts from package:web instead of those from dart:html. ' + 'This extension will be removed from drift once `dart:html` is removed ' + 'from the SDK.', + ) + StreamChannel channel({bool explicitClose = false}) { + throw 'If this import was resolved, dart:html is not available.'; + } +} diff --git a/drift/lib/web.dart b/drift/lib/web.dart index c1003d3ad..4be11185f 100644 --- a/drift/lib/web.dart +++ b/drift/lib/web.dart @@ -12,5 +12,6 @@ library drift.web; export 'src/web/sql_js.dart'; export 'src/web/storage.dart' hide CustomSchemaVersionSave; export 'src/web/web_db.dart'; -export 'src/web/channel_legacy.dart'; +export 'src/web/channel_legacy_dummy.dart' + if (dart.library.html) 'src/web/channel_legacy.dart'; export 'src/web/channel_new.dart';