chanworker 1.0.1

Chanworker #

  • Simple abstraction over Streams and Web Worker.
  • Can run with or with worker by used environment variable inside build.yaml.

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");
  });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  chanworker: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:chanworker/chanworker.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
44
Learn more about scoring.

We analyzed this package on Aug 19, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: web

Primary library: package:chanworker/chanworker.dart with components: html, js.

Maintenance issues and suggestions

Provide a file named CHANGELOG.md. (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (nanoid).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
built_collection >=2.0.0 <5.0.0 4.2.2
built_value >=5.5.5 <7.0.0 6.7.0
js ^0.6.1+1 0.6.1+1
nanoid ^0.0.6 0.0.6 0.1.0
superpower ^0.4.0 0.4.0
Transitive dependencies
collection 1.14.12
fixnum 0.10.9
matcher 0.12.5
meta 1.1.7
path 1.6.4
quiver 2.0.5
stack_trace 1.9.3
Dev dependencies
build_runner ^1.1.2
build_web_compilers ^1.0.0
built_value_generator ^6.2.0
test ^1.0.0