initIsolate function

Future<SpawnedIsolate> initIsolate(
  1. String name,
  2. dynamic callback(
    1. List<Object?>
    ), {
  3. Function? onListenCallback,
  4. List? callbackArgs,
  5. bool verbose = false,
})

Implementation

Future<SpawnedIsolate> initIsolate(
	String name,
	Function(List<Object?>) callback,
	{
		Function? onListenCallback,
		List<dynamic>? callbackArgs,
		bool verbose=false
	}) async {

	Isolate isolate;
	var isolateName = '[isolate][$name]';
	var completer = Completer<SpawnedIsolate>();
	var receivePort = ReceivePort();
	var isolateParts = <String, dynamic>{};

	isolate = await Isolate.spawn(callback, [isolateName, receivePort.sendPort, callbackArgs]);
	isolateParts['isolate'] = isolate;
	isolateParts['receiver'] = receivePort;
	if(verbose){
		pretifyOutput('$isolateName ----- started ---- ', color: AqColor.cyann);
	}

	receivePort.listen((data) async {


		if(data is SendPort){
			isolateParts['sendPort'] = data;
			completer.complete(SpawnedIsolate(
				isolate: isolate,
				sendPort: data,
				receivePort: receivePort
			));
		} else if(data == 'done'){
			receivePort.close();
			if(verbose){
				pretifyOutput('$isolateName ------ ended -----', color: AqColor.red);
			}
		} else {
			if(verbose){
				pretifyOutput('$isolateName: $data');
			}

			if(onListenCallback != null){
				await onListenCallback(receivePort, data);
			}
		}
	});

	return completer.future;
}