|
|
|
@ -38,18 +38,22 @@ export class VMProvider { |
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
this.worker.addEventListener('message', (msg) => { |
|
|
|
|
if (msg.data.cmd === 'sendAsyncResult' && stamps[msg.data.stamp]) { |
|
|
|
|
const result = msg.data.result |
|
|
|
|
// if (stamps[msg.data.stamp].request && msg.data.result) result = msg.data.result.result
|
|
|
|
|
|
|
|
|
|
console.log('message', msg) |
|
|
|
|
if (msg.data.cmd === 'requestResult' && stamps[msg.data.stamp]) { |
|
|
|
|
if (msg.data.error) { |
|
|
|
|
stamps[msg.data.stamp].reject(msg.data.error) |
|
|
|
|
} else { |
|
|
|
|
stamps[msg.data.stamp].resolve(msg.data.result) |
|
|
|
|
} |
|
|
|
|
} else if (msg.data.cmd === 'sendAsyncResult' && stamps[msg.data.stamp]) { |
|
|
|
|
if (stamps[msg.data.stamp].callback) { |
|
|
|
|
stamps[msg.data.stamp].callback(msg.data.error, result) |
|
|
|
|
stamps[msg.data.stamp].callback(msg.data.error, msg.data.result) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if (msg.data.error) { |
|
|
|
|
stamps[msg.data.stamp].reject(msg.data.error) |
|
|
|
|
} else { |
|
|
|
|
stamps[msg.data.stamp].resolve(result) |
|
|
|
|
stamps[msg.data.stamp].resolve(msg.data.result) |
|
|
|
|
} |
|
|
|
|
} else if (msg.data.cmd === 'initiateResult') { |
|
|
|
|
if (!msg.data.error) { |
|
|
|
@ -58,7 +62,8 @@ export class VMProvider { |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
const stamp = Date.now() + incr |
|
|
|
|
incr++ |
|
|
|
|
stamps[stamp] = { callback, resolve, reject, sendAsync: true } |
|
|
|
|
stamps[stamp] = { callback, resolve, reject } |
|
|
|
|
console.log('sendAsync', query, stamp) |
|
|
|
|
this.worker.postMessage({ cmd: 'sendAsync', query, stamp }) |
|
|
|
|
}) |
|
|
|
|
}, |
|
|
|
@ -66,8 +71,9 @@ export class VMProvider { |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
const stamp = Date.now() + incr |
|
|
|
|
incr++ |
|
|
|
|
stamps[stamp] = { resolve, reject, request: true } |
|
|
|
|
this.worker.postMessage({ cmd: 'sendAsync', query, stamp }) |
|
|
|
|
stamps[stamp] = { resolve, reject } |
|
|
|
|
console.log('request', query, stamp) |
|
|
|
|
this.worker.postMessage({ cmd: 'request', query, stamp }) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|