Trên GitHub, nolanlawson/promise-worker đã đạt 481 sao, ngôn ngữ JavaScript. Promise-based messaging for Web Workers and Service Workers
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
A small and performant library for communicating with Web Workers or Service Workers, using Promises. Post a message to the worker, get a message back.
Goals:
Tiny footprint (~700 bytes min+gz)
Assumes you have a separate worker.js file (easier to debug, better browser support)
Live examples:
Web Workers
Service Workers
Usage
Install:
npm install promise-worker
Inside your main bundle:
// main.js
var PromiseWorker = require('promise-worker');
var worker = new Worker('worker.js');
var promiseWorker = new PromiseWorker(worker);
promiseWorker.postMessage('ping').then(function (response) {
// handle response
}).catch(function (error) {
// handle error
});
Note that stacktraces cannot be sent from the worker to the main thread, so you
will have to debug those errors yourself. This library does however, print
messages to console.error(), so you should see them there.
Multi-type messages
If you need to send messages of multiple types to the worker, just add
some type information to the message you send:
Communicating with a Service Worker is the same as with a Web Worker.
However, you have to wait for the Service Worker to install and start controlling the page. Here's an example:
navigator.serviceWorker.register('sw.js', {
scope: './'
}).then(function () {
if (navigator.serviceWorker.controller) {
// already active and controlling this page
return navigator.serviceWorker;
}
// wait for a new service worker to control this page
return new Promise(function (resolve) {
function onControllerChange() {
navigator.serviceWorker.removeEventListener('controllerchange', onControllerChange);
resolve(navigator.serviceWorker);
}
navigator.serviceWorker.addEventListener('controllerchange', onControllerChange);
});
}).then(function (worker) { // the worker is ready
var promiseWorker = new PromiseWorker(worker);
return promiseWorker.postMessage('hello worker!');
}).catch(console.log.bind(console));
Then inside your Service Worker:
var registerPromiseWorker = require('../register');
registerPromiseWorker(function (msg) {
return 'hello main thread!';
});
self.addEventListener('activate', function(event) {
event.waitUntil(self.clients.claim()); // activate right now
});
Browser support
Note that as of v2.0.0, promise-worker does not contain a built-in Promise
polyfill. Use something like es6-promise
if you need to support browsers that don't support Promises.
See .zuul.yml for the full list
of tested browsers. Assuming you have a Promise polyfill, the supported browsers should be:
Chrome
Firefox
Safari 8+
IE 10+
Edge
iOS 8+
Android 4.4+
If a browser doesn't support Web Workers but you still want to use this library,
then you can use pseudo-worker.
For Service Worker support, Chrome 40 and 41 are known to be buggy (see #9), but 42+ are supported.
This library is not designed to run in Node.js.
API
Main bundle
new PromiseWorker(worker)
Create a new PromiseWorker, using the given worker.
worker - the Worker or PseudoWorker to use.
PromiseWorker.postMessage(message)
Send a message to the worker and return a Promise.
message - object - required
The message to send.
returns a Promise
Worker bundle
Register a message handler inside of the worker. Your handler consumes a message
and returns a Promise or value.
registerPromiseWorker(function)
function
Takes a message, returns a Promise or a value.
Testing the library
First:
npm install
Then to test in Node (using an XHR/PseudoWorker shim):
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/nolanlawson/promise-worker là nguồn chính thức.
nolanlawson/promise-worker có bao nhiêu sao?
nolanlawson/promise-worker có 481 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/nolanlawson/promise-worker. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
nolanlawson/promise-worker có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho nolanlawson/promise-worker. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
nolanlawson/promise-worker có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho nolanlawson/promise-worker. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
nolanlawson/promise-worker còn đang phát triển không?
Commit gần nhất trên nolanlawson/promise-worker là 4.8 năm trước (theo timestamp GitHub). Repo có 39 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
nolanlawson/promise-worker dùng license gì?
nolanlawson/promise-worker phát hành theo license Apache-2.0. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
nolanlawson/promise-worker viết bằng ngôn ngữ gì?
nolanlawson/promise-worker chủ yếu viết bằng JavaScript. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
promise-worker có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về promise-worker.