aboutsummaryrefslogtreecommitdiff
path: root/src/crypto/nodeWorker.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-28 16:27:34 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-28 21:55:16 +0200
commite7fa87bcc0052e1e99c6894e7e27a122374956b3 (patch)
tree56c243d08ae357533ebdb4fbf41211aa0fc914ce /src/crypto/nodeWorker.ts
parent08bd3dc0e8a3c2370e4e8abbaa241eaafc144f4c (diff)
downloadwallet-core-e7fa87bcc0052e1e99c6894e7e27a122374956b3.tar.xz
documentation and tslint settings to check for docs
Diffstat (limited to 'src/crypto/nodeWorker.ts')
-rw-r--r--src/crypto/nodeWorker.ts23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/crypto/nodeWorker.ts b/src/crypto/nodeWorker.ts
index 4352b66c2..fa942387a 100644
--- a/src/crypto/nodeWorker.ts
+++ b/src/crypto/nodeWorker.ts
@@ -22,10 +22,22 @@ const fork = require("child_process").fork;
const nodeWorkerEntry = path.join(__dirname, "nodeWorkerEntry.js");
+/**
+ * Worker implementation that uses node subprocesses.
+ */
export class Worker {
- child: any;
+ private child: any;
+
+ /**
+ * Function to be called when we receive a message from the worker thread.
+ */
onmessage: undefined | ((m: any) => void);
+
+ /**
+ * Function to be called when we receive an error from the worker thread.
+ */
onerror: undefined | ((m: any) => void);
+
constructor(scriptFilename: string) {
this.child = fork(nodeWorkerEntry);
this.onerror = undefined;
@@ -55,6 +67,9 @@ export class Worker {
this.child.send({scriptFilename, cwd: process.cwd()});
}
+ /**
+ * Add an event listener for either an "error" or "message" event.
+ */
addEventListener(event: "message" | "error", fn: (x: any) => void): void {
switch (event) {
case "message":
@@ -66,10 +81,16 @@ export class Worker {
}
}
+ /**
+ * Send a message to the worker thread.
+ */
postMessage (msg: any) {
this.child.send(JSON.stringify({data: msg}));
}
+ /**
+ * Forcibly terminate the worker thread.
+ */
terminate () {
this.child.kill("SIGINT");
}