Skip to content

Commit

Permalink
Merge pull request #12 from wellcaffeinated/master
Browse files Browse the repository at this point in the history
Update for comlink 4.x
  • Loading branch information
developit committed Jan 10, 2020
2 parents 57b5f48 + 7d4a9c4 commit 01f3b2b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dist
coverage
package-lock.json
yarn.lock
coverage/*
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ In the example below, the sole difference between running `MyClass` on a Worker
import rnd from 'random-int';

// Export as you would in a normal module:
export function meaningOfLife(){
return 42;
}

export class MyClass {
constructor(value = rnd()) {
this.value = value;
Expand All @@ -50,13 +54,16 @@ export class MyClass {
**main.js**: _(our demo, on the main thread)_

```js
import { MyClass } from 'comlink-loader!./my-class';
import worker from 'comlink-loader!./my-class';
const inst = worker();

await inst.meaningOfLife(); // 42

const inst = await new MyClass(42); // notice the await
const obj = await new inst.MyClass(42); // notice the await

await inst.increment();
await obj.increment();

await inst.getValue(); // 43
await obj.getValue(); // 43
```

## License
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
{
"name": "Jason Miller",
"email": "developit@google.com"
},
{
"name": "Jasper Palfree",
"email": "jasper@wellcaffeinated.net"
}
],
"license": "Apache-2.0",
Expand Down
12 changes: 9 additions & 3 deletions src/comlink-worker-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
*/

export default function rpcWorkerLoader (content) {
return `import {expose} from 'comlink';
${content}
for(var $$ in __webpack_exports__)if ($$!='__esModule')expose(__webpack_exports__[$$],self)`;
return `import { expose } from 'comlink';
${content};
expose(
Object.keys(__webpack_exports__).reduce(function(r,k){
if (k=='__esModule') return r;
r[k] = __webpack_exports__[k];
return r
},{})
)`;
}
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ loader.pitch = function (request) {
}

return `
import {wrap} from 'comlink';
${multi ? '' : 'var inst;'}
import { wrap } from 'comlink';
var inst;
var worker = wrap(require('!worker-loader?${JSON.stringify(workerLoaderOptions)}!${slash(path.resolve(__dirname, 'comlink-worker-loader.js'))}!${request}');
export default function f() {
${multi ? 'var inst =' : 'inst = inst ||'} wrap(require('!worker-loader?${JSON.stringify(workerLoaderOptions)}!${slash(path.resolve(__dirname, 'comlink-worker-loader.js'))}!${request}')());
return this instanceof f ? new inst : inst;
if (this instanceof f) return wrap(worker());
return inst = inst || wrap(worker());
}
`.replace(/\n\s*/g, '');
};
6 changes: 3 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import sinon from 'sinon';
import 'jasmine-sinon';
import './other';
import MyClass from 'comlink-loader!./worker';
import worker from 'comlink-loader!./worker';

const OriginalWorker = self.Worker;
self.Worker = sinon.spy((url, opts) => new OriginalWorker(url, opts));
Expand All @@ -26,7 +26,7 @@ describe('worker', () => {
let inst;

it('should be instantiable', async () => {
inst = await new MyClass();
inst = await new (worker().MyClass)();
expect(self.Worker).toHaveBeenCalledOnce();
});

Expand All @@ -52,7 +52,7 @@ describe('worker', () => {
it('should re-use Worker instances after the first instance', async () => {
sinon.reset(self.Worker);

const secondInst = await new MyClass();
const secondInst = await new (worker().MyClass)();
expect(secondInst).not.toBe(inst);
expect(await secondInst.foo()).toBe(1);

Expand Down

0 comments on commit 01f3b2b

Please sign in to comment.