easy-web-worker 1.0.1 → 1.0.3

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,4 +1,4 @@
1
- import * as IEasyWebWorker from './EasyWebWorkerTypes';
1
+ import * as IEasyWebWorker from "./EasyWebWorkerTypes";
2
2
  export declare class EasyWebWorkerFactory {
3
3
  private getImportScriptsTemplate;
4
4
  blob(source: string, imports?: string[]): string;
@@ -1 +1 @@
1
- {"version":3,"file":"EasyWebWorkerFactory.d.ts","sourceRoot":"","sources":["../src/EasyWebWorkerFactory.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,cAAc,MAAM,sBAAsB,CAAC;AAEvD,qBAAa,oBAAoB;IAE/B,OAAO,CAAC,wBAAwB;IAMzB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,MAAM,EAAO;IAS3C,UAAU,CAAC,QAAQ,GAAG,IAAI,EAAE,OAAO,GAAG,IAAI,EAC/C,MAAM,EAAE,cAAc,CAAC,iBAAiB,CAAC,QAAQ,EAAE,OAAO,CAAC,GAAG,cAAc,CAAC,iBAAiB,CAAC,QAAQ,EAAE,OAAO,CAAC,EAAE,EACnH,OAAO,GAAE,MAAM,EAAO;CAazB;;AAED,wBAA0C"}
1
+ {"version":3,"file":"EasyWebWorkerFactory.d.ts","sourceRoot":"","sources":["../src/EasyWebWorkerFactory.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,cAAc,MAAM,sBAAsB,CAAC;AAEvD,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,wBAAwB;IAMzB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,MAAM,EAAO;IAW3C,UAAU,CAAC,QAAQ,GAAG,IAAI,EAAE,OAAO,GAAG,IAAI,EAC/C,MAAM,EACF,cAAc,CAAC,iBAAiB,CAAC,QAAQ,EAAE,OAAO,CAAC,GACnD,cAAc,CAAC,iBAAiB,CAAC,QAAQ,EAAE,OAAO,CAAC,EAAE,EACzD,OAAO,GAAE,MAAM,EAAO;CAwBzB;;AAED,wBAA0C"}
@@ -5,25 +5,26 @@ const EasyWebWorkerFixtures_1 = require("./EasyWebWorkerFixtures");
5
5
  class EasyWebWorkerFactory {
6
6
  getImportScriptsTemplate(scripts = []) {
7
7
  if (!scripts.length)
8
- return '';
8
+ return "";
9
9
  return `self.importScripts(["${scripts.join('","')}"]);`;
10
10
  }
11
11
  blob(source, imports = []) {
12
12
  const content = `${this.getImportScriptsTemplate(imports)}
13
13
  ${source}`;
14
14
  return (window.URL || window.webkitURL).createObjectURL(new Blob([content], {
15
- type: 'application/javascript',
15
+ type: "application/javascript",
16
16
  }));
17
17
  }
18
18
  blobWorker(source, imports = []) {
19
- const templateSource = EasyWebWorkerFixtures_1.WorkerTemplate.toString();
20
- const template = templateSource.substring(templateSource.indexOf('{') + 1, templateSource.length - 1);
19
+ const template = EasyWebWorkerFixtures_1.WorkerTemplate();
21
20
  const contentCollection = Array.isArray(source) ? source : [source];
22
21
  return (window.URL || window.webkitURL).createObjectURL(new Blob([
23
22
  `${this.getImportScriptsTemplate(imports)}
24
23
  ${template}
25
- ${contentCollection.map((content, index) => `// content #${index}\n(${content.toString()})(easyWorker, self);`).join('\n\n')}`,
26
- ], { type: 'application/javascript' }));
24
+ ${contentCollection
25
+ .map((content, index) => `// content #${index}\n(${content.toString()})(easyWorker, self);`)
26
+ .join("\n\n")}`,
27
+ ], { type: "application/javascript" }));
27
28
  }
28
29
  }
29
30
  exports.EasyWebWorkerFactory = EasyWebWorkerFactory;
@@ -1,4 +1,4 @@
1
1
  export declare const generatedId: () => string;
2
- export declare const WorkerTemplate: (self: any) => void;
2
+ export declare const WorkerTemplate: () => string;
3
3
  export default generatedId;
4
4
  //# sourceMappingURL=EasyWebWorkerFixtures.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"EasyWebWorkerFixtures.d.ts","sourceRoot":"","sources":["../src/EasyWebWorkerFixtures.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,WAAW,QAAO,MAA6E,CAAC;AAE7G,eAAO,MAAM,cAAc,SAAU,GAAG,KAAG,IA0C1C,CAAC;AAEF,eAAe,WAAW,CAAC"}
1
+ {"version":3,"file":"EasyWebWorkerFixtures.d.ts","sourceRoot":"","sources":["../src/EasyWebWorkerFixtures.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,WAAW,QAAO,MACsC,CAAC;AAEtE,eAAO,MAAM,cAAc,cA6C1B,CAAC;AAEF,eAAe,WAAW,CAAC"}
@@ -3,33 +3,51 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WorkerTemplate = exports.generatedId = void 0;
4
4
  const generatedId = () => `${new Date().getTime()}${Math.random().toString(36).substr(2, 9)}`;
5
5
  exports.generatedId = generatedId;
6
- const WorkerTemplate = (self) => {
7
- const easyWorker = new (class {
8
- constructor() {
9
- this.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
- this.onMessage = (callback) => {
14
- this.onMessageCallback = callback;
15
- };
16
- }
17
- })();
18
- // eslint-disable-next-line no-param-reassign
19
- self.onmessage = (event) => {
20
- const { messageId, payload } = event.data;
21
- // each message should have his own resolution methods
22
- const message = new (class {
23
- constructor() {
24
- this.payload = payload;
25
- this.messageId = messageId;
26
- this.resolve = (...result) => self.postMessage({ messageId, payload: result });
27
- this.reject = (error) => this.resolve({ error });
28
- this.reportProgress = (progressPercentage) => self.postMessage({ messageId, progressPercentage });
29
- }
30
- })();
31
- easyWorker.onMessageCallback(message, event);
6
+ const WorkerTemplate = () => `
7
+ // this code was auto-generated
8
+ const easyWorker = new (class
9
+ implements IEasyWebWorker.IEasyWorkerInstance<any, any>
10
+ {
11
+ public onMessageCallback: (
12
+ message: IEasyWebWorker.IEasyWebWorkerMessage<any>,
13
+ event: MessageEvent<IEasyWebWorker.IMessageData<any>>
14
+ ) => void = () => {
15
+ // eslint-disable-next-line no-throw-literal
16
+ throw "you didnt defined a message-callback, please assign a callback by calling IEasyWorkerInstance.onMessage";
17
+ };
18
+
19
+ public onMessage = (
20
+ callback: (
21
+ message: IEasyWebWorker.IEasyWebWorkerMessage<any, any>,
22
+ event: MessageEvent<IEasyWebWorker.IMessageData<any>>
23
+ ) => void
24
+ ): void => {
25
+ this.onMessageCallback = callback;
32
26
  };
33
- };
27
+ })();
28
+
29
+ self.onmessage = (event: MessageEvent<any>) => {
30
+ const { messageId, payload } = event.data;
31
+
32
+ // each message should have his own resolution methods
33
+ const message = new (class
34
+ implements IEasyWebWorker.IEasyWebWorkerMessage<any, any>
35
+ {
36
+ payload: any = payload;
37
+
38
+ messageId: string = messageId;
39
+
40
+ public resolve = (...result: [null?] | [any]) =>
41
+ self.postMessage({ messageId, payload: result });
42
+
43
+ public reject = (error: string | Error) => this.resolve({ error });
44
+
45
+ public reportProgress = (progressPercentage: number) =>
46
+ self.postMessage({ messageId, progressPercentage });
47
+ })();
48
+
49
+ easyWorker.onMessageCallback(message, event);
50
+ };
51
+ `;
34
52
  exports.WorkerTemplate = WorkerTemplate;
35
53
  exports.default = exports.generatedId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easy-web-worker",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "This is a library to easily handle and create web-workers, if you are a web developer and never tried this... do not wait any longer! Workers are an amazing option to improve user experience and make your webpages even greater",
5
5
  "main": "lib/EasyWebWorker.js",
6
6
  "files": [