Comparing version 1.6.0 to 1.6.1-proxy-unsubscribe
@@ -59,3 +59,10 @@ /* | ||
worker.addEventListener("message", messageHandler); | ||
return () => worker.removeEventListener("message", messageHandler); | ||
return () => { | ||
const cancelMessage = { | ||
type: MasterMessageType.cancel, | ||
uid: jobUID | ||
}; | ||
worker.postMessage(cancelMessage); | ||
worker.removeEventListener("message", messageHandler); | ||
}; | ||
}); | ||
@@ -62,0 +69,0 @@ } |
@@ -5,2 +5,3 @@ ///////////////////////////// | ||
(function (MasterMessageType) { | ||
MasterMessageType["cancel"] = "cancel"; | ||
MasterMessageType["run"] = "run"; | ||
@@ -7,0 +8,0 @@ })(MasterMessageType || (MasterMessageType = {})); |
@@ -20,2 +20,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
let exposeCalled = false; | ||
const activeSubscriptions = new Map(); | ||
const isMasterJobCancelMessage = (thing) => thing && thing.type === MasterMessageType.cancel; | ||
const isMasterJobRunMessage = (thing) => thing && thing.type === MasterMessageType.run; | ||
@@ -108,3 +110,10 @@ /** | ||
if (isObservable(syncResult)) { | ||
syncResult.subscribe(value => postJobResultMessage(jobUID, false, serialize(value)), error => postJobErrorMessage(jobUID, serialize(error)), () => postJobResultMessage(jobUID, true)); | ||
const subscription = syncResult.subscribe(value => postJobResultMessage(jobUID, false, serialize(value)), error => { | ||
postJobErrorMessage(jobUID, serialize(error)); | ||
activeSubscriptions.delete(jobUID); | ||
}, () => { | ||
postJobResultMessage(jobUID, true); | ||
activeSubscriptions.delete(jobUID); | ||
}); | ||
activeSubscriptions.set(jobUID, subscription); | ||
} | ||
@@ -157,2 +166,12 @@ else { | ||
} | ||
Implementation.subscribeToMasterMessages(messageData => { | ||
if (isMasterJobCancelMessage(messageData)) { | ||
const jobUID = messageData.uid; | ||
const subscription = activeSubscriptions.get(jobUID); | ||
if (subscription) { | ||
subscription.unsubscribe(); | ||
activeSubscriptions.delete(jobUID); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -159,0 +178,0 @@ if (typeof self !== "undefined" && typeof self.addEventListener === "function" && Implementation.isWorkerRuntime()) { |
@@ -65,3 +65,10 @@ "use strict"; | ||
worker.addEventListener("message", messageHandler); | ||
return () => worker.removeEventListener("message", messageHandler); | ||
return () => { | ||
const cancelMessage = { | ||
type: messages_1.MasterMessageType.cancel, | ||
uid: jobUID | ||
}; | ||
worker.postMessage(cancelMessage); | ||
worker.removeEventListener("message", messageHandler); | ||
}; | ||
}); | ||
@@ -68,0 +75,0 @@ } |
@@ -8,4 +8,9 @@ export interface SerializedError { | ||
export declare enum MasterMessageType { | ||
cancel = "cancel", | ||
run = "run" | ||
} | ||
export declare type MasterJobCancelMessage = { | ||
type: MasterMessageType.cancel; | ||
uid: number; | ||
}; | ||
export declare type MasterJobRunMessage = { | ||
@@ -17,3 +22,3 @@ type: MasterMessageType.run; | ||
}; | ||
export declare type MasterSentMessage = MasterJobRunMessage; | ||
export declare type MasterSentMessage = MasterJobCancelMessage | MasterJobRunMessage; | ||
export declare enum WorkerMessageType { | ||
@@ -20,0 +25,0 @@ error = "error", |
@@ -8,2 +8,3 @@ "use strict"; | ||
(function (MasterMessageType) { | ||
MasterMessageType["cancel"] = "cancel"; | ||
MasterMessageType["run"] = "run"; | ||
@@ -10,0 +11,0 @@ })(MasterMessageType = exports.MasterMessageType || (exports.MasterMessageType = {})); |
@@ -28,2 +28,4 @@ "use strict"; | ||
let exposeCalled = false; | ||
const activeSubscriptions = new Map(); | ||
const isMasterJobCancelMessage = (thing) => thing && thing.type === messages_1.MasterMessageType.cancel; | ||
const isMasterJobRunMessage = (thing) => thing && thing.type === messages_1.MasterMessageType.run; | ||
@@ -116,3 +118,10 @@ /** | ||
if (isObservable(syncResult)) { | ||
syncResult.subscribe(value => postJobResultMessage(jobUID, false, common_1.serialize(value)), error => postJobErrorMessage(jobUID, common_1.serialize(error)), () => postJobResultMessage(jobUID, true)); | ||
const subscription = syncResult.subscribe(value => postJobResultMessage(jobUID, false, common_1.serialize(value)), error => { | ||
postJobErrorMessage(jobUID, common_1.serialize(error)); | ||
activeSubscriptions.delete(jobUID); | ||
}, () => { | ||
postJobResultMessage(jobUID, true); | ||
activeSubscriptions.delete(jobUID); | ||
}); | ||
activeSubscriptions.set(jobUID, subscription); | ||
} | ||
@@ -165,2 +174,12 @@ else { | ||
} | ||
implementation_1.default.subscribeToMasterMessages(messageData => { | ||
if (isMasterJobCancelMessage(messageData)) { | ||
const jobUID = messageData.uid; | ||
const subscription = activeSubscriptions.get(jobUID); | ||
if (subscription) { | ||
subscription.unsubscribe(); | ||
activeSubscriptions.delete(jobUID); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -167,0 +186,0 @@ exports.expose = expose; |
{ | ||
"name": "threads", | ||
"version": "1.6.0", | ||
"version": "1.6.1-proxy-unsubscribe", | ||
"description": "Web workers & worker threads as simple as a function call", | ||
"main": "dist/index.js", | ||
"module": "dist-esm/index.js", | ||
"exports": { | ||
".": "./index.mjs", | ||
"./observable": "./observable.mjs", | ||
"./register": "./register.mjs", | ||
"./worker": "./worker.mjs" | ||
}, | ||
"scripts": { | ||
@@ -27,2 +21,20 @@ "prebuild": "rimraf dist/ dist-esm/", | ||
}, | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.js", | ||
"default": "./index.mjs" | ||
}, | ||
"./observable": { | ||
"require": "./observable.js", | ||
"default": "./observable.mjs" | ||
}, | ||
"./register": { | ||
"require": "./register.js", | ||
"default": "./register.mjs" | ||
}, | ||
"./worker": { | ||
"require": "./worker.js", | ||
"default": "./worker.mjs" | ||
} | ||
}, | ||
"sideEffects": [ | ||
@@ -29,0 +41,0 @@ "./dist*/master/register.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
174698
3950
1