{"name":"comlink","version":"4.0.0-alpha.8","description":"Comlink makes WebWorkers enjoyable","main":"dist/umd/comlink.js","module":"dist/esm/comlink.mjs","types":"dist/umd/comlink.d.ts","scripts":{"build":"rollup -c","test:unit":"karma start","test:types":"tsc -t esnext -m esnext --lib esnext,dom --moduleResolution node --noEmit tests/type-checks.ts","test":"npm run fmt_test && npm run build && npm run test:types && npm run test:unit","fmt":"prettier --write ./*.{js,ts,md,json,html} ./{src,docs,tests}/**/*.{js,ts,md,json,html}","fmt_test":"test $(prettier -l ./*.{js,ts,md,json,html} ./{src,docs,tests}/**/*.{js,ts,md,json,html} | wc -l) -eq 0","watchtest":"CHROME_ONLY=1 karma start --no-single-run"},"husky":{"hooks":{"pre-commit":"npm test"}},"keywords":[],"author":{"name":"Surma","email":"surma@google.com"},"repository":{"type":"git","url":"git+https://github.com/GoogleChromeLabs/comlink.git"},"license":"Apache-2.0","devDependencies":{"chai":"4.2.0","conditional-type-checks":"^1.0.0","husky":"^1.3.1","karma":"^4.0.1","karma-chai":"0.1.0","karma-chrome-launcher":"2.2.0","karma-detect-browsers":"^2.3.3","karma-firefox-launcher":"1.1.0","karma-mocha":"1.3.0","karma-safari-launcher":"^1.0.0","karma-safaritechpreview-launcher":"2.0.2","mocha":"5.2.0","prettier":"1.16.1","rimraf":"^2.6.3","rollup":"^1.6.0","rollup-plugin-terser":"^4.0.4","rollup-plugin-typescript2":"^0.19.3","typescript":"^3.3.3333"},"dependencies":{},"readme":"# Comlink\n\nComlink makes [WebWorkers][webworker] enjoyable. Comlink is a **tiny library (1.1kB)**, that removes the mental barrier of thinking about `postMessage` and hides the fact that you are working with workers.\n\nAt a more abstract level it is an RPC implementation for `postMessage` and [ES6 Proxies][es6 proxy].\n\n```\n$ npm install --save comlink@alpha\n```\n\n![Comlink in action](https://user-images.githubusercontent.com/234957/54164510-cdab2d80-4454-11e9-92d0-7356aa6c5746.png)\n\n## Browsers support & bundle size\n\n![Chrome 56+](https://img.shields.io/badge/Chrome-56+-green.svg?style=flat-square)\n![Edge 15+](https://img.shields.io/badge/Edge-15+-green.svg?style=flat-square)\n![Firefox 52+](https://img.shields.io/badge/Firefox-52+-green.svg?style=flat-square)\n![Opera 43+](https://img.shields.io/badge/Opera-43+-green.svg?style=flat-square)\n![Safari 10.1+](https://img.shields.io/badge/Safari-10.1+-green.svg?style=flat-square)\n![Samsung Internet 6.0+](https://img.shields.io/badge/Samsung_Internet-6.0+-green.svg?style=flat-square)\n\nBrowsers without [ES6 Proxy] support can use the [proxy-polyfill].\n\n**Size**: ~2.5k, ~1.2k gzip’d, ~1.1k brotli’d\n\n## Introduction\n\nOn mobile phones, and especially on low-end mobile phones, it is important to keep the main thread as idle as possible so it can respond to user interactions quickly and provide a jank-free experience. **The UI thread ought to be for UI work only**. WebWorkers are a web API that allow you to run code in a separate thread. To communicate with another thread, WebWorkers offer the `postMessage` API. You can send JavaScript objects as messages using `myWorker.postMessage(someObject)`, triggering a `message` event inside the worker.\n\nComlink turns this messaged-based API into a something more developer-friendly by providing an RPC implementation: Values from one thread can be used within the other thread (and vice versa) just like local values.\n\n## API\n\n### `Comlink.wrap(endpoint)` and `Comlink.expose(value, endpoint?)`\n\nComlink’s goal is to make _exposed_ values from one thread available in the other. `expose` exposes `value` on `endpoint`, where `endpoint` is a [`postMessage`-like interface][endpoint].\n\n`wrap` wraps the _other_ end of the message channel and returns a proxy. The proxy will have all properties and functions of the exposed value, but access and invocations are inherintly asynchronous. This means that a function that returns a number will now return _a promise_ for a number. **As a rule of thumb: If you are using the proxy, put `await` in front of it.** Exceptions will be caught and re-thrown on the other side.\n\n### `Comlink.transfer(value, transferables)` and `Comlink.proxy(value)`\n\nBy default, every function parameter, return value and object property value is copied, in the sense of [structured cloning]. Structured cloning can be thought of as deep copying, but has some limitations. See [this table][structured clone table] for details.\n\nIf you want a value to be transferred rather than copied — provided the value is or contains a [`Transferable`][transferable] — you can wrap the value in a `transfer()` call and provide a list of transferable values:\n\n```js\nconst data = new Uint8Array([1, 2, 3, 4, 5]);\nawait myProxy.someFunction(Comlink.transfer(data, [data.buffer]));\n```\n\nLastly, you can use `Comlink.proxy(value)`. When using this Comlink will neither copy nor transfer the value, but instead send a proxy. Both threads now work on the same value. This is useful for callbacks, for example, as functions are neither structured cloneable nor transferable.\n\n```js\nmyProxy.onready = Comlink.proxy(data => {\n  /* ... */\n});\n```\n\n### Transfer handlers and event listeners\n\nIt is common that you want to use Comlink to add an event listener, where the event source is on another thread:\n\n```js\nbutton.addEventListener(\"click\", myProxy.onClick.bind(myProxy));\n```\n\nWhile this won’t throw immediately, `onClick` will never actually be called. This is because [`Event`][event] is neither structured cloneable nor transferable. As a workaround, Comlink offers transfer handlers.\n\nEach function parameter and return value is given to _all_ registered transfer handlers. If one of the event handler signals that it can process the value by returning `true` from `canHandle()`, it is now responsible for serializing the value to sturctured cloneable data and for deserializing the value. A transfer handler has be set up on _both sides_ of the message channel. Here’s an example transfer handler for events:\n\n```js\nComlink.transferHandlers.set(\"EVENT\", {\n  canHandle: obj => obj instanceOf Event,\n  serialize: ev => {\n    return {\n      target: {\n        id: ev.target.id\n        classList: [...ev.target.classList]\n      }\n    };\n  }\n  deserialize: obj => obj\n});\n```\n\nNote that this particular transfer handler won’t create an actual `Event`, but just an object that has the `event.target.id` and `event.target.classList` property. Often, this enough. If not, the transfer handler can be easily augmented to provide all necessary data.\n\n[webworker]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API\n[umd]: https://github.com/umdjs/umd\n[transferable]: https://developer.mozilla.org/en-US/docs/Web/API/Transferable\n[messageport]: https://developer.mozilla.org/en-US/docs/Web/API/MessagePort\n[examples]: https://github.com/GoogleChromeLabs/comlink/tree/master/docs/examples\n[dist]: https://github.com/GoogleChromeLabs/comlink/tree/master/dist\n[delivrjs]: https://cdn.jsdelivr.net/\n[es6 proxy]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy\n[proxy-polyfill]: https://github.com/GoogleChrome/proxy-polyfill\n[endpoint]: src/protocol.js\n[structured cloning]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm\n[structured clone table]: structured-clone-table.md\n[event]: https://developer.mozilla.org/en-US/docs/Web/API/Event\n\n---\n\nLicense Apache-2.0\n","readmeFilename":"README.md","gitHead":"36fbc091dd86446349f6dc9e81a5f66e032a2d72","bugs":{"url":"https://github.com/GoogleChromeLabs/comlink/issues"},"homepage":"https://github.com/GoogleChromeLabs/comlink#readme","_id":"comlink@4.0.0-alpha.8","_nodeVersion":"11.9.0","_npmVersion":"6.9.0","dist":{"integrity":"sha512-9qyRWUcAQnbCaQLgdmSYOMu9b8nfaNqQWnwNuw7reXLuwtea6MjlmKwseASIkRHEb3doki3k57xxwSlmbVAtDw==","shasum":"6586aa6ca920f0a17d65cf15a253a9afecf10590","tarball":"http://registry-npm.ywwl.com/comlink/download/comlink-4.0.0-alpha.8.tgz","fileCount":53,"unpackedSize":156412,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcknHrCRA9TVsSAnZWagAAg40QAI1swzAA6bMIN/Zam5pf\naEpKLkT/7AT1wAtpBJGe5nZ/oZ15BijBy37iHCtbCV66a+I9ojqFip9teDOR\ntXxwtZmqS7jeqVbPUKMI3EMOIHpLmoczziq0ABH+mluD//SD3GWuXmONy14D\nCBfA7Zn37U7ebEUrIXY5GWu5+B3oIbot/y63+gTg3ExpKtDZ72TrW8p4MHJN\nMXEu6MUakMDhfFzAJRb2xCL5t01XNbx9/6twGLeghnI/lr0qtzWvy87hqiyx\nKcNeFCvDKhwXy/lpPctruRWLrMI9sDPvrsUTWQtEfRAhZ+SJbdwYthEmF5Op\n5LciH7Oy31oEOq8U6OUG8iLH3Ft+0QY/P+1yDb8cL9acIxL2clP0d6r8nPCM\nNOOxmx+ilrx0Np8GSfBZ2qaGnMK5QdwKn6lRQOXzyNrg2xorSMGqfQ1vyqWk\nKxRZZKIwBeuNfaFq7uLNElvjQupI4kGuNQ2sFE9JCAftAL05ZEu6A0J4Ot+v\nfbayUV+UfMKQmEHubWD8Fg4th3pl2UWSBxhPfF74N0hk+YGnDR8OKHLYwTJl\n/9O+z+LazLGG5IeD51MokWc4OgPS1hmGYIWNsfv++NDQmwTE3QpPGJOzoCXK\n5gJJiL//ReuBSCBy5C3lr8yYrw5f5jTegvImq4t62duCKLhQ/SGXboJiKMln\nQYBY\r\n=WOxd\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGg+JuWYtUrmc+ydEKO0kbn9+m7e3FLcoxrxlLlSVNypAiEAjZbY6zrzcaNreavIen8toRs/dPLvt3jklDjK535nHvE="}],"size":33428,"noattachment":false,"key":"/comlink/-/comlink-4.0.0-alpha.8.tgz"},"maintainers":[{"email":"jason@developit.ca","name":"developit"},{"email":"surma@surma.link","name":"surma"}],"_npmUser":{"name":"surma","email":"surma@surma.link"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/comlink_4.0.0-alpha.8_1553101290509_0.2373404418410543"},"_hasShrinkwrap":false,"publish_time":1553101290683,"_cnpm_publish_time":1553101290683,"dist-tags":{"alpha":"4.0.0-alpha.10","latest":"4.4.2","next":"4.0.0-alpha.10"}}