easy-web-worker 1.0.2 → 1.0.4
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 +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;
|
|
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:
|
|
15
|
+
type: "application/javascript",
|
|
16
16
|
}));
|
|
17
17
|
}
|
|
18
18
|
blobWorker(source, imports = []) {
|
|
19
|
-
const
|
|
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
|
|
26
|
-
|
|
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 +1 @@
|
|
|
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,
|
|
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,37 +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 = () =>
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
this.resolve = (...result) =>
|
|
27
|
-
// @ts-ignore
|
|
28
|
-
self.postMessage({ messageId, payload: result });
|
|
29
|
-
this.reject = (error) => this.resolve({ error });
|
|
30
|
-
this.reportProgress = (progressPercentage) =>
|
|
31
|
-
// @ts-ignore
|
|
32
|
-
self.postMessage({ messageId, progressPercentage });
|
|
33
|
-
}
|
|
34
|
-
})();
|
|
35
|
-
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;
|
|
36
26
|
};
|
|
37
|
-
};
|
|
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
|
+
`;
|
|
38
52
|
exports.WorkerTemplate = WorkerTemplate;
|
|
39
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.
|
|
3
|
+
"version": "1.0.4",
|
|
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": [
|