Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-webworker

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-webworker - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

dist/.git/COMMIT_EDITMSG

29

dist/index.d.ts

@@ -1,12 +0,25 @@

export declare class AsyncWorker {
declare class DeferredPromise<T> extends Promise<T> {
resolve: (T: any) => void;
reject: (T: any) => void;
}
export declare class BaseAsyncWorker {
worker: Worker;
deferredPromise: [DeferredPromise<any>, number] | undefined;
terminate: Worker['terminate'];
onerror: Worker['onerror'];
constructor(workerUrl: string);
_retrieveMessage: (e: MessageEvent) => void;
_failPromise: (key: any) => void;
postMessage: (message: string, sharedBuffer?: Transferable[], timeout?: number) => Promise<any>;
}
export declare class AsyncWorker extends BaseAsyncWorker {
worker: Worker;
messageQueue: {
[key: string]: Promise<any>;
} | Promise<any>;
withQueue: boolean;
constructor(workerUrl: string, withQueue?: boolean);
[key: string]: [DeferredPromise<any>, number];
};
constructor(workerUrl: string);
_retrieveMessage: (e: MessageEvent) => void;
postMessage: (message: string) => Promise<any>;
terminate: () => void;
onerror: (this: AbstractWorker, ev: ErrorEvent) => any;
_failPromise: (key: string) => () => void;
postMessage: (message: string, sharedBuffer?: Transferable[], timeout?: number) => Promise<any>;
}
export {};

@@ -0,48 +1,63 @@

class DeferredPromise extends Promise {
}
function getDeferredPromise() {
let res, rej;
const deferredPromise = new Promise((resolve, reject) => {
res = resolve;
rej = reject;
let r, j;
const deferredPromise = new DeferredPromise((resolve, reject) => {
r = resolve;
j = reject;
});
deferredPromise['resolve'] = res;
deferredPromise['reject'] = rej;
deferredPromise.resolve = r;
deferredPromise.reject = j;
console.log('deferredPromise', deferredPromise);
return deferredPromise;
}
export class AsyncWorker {
constructor(workerUrl, withQueue = false) {
export class BaseAsyncWorker {
constructor(workerUrl) {
this._retrieveMessage = (e) => {
let promise, message;
if (this.withQueue) {
const parsed = JSON.parse(e.data);
message = parsed.message;
promise = this.messageQueue[parsed.key];
delete this.messageQueue[parsed.key];
}
else {
message = JSON.parse(e.data);
promise = this.messageQueue;
}
console.log('this.messageQueue', this.messageQueue);
return promise.resolve(message);
const [promise, handle] = this.deferredPromise;
clearTimeout(handle);
return promise.resolve(e.data);
};
this.postMessage = (message) => {
this._failPromise = (key) => {
const [promise] = this.deferredPromise;
return promise.reject('Timeout');
};
this.postMessage = (message, sharedBuffer = undefined, timeout = 5000) => {
const deferredPromise = getDeferredPromise();
if (this.withQueue) {
const key = Math.random().toString(36);
this.messageQueue[key] = deferredPromise;
this.worker.postMessage(JSON.stringify({ key, message }));
}
else {
this.messageQueue = deferredPromise;
this.worker.postMessage(JSON.stringify(message));
}
const timeoutHandler = setTimeout(this._failPromise, timeout);
this.deferredPromise = [deferredPromise, timeoutHandler];
this.worker.postMessage(message, sharedBuffer);
return deferredPromise;
};
this.worker = new Worker(workerUrl);
this.worker.onmessage = this._retrieveMessage;
this.terminate = this.worker.terminate;
this.onerror = this.worker.onerror;
this.worker = new Worker(workerUrl);
this.withQueue = withQueue;
this.messageQueue = withQueue ? {} : undefined;
this.worker.onmessage = this._retrieveMessage;
}
}
export class AsyncWorker extends BaseAsyncWorker {
constructor(workerUrl) {
super(workerUrl);
this._retrieveMessage = (e) => {
const { message, key } = JSON.parse(e.data);
const [promise, handle] = this.messageQueue[key];
clearTimeout(handle);
delete this.messageQueue[key];
return promise.resolve(message);
};
this._failPromise = (key) => () => {
const [promise] = this.messageQueue[key];
delete this.messageQueue[key];
return promise.reject('Timeout');
};
this.postMessage = (message, sharedBuffer = undefined, timeout = 10000) => {
const deferredPromise = getDeferredPromise();
const key = Math.random().toString(36);
const handle = setTimeout(this._failPromise(key), timeout);
this.messageQueue[key] = [deferredPromise, handle];
this.worker.postMessage(JSON.stringify({ key, message }), sharedBuffer);
return deferredPromise;
};
this.messageQueue = {};
}
}
{
"name": "async-webworker",
"version": "0.1.0",
"description": "Communicate with weborker using promises",
"version": "0.2.0",
"description": "Communicate with webworker using promises",
"license": "MIT",

@@ -6,0 +6,0 @@ "repository": "YeasterEgg/async-webworker",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc