Comparing version 1.6.1 to 1.6.2
@@ -59,3 +59,12 @@ /* | ||
worker.addEventListener("message", messageHandler); | ||
return () => worker.removeEventListener("message", messageHandler); | ||
return () => { | ||
if (asyncType === "observable" || !asyncType) { | ||
const cancelMessage = { | ||
type: MasterMessageType.cancel, | ||
uid: jobUID | ||
}; | ||
worker.postMessage(cancelMessage); | ||
} | ||
worker.removeEventListener("message", messageHandler); | ||
}; | ||
}); | ||
@@ -62,0 +71,0 @@ } |
@@ -5,2 +5,3 @@ ///////////////////////////// | ||
(function (MasterMessageType) { | ||
MasterMessageType["cancel"] = "cancel"; | ||
MasterMessageType["run"] = "run"; | ||
@@ -7,0 +8,0 @@ })(MasterMessageType || (MasterMessageType = {})); |
@@ -13,10 +13,15 @@ /// <reference lib="dom" /> | ||
}; | ||
let muxingHandlerSetUp = false; | ||
const messageHandlers = new Set(); | ||
const subscribeToMasterMessages = function subscribeToMasterMessages(onMessage) { | ||
const messageHandler = (messageEvent) => { | ||
onMessage(messageEvent.data); | ||
}; | ||
const unsubscribe = () => { | ||
self.removeEventListener("message", messageHandler); | ||
}; | ||
self.addEventListener("message", messageHandler); | ||
if (!muxingHandlerSetUp) { | ||
// We have one multiplexing message handler as tiny-worker's | ||
// addEventListener() only allows you to set a single message handler | ||
self.addEventListener("message", ((event) => { | ||
messageHandlers.forEach(handler => handler(event.data)); | ||
})); | ||
muxingHandlerSetUp = true; | ||
} | ||
messageHandlers.add(onMessage); | ||
const unsubscribe = () => messageHandlers.delete(onMessage); | ||
return unsubscribe; | ||
@@ -23,0 +28,0 @@ }; |
@@ -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,12 @@ "use strict"; | ||
worker.addEventListener("message", messageHandler); | ||
return () => worker.removeEventListener("message", messageHandler); | ||
return () => { | ||
if (asyncType === "observable" || !asyncType) { | ||
const cancelMessage = { | ||
type: messages_1.MasterMessageType.cancel, | ||
uid: jobUID | ||
}; | ||
worker.postMessage(cancelMessage); | ||
} | ||
worker.removeEventListener("message", messageHandler); | ||
}; | ||
}); | ||
@@ -68,0 +77,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 = {})); |
@@ -15,10 +15,15 @@ "use strict"; | ||
}; | ||
let muxingHandlerSetUp = false; | ||
const messageHandlers = new Set(); | ||
const subscribeToMasterMessages = function subscribeToMasterMessages(onMessage) { | ||
const messageHandler = (messageEvent) => { | ||
onMessage(messageEvent.data); | ||
}; | ||
const unsubscribe = () => { | ||
self.removeEventListener("message", messageHandler); | ||
}; | ||
self.addEventListener("message", messageHandler); | ||
if (!muxingHandlerSetUp) { | ||
// We have one multiplexing message handler as tiny-worker's | ||
// addEventListener() only allows you to set a single message handler | ||
self.addEventListener("message", ((event) => { | ||
messageHandlers.forEach(handler => handler(event.data)); | ||
})); | ||
muxingHandlerSetUp = true; | ||
} | ||
messageHandlers.add(onMessage); | ||
const unsubscribe = () => messageHandlers.delete(onMessage); | ||
return unsubscribe; | ||
@@ -25,0 +30,0 @@ }; |
@@ -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.1", | ||
"version": "1.6.2", | ||
"description": "Web workers & worker threads as simple as a function call", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.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
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
175424
3964