From 478a089e521de81ca47fc54518bc6a241823d9ae Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sat, 27 May 2017 22:55:52 +0200 Subject: fix module loading for node under fake web workers --- src/crypto/cryptoApi.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'src/crypto/cryptoApi.ts') diff --git a/src/crypto/cryptoApi.ts b/src/crypto/cryptoApi.ts index a386eab42..b291cd17d 100644 --- a/src/crypto/cryptoApi.ts +++ b/src/crypto/cryptoApi.ts @@ -38,6 +38,9 @@ import { OfferRecord, CoinWithDenom, } from "../wallet"; +import * as timer from "../timer"; + +import { startWorker } from "./startWorker"; /** @@ -57,7 +60,7 @@ interface WorkerState { /** * Timer to terminate the worker if it's not busy enough. */ - terminationTimerHandle: number|null; + terminationTimerHandle: timer.TimerHandle|null; } interface WorkItem { @@ -73,6 +76,8 @@ interface WorkItem { } + + /** * Number of different priorities. Each priority p * must be 0 <= p < NUM_PRIO. @@ -98,7 +103,7 @@ export class CryptoApi { ws.currentWorkItem = work; this.numBusy++; if (!ws.w) { - let w = new Worker("/dist/cryptoWorker-bundle.js"); + let w = startWorker(); w.onmessage = (m: MessageEvent) => this.handleWorkerMessage(ws, m); w.onerror = (e: ErrorEvent) => this.handleWorkerError(ws, e); ws.w = w; @@ -114,7 +119,8 @@ export class CryptoApi { resetWorkerTimeout(ws: WorkerState) { if (ws.terminationTimerHandle != null) { - clearTimeout(ws.terminationTimerHandle); + ws.terminationTimerHandle.clear(); + ws.terminationTimerHandle = null; } let destroy = () => { // terminate worker if it's idle @@ -123,7 +129,7 @@ export class CryptoApi { ws.w = null; } }; - ws.terminationTimerHandle = window.setTimeout(destroy, 20 * 1000); + ws.terminationTimerHandle = timer.after(20 * 1000, destroy); } handleWorkerError(ws: WorkerState, e: ErrorEvent) { @@ -182,7 +188,14 @@ export class CryptoApi { } constructor() { - this.workers = new Array((navigator as any)["hardwareConcurrency"] || 2); + let concurrency = 2; + try { + // only works in the browser + concurrency = (navigator as any)["hardwareConcurrency"]; + } catch (e) { + // ignore + } + this.workers = new Array(concurrency); for (let i = 0; i < this.workers.length; i++) { this.workers[i] = { @@ -199,7 +212,7 @@ export class CryptoApi { private doRpc(operation: string, priority: number, ...args: any[]): Promise { - let start = performance.now(); + let start = timer.performanceNow(); let p = new Promise((resolve, reject) => { let rpcId = this.nextRpcId++; @@ -228,7 +241,7 @@ export class CryptoApi { }); return p.then((r: T) => { - console.log(`rpc ${operation} took ${performance.now() - start}ms`); + console.log(`rpc ${operation} took ${timer.performanceNow() - start}ms`); return r; }); } -- cgit v1.2.3