aboutsummaryrefslogtreecommitdiff
path: root/src/crypto/cryptoApi.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-27 22:55:52 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-27 22:55:52 +0200
commit478a089e521de81ca47fc54518bc6a241823d9ae (patch)
tree434360d5090263688950ae180184c96f590ab9ff /src/crypto/cryptoApi.ts
parent9a1b2c8ccc5f079dae4966dfd011f4076a53dc20 (diff)
downloadwallet-core-478a089e521de81ca47fc54518bc6a241823d9ae.tar.xz
fix module loading for node under fake web workers
Diffstat (limited to 'src/crypto/cryptoApi.ts')
-rw-r--r--src/crypto/cryptoApi.ts27
1 files changed, 20 insertions, 7 deletions
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<WorkerState>((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<WorkerState>(concurrency);
for (let i = 0; i < this.workers.length; i++) {
this.workers[i] = {
@@ -199,7 +212,7 @@ export class CryptoApi {
private doRpc<T>(operation: string, priority: number,
...args: any[]): Promise<T> {
- 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;
});
}