Skip to content

Instantly share code, notes, and snippets.

@kitsuniru
Last active September 18, 2023 16:50
Show Gist options
  • Save kitsuniru/ed02e520963764992196eece0cd85b1a to your computer and use it in GitHub Desktop.
Save kitsuniru/ed02e520963764992196eece0cd85b1a to your computer and use it in GitHub Desktop.
'closure_1 as closure_2' problem

'closure_1 as closure_2' problem

Created with <3 with dartpad.dev.

import 'dart:isolate';
void main() async {
final toMainIsolate = ReceivePort();
final stream = toMainIsolate.asBroadcastStream();
// Isolate
final isolate = await Isolate.spawn(isolateWorker, toMainIsolate.sendPort);
final SendPort sendPort = await stream.first;
sendPort.send((TargetAction action) => action.handleAction());
}
class SomeReceiver {
final TargetAction targetAction = TargetAction();
void receiveAction(void Function(Object) handleAction) => handleAction(targetAction);
}
class TargetAction {
void handleAction() => print('HOORAY');
}
void isolateWorker(SendPort toMainIsolate) async {
final fromMainIsolate = ReceivePort();
final stream = fromMainIsolate.asBroadcastStream();
toMainIsolate.send(fromMainIsolate.sendPort);
final receiver = SomeReceiver();
final action = await stream.first;
receiver.receiveAction(action);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment