KIsolateAccess mixin

Mixin that gives any class convenient, named access to isolate operations.

Mix this into any class — a service, a controller, a repository — and call isolateRun or isolateSpawn directly without importing or referencing KIsolate or KIsolateContinuous at the call site.

class ImageProcessor with SmartIsolateAccess {
  Future<Uint8List> compress(Uint8List bytes) async {
    return isolateRun<Uint8List, double, Uint8List>(
      (data, emit) async {
        emit(0.5);
        return _doCompress(data);
      },
      bytes,
      onProgress: (pct) => print('${(pct * 100).round()}%'),
    );
  }
}

class HeavySearchService with SmartIsolateAccess {
  late final SmartIsolateContinuous<String, List<String>> _worker;

  Future<void> init() async {
    _worker = await isolateSpawn(
      (register) async {
        final index = await buildIndex(); // heavy init, runs once
        register((query, respond) => respond(index.search(query)));
      },
    );
  }

  Future<List<String>> search(String q) => _worker.execute(q);
  void dispose() => _worker.dispose();
}

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

isolateRun<TArg, TProgress, TResult>(Future<TResult> task(TArg arg, void emit(TProgress)), TArg arg, {void onProgress(TProgress)?}) Future<TResult>
Runs a one-shot task in a new isolate. Equivalent to KIsolate.run.
isolateSpawn<TArg, TResult>(Future<void> initialize(void registerHandler(void (TArg, void (TResult)))), {RootIsolateToken? rootIsolateToken, int maxQueueSize = kDefaultMaxQueueSize, int agingThreshold = 10}) Future<KIsolateContinuous<TArg, TResult>>
Spawns a persistent isolate worker. Equivalent to KIsolateContinuous.spawn.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited