Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update a bunch of stuff for 2.0.0 #28

Merged
merged 15 commits into from
Sep 6, 2018
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ env:
matrix:
- COMMAND=test
- COMMAND=test-browser
- COMMAND=test-phantom
- COMMAND=report-coverage
global:
- secure: ug76MyYP5+/TL0HOJVCg7dbB2lyN8efBoJ9CjRQxlkZbiOOXVXMEi86wifAe+xeZVnCrB0S+2EX0jFtezmg9az3f1ktuN5TcjSWPJhtM8MpqX0RBSGvQtNY5GeDfsQcVWL4C4eC/zADBPLAvdznsE5OlFYrO9TjSXiPwekabU8L9Oxk7Yq0yZ/WhSGooNL9B/yaGO/inRDa65Ch7yZwoND6L8UmF3R12FXQM6Sn6OAhUgWRKfYPjDfCfv8pXGLNYkllv9pCEj9C65+sP7k6OA5L7pBLQj6S6Bz0n7XIpaGfSHgnTLOvd4f6ebrLDa71+b/bSagNMKTuQ3pGvEibKtwBQv+HF2qHhf4rnVrUFSYaXFSvjIfN/GCO+DI1B5o8T6y2Gyy2wSPoLxbRa0sZiHRuHBpsS3WrAwxBKYyDMof5nVkBBje6Ih5IiwKKbsvCXhocEwX1CKXsqXHd/hopCOQra2v5PVUTBmjV2LdV1CsVIKXm0JYr0VE3ab7PMHpgjtUHgDP1cclGZqTAi8PtfWd98apMUbXoKqXW9v9+f6ARtz+IfHLtPqrjTe2dB38SnVsZqrsopiqxTVKgTcz1vY3FA8Wg3HLOuhD529a9sed/gYCxA/u+QSAo4WuysqWqf6sUyq6uSBFM2ClzdBTgIxcltgWrB4y35kFav1+WNtsQ=
- secure: IaeC5qFM1EarirH1n5gARuuGG4OQBEccpjmZz34PyhbJyEB9wOmwgbsMrggDiu7bUAJTx6WPIzVagYiJyK/ATbh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+ZtNk5woEuq7stYZ8E4wddfWDvfokIszCHeknGCpVLJiKC3zDjV8slQbn6gL/qv/NXEvQPRMzXLwOAd8AOOCa73nT9h6T8IQr/0QV8IUD5Ec0epxIZRg1Kp0kMd5P8fWOBviXdFAoCbdi6pDAAZtpZD8hCgJ3guktabXXT25C+xSf2s1w4oAwrXpyHHY9QkHNnlGj1Meh74=
branches:
only:
- master
18 changes: 8 additions & 10 deletions .zuul.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
ui: mocha-bdd
concurrency: 1
tunnel:
type: localtunnel
https: true
type: ngrok
browsers:
- name: chrome
version: 42..latest
version: latest
- name: firefox
version: 45..latest
version: latest
- name: safari
version: [8, 9..latest]
version: latest
- name: ie
version: 10..latest
- name: microsoftedge
version: 13..latest
version: latest
- name: iphone
version: [8.4, 9.2]
- name: ipad
version: [8.4, 9.2]
version: latest
- name: android
version: 4.4..latest
version: 4.4
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Changelog
====

v2.0.0
---

- Stopped stringifying worker messages ([#14](https://github.com/nolanlawson/promise-worker/pull/14))
- Removed built-in Promise polyfill

v1.0.0
---

- Initial release
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ A small and performant library for communicating with Web Workers or Service Wor

**Goals:**

* Tiny footprint (~2.5kB min+gz)
* Tiny footprint (~1.4kB min+gz)
* Assumes you have a separate `worker.js` file (easier to debug, better browser support)
* `JSON.stringify`s messages [for performance](http://nolanlawson.com/2016/02/29/high-performance-web-worker-messages/)

**Live examples:**

Expand Down Expand Up @@ -202,8 +201,12 @@ self.addEventListener('activate', function(event) {
Browser support
----

Note that as of v2.0.0, `promise-worker` does not contain a built-in Promise
polyfill. Use something like [es6-promise](https://github.com/stefanpenner/es6-promise)
if you need to support [browsers that don't support Promises](https://caniuse.com/#feat=promises).

See [.zuul.yml](https://github.com/nolanlawson/promise-worker/blob/master/.zuul.yml) for the full list
of tested browsers, but basically:
of tested browsers. Assuming you have a Promise polyfill, the supported browsers should be:

* Chrome
* Firefox
Expand Down Expand Up @@ -269,10 +272,6 @@ Or to test in a browser using SauceLabs:

npm run test-browser

Or to test in PhantomJS:

npm run test-phantom

Or to test with coverage reports:

npm run coverage
46 changes: 23 additions & 23 deletions bin/build-worker-bundles.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
'use strict';
'use strict'

var Promise = require('lie');
var denodeify = require('denodeify');
var rimraf = denodeify(require('rimraf'));
var mkdirp = denodeify(require('mkdirp'));
var glob = require('glob-promise');
var streamToPromise = require('stream-to-promise');
var browserify = require('browserify');
var fs = require('fs');
var writeFile = denodeify(fs.writeFile);
var path = require('path');
var Promise = require('lie')
var denodeify = require('denodeify')
var rimraf = denodeify(require('rimraf'))
var mkdirp = denodeify(require('mkdirp'))
var glob = require('glob-promise')
var streamToPromise = require('stream-to-promise')
var browserify = require('browserify')
var fs = require('fs')
var writeFile = denodeify(fs.writeFile)
var path = require('path')

Promise.resolve().then(function () {
return rimraf('test/bundle');
return rimraf('test/bundle')
}).then(function () {
return mkdirp('test/bundle');
return mkdirp('test/bundle')
}).then(function () {
return glob('test/worker*js');
return glob('test/worker*js')
}).then(function (files) {
return Promise.all(files.map(function (file) {
var b = browserify(file, {debug: true});
var b = browserify(file, { debug: true })
if (process.env.COVERAGE === '1') {
b = b.transform('istanbulify');
b = b.transform('istanbulify')
}
b = b.bundle();
b = b.bundle()
return streamToPromise(b).then(function (buff) {
var outputFile = 'bundle-' + path.basename(file);
return writeFile(outputFile, buff, 'utf-8');
});
}));
var outputFile = 'bundle-' + path.basename(file)
return writeFile(outputFile, buff, 'utf-8')
})
}))
}).catch(function (err) {
console.log(err.stack);
});
console.log(err.stack)
})
69 changes: 33 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,67 @@
'use strict';
'use strict'

/* istanbul ignore next */
var MyPromise = typeof Promise !== 'undefined' ? Promise : require('lie');
var messageIds = 0

var messageIds = 0;

function onMessage(self, e) {
var message = e.data;
function onMessage (self, e) {
var message = e.data
if (!Array.isArray(message) || message.length < 2) {
// Ignore - this message is not for us.
return;
return
}
var messageId = message[0];
var error = message[1];
var result = message[2];
var messageId = message[0]
var error = message[1]
var result = message[2]

var callback = self._callbacks[messageId];
var callback = self._callbacks[messageId]

if (!callback) {
// Ignore - user might have created multiple PromiseWorkers.
// This message is not for us.
return;
return
}

delete self._callbacks[messageId];
callback(error, result);
delete self._callbacks[messageId]
callback(error, result)
}

function PromiseWorker(worker) {
var self = this;
self._worker = worker;
self._callbacks = {};
function PromiseWorker (worker) {
var self = this
self._worker = worker
self._callbacks = {}

worker.addEventListener('message', function (e) {
onMessage(self, e);
});
onMessage(self, e)
})
}

PromiseWorker.prototype.postMessage = function (userMessage) {
var self = this;
var messageId = messageIds++;
var self = this
var messageId = messageIds++

var messageToSend = [messageId, userMessage];
var messageToSend = [messageId, userMessage]

return new MyPromise(function (resolve, reject) {
return new Promise(function (resolve, reject) {
self._callbacks[messageId] = function (error, result) {
if (error) {
return reject(new Error(error.message));
return reject(new Error(error.message))
}
resolve(result);
};
resolve(result)
}

/* istanbul ignore if */
if (typeof self._worker.controller !== 'undefined') {
// service worker, use MessageChannels because e.source is broken in Chrome < 51:
// https://bugs.chromium.org/p/chromium/issues/detail?id=543198
var channel = new MessageChannel();
var channel = new MessageChannel()
channel.port1.onmessage = function (e) {
onMessage(self, e);
};
self._worker.controller.postMessage(messageToSend, [channel.port2]);
onMessage(self, e)
}
self._worker.controller.postMessage(messageToSend, [channel.port2])
} else {
// web worker
self._worker.postMessage(messageToSend);
self._worker.postMessage(messageToSend)
}
});
};
})
}

module.exports = PromiseWorker;
module.exports = PromiseWorker
Loading