IsolateWorkerBase class abstract
Base class for isolate-backed worker objects.
Provides the standard boilerplate shared across single-channel isolate workers:
- An IsolateRpcClient for send/receive communication
- An isReady flag
- A sendRequest guard that rejects calls before initialization
- A dispose implementation that kills the isolate and closes the port
Lifecycle safety
dispose calls IsolateRpcClient.failAllAndDispose synchronously
(which internally calls isolate.kill()) before any await. This ensures
the background isolate is terminated immediately even if the caller does not
await the returned future, preventing use-after-free on native TFLite memory.
Subclasses must:
- Call
await initWorker(spawnFn)during their own initialization to spawn the isolate and complete the handshake. - Override workerDisposeOp to specify the dispose operation name (if any).
- Override
dispose()and callawait super.dispose()when cleaning up.
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isReady → bool
-
Returns true if the worker has been initialized and is ready for requests.
no setter
- rpc → IsolateRpcClient
-
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- workerDisposeOp → String?
-
The name of the operation to send to the isolate when disposing.
no setter
Methods
-
dispose(
) → Future< void> - Disposes the worker: fails pending requests, sends workerDisposeOp to the isolate (if non-null), kills the isolate, and closes ports.
-
disposeGracefully(
{Duration timeout = const Duration(seconds: 5)}) → Future< void> - Gracefully disposes the worker: sends workerDisposeOp to the isolate as a request and awaits its acknowledgement — giving the isolate a chance to free native resources — before force-killing it via dispose.
-
initWorker(
Future< Isolate> spawnFn(SendPort sendPort), {Duration timeout = const Duration(seconds: 30), String timeoutMessage = 'Worker initialization timed out', void onResponse(dynamic)?, bool markReady = true}) → Future<void> - Spawns the background isolate and completes the handshake.
-
markInitialized(
) → void - Marks the worker as initialized and ready for requests.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
sendRequest<
T> (String operation, Map< String, dynamic> params) → Future<T> - Sends a typed request to the background isolate.
-
sendRequestUnchecked<
T> (String operation, Map< String, dynamic> params) → Future<T> - Sends a typed request directly to the background isolate, bypassing the isReady guard.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited