isolate_channel
v0.6.1Communication channels for isolates based on Flutter's platform channels
Архив пакета: https://pubdev.letsnova.ru/api/archives/isolate_channel/0.6.1.tar.gz
dart pub add isolate_channelREADME
isolate_channel
Communication channels for isolates based on Flutter's platform channels
Features
- Helper functions to spawn, setup, and connect to isolates
- Method channels for making method calls to an isolate and receiving the result
- Event channels for receiving streamed events from an isolate
Usage
In the parent isolate, use the spawnIsolate function to spawn a new isolate and connect to it
final connection = await spawnIsolate(isolateEntryPoint);
In the newly spawned isolate, use the setupIsolate function to set up the isolate for channel communication
void isolateEntryPoint(SendPort? send) {
final connection = setupIsolate(send);
}
Now create communication channels in both isolates
final methodChannel = IsolateMethodChannel('method_channel', connection);
final eventChannel = IsolateEventChannel('event_channel', connection);
Set up handlers for the channels in the newly spawned isolate
methodChannel.setMethodCallHandler((call) {
switch (call.method) {
case 'example_method':
print(call.arguments);
return 'World!';
default:
return call.notImplemented();
}
});
eventChannel.setStreamHandler(
IsolateStreamHandler.inline(
onListen: (arguments, sink) {
sink.success(arguments);
sink.error(
code: 'error',
message: 'Something went wrong',
details: 1234,
);
sink.endOfStream();
},
onCancel: (arguments) => print('Canceled: $arguments'),
),
);
Now you can use the channels to communicate
final result = await methodChannel.invokeMethod('example_method', 'Hello');
final stream = eventChannel.receiveBroadcastStream();
And close the connection when you're done
connection.close();
Connect to a running isolate
Pass an isolate's send port to the connectToIsolate function to connect to it. A send port can be retrieved from an IsolateNameServer.
final connection = await connectToIsolate(send);
История изменений
0.6.1
- Fixes README
0.6.0
- Adds
onSendPortReadyinsetupIsolateto support headless restarts - The
IsolateEntryPointandsetupIsolatefunctions now accept a nullablesendparameter to support headless restarts - The
connectToIsolatefunction now supportsonExitandonErrorcallback setup
0.5.0
- Exposes the
spawnIsolateConnectionfunction that allows connecting to custom isolates - Removes the
spawnparameters from thespawnIsolateandspawnUriIsolatefunctions
0.4.2
- Updates README to reflect that
connectToIsolatereturns aFuture
0.4.1
- Loosens constraint on
meta
0.4.0
- Allows passing an
IsolateSpawnerto support custom isolate implementations such asFlutterIsolate - Makes
connectToIsolatereturn aFutureso that connection failure can be handled
0.3.0
- Removes unnecessary type parameters from spawn methods
0.2.2+1
- Adds README badges
0.2.2
- Handles more edge cases in
IsolateEventChannel
0.2.1
- Does not cancel handler subscription when
onCancelis called inIsolateEventChannel
0.2.0
- BREAKING: Refactors
IsolateException.notImplemented(...)intocall.notImplemented() - Supports communication between isolates spawned with
Isolate.spawnUri - EventChannel optimizations
0.1.1
- Handles exceptions thrown in method call handlers
0.1.0
- Initial release
