executorservices 1.0.6 copy "executorservices: ^1.0.6" to clipboard
executorservices: ^1.0.6 copied to clipboard

outdated

A Dart package providing services that allow you to execute dart code in different executors, ex - isolates.

example/example.dart

import "dart:async";
import "dart:io";
import "dart:math";

import "package:executorservices/executorservices.dart";
import "package:http/http.dart" as http;

import "utils.dart";

void main() {
  final executorService = ExecutorService.newSingleExecutor();

  executorService
      .submit(
        GetJsonFromUrlTask("https://jsonplaceholder.typicode.com/posts/1"),
      )
      .then((data) => printRunningIsolate("GetJsonFromUrlTask:result\n$data"));

  executorService
      .submitAction(onShotFunction)
      .then((_) => printRunningIsolate("onShotFunction:done"));

  executorService.submitCallable(getRandomNumber, 10).then(
        (number) => printRunningIsolate(
          "getRandomNumber:result:$number",
        ),
      );

  executorService.submitCallable(getRandomNumberSync, 100).then(
        (number) => printRunningIsolate(
      "getRandomNumberSync:result:$number",
    ),
  );

  executorService
      .submitFunction2(getFullName, "Darel", "Bitsy")
      .then((result) => printRunningIsolate("getFullName:result:$result"));

  executorService
      .submitFunction3(greet, "Darel", "Bitsy", "bdeg")
      .then((result) => printRunningIsolate("greet:result:$result"));
}

void onShotFunction() {
  printRunningIsolate("onShotFunction:enter");
  sleep(Duration(seconds: 3));
}

Future<int> getRandomNumber(final int max) async {
  printRunningIsolate("getRandomNumber:enter");
  await Future.delayed(Duration(seconds: 2));
  return Random.secure().nextInt(max);
}

int getRandomNumberSync(final int max) {
  printRunningIsolate("getRandomNumber:enter");
  sleep(Duration(seconds: 2));
  return Random.secure().nextInt(max);
}

Future<String> getFullName(final String firstName, final String lastName) {
  printRunningIsolate("getRandomNumber");
  return Future.delayed(Duration(seconds: 1))
      .then((_) => "$firstName $lastName");
}

Future<String> greet(
  final String firstName,
  final String lastName,
  final String surname,
) {
  printRunningIsolate("greet");
  return Future.delayed(Duration(seconds: 2))
      .then((_) => "Hello $firstName $lastName $surname");
}

class GetJsonFromUrlTask extends Task<String> {
  GetJsonFromUrlTask(this.url);

  final String url;

  @override
  FutureOr<String> execute() {
    return http.get(url).then((response) {
      printRunningIsolate(url);
      return response.body;
    });
  }
}
5
likes
40
pub points
9%
popularity

Publisher

unverified uploader

A Dart package providing services that allow you to execute dart code in different executors, ex - isolates.

Repository (GitHub)
View/report issues

License

GPL-3.0 (LICENSE)

Dependencies

meta

More

Packages that depend on executorservices