Skip to content

Commit

Permalink
🐛 FIx BrowserExtensionRPC ports release.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chralu committed May 30, 2024
1 parent e8b6f65 commit 5396597
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/infrastructure/rpc/browser_extension_aws.nonweb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ class BrowserExtensionAWS {

Future<void> run() async {}

void stop() {}
Future<void> stop() async {}
}
35 changes: 24 additions & 11 deletions lib/infrastructure/rpc/browser_extension_aws.web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:stream_channel/stream_channel.dart';
class BrowserExtensionAWS {
BrowserExtensionAWS() {
print('[AWCBrowserExtension] Init');
_interopConnectionReceived = allowInterop(_connectionReceived);
}
static const logName = 'Browser RPC Server';

Expand All @@ -19,16 +20,18 @@ class BrowserExtensionAWS {

final Set<AWCJsonRPCServer> _peerServers = {};

void _connectionReceived(port) {
late Function(BrowserExtensionPort port) _interopConnectionReceived;
void _connectionReceived(BrowserExtensionPort port) {
print('[AWCBrowserExtension] external connection received ');
final channel = BrowserExtensionMessagePortStreamChannel(port: port);
final peerServer = AWCJsonRPCServer(channel.cast<String>());
_peerServers.add(peerServer);

port.onDisconnect.addListener((_, __) {
port.onDisconnect.addListener(allowInterop((_) async {
print('[AWCBrowserExtension] external connection closed ');
await peerServer.close();
_peerServers.remove(peerServer);
});
}));

unawaited(peerServer.listen());
}
Expand All @@ -42,9 +45,8 @@ class BrowserExtensionAWS {

print('[AWCBrowserExtension] Starting');

onConnectExternal.addListener(allowInterop(_connectionReceived));
onConnectExternal.addListener(_interopConnectionReceived);
_isRunning = true;
// sendMessage('extensionPopupReady');
},
(error, stack) {
print(
Expand All @@ -53,12 +55,12 @@ class BrowserExtensionAWS {
},
);

void stop() {
Future<void> stop() async {
print('[AWCBrowserExtension] Stopping');
_isRunning = false;
onConnectExternal.removeListener(allowInterop(_connectionReceived));
onConnectExternal.removeListener(_interopConnectionReceived);
for (final peerServer in _peerServers) {
peerServer.close();
await peerServer.close();
}
_peerServers.clear();
}
Expand All @@ -78,18 +80,29 @@ class BrowserExtensionMessagePortStreamChannel
print('Wallet message received done $message');
}),
);
print('Wallet Init WebMessage PortStreamchannel 1 ');

_out.stream.listen((event) {
_out.onCancel = close;

_outSubscription = _out.stream.listen((event) {
print('Wallet response sent $event');
port.postMessage(event);
});
}

Future<void> close() async {
print('Wallet releases port');
_out.onCancel = null;
await _outSubscription.cancel();
await _out.close();
port.disconnect();

_out.sink.done.whenComplete(port.disconnect);
await _in.close();
print('Wallet port release done');
}

final BrowserExtensionPort port;
final _in = StreamController<String>(sync: true);
late StreamSubscription _outSubscription;
final _out = StreamController<String>(sync: true);

@override
Expand Down
8 changes: 4 additions & 4 deletions lib/ui/views/rpc_command_receiver/rpc_command_receiver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ class _RPCCommandReceiverState extends ConsumerState<RPCCommandReceiver> {
_disposeCommandDispatcher();
}

void _disposeWebsocketRPC() {
Future<void> _disposeWebsocketRPC() async {
print('Dispose websocket RPC');
sl.get<ArchethicWebsocketRPCServer>().stop();
await sl.get<ArchethicWebsocketRPCServer>().stop();
}

void _disposeBrowserExtensionRPC() {
Future<void> _disposeBrowserExtensionRPC() async {
print('Dispose Browser extension RPC');
_browserExtensionAWS?.stop();
await _browserExtensionAWS?.stop();
}

void _disposeCommandDispatcher() {
Expand Down

0 comments on commit 5396597

Please sign in to comment.