chanworker 1.0.1 copy "chanworker: ^1.0.1" to clipboard
chanworker: ^1.0.1 copied to clipboard

Plugin module for enabling web workers in an abstract independant manner.

example/main.dart

import 'dart:html';

import 'package:chanworker/chanworker.dart';
import 'package:superpower/superpower.dart';

import 'lib/aggregating_echo.dart';
import 'lib/echo_fib.dart';

void main() {
  Channel aggEchoServiceChannel = Channel<String>();
  Channel<AgMessage> echoingFibServiceChannel = Channel<AgMessage>();
  DateTime _string_sent, _string_received, _typed_sent, _typed_received;
  int _callCount = 0;
  int _type_callCount = 0;

  /// Setup the web page elements.
  TextInputElement input = querySelector('#input');
  ButtonElement okButton = querySelector('#OkButton');
  ParagraphElement output = querySelector('#output');
  ParagraphElement runningIn = querySelector('#runLocation');
  ParagraphElement builtWhere = querySelector('#buildModeValue');
  ParagraphElement roundTripTime = querySelector('#roundTrip');
  ParagraphElement roundTripAvg = querySelector('#roundTripAvg');
  ParagraphElement callsSent = querySelector('#callsSent');
  ParagraphElement replies = querySelector('#replies');
  TextInputElement input_typed = querySelector('#input_typed');
  ButtonElement okButton_typed = querySelector('#OkButton_typed');
  ParagraphElement output_typed = querySelector('#output_typed');
  ParagraphElement runningIn_typed = querySelector('#runLocation_typed');
  ParagraphElement builtWhere_typed = querySelector('#buildModeValue_typed');
  ParagraphElement roundTripTime_typed = querySelector('#roundTrip_typed');
  ParagraphElement roundTripAvg_typed = querySelector('#roundTripAvg_typed');
  ParagraphElement callsSent_typed = querySelector('#callsSent_typed');
  ParagraphElement replies_typed = querySelector('#replies_typed');
  ParagraphElement fibValue = querySelector('#fibValue');
  output.text = 'Waiting for some data from Aggregating Echo Service.';

  $List allStringTripTimes = $List<int>([]);
  $List allTypeTripTimes = $List<int>([]);

  /// Decide how [AggregatingEcho] should execute.
  if (const bool.fromEnvironment('USE_WORKER', defaultValue: false)) {
    builtWhere.text = "Release Build";
    runningIn.text = "AggEchoService Running in Web Worker";
    aggEchoServiceChannel =
        WorkerChannel<String>(Worker('lib/aggregating_echo.dart.js'));
    echoingFibServiceChannel = WorkerChannel<AgMessage>(
        Worker('lib/echo_fib.dart.js'),
        serializer: serializers);
  } else {
    builtWhere.text = "Development Build";
    runningIn.text = "AggEchoService Running in Main Browser Thread";
    AggregatingEcho(Channel.interconnectWith(aggEchoServiceChannel));
    EchoingFib(Channel<AgMessage>.interconnectWith(echoingFibServiceChannel));
  }

  /// On each click of the OK button.
  okButton.onClick.listen((_) {
    _callCount++;
    _string_sent = DateTime.now();
    aggEchoServiceChannel.dispatchPort.add(input.value);
    callsSent.text = _callCount.toString();
    input.value = '';
    output.text = 'Waiting . . . ';
  });

  aggEchoServiceChannel.receivePort.listen((msg) {
    _string_received = DateTime.now();
    Duration time = _string_received.difference(_string_sent);
    allStringTripTimes.add(time.inMilliseconds);
    String result = msg as String;
    output.text = result;
    replies.text = allStringTripTimes.length.toString();
    roundTripTime.text = ("${(time.inMilliseconds).toString()} Milliseconds.");
    roundTripAvg.text =
        ("${allStringTripTimes.average().round().toString()} Milliseconds");
  });

  okButton_typed.onClick.listen((_) {
    _type_callCount++;
    _typed_sent = DateTime.now();
    echoingFibServiceChannel.dispatchPort
        .add(AgMessage((b) => b..whatWasSaid = input_typed.value));
    callsSent_typed.text = _type_callCount.toString();
    input_typed.value = '';
    output_typed.text = 'Waiting . . . ';
  });

  echoingFibServiceChannel.receivePort.listen((AgMessage msg) {
    _typed_received = DateTime.now();
    Duration time = _typed_received.difference(_typed_sent);
    allTypeTripTimes.add(time.inMilliseconds);
    output_typed.text = msg.allTogether;
    replies_typed.text = allTypeTripTimes.length.toString();
    fibValue.text =
        "Fibonacci Number: ${allTypeTripTimes.length.toString()} is: ${msg.fibValue.toString()}";
    roundTripTime_typed.text =
        ("${(time.inMilliseconds).toString()} Milliseconds.");
    roundTripAvg_typed.text =
        ("${allTypeTripTimes.average().round().toString()} Milliseconds");
  });
}
0
likes
25
pub points
0%
popularity

Publisher

unverified uploader

Plugin module for enabling web workers in an abstract independant manner.

Homepage

License

AGPL-3.0 (LICENSE)

Dependencies

built_collection, built_value, js, nanoid, superpower

More

Packages that depend on chanworker