{"name":"comlink","version":"4.0.0-alpha.2","description":"Comlink makes WebWorkers enjoyable","main":"dist/umd/comlink.js","module":"dist/esm/comlink.js","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} ./{src,docs,tests}/**/*.{js,ts,md,json}","fmt_test":"test $(prettier -l ./*.{js,ts,md,json} ./{src,docs,tests}/**/*.{js,ts,md,json} | wc -l) -eq 0","watchtest":"karma start --no-single-run --browsers ChromeHeadless"},"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":"3.1.4","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","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’s goal is to make [WebWorkers][webworker] enjoyable. Comlink removes the mental barrier of thinking about `postMessage` and hides the fact that you are working with workers.\n\n> Note: Comlink’s goal is to be a building-block for higher-level abstraction libraries. For example, take a look at [Clooney].\n\n```js\n// main.js\nconst MyClass = Comlink.proxy(new Worker(\"worker.js\"));\n// `instance` is an instance of `MyClass` that lives in the worker!\nconst instance = await new MyClass();\nawait instance.logSomething(); // logs “myValue = 42”\n```\n\n```js\n// worker.js\nconst myValue = 42;\nclass MyClass {\n  logSomething() {\n    console.log(`myValue = ${myValue}`);\n  }\n}\nComlink.expose(MyClass, self);\n```\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**: ~3.9k, ~1.6k gzip’d\n\n## Introduction\n\nWebWorkers 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 messages in form of [transferable] JavaScript objects using `myWorker.postMessage(someObject)`, triggering a `message` event inside the worker.\n\nComlink turns this messaged-based API into a something more developer-friendly: Values from one thread can be used within the other thread (and vice versa) just like local values.\n\nComlink can be used with anything that offers `postMessage` like windows, iframes and ServiceWorkers.\n\n## Download\n\nYou can download Comlink from the [dist folder][dist]. Alternatively, you can\ninstall it via npm\n\n```\n$ npm install --save comlinkjs\n```\n\nor use a CDN like [delivrjs]:\n\n```\nhttps://cdn.jsdelivr.net/npm/comlinkjs@3.1.1/umd/comlink.js\n```\n\n## Examples\n\nThere’s a collection of examples in the [examples directory][examples].\n\n## API\n\nThe Comlink module exports 3 functions:\n\n### `Comlink.proxy(endpoint)`\n\n> Returns the value that is exposed on the other side of `endpoint`.\n\n`proxy` creates an ES6 proxy and sends all operations performed on that proxy through `endpoint`. `endpoint` can be a `Window`, a `Worker` or a `MessagePort`.\\* The other endpoint of the channel should be passed to `Comlink.expose`.\n\nIf you invoke function, all parameters will be structurally cloned or transferred if they are [transferable]. If you want to pass a function as a parameters (e.g. callbacks), make sure to use `proxyValue` (see below). Same applies to the return value of a function.\n\n\\*) Technically it can be any object with `postMessage`, `addEventListener` and\n`removeEventListener`.\n\n### `Comlink.expose(obj, endpoint)`\n\n> Exposes `obj` to `endpoint`. Use `Comlink.proxy` on the other end of `endpoint`.\n\n`expose` is the counter-part to `proxy`. It listens for RPC messages on `endpoint` and applies the operations to `obj`. Return values of functions will be structurally cloned or transfered if they are [transferable].\n\n### `Comlink.proxyValue(value)`\n\n> Makes sure a parameter or return value is proxied, not copied.\n\nBy default, all parameters to a function that are not [transferable] are copied (structural clone):\n\n```js\n// main.js\nconst api = Comlink.proxy(new Worker(\"worker.js\"));\nconst obj = { x: 0 };\nawait api.setXto4(obj);\nconsole.log(obj.x); // logs 0\n```\n\nThe worker receives a copy of `obj`, so any mutation of `obj` done by the worker won’t affect the original object. If the value should _not_ be copied but instead be proxied, use `Comlink.proxyValue`:\n\n```diff\n- await api.setXto4(obj);\n+ await api.setXto4(Comlink.proxyValue(obj));\n```\n\n`console.log(obj.x)` will now log 4.\n\nKeep in mind that functions cannot be copied. Unless they are used in combination with `Comlink.proxyValue`, they will get discarded during copy.\n\n[clooney]: https://github.com/GoogleChromeLabs/clooney\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\n---\n\nLicense Apache-2.0\n","readmeFilename":"README.md","gitHead":"d773ffb7cbfcde2990e47033bdfef0cabb327733","bugs":{"url":"https://github.com/GoogleChromeLabs/comlink/issues"},"homepage":"https://github.com/GoogleChromeLabs/comlink#readme","_id":"comlink@4.0.0-alpha.2","_nodeVersion":"11.9.0","_npmVersion":"6.8.0","dist":{"integrity":"sha512-SHOfv510fSx7c53wZMvayUpSFlCWdQxC3ou8y/1/PehCRMM46ZyXRusN2NUnrLz+Km/2ibD7DmyR5+3sidB2vg==","shasum":"73e32ca0809ee939dc9a309db0197531b8fea533","tarball":"http://registry-npm.ywwl.com/comlink/download/comlink-4.0.0-alpha.2.tgz","fileCount":66,"unpackedSize":160819,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJchaRSCRA9TVsSAnZWagAAjFkQAIZiYx8BQDPbuDzUuTwa\nTblfMFBoL1QN0x8N9hkZgN1bK7/G+bnGfHTkhMZKglz/E0q4cRY7SlK/Yu/s\nhiWDx2ml7lSBGDjp1ecKkAl/FPeOY/+7f/BBv4eIgclPNt3s2Ra7OJQONoAG\nDPGMv3uuPD3Wsb70FDdfnucjyiYfVhfxb9L7V4EBU15OdBgd67Uw3kTnDw4b\nItK+5b3auXTIulBha6G+Xa2BjkT71D9tqq8hrcZY9X9qj3xKXk9DTW48h6ol\nJeIv+UKUfS2/SNpRo7XR9ZmbfdEIL5Q/eSswD7gxkyb7xDh/2HJV0p6iHcVd\nC31jsXBlwUJldSCMjtegAHclTxLQZpMM9fS78yW6CuAc+0z8HpEVZ8sNxXW0\nsicWmax6LfwzfdArS2pAlV1gBZHUlRYCxV38V1lK36VupEqdkPVTz8vAIXVK\n34+YYi5LfK2yjKfbECxMXar/PE+suEd5BUBh7babNWBPIqBupriYg2EpXjrT\nbcCpdaVdPJmH93OqubiUWeh3loGUBtjBjQpbZhITVNNHLuF71LCIk1J2i2BR\nwLeAVFdlFpmE89gMUkr3p8v2DqYxml8WaPFJqt+3GzutJJBHjF7lXWMmCKKr\nlgcsNlpvdu1nLWxAIVg9FcszFumSU/xzFxaDoB3V98/Xsb5dsjCD/FofnDAe\noU3I\r\n=2Df9\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIF2RNBanUN6/+5eV27nXtRsbJxW4FQThjoCLPRm76S+SAiBq/SDLVE3DLqq0/d4VaomYaWdZgU2naLnfcr9mEoy68Q=="}],"size":36360,"noattachment":false,"key":"/comlink/-/comlink-4.0.0-alpha.2.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.2_1552262226065_0.6608809532597848"},"_hasShrinkwrap":false,"publish_time":1552262226228,"_cnpm_publish_time":1552262226228,"dist-tags":{"alpha":"4.0.0-alpha.10","latest":"4.4.2","next":"4.0.0-alpha.10"}}