-
-
Notifications
You must be signed in to change notification settings - Fork 273
feat(gotrue, supabase, supabase_flutter, realtime_client): Use web package to access web APIs #1135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f70070e
fix: Use web package to access web APIs
dshukertjr b518151
widen the version constraints
dshukertjr 5aebb19
bumpup the test flutter version
dshukertjr 49c963b
update the minimum test version to v3.16.x
dshukertjr 51bc5e2
update the minimum test version to v3.19.x
dshukertjr 43b2ae6
update the version constraints of supabase and supabase_flutter accod…
dshukertjr edc7348
remove analyzer issue
dshukertjr e8fc60e
update dart.library.html to the new one
dshukertjr c9180aa
minor fix
dshukertjr 50296cc
update comment
dshukertjr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,28 +1,37 @@ | ||
| import 'dart:async'; | ||
| import 'dart:convert'; | ||
| import 'dart:html' as html; | ||
| import 'dart:js_util' as js_util; | ||
| import 'dart:js_interop'; | ||
|
|
||
| import 'package:gotrue/src/types/types.dart'; | ||
| import 'package:logging/logging.dart'; | ||
| import 'package:web/web.dart' as web; | ||
|
|
||
| final _log = Logger('supabase.auth'); | ||
|
|
||
| BroadcastChannel getBroadcastChannel(String broadcastKey) { | ||
| final broadcast = html.BroadcastChannel(broadcastKey); | ||
| return ( | ||
| onMessage: broadcast.onMessage.map((event) { | ||
| final dataMap = js_util.dartify(event.data); | ||
| final broadcast = web.BroadcastChannel(broadcastKey); | ||
| final controller = StreamController<Map<String, dynamic>>(); | ||
|
|
||
| broadcast.addEventListener( | ||
| 'message', | ||
| (web.Event event) { | ||
| if (event is web.MessageEvent) { | ||
| final dataMap = event.data.dartify(); | ||
| controller.add(json.decode(json.encode(dataMap))); | ||
| } | ||
| } as web.EventListener, | ||
| ); | ||
|
|
||
| // some parts have the wrong map type. This is an easy workaround and | ||
| // should be efficient enough for the small session and user data | ||
| return json.decode(json.encode(dataMap)); | ||
| }), | ||
| return ( | ||
| onMessage: controller.stream, | ||
| postMessage: (message) { | ||
| _log.finest('Broadcasting message: $message'); | ||
| _log.fine('Broadcasting event: ${message['event']}'); | ||
| final jsMessage = js_util.jsify(message); | ||
| broadcast.postMessage(jsMessage); | ||
| broadcast.postMessage(message.jsify() as JSAny); | ||
| }, | ||
| close: () { | ||
| broadcast.close(); | ||
| controller.close(); | ||
| }, | ||
| close: broadcast.close, | ||
| ); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,57 @@ | ||
| @TestOn('browser') | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:gotrue/src/broadcast_web.dart'; | ||
| import 'package:gotrue/src/types/types.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() { | ||
| group('getBroadcastChannel', () { | ||
| late BroadcastChannel channel1; | ||
| late BroadcastChannel channel2; | ||
|
|
||
| setUp(() { | ||
| channel1 = getBroadcastChannel('test-channel'); | ||
| channel2 = getBroadcastChannel('test-channel'); | ||
| }); | ||
|
|
||
| tearDown(() { | ||
| channel1.close(); | ||
| channel2.close(); | ||
| }); | ||
|
|
||
| test('can send and receive messages between channels', () async { | ||
| final completer = Completer<Map<String, dynamic>>(); | ||
|
|
||
| // Listen for messages on channel2 | ||
| final subscription = channel2.onMessage.listen((message) { | ||
| completer.complete(message); | ||
| }); | ||
|
|
||
| // Send message from channel1 | ||
| final testMessage = { | ||
| 'event': 'test-event', | ||
| 'data': {'foo': 'bar'} | ||
| }; | ||
| channel1.postMessage(testMessage); | ||
|
|
||
| // Wait for the message to be received | ||
| final receivedMessage = await completer.future; | ||
|
|
||
| expect(receivedMessage['event'], equals('test-event')); | ||
| expect(receivedMessage['data']['foo'], equals('bar')); | ||
|
|
||
| await subscription.cancel(); | ||
| }); | ||
|
|
||
| test('can close channels', () async { | ||
| channel1.close(); | ||
|
|
||
| // Verify that sending messages after closing throws | ||
| expect( | ||
| () => channel1.postMessage({'event': 'test'}), | ||
| throwsA(anything), | ||
| ); | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.