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

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter 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]
50
Overall:
Weighted score of the above. [more]
40
Learn more about scoring.

We analyzed this package on Feb 13, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

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. (-20 points)

The version constraint in pubspec.yaml does not support the latest published versions for 2 dependencies (built_value, nanoid).

Package is getting outdated. (-9.59 points)

The package was last published 57 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
built_collection >=2.0.0 <5.0.0 4.3.2
built_value >=5.5.5 <7.0.0 6.8.2 7.0.9
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
analyzer 0.38.5 0.39.4
analyzer_plugin 0.2.1
args 1.5.2
async 2.4.0
build 1.2.2
build_config 0.4.2
charcode 1.1.3
checked_yaml 1.0.2
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
dart_style 1.3.3
fixnum 0.10.11
front_end 0.1.27 0.1.29
glob 1.2.0
html 0.14.0+3
json_annotation 3.0.1
kernel 0.3.27 0.3.29
logging 0.11.4
matcher 0.12.6
meta 1.1.8
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0 2.0.0
path 1.6.4
pedantic 1.9.0
pub_semver 1.4.3
pubspec_parse 0.1.5
quiver 2.1.2+1
source_gen 0.9.4+6 0.9.4+7
source_span 1.6.0
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0
Dev dependencies
build_runner ^1.1.2
build_web_compilers ^1.0.0
built_value_generator ^6.2.0 6.8.2 7.0.9
test ^1.0.0