@wixc3/isomorphic-worker
Advanced tools
Comparing version 1.0.9 to 1.1.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.worker = void 0; | ||
var UniversalWorkerUser = /** @class */ (function () { | ||
function UniversalWorkerUser(self) { | ||
const getMessageData = (message) => { | ||
if (message instanceof MessageEvent) { | ||
return message.data; | ||
} | ||
return message; | ||
}; | ||
class UniversalWorkerUser { | ||
constructor(self) { | ||
this.self = self; | ||
} | ||
UniversalWorkerUser.prototype.postMessage = function (message) { | ||
this.self.postMessage(message); | ||
}; | ||
UniversalWorkerUser.prototype.addEventListener = function (type, callback) { | ||
this.self.addEventListener(type, function (e) { | ||
postMessage(message) { | ||
// transferables are here to support the browser polyfill | ||
this.self.postMessage(getMessageData(message), message['transferables']); | ||
} | ||
addEventListener(type, callback) { | ||
this.self.addEventListener(type, (e) => { | ||
callback({ | ||
@@ -18,6 +25,5 @@ data: 'data' in e ? e.data : undefined, | ||
}); | ||
}; | ||
return UniversalWorkerUser; | ||
}()); | ||
} | ||
} | ||
exports.worker = new UniversalWorkerUser(self); | ||
//# sourceMappingURL=browser-worker-scope.js.map |
@@ -1,3 +0,5 @@ | ||
import type { UniversalWorker, WorkerScript } from './types'; | ||
export declare const Worker: new (url: WorkerScript, options: WorkerOptions) => UniversalWorker; | ||
import './browser-polyfill/main-worker-host'; | ||
import './browser-polyfill/nested-worker-polyfill'; | ||
import type { UniversalWorkerConstructor } from './types'; | ||
export declare const Worker: UniversalWorkerConstructor; | ||
//# sourceMappingURL=browser-worker.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Worker = void 0; | ||
require("./browser-polyfill/main-worker-host"); | ||
require("./browser-polyfill/nested-worker-polyfill"); | ||
// globalThis in case of initial worker script inside a worker | ||
exports.Worker = globalThis.Worker; | ||
//# sourceMappingURL=browser-worker.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.worker = void 0; | ||
var worker_threads_1 = require("worker_threads"); | ||
var port = (function getPort() { | ||
const worker_threads_1 = require("worker_threads"); | ||
const port = (function getPort() { | ||
if (!worker_threads_1.parentPort) { | ||
@@ -11,18 +11,17 @@ throw new Error('parentPort required for worker processors'); | ||
})(); | ||
var UniversalWorkerUser = /** @class */ (function () { | ||
function UniversalWorkerUser(portOrWorkerSelf) { | ||
class UniversalWorkerUser { | ||
constructor(portOrWorkerSelf) { | ||
this.portOrWorkerSelf = portOrWorkerSelf; | ||
this.workerData = worker_threads_1.workerData; | ||
} | ||
UniversalWorkerUser.prototype.postMessage = function (message) { | ||
postMessage(message) { | ||
this.portOrWorkerSelf.postMessage(message); | ||
}; | ||
UniversalWorkerUser.prototype.addEventListener = function (type, callback) { | ||
} | ||
addEventListener(type, callback) { | ||
this.portOrWorkerSelf.on(type, function MessageFromNodeWorker(message) { | ||
callback({ data: message }); | ||
}); | ||
}; | ||
return UniversalWorkerUser; | ||
}()); | ||
} | ||
} | ||
exports.worker = new UniversalWorkerUser(port); | ||
//# sourceMappingURL=node-worker-scope.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Worker = void 0; | ||
var worker_threads_1 = require("worker_threads"); | ||
var NodeWorker = /** @class */ (function () { | ||
function NodeWorker(url, options) { | ||
const worker_threads_1 = require("worker_threads"); | ||
class NodeWorker { | ||
constructor(url, options) { | ||
this.worker = new worker_threads_1.Worker(url, options); | ||
} | ||
NodeWorker.prototype.postMessage = function (message) { | ||
postMessage(message) { | ||
this.worker.postMessage(message); | ||
}; | ||
NodeWorker.prototype.addEventListener = function (type, callback) { | ||
this.worker.on(type, function (message) { return callback({ data: message }); }); | ||
}; | ||
NodeWorker.prototype.terminate = function () { | ||
} | ||
addEventListener(type, callback) { | ||
this.worker.on(type, (message) => callback({ data: message })); | ||
} | ||
terminate() { | ||
return this.worker.terminate(); | ||
}; | ||
return NodeWorker; | ||
}()); | ||
} | ||
} | ||
exports.Worker = NodeWorker; | ||
//# sourceMappingURL=node-worker.js.map |
export type MessageType = 'message' | 'error'; | ||
export type WorkerScript = string | URL; | ||
export interface UniversalWorkerOptions { | ||
export interface WorkerOptions { | ||
type?: 'classic' | 'module'; | ||
@@ -14,2 +14,6 @@ /** | ||
} | ||
export type UniversalWorkerOptions = WorkerOptions | undefined; | ||
export interface UniversalWorkerConstructor { | ||
new (url: WorkerScript, options: WorkerOptions): UniversalWorker; | ||
} | ||
export interface UniversalWorker { | ||
@@ -16,0 +20,0 @@ postMessage: (message: unknown) => void; |
{ | ||
"name": "@wixc3/isomorphic-worker", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"description": "simplified cross worker API for web workers and worker threads", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/wixplosives/isomorphic-worker", |
import type { UniversalWorkerUserMethods, UniversalMessage } from './types'; | ||
const getMessageData = (message: unknown): unknown => { | ||
if (message instanceof MessageEvent) { | ||
return message.data; | ||
} | ||
return message; | ||
}; | ||
class UniversalWorkerUser implements UniversalWorkerUserMethods { | ||
@@ -7,3 +14,4 @@ constructor(private self: Worker) {} | ||
public postMessage(message: unknown) { | ||
this.self.postMessage(message); | ||
// transferables are here to support the browser polyfill | ||
this.self.postMessage(getMessageData(message), (message as { transferables: [] })['transferables']); | ||
} | ||
@@ -10,0 +18,0 @@ |
@@ -1,6 +0,6 @@ | ||
import type { UniversalWorker, WorkerScript } from './types'; | ||
import './browser-polyfill/main-worker-host'; | ||
import './browser-polyfill/nested-worker-polyfill'; | ||
import type { UniversalWorkerConstructor } from './types'; | ||
// globalThis in case of initial worker script inside a worker | ||
export const Worker = globalThis.Worker as { | ||
new (url: WorkerScript, options: WorkerOptions): UniversalWorker; | ||
}; | ||
export const Worker = globalThis.Worker as UniversalWorkerConstructor; |
@@ -5,3 +5,3 @@ { | ||
"outDir": "../dist", | ||
"target": "es5", | ||
"target": "es2015", | ||
"rootDirs": ["./"], | ||
@@ -8,0 +8,0 @@ "module": "CommonJS", |
@@ -5,3 +5,3 @@ export type MessageType = 'message' | 'error'; | ||
export interface UniversalWorkerOptions { | ||
export interface WorkerOptions { | ||
type?: 'classic' | 'module'; | ||
@@ -15,6 +15,10 @@ /** | ||
*/ | ||
workerData?: any; | ||
// TODO: check if this can be standardized by query params | ||
workerData?: any; // TODO: check if this can be standardized by query params | ||
} | ||
export type UniversalWorkerOptions = WorkerOptions | undefined; | ||
export interface UniversalWorkerConstructor { | ||
new (url: WorkerScript, options: WorkerOptions): UniversalWorker; | ||
} | ||
export interface UniversalWorker { | ||
@@ -21,0 +25,0 @@ postMessage: (message: unknown) => void; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
31606
51
539
1