Skip to content

Commit

Permalink
updated to work with comlink 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
wellcaffeinated committed May 24, 2019
1 parent 6aa4568 commit d0f1e7b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ build
dist
package-lock.json
yarn.lock
coverage/*
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
{
"name": "Jason Miller",
"email": "[email protected]"
},
{
"name": "Jasper Palfree",
"email": "[email protected]"
}
],
"license": "Apache-2.0",
Expand All @@ -59,11 +63,12 @@
"jasmine-sinon": "^0.4.0",
"karmatic": "^1.0.5",
"microbundle": "^0.4.2",
"sinon": "^5.1.0"
"sinon": "^5.1.0",
"webpack": "^4.32.2"
},
"dependencies": {
"comlinkjs": "^2.4.1",
"comlink": "^4.0.1",
"loader-utils": "^1.1.0",
"worker-loader": "^2.0.0"
"worker-loader": "https://github.com/wellcaffeinated/worker-loader"
}
}
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 {Comlink} from 'comlinkjs';
${content}
for(var $$ in __webpack_exports__)if ($$!='__esModule')Comlink.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
},{})
)`;
}
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ export default function loader () { }

loader.pitch = function (request) {
const options = loaderUtils.getOptions(this) || {};
const multi = options.multiple || options.multi || options.singleton === false;
// const multi = options.multiple || options.multi || options.singleton === false;

return `
import {Comlink} from 'comlinkjs';
${multi ? '' : 'var inst;'}
import { wrap } from 'comlink';
var inst;
var worker = require('!worker-loader?${JSON.stringify(options)}!${path.resolve(__dirname, 'comlink-worker-loader.js')}!${request}');
export default function f() {
${multi ? 'var inst =' : 'inst = inst ||'} Comlink.proxy(require('!worker-loader?${JSON.stringify(options)}!${path.resolve(__dirname, 'comlink-worker-loader.js')}!${request}')());
return this instanceof f ? new inst : inst;
inst = inst || wrap(worker());
return (this instanceof f) ? wrap(worker()) : inst;
}
`.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 d0f1e7b

Please sign in to comment.