From 7345bed499668eb7a14bcf675a61d72706d3b445 Mon Sep 17 00:00:00 2001 From: cades kao Date: Thu, 11 Jan 2018 02:03:26 +0800 Subject: [PATCH 1/5] return early, avoid else --- src/index.js | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/index.js b/src/index.js index 386495e..b8e2776 100644 --- a/src/index.js +++ b/src/index.js @@ -55,30 +55,29 @@ export default function workerize(code) { }; */ ctx.addEventListener('message', ({ data }) => { - if (data.type==='RPC') { - let id = data.id; - if (id!=null) { - if (data.method) { - let method = rpcMethods[data.method]; - if (method==null) { - ctx.postMessage({ type: 'RPC', id, error: 'NO_SUCH_METHOD' }); - } - else { - Promise.resolve() - .then( () => method.apply(null, data.params) ) - .then( result => { ctx.postMessage({ type: 'RPC', id, result }); }) - .catch( error => { ctx.postMessage({ type: 'RPC', id, error }); }); - } - } - else { - let callback = callbacks[id]; - if (callback==null) throw Error(`Unknown callback ${id}`); - delete callbacks[id]; - if (data.error) callback.reject(Error(data.error)); - else callback.resolve(data.result); - } + if (data.type!=='RPC') return; + let id = data.id; + if (id==null) return; + if (data.method) { + let method = rpcMethods[data.method]; + if (method==null) { + ctx.postMessage({ type: 'RPC', id, error: 'NO_SUCH_METHOD' }); + return; } + + Promise.resolve() + .then( () => method.apply(null, data.params) ) + .then( result => { ctx.postMessage({ type: 'RPC', id, result }); }) + .catch( error => { ctx.postMessage({ type: 'RPC', id, error }); }); + return; } + + let callback = callbacks[id]; + if (callback==null) throw Error(`Unknown callback ${id}`); + delete callbacks[id]; + data.error + ? callback.reject(Error(data.error)) + : callback.resolve(data.result); }); } setup(worker, worker.rpcMethods, callbacks); From e083979bcd44403b33f70443c3a2d8f58ed45011 Mon Sep 17 00:00:00 2001 From: hong-jen kao Date: Thu, 11 Jan 2018 15:16:05 +0800 Subject: [PATCH 2/5] remove extra .then() call --- src/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index b8e2776..c80b7a9 100644 --- a/src/index.js +++ b/src/index.js @@ -65,8 +65,7 @@ export default function workerize(code) { return; } - Promise.resolve() - .then( () => method.apply(null, data.params) ) + Promise.resolve(() => method.apply(null, data.params)) .then( result => { ctx.postMessage({ type: 'RPC', id, result }); }) .catch( error => { ctx.postMessage({ type: 'RPC', id, error }); }); return; From 01b6724bec69ca52a1100361b63dfb8b037d7466 Mon Sep 17 00:00:00 2001 From: cades kao Date: Fri, 12 Jan 2018 01:54:14 +0800 Subject: [PATCH 3/5] bugfix: resolve value instead of function --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index c80b7a9..e1b652a 100644 --- a/src/index.js +++ b/src/index.js @@ -65,7 +65,7 @@ export default function workerize(code) { return; } - Promise.resolve(() => method.apply(null, data.params)) + Promise.resolve(method.apply(null, data.params)) .then( result => { ctx.postMessage({ type: 'RPC', id, result }); }) .catch( error => { ctx.postMessage({ type: 'RPC', id, error }); }); return; From e031ffcbe37e35245fef37eca9dec80f6a20c09e Mon Sep 17 00:00:00 2001 From: cades kao Date: Fri, 12 Jan 2018 10:44:33 +0800 Subject: [PATCH 4/5] bugfix: utilize .then() to handle thrown error --- src/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index e1b652a..847f5e8 100644 --- a/src/index.js +++ b/src/index.js @@ -65,7 +65,8 @@ export default function workerize(code) { return; } - Promise.resolve(method.apply(null, data.params)) + Promise.resolve() + .then( method.apply(null, data.params) ) .then( result => { ctx.postMessage({ type: 'RPC', id, result }); }) .catch( error => { ctx.postMessage({ type: 'RPC', id, error }); }); return; From 4289eb10e12140aa716c5e5aae764a49bf813cce Mon Sep 17 00:00:00 2001 From: cades kao Date: Fri, 12 Jan 2018 10:45:59 +0800 Subject: [PATCH 5/5] fix: use function instead of a value --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 847f5e8..b8e2776 100644 --- a/src/index.js +++ b/src/index.js @@ -66,7 +66,7 @@ export default function workerize(code) { } Promise.resolve() - .then( method.apply(null, data.params) ) + .then( () => method.apply(null, data.params) ) .then( result => { ctx.postMessage({ type: 'RPC', id, result }); }) .catch( error => { ctx.postMessage({ type: 'RPC', id, error }); }); return;