dorker 0.1.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 67

Web Worker and Dart #

This package provides 2 things.

  1. Dart wrappers for Web Worker. One that talks to the Web Worker, and one that inside a Web Worker. All in the same project, written in Dart.
  2. A solution to develop with dartdevc and webdev serve even when Web Worker will not start up correctly.

Some background about point number 2:

Web Worker is all along working well with webdev serve --release, or compiling it with dart2js. But having Web Worker code actively developing alongside the main application in the same project with dartdevc is not possible because Worker will not launch correctly with webdev serve.

This is likely a bug, or a feature yet to be supported. Before it is figured out by Dart, this package can bridge that gap, with a little bit of work.

Normal Web Worker Usage #

These examples will only work if you use webdev serve --release.

Inside the Web Worker

import 'package:dorker/dorker.dart';

main() {
  var _boss = DorkerBoss();
  _boss.onMessage.listen((data_from_main) => print(data_from_main));
  _boss.postMessage.add('Yes Boss');
}

In the main application

import 'dart:html';

import 'package:dorker/dorker.dart';

void main() {
    _dorker = DorkerWorker(Worker('worker.dart.js'));
    _dorker.onMessage.listen((data_from_worker) => print(data_from_worker));
    _dorker.postMessage.add('Start working!');
}

Develop Web Worker powered project with dartdevc

The main idea is encapsulate the communication to/from Web Worker with Dorker. And then passing a special environment variable when we build with webdev serve --release. This can be done with a build.yaml, which is in this repro.

import 'dart:html';

import 'package:dorker/dorker.dart';

import 'lib/concater.dart';

void main() {
  Dorker _concatDorker;
  if (const String.fromEnvironment('USE_WORKER') == 'true') {
    print('Asked to use worker');
    _concatDorker = DorkerWorker(Worker('worker/concater_worker.dart.js'));
  } else 
  {
    print('Not using worker');
    _concatDorker = Dorker();
    Concater(Dorker.CrossLink(_concatDorker));
  }
}

Example folder provided a good guideline of how to pull this off. You can the example by running either webdev serve or webdev serve -r.

To follow my personal story of how I come to this, you can check out my post here.

0.1.1 #

  • Added messaging helper.

0.1.0 #

  • Initial version.
  • Lightly tested in real use case.

example/main.dart

import 'dart:html';

import 'package:dorker/dorker.dart';

import 'lib/concater.dart';

void main() {
  Dorker _concatDorker;
  if (const String.fromEnvironment('USE_WORKER') == 'true') {
    print('Asked to use worker');
    _concatDorker = DorkerWorker(Worker('worker/concater_worker.dart.js'));
  } else 
  {
    print('Not using worker');
    _concatDorker = Dorker();
    Concater(Dorker.CrossLink(_concatDorker));
  }

  var input = querySelector('#input') as TextInputElement;
  var concatButton = querySelector('#btConcat') as ButtonElement;
  
  concatButton.onClick.listen((_) {
    _concatDorker.postMessage.add(input.value);
  });

  _concatDorker.onMessage.listen((data){
    querySelector('#output').text = 'Concated: $data';
  });

  querySelector('#output').text = 'Use the input and button.';
}

Use this package as a library

1. Depend on it

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


dependencies:
  dorker: ^0.1.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:dorker/dorker.dart';
  
Version Uploaded Documentation Archive
0.1.1 Apr 4, 2019 Go to the documentation of dorker 0.1.1 Download dorker 0.1.1 archive
0.1.0 Dec 15, 2018 Go to the documentation of dorker 0.1.0 Download dorker 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
35
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
67
Learn more about scoring.

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

  • Dart: 2.2.0
  • pana: 0.12.14

Platforms

Detected platforms: web

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

Health suggestions

Fix lib/src/dorker_webworker.dart. (-0.50 points)

Analysis of lib/src/dorker_webworker.dart reported 1 hint:

line 20 col 10: Avoid return types on setters.

Format lib/dorker.dart.

Run dartfmt to format lib/dorker.dart.

Format lib/dorker_messaging.dart.

Run dartfmt to format lib/dorker_messaging.dart.

Format lib/src/dorker_base.dart.

Run dartfmt to format lib/src/dorker_base.dart.

Format lib/src/dorker_messaging.dart.

Run dartfmt to format lib/src/dorker_messaging.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
js ^0.6.1+1 0.6.1+1
meta ^1.1.6 1.1.7
Dev dependencies
build_runner ^1.1.2
build_web_compilers ^1.0.0
test ^1.0.0