Created with <3 with dartpad.dev.
Last active
September 18, 2023 16:50
-
-
Save arxdeus/ed02e520963764992196eece0cd85b1a to your computer and use it in GitHub Desktop.
'closure_1 as closure_2' problem
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
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