easy-web-worker 1.0.6 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,40 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WorkerTemplate = exports.generatedId = void 0;
4
- const generatedId = () => `${new Date().getTime()}${Math.random().toString(36).substr(2, 9)}`;
4
+ var generatedId = function () {
5
+ return "".concat(new Date().getTime()).concat(Math.random().toString(36).substr(2, 9));
6
+ };
5
7
  exports.generatedId = generatedId;
6
- const WorkerTemplate = () => `
7
- // this code was auto-generated
8
- const easyWorker = new (class {
9
- onMessageCallback = () => {
10
- // eslint-disable-next-line no-throw-literal
11
- throw "you didnt defined a message-callback, please assign a callback by calling IEasyWorkerInstance.onMessage";
12
- };
13
-
14
- onMessage = (callback) => {
15
- this.onMessageCallback = callback;
16
- };
17
- })();
18
-
19
- self.onmessage = (event) => {
20
- const { messageId, payload } = event.data;
21
-
22
- // each message should have his own resolution methods
23
- const message = new (class {
24
- payload = payload;
25
-
26
- messageId = messageId;
27
-
28
- resolve = (...result) => self.postMessage({ messageId, payload: result });
29
-
30
- reject = (error) => this.resolve({ error });
31
-
32
- reportProgress = (progressPercentage) =>
33
- self.postMessage({ messageId, progressPercentage });
34
- })();
35
-
36
- easyWorker.onMessageCallback(message, event);
37
- };
38
- `;
8
+ /**
9
+ * This is the template of the worker, should be and string to avoid compilation issues
10
+ */
11
+ var WorkerTemplate = function () { return "\n //this-code-was auto-generated\n const easyWorker = new (class {\n onMessageCallback = () => {\n throw \"you didn't defined a message-callback, please assign a callback by calling IEasyWorkerInstance.onMessage\";\n };\n\n onMessage = (callback) => {\n this.onMessageCallback = callback;\n };\n })();\n\n self.onmessage = (event) => {\n const { messageId, payload } = event.data;\n\n // each message should have his own resolution methods\n const message = new (class {\n payload = payload;\n\n messageId = messageId;\n\n resolve = (...result) => self.postMessage({ messageId, payload: result });\n\n reject = (reason) => self.postMessage({ messageId, reason });\n\n cancel = (reason) => self.postMessage({ messageId, reason, wasCanceled: true });\n\n reportProgress = (progressPercentage) => self.postMessage({ messageId, progressPercentage });\n })();\n\n easyWorker.onMessageCallback(message, event);\n };\n"; };
39
12
  exports.WorkerTemplate = WorkerTemplate;
40
- exports.default = exports.generatedId;
@@ -1,20 +1,50 @@
1
- import * as IEasyWebWorker from './EasyWebWorkerTypes';
2
- declare class EasyWebWorkerMessage<IPayload = null, IResult = void> implements IEasyWebWorker.IEasyWebWorkerMessage<IPayload, IResult> {
3
- payload: IPayload;
1
+ import { TDecoupledCancelablePromise, TOnProgressCallback } from 'cancelable-promise-jq';
2
+ /**
3
+ * This class represents a message that will be send to a worker
4
+ */
5
+ export declare class EasyWebWorkerMessage<TPayload = null, TResult = void> {
6
+ payload: TPayload;
7
+ /**
8
+ * This is the message id, it's generated automatically
9
+ */
4
10
  messageId: string;
11
+ /**
12
+ * This flag will be true when the message was completed
13
+ */
5
14
  wasCompleted: boolean;
15
+ /**
16
+ * This flag will be true when the message was canceled
17
+ */
6
18
  wasCanceled: boolean;
7
- promise: Promise<IResult>;
8
- resolve: (...args: IResult extends void ? [null?] : [IResult]) => void;
9
- reject: (reason: Error) => void;
10
- reportProgress: IEasyWebWorker.OnProgressCallback;
11
- constructor(payload: IPayload);
12
- protected createPromise(): {
13
- promise: Promise<IResult>;
14
- resolve: (...args: IResult extends void ? [null?] : [IResult]) => void;
15
- reject: (reason: Error) => void;
16
- };
17
- cancel(): void;
19
+ /**
20
+ * Reason of the cancelation or rejection of the message
21
+ */
22
+ reason?: unknown;
23
+ /**
24
+ * Decoupled promise that will be resolved when the message is completed
25
+ * The decoupled promise is a promise that can be resolved, rejected or canceled from outside
26
+ */
27
+ decoupledPromise: TDecoupledCancelablePromise<TResult>;
28
+ /**
29
+ * This method will resolve the decoupled promise
30
+ */
31
+ resolve: (...args: TResult extends void ? [null?] : [TResult]) => void;
32
+ /**
33
+ * This method will reject the decoupled promise
34
+ */
35
+ reject: (reason: unknown) => void;
36
+ /**
37
+ * This method will cancel the decoupled promise
38
+ */
39
+ cancel: (reason: unknown) => void;
40
+ /**
41
+ * This method will report the progress of the message
42
+ * @param {number} progressPercentage - This is the progress percentage
43
+ */
44
+ reportProgress: TOnProgressCallback;
45
+ /**
46
+ * This is the constructor of the class
47
+ * @param {TPayload} payload - This are the parameters included in the message
48
+ */
49
+ constructor(payload: TPayload);
18
50
  }
19
- export default EasyWebWorkerMessage;
20
- //# sourceMappingURL=EasyWebWorkerMessage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"EasyWebWorkerMessage.d.ts","sourceRoot":"","sources":["../src/EasyWebWorkerMessage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,cAAc,MAAM,sBAAsB,CAAC;AAEvD,cAAM,oBAAoB,CAAC,QAAQ,GAAG,IAAI,EAAE,OAAO,GAAG,IAAI,CAAE,YAAW,cAAc,CAAC,qBAAqB,CAAC,QAAQ,EAAE,OAAO,CAAC;IAkBzG,OAAO,EAAE,QAAQ;IAhB7B,SAAS,EAAE,MAAM,CAAC;IAElB,YAAY,UAAS;IAErB,WAAW,UAAS;IAEpB,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAE1B,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,SAAS,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC;IAEvE,MAAM,EAAE,CAAC,MAAM,EAAE,KAAK,KAAK,IAAI,CAAC;IAEhC,cAAc,EAAE,cAAc,CAAC,kBAAkB,CAEvD;gBAEkB,OAAO,EAAE,QAAQ;IASpC,SAAS,CAAC,aAAa,IAAI;QACzB,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;QAC1B,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,SAAS,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC;QACvE,MAAM,EAAE,CAAC,MAAM,EAAE,KAAK,KAAK,IAAI,CAAA;KAC9B;IAeI,MAAM;CAId;AAED,eAAe,oBAAoB,CAAC"}
1
+ {"version":3,"file":"EasyWebWorkerMessage.d.ts","sourceRoot":"","sources":["../src/EasyWebWorkerMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,EAE3B,mBAAmB,EACpB,MAAM,uBAAuB,CAAC;AAG/B;;GAEG;AACH,qBAAa,oBAAoB,CAAC,QAAQ,GAAG,IAAI,EAAE,OAAO,GAAG,IAAI;IAwD5C,OAAO,EAAE,QAAQ;IAvDpC;;OAEG;IACI,SAAS,EAAE,MAAM,CAAC;IAEzB;;OAEG;IACI,YAAY,UAAS;IAE5B;;OAEG;IACI,WAAW,UAAS;IAE3B;;OAEG;IACI,MAAM,CAAC,EAAE,OAAO,CAAC;IAExB;;;OAGG;IACI,gBAAgB,EAAE,2BAA2B,CAAC,OAAO,CAAC,CAAC;IAE9D;;OAEG;IACI,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,SAAS,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC;IAE9E;;OAEG;IACI,MAAM,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;IAEzC;;OAEG;IACI,MAAM,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;IAEzC;;;OAGG;IACI,cAAc,EAAE,mBAAmB,CAIxC;IAEF;;;OAGG;gBACgB,OAAO,EAAE,QAAQ;CAiBrC"}
@@ -1,33 +1,44 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const EasyWebWorkerFixtures_1 = require("./EasyWebWorkerFixtures");
4
- class EasyWebWorkerMessage {
5
- constructor(payload) {
3
+ exports.EasyWebWorkerMessage = void 0;
4
+ var cancelable_promise_jq_1 = require("cancelable-promise-jq");
5
+ var EasyWebWorkerFixtures_1 = require("./EasyWebWorkerFixtures");
6
+ /**
7
+ * This class represents a message that will be send to a worker
8
+ */
9
+ var EasyWebWorkerMessage = /** @class */ (function () {
10
+ /**
11
+ * This is the constructor of the class
12
+ * @param {TPayload} payload - This are the parameters included in the message
13
+ */
14
+ function EasyWebWorkerMessage(payload) {
15
+ var _this = this;
6
16
  this.payload = payload;
17
+ /**
18
+ * This flag will be true when the message was completed
19
+ */
7
20
  this.wasCompleted = false;
21
+ /**
22
+ * This flag will be true when the message was canceled
23
+ */
8
24
  this.wasCanceled = false;
9
- this.reportProgress = () => {
25
+ /**
26
+ * This method will report the progress of the message
27
+ * @param {number} progressPercentage - This is the progress percentage
28
+ */
29
+ this.reportProgress = function () {
10
30
  throw new Error('Message should receive a progress-callback in order to be able to report... send(..).onProgressCallback');
11
31
  };
12
- this.messageId = EasyWebWorkerFixtures_1.generatedId();
13
- const { promise, resolve, reject } = this.createPromise();
14
- this.promise = promise;
15
- this.resolve = resolve;
16
- this.reject = reject;
17
- }
18
- createPromise() {
19
- let resolve = () => { };
20
- let reject = () => { };
21
- const promise = new Promise((_resolve, _reject) => {
22
- resolve = _resolve;
23
- reject = _reject;
32
+ this.messageId = (0, EasyWebWorkerFixtures_1.generatedId)();
33
+ this.decoupledPromise = (0, cancelable_promise_jq_1.createDecoupledPromise)();
34
+ this.resolve = this.decoupledPromise.resolve;
35
+ this.reject = this.decoupledPromise.reject;
36
+ this.cancel = this.decoupledPromise.cancel;
37
+ this.reportProgress = this.decoupledPromise.promise.reportProgress;
38
+ this.decoupledPromise.onCancel(function () {
39
+ _this.wasCanceled = true;
24
40
  });
25
- return {
26
- promise, reject, resolve,
27
- };
28
- }
29
- cancel() {
30
- this.wasCanceled = true;
31
41
  }
32
- }
33
- exports.default = EasyWebWorkerMessage;
42
+ return EasyWebWorkerMessage;
43
+ }());
44
+ exports.EasyWebWorkerMessage = EasyWebWorkerMessage;
@@ -1,87 +1,97 @@
1
- export declare type OnProgressCallback = (progressPercentage: number) => void;
1
+ /**
2
+ * EasyWorker Config
3
+ * */
2
4
  export interface IWorkerConfig {
3
5
  /**
4
6
  ** External scripts that you want to include in your worker
5
7
  */
6
8
  scripts: string[];
7
9
  /**
8
- ** Identifier of your worker, this is in case you want to add a name to your worker file, otherwize an unique generic Id will be added as a name
10
+ ** Identifier of your worker, this is in case you want to add a name to your worker file, otherwise an unique generic Id will be added as a name
9
11
  */
10
12
  name: string;
13
+ /**
14
+ * Callback that will be executed when the worker fails, this not necessary affect the current message execution
15
+ * */
16
+ onWorkerError: (error: ErrorEvent) => void;
11
17
  }
18
+ /**
19
+ * This is the structure that the messages of the worker will have
20
+ */
12
21
  export interface IMessageData<IPayload = null> {
22
+ /**
23
+ * This are the parameters included in the message
24
+ */
13
25
  payload: IPayload;
14
- error: string;
15
- progressPercentage: number;
26
+ /**
27
+ * The messages from the worker could include errors if something went wrong
28
+ * */
29
+ reason: unknown;
30
+ /**
31
+ * Indicates if the message was canceled
32
+ * */
33
+ wasCanceled: boolean;
34
+ /**
35
+ * The messages from the worker could include a progress percentage
36
+ * */
37
+ progressPercentage?: number;
38
+ /**
39
+ * This is the message id
40
+ * */
16
41
  messageId: string;
17
42
  }
18
43
  export interface IEasyWorkerInstance<IPayload = null, IResult = void> {
19
44
  /**
20
- * Use this method to defined which will be the functionality of your worker when a message is send to it
21
- * @param {(message: IEasyWebWorkerMessage<IPayload, IResult>, event: MessageEvent<IMessageData<IPayload>>) => void} callback - current message
22
- // * @returns {void}
23
- * */
45
+ * Use this method to defined which will be the functionality of your worker when a message is send to it
46
+ * @param {function} callback - This is the callback that will be executed when a message is received
47
+ * @param {IEasyWebWorkerMessage} callback.message - This is the message that was received
48
+ * @param {IEasyWebWorkerMessage} callback.message.messageId - This is the message id
49
+ * @param {IEasyWebWorkerMessage} callback.message.payload - This are the parameters included in the message
50
+ * @param {IEasyWebWorkerMessage} callback.message.reject - This method is used to reject the message from inside the worker
51
+ * @param {IEasyWebWorkerMessage} callback.message.reportProgress - This method is used to report the progress of the message from inside the worker
52
+ * @param {IEasyWebWorkerMessage} callback.message.resolve - This method is used to resolve the message from inside the worker
53
+ * @param {MessageEvent} callback.event - This is the event that was received
54
+ * */
24
55
  onMessage(callback: (message: IEasyWebWorkerMessage<IPayload, IResult>, event: MessageEvent<IMessageData<IPayload>>) => void): void;
25
56
  }
26
57
  export interface IEasyWebWorkerMessage<IPayload = null, IResult = void> {
58
+ /**
59
+ * This is the message id
60
+ */
27
61
  messageId: string;
28
- payload: IPayload;
29
- reject: (reason: Error) => void;
30
- reportProgress(progressPercentage: number): void;
31
- resolve: (...args: IResult extends void ? [null?] : [IResult]) => void;
32
- }
33
- /**
34
- * This type defined the structure that a WorkerBody should have.
35
- * @template IPayload - Indicates if your WORKERS messages requires a parameter to be provided, NULL indicates they doesn't
36
- * @template IResult - Indicates if your WORKERS messages has a result... NULL indicates all you messages are Promise<void>
37
- * @param {IEasyWorkerInstance<IPayload, IResult>} easyWorker - ,
38
- */
39
- export declare type EasyWebWorkerBody<IPayload = null, IResult = void> = (easyWorker: IEasyWorkerInstance<IPayload, IResult>, context: any) => void;
40
- export interface IMessagePromise<IResult = void> extends Promise<IResult> {
41
- onProgress: (callback: OnProgressCallback) => Promise<IResult>;
42
- }
43
- /**
44
- * This is a class to create global-store objects
45
- * @template IPayload - Indicates if your WORKERS messages requires a parameter to be provided, NULL indicates they doesn't
46
- * @template IResult - Indicates if your WORKERS messages has a result... NULL indicates all you messages are Promise<void>
47
- * @param {IEasyWebWorker.MessageBody<IPayload, IResult> | IEasyWebWorker.MessageBody<IPayload, IResult>[]} workerBody -
48
- * this parameter should be a function or set of functions that will become the body of your Web-Worker
49
- * IMPORTANT!! all WORKERS content is gonna be transpiled on run time, so you can not use any variable, method of resource that weren't included into the WORKER.
50
- * the above the reason of why we are injecting all worker context into the MessageBody Callbacks, so,
51
- * you could easily identify what is on the context of your Worker.
52
- * @param {Partial<IEasyWebWorker.IWorkerConfig>} WorkerConfig - You could add extra configuration to your worker,
53
- * consult IWorkerConfig description to have more information
54
- * */
55
- export interface IEasyWebWorker<IPayload = null, IResult = void> {
56
- name: string;
57
- workerUrl: string;
58
62
  /**
59
- * This method will remove the WebWorker and the BlobUrl
60
- */
61
- dispose(): void;
63
+ * This are the parameters included in the message
64
+ * */
65
+ payload: IPayload;
62
66
  /**
63
- * Disable the resolve of all current WebWorkers messages, no any of the current messages gonna call onProgress callback, neither promise.resolve
64
- */
65
- cancelAll(): void;
67
+ * This method is used to reject the message from inside the worker
68
+ * */
69
+ reject: (reason?: unknown) => void;
66
70
  /**
67
- * Send a message to the worker queue
68
- * @param {IPayload} payload - whatever json data you want to send to the worker
69
- * @returns {IMessagePromise<IResult>} generated defer that will be resolved when the message completed
70
- */
71
- send(...payload: IPayload extends null ? [null?] : [IPayload]): IMessagePromise<IResult>;
71
+ * This method is used to report the progress of the message from inside the worker
72
+ * */
73
+ reportProgress(progressPercentage: number): void;
72
74
  /**
73
- * Web Workers works as a QUEUE, sometimes a new message actually would be the only message that you'll want to resolve...
74
- * you could use OVERRIDE to that purpose.
75
- * @param {IPayload} payload - whatever json data you want to send to the worker
76
- * @returns {IMessagePromise<IResult>} generated defer that will be resolved when the message completed
77
- */
78
- override(...payload: IPayload extends null ? [null?] : [IPayload]): IMessagePromise<IResult>;
75
+ * This method is used to resolve the message from inside the worker
76
+ * */
77
+ resolve: (...args: IResult extends void ? [null?] : [IResult]) => void;
79
78
  /**
80
- * Web Workers works as a QUEUE, sometimes a new message actually would be the only message that you'll want to resolve...
81
- * you could use OVERRIDE to that purpose.
82
- * @param {IPayload} payload - whatever json data you want to send to the worker
83
- * @returns {IMessagePromise<IResult>} generated defer that will be resolved when the message completed
84
- */
85
- overrideAfterCurrent(...payload: IPayload extends null ? [null?] : [IPayload]): IMessagePromise<IResult>;
79
+ * This method is used to cancel the message from inside the worker
80
+ * */
81
+ cancel: (reason?: unknown) => void;
86
82
  }
87
- //# sourceMappingURL=EasyWebWorkerTypes.d.ts.map
83
+ /**
84
+ * This type defined the structure that a WorkerBody should have.
85
+ * @template IPayload - Indicates if your WORKERS messages requires a parameter to be provided, NULL indicates they doesn't
86
+ * @template IResult - Indicates if your WORKERS messages has a result... NULL indicates all you messages are Promise<void>
87
+ * @param {IEasyWorkerInstance<IPayload, IResult>} easyWorker - ,
88
+ */
89
+ export type EasyWebWorkerBody<IPayload = null, IResult = void> = (
90
+ /**
91
+ * This is the instance of the worker that you can use to communicate with the main thread
92
+ */
93
+ easyWorker: IEasyWorkerInstance<IPayload, IResult>,
94
+ /**
95
+ * This is the context of the worker, you can use it to access to the global scope of the worker
96
+ * */
97
+ context: any) => void;
@@ -1 +1 @@
1
- {"version":3,"file":"EasyWebWorkerTypes.d.ts","sourceRoot":"","sources":["../src/EasyWebWorkerTypes.ts"],"names":[],"mappings":"AAAA,oBAAY,kBAAkB,GAAG,CAAC,kBAAkB,EAAE,MAAM,KAAK,IAAI,CAAC;AAEtE,MAAM,WAAW,aAAa;IAC5B;;OAEG;IACH,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,YAAY,CAAC,QAAQ,GAAG,IAAI;IAC3C,OAAO,EAAE,QAAQ,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,kBAAkB,EAAE,MAAM,CAAC;IAC3B,SAAS,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,mBAAmB,CAAC,QAAQ,GAAG,IAAI,EAAE,OAAO,GAAG,IAAI;IAClE;;;;QAII;IACJ,SAAS,CACP,QAAQ,EAAE,CACR,OAAO,EAAE,qBAAqB,CAAC,QAAQ,EAAE,OAAO,CAAC,EACjD,KAAK,EAAE,YAAY,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,KACxC,IAAI,GACR,IAAI,CAAC;CAET;AAED,MAAM,WAAW,qBAAqB,CAAC,QAAQ,GAAG,IAAI,EAAE,OAAO,GAAG,IAAI;IAEp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;IACL,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IAEnC;;SAEK;IACL,cAAc,CAAC,kBAAkB,EAAE,MAAM,GAAG,IAAI,CAAC;IAEjD;;SAEK;IACL,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,SAAS,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC;IAEvE;;SAEK;IACL,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CACpC;AAED;;;;;GAKG;AACH,MAAM,MAAM,iBAAiB,CAAC,QAAQ,GAAG,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI;AAC/D;;GAEG;AACH,UAAU,EAAE,mBAAmB,CAAC,QAAQ,EAAE,OAAO,CAAC;AAElD;;KAEK;AACL,OAAO,EAAE,GAAG,KACT,IAAI,CAAC"}
@@ -1,18 +1,64 @@
1
- import * as IEasyWebWorker from './EasyWebWorkerTypes';
2
- export declare class WorkerMessage<IPayload = null, IResult = void> implements IEasyWebWorker.IEasyWebWorkerMessage<IPayload, IResult> {
1
+ import { IEasyWebWorkerMessage, IEasyWorkerInstance, IMessageData } from './EasyWebWorkerTypes';
2
+ /**
3
+ * This message wrap the interaction with the worker to ensure that the message is correctly synchronized with the EasyWebWorker
4
+ */
5
+ export declare class WorkerMessage<IPayload = null, IResult = void> implements IEasyWebWorkerMessage<IPayload, IResult> {
3
6
  payload: IPayload;
4
7
  messageId: string;
5
8
  protected messageTargetOrigin: string;
9
+ /**
10
+ * @param {IPayload} payload - This are the parameters included in the message
11
+ * @param {string} messageId - This is the message id
12
+ * @param {string} messageTargetOrigin - This is the target origin of the message
13
+ */
6
14
  constructor(payload: IPayload, messageId: string, messageTargetOrigin?: string);
7
- resolve(...result: [null?] | [any]): void;
8
- reject(error: string | Error): void;
15
+ /**
16
+ * This method is used to resolve the message from inside the worker
17
+ */
18
+ resolve(...payload: [null?] | [any]): void;
19
+ /**
20
+ * This method is used to reject the message from inside the worker
21
+ * */
22
+ reject(reason: unknown): void;
23
+ /**
24
+ * This method is used to cancel the message from inside the worker
25
+ */
26
+ cancel(reason?: unknown): void;
27
+ /**
28
+ * This method is used to report the progress of the message from inside the worker
29
+ * */
9
30
  reportProgress(progressPercentage: number): void;
10
31
  }
11
- export declare class StaticEasyWebWorker<IPayload = null, IResult = void> implements IEasyWebWorker.IEasyWorkerInstance<IPayload, IResult> {
12
- onMessageCallback: (message: IEasyWebWorker.IEasyWebWorkerMessage<IPayload, IResult>, event: MessageEvent<IEasyWebWorker.IMessageData<IPayload>>) => void;
13
- constructor(onMessageCallback: (message: IEasyWebWorker.IEasyWebWorkerMessage<IPayload, IResult>, event: MessageEvent<IEasyWebWorker.IMessageData<IPayload>>) => void, messageTargetOrigin?: string);
32
+ /**
33
+ * This class is used to create a static worker
34
+ * @param {function} onMessageCallback - This is the callback that will be executed when a message is received
35
+ * @param {IEasyWebWorkerMessage} onMessageCallback.message - This is the message that was received
36
+ * @param {IEasyWebWorkerMessage} onMessageCallback.message.messageId - This is the message id
37
+ * @param {IEasyWebWorkerMessage} onMessageCallback.message.payload - This are the parameters included in the message
38
+ * @param {IEasyWebWorkerMessage} onMessageCallback.message.reject - This method is used to reject the message from inside the worker
39
+ * @param {IEasyWebWorkerMessage} onMessageCallback.message.reportProgress - This method is used to report the progress of the message from inside the worker
40
+ * @param {IEasyWebWorkerMessage} onMessageCallback.message.resolve - This method is used to resolve the message from inside the worker
41
+ * @param {MessageEvent} onMessageCallback.event - This is the event that was received
42
+ * @param {string} messageTargetOrigin - This is the target origin of the message
43
+ */
44
+ export declare class StaticEasyWebWorker<IPayload = null, IResult = void> implements IEasyWorkerInstance<IPayload, IResult> {
45
+ onMessageCallback: (message: IEasyWebWorkerMessage<IPayload, IResult>, event: MessageEvent<IMessageData<IPayload>>) => void;
46
+ constructor(onMessageCallback: (message: IEasyWebWorkerMessage<IPayload, IResult>, event: MessageEvent<IMessageData<IPayload>>) => void, messageTargetOrigin?: string);
47
+ /**
48
+ * This method is used to define the onMessage callback
49
+ * @param {string} messageTargetOrigin - This is the target origin of the message
50
+ * */
14
51
  defineOnMessage(messageTargetOrigin: string): void;
15
- onMessage(callback: (message: IEasyWebWorker.IEasyWebWorkerMessage<IPayload, IResult>, event: MessageEvent<IEasyWebWorker.IMessageData<IPayload>>) => void): void;
52
+ /**
53
+ * This method is used to define the onMessage callback
54
+ * @param {function} callback - This is the callback that will be executed when a message is received
55
+ * @param {IEasyWebWorkerMessage} callback.message - This is the message that was received
56
+ * @param {IEasyWebWorkerMessage} callback.message.messageId - This is the message id
57
+ * @param {IEasyWebWorkerMessage} callback.message.payload - This are the parameters included in the message
58
+ * @param {IEasyWebWorkerMessage} callback.message.reject - This method is used to reject the message from inside the worker
59
+ * @param {IEasyWebWorkerMessage} callback.message.reportProgress - This method is used to report the progress of the message from inside the worker
60
+ * @param {IEasyWebWorkerMessage} callback.message.resolve - This method is used to resolve the message from inside the worker
61
+ * @param {MessageEvent} callback.event - This is the event that was received
62
+ */
63
+ onMessage(callback: (message: IEasyWebWorkerMessage<IPayload, IResult>, event: MessageEvent<IMessageData<IPayload>>) => void): void;
16
64
  }
17
- export default StaticEasyWebWorker;
18
- //# sourceMappingURL=StaticEasyWebWorker.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"StaticEasyWebWorker.d.ts","sourceRoot":"","sources":["../src/StaticEasyWebWorker.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,cAAc,MAAM,sBAAsB,CAAC;AAEvD,qBAAa,aAAa,CAAC,QAAQ,GAAG,IAAI,EAAE,OAAO,GAAG,IAAI,CAAE,YAAW,cAAc,CAAC,qBAAqB,CAAC,QAAQ,EAAE,OAAO,CAAC;IAEzG,OAAO,EAAE,QAAQ;IAAS,SAAS,EAAE,MAAM;IAAE,SAAS,CAAC,mBAAmB,EAAE,MAAM;gBAAlF,OAAO,EAAE,QAAQ,EAAS,SA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,CAAC,CAAC,KACxC,IAAI,GACR,IAAI;CAGR"}
@@ -1,40 +1,99 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StaticEasyWebWorker = exports.WorkerMessage = void 0;
4
- class WorkerMessage {
5
- constructor(payload, messageId, messageTargetOrigin = '*') {
4
+ /**
5
+ * This message wrap the interaction with the worker to ensure that the message is correctly synchronized with the EasyWebWorker
6
+ */
7
+ var WorkerMessage = /** @class */ (function () {
8
+ /**
9
+ * @param {IPayload} payload - This are the parameters included in the message
10
+ * @param {string} messageId - This is the message id
11
+ * @param {string} messageTargetOrigin - This is the target origin of the message
12
+ */
13
+ function WorkerMessage(payload, messageId, messageTargetOrigin) {
14
+ if (messageTargetOrigin === void 0) { messageTargetOrigin = '*'; }
6
15
  this.payload = payload;
7
16
  this.messageId = messageId;
8
17
  this.messageTargetOrigin = messageTargetOrigin;
9
18
  }
10
- resolve(...result) {
11
- self.postMessage({ messageId: this.messageId, payload: result }, this.messageTargetOrigin);
12
- }
13
- reject(error) {
14
- this.resolve({ error });
15
- }
16
- reportProgress(progressPercentage) {
17
- self.postMessage({ messageId: this.messageId, progressPercentage }, this.messageTargetOrigin);
18
- }
19
- }
19
+ /**
20
+ * This method is used to resolve the message from inside the worker
21
+ */
22
+ WorkerMessage.prototype.resolve = function () {
23
+ var payload = [];
24
+ for (var _i = 0; _i < arguments.length; _i++) {
25
+ payload[_i] = arguments[_i];
26
+ }
27
+ var messageId = this.messageId;
28
+ self.postMessage({ messageId: messageId, payload: payload }, this.messageTargetOrigin);
29
+ };
30
+ /**
31
+ * This method is used to reject the message from inside the worker
32
+ * */
33
+ WorkerMessage.prototype.reject = function (reason) {
34
+ this.resolve({ error: reason });
35
+ };
36
+ /**
37
+ * This method is used to cancel the message from inside the worker
38
+ */
39
+ WorkerMessage.prototype.cancel = function (reason) {
40
+ this.resolve({ cancelled: true, error: reason });
41
+ };
42
+ /**
43
+ * This method is used to report the progress of the message from inside the worker
44
+ * */
45
+ WorkerMessage.prototype.reportProgress = function (progressPercentage) {
46
+ var messageId = this.messageId;
47
+ self.postMessage({ messageId: messageId, progressPercentage: progressPercentage }, this.messageTargetOrigin);
48
+ };
49
+ return WorkerMessage;
50
+ }());
20
51
  exports.WorkerMessage = WorkerMessage;
21
- class StaticEasyWebWorker {
22
- constructor(onMessageCallback, messageTargetOrigin = '*') {
52
+ /**
53
+ * This class is used to create a static worker
54
+ * @param {function} onMessageCallback - This is the callback that will be executed when a message is received
55
+ * @param {IEasyWebWorkerMessage} onMessageCallback.message - This is the message that was received
56
+ * @param {IEasyWebWorkerMessage} onMessageCallback.message.messageId - This is the message id
57
+ * @param {IEasyWebWorkerMessage} onMessageCallback.message.payload - This are the parameters included in the message
58
+ * @param {IEasyWebWorkerMessage} onMessageCallback.message.reject - This method is used to reject the message from inside the worker
59
+ * @param {IEasyWebWorkerMessage} onMessageCallback.message.reportProgress - This method is used to report the progress of the message from inside the worker
60
+ * @param {IEasyWebWorkerMessage} onMessageCallback.message.resolve - This method is used to resolve the message from inside the worker
61
+ * @param {MessageEvent} onMessageCallback.event - This is the event that was received
62
+ * @param {string} messageTargetOrigin - This is the target origin of the message
63
+ */
64
+ var StaticEasyWebWorker = /** @class */ (function () {
65
+ function StaticEasyWebWorker(onMessageCallback, messageTargetOrigin) {
66
+ if (messageTargetOrigin === void 0) { messageTargetOrigin = '*'; }
23
67
  this.onMessageCallback = onMessageCallback;
24
68
  this.defineOnMessage(messageTargetOrigin);
25
69
  }
26
- defineOnMessage(messageTargetOrigin) {
27
- self.onmessage = (event) => {
28
- const { messageId, payload } = event.data;
70
+ /**
71
+ * This method is used to define the onMessage callback
72
+ * @param {string} messageTargetOrigin - This is the target origin of the message
73
+ * */
74
+ StaticEasyWebWorker.prototype.defineOnMessage = function (messageTargetOrigin) {
75
+ var _this = this;
76
+ self.onmessage = function (event) {
77
+ var _a = event.data, messageId = _a.messageId, payload = _a.payload;
29
78
  // each message should have his own resolution methods
30
- const message = new WorkerMessage(payload, messageId, messageTargetOrigin);
31
- this.onMessageCallback.call(this, message, event);
79
+ var message = new WorkerMessage(payload, messageId, messageTargetOrigin);
80
+ _this.onMessageCallback.call(_this, message, event);
32
81
  };
33
- }
34
- onMessage(callback) {
82
+ };
83
+ /**
84
+ * This method is used to define the onMessage callback
85
+ * @param {function} callback - This is the callback that will be executed when a message is received
86
+ * @param {IEasyWebWorkerMessage} callback.message - This is the message that was received
87
+ * @param {IEasyWebWorkerMessage} callback.message.messageId - This is the message id
88
+ * @param {IEasyWebWorkerMessage} callback.message.payload - This are the parameters included in the message
89
+ * @param {IEasyWebWorkerMessage} callback.message.reject - This method is used to reject the message from inside the worker
90
+ * @param {IEasyWebWorkerMessage} callback.message.reportProgress - This method is used to report the progress of the message from inside the worker
91
+ * @param {IEasyWebWorkerMessage} callback.message.resolve - This method is used to resolve the message from inside the worker
92
+ * @param {MessageEvent} callback.event - This is the event that was received
93
+ */
94
+ StaticEasyWebWorker.prototype.onMessage = function (callback) {
35
95
  this.onMessageCallback = callback;
36
- }
37
- ;
38
- }
96
+ };
97
+ return StaticEasyWebWorker;
98
+ }());
39
99
  exports.StaticEasyWebWorker = StaticEasyWebWorker;
40
- exports.default = StaticEasyWebWorker;
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './EasyWebWorker';
2
+ export * from './EasyWebWorkerTypes';
3
+ export * from './StaticEasyWebWorker';
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./EasyWebWorker"), exports);
18
+ __exportStar(require("./EasyWebWorkerTypes"), exports);
19
+ __exportStar(require("./StaticEasyWebWorker"), exports);