-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Broadcast updates across tabs if needed (#74)
* Share database updates across tabs * Avoid unecessary toList() * Remove unused constructor * Also use events locally * Explain duplicate add
- Loading branch information
Showing
4 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/sqlite_async/lib/src/web/database/broadcast_updates.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import 'dart:js_interop'; | ||
|
||
import 'package:sqlite_async/sqlite_async.dart'; | ||
import 'package:web/web.dart' as web; | ||
|
||
/// Utility to share received [UpdateNotification]s with other tabs using | ||
/// broadcast channels. | ||
class BroadcastUpdates { | ||
final web.BroadcastChannel _channel; | ||
|
||
BroadcastUpdates(String name) | ||
: _channel = web.BroadcastChannel('sqlite3_async_updates/$name'); | ||
|
||
Stream<UpdateNotification> get updates { | ||
return web.EventStreamProviders.messageEvent | ||
.forTarget(_channel) | ||
.map((event) { | ||
final data = event.data as _BroadcastMessage; | ||
if (data.a == 0) { | ||
final payload = data.b as JSArray<JSString>; | ||
return UpdateNotification( | ||
payload.toDart.map((e) => e.toDart).toSet()); | ||
} else { | ||
return null; | ||
} | ||
}) | ||
.where((e) => e != null) | ||
.cast(); | ||
} | ||
|
||
void send(UpdateNotification notification) { | ||
_channel.postMessage(_BroadcastMessage.notifications(notification)); | ||
} | ||
} | ||
|
||
@JS() | ||
@anonymous | ||
extension type _BroadcastMessage._(JSObject _) implements JSObject { | ||
external int get a; | ||
external JSAny get b; | ||
|
||
external factory _BroadcastMessage({required int a, required JSAny b}); | ||
|
||
factory _BroadcastMessage.notifications(UpdateNotification notification) { | ||
return _BroadcastMessage( | ||
a: 0, | ||
b: notification.tables.map((e) => e.toJS).toList().toJS, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters