@anonaddy/omni-worker 0.1.0 → 0.2.0

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.
Files changed (127) hide show
  1. package/README.md +22 -14
  2. package/dist/cjs/helpers/builder.js +22 -0
  3. package/dist/cjs/helpers/builder.js.map +1 -0
  4. package/dist/cjs/helpers/pool.js +9 -0
  5. package/dist/cjs/helpers/pool.js.map +1 -0
  6. package/dist/cjs/node/builder/helpers.js +14 -28
  7. package/dist/cjs/node/builder/helpers.js.map +1 -1
  8. package/dist/cjs/node/builder/index.js +2 -1
  9. package/dist/cjs/node/builder/index.js.map +1 -1
  10. package/dist/cjs/node/index.js +110 -51
  11. package/dist/cjs/node/index.js.map +1 -1
  12. package/dist/cjs/node/pool.js +131 -73
  13. package/dist/cjs/node/pool.js.map +1 -1
  14. package/dist/cjs/node.js +6 -0
  15. package/dist/cjs/node.js.map +1 -0
  16. package/dist/cjs/types/helpers.js +11 -0
  17. package/dist/cjs/types/helpers.js.map +1 -0
  18. package/dist/cjs/types/node-omni-worker.js +3 -0
  19. package/dist/cjs/types/node-omni-worker.js.map +1 -0
  20. package/dist/cjs/types/web-omni-worker.js +3 -0
  21. package/dist/cjs/types/web-omni-worker.js.map +1 -0
  22. package/dist/cjs/web/builder/helpers.js +9 -0
  23. package/dist/cjs/web/builder/helpers.js.map +1 -0
  24. package/dist/cjs/{node/builder/node.js → web/builder/index.js} +10 -17
  25. package/dist/cjs/web/builder/index.js.map +1 -0
  26. package/dist/cjs/web/builder/model.js +3 -0
  27. package/dist/cjs/web/builder/model.js.map +1 -0
  28. package/dist/cjs/web/index.js +156 -0
  29. package/dist/cjs/web/index.js.map +1 -0
  30. package/dist/cjs/web/pool.js +143 -0
  31. package/dist/cjs/web/pool.js.map +1 -0
  32. package/dist/cjs/web/provider/helpers.js +9 -0
  33. package/dist/cjs/web/provider/helpers.js.map +1 -0
  34. package/dist/cjs/web/provider/index.js +69 -0
  35. package/dist/cjs/web/provider/index.js.map +1 -0
  36. package/dist/cjs/web/provider/model.js +3 -0
  37. package/dist/cjs/web/provider/model.js.map +1 -0
  38. package/dist/cjs/web.js +8 -0
  39. package/dist/cjs/web.js.map +1 -0
  40. package/dist/mjs/helpers/builder.js +16 -0
  41. package/dist/mjs/helpers/builder.js.map +1 -0
  42. package/dist/mjs/helpers/pool.js +6 -0
  43. package/dist/mjs/helpers/pool.js.map +1 -0
  44. package/dist/mjs/node/builder/helpers.js +14 -27
  45. package/dist/mjs/node/builder/helpers.js.map +1 -1
  46. package/dist/mjs/node/builder/index.js +4 -3
  47. package/dist/mjs/node/builder/index.js.map +1 -1
  48. package/dist/mjs/node/index.js +111 -51
  49. package/dist/mjs/node/index.js.map +1 -1
  50. package/dist/mjs/node/pool.js +132 -73
  51. package/dist/mjs/node/pool.js.map +1 -1
  52. package/dist/mjs/node.js +2 -0
  53. package/dist/mjs/node.js.map +1 -0
  54. package/dist/mjs/types/helpers.js +8 -0
  55. package/dist/mjs/types/helpers.js.map +1 -0
  56. package/dist/mjs/types/node-omni-worker.js +2 -0
  57. package/dist/mjs/types/node-omni-worker.js.map +1 -0
  58. package/dist/mjs/types/web-omni-worker.js +2 -0
  59. package/dist/mjs/types/web-omni-worker.js.map +1 -0
  60. package/dist/mjs/web/builder/helpers.js +6 -0
  61. package/dist/mjs/web/builder/helpers.js.map +1 -0
  62. package/dist/mjs/web/builder/index.js +10 -0
  63. package/dist/mjs/web/builder/index.js.map +1 -0
  64. package/dist/mjs/web/builder/model.js +2 -0
  65. package/dist/mjs/web/builder/model.js.map +1 -0
  66. package/dist/mjs/web/index.js +120 -0
  67. package/dist/mjs/web/index.js.map +1 -0
  68. package/dist/mjs/web/pool.js +140 -0
  69. package/dist/mjs/web/pool.js.map +1 -0
  70. package/dist/mjs/web/provider/helpers.js +6 -0
  71. package/dist/mjs/web/provider/helpers.js.map +1 -0
  72. package/dist/mjs/web/provider/index.js +32 -0
  73. package/dist/mjs/web/provider/index.js.map +1 -0
  74. package/dist/mjs/web/provider/model.js +2 -0
  75. package/dist/mjs/web/provider/model.js.map +1 -0
  76. package/dist/mjs/web.js +3 -0
  77. package/dist/mjs/web.js.map +1 -0
  78. package/dist/types/helpers/builder.d.ts +2 -0
  79. package/dist/types/helpers/builder.d.ts.map +1 -0
  80. package/dist/types/helpers/pool.d.ts +2 -0
  81. package/dist/types/helpers/pool.d.ts.map +1 -0
  82. package/dist/types/node/builder/helpers.d.ts +0 -1
  83. package/dist/types/node/builder/helpers.d.ts.map +1 -1
  84. package/dist/types/node/builder/index.d.ts +2 -2
  85. package/dist/types/node/builder/index.d.ts.map +1 -1
  86. package/dist/types/node/index.d.ts +15 -13
  87. package/dist/types/node/index.d.ts.map +1 -1
  88. package/dist/types/node/pool.d.ts +4 -3
  89. package/dist/types/node/pool.d.ts.map +1 -1
  90. package/dist/types/node.d.ts +3 -0
  91. package/dist/types/node.d.ts.map +1 -0
  92. package/dist/types/types/helpers.d.ts +6 -0
  93. package/dist/types/types/helpers.d.ts.map +1 -0
  94. package/dist/types/types/node-omni-worker.d.ts +90 -0
  95. package/dist/types/types/node-omni-worker.d.ts.map +1 -0
  96. package/dist/types/types/web-omni-worker.d.ts +103 -0
  97. package/dist/types/types/web-omni-worker.d.ts.map +1 -0
  98. package/dist/types/web/builder/helpers.d.ts +3 -0
  99. package/dist/types/web/builder/helpers.d.ts.map +1 -0
  100. package/dist/types/web/builder/index.d.ts +8 -0
  101. package/dist/types/web/builder/index.d.ts.map +1 -0
  102. package/dist/types/web/builder/model.d.ts +11 -0
  103. package/dist/types/web/builder/model.d.ts.map +1 -0
  104. package/dist/types/web/index.d.ts +35 -0
  105. package/dist/types/web/index.d.ts.map +1 -0
  106. package/dist/types/web/pool.d.ts +31 -0
  107. package/dist/types/web/pool.d.ts.map +1 -0
  108. package/dist/types/web/provider/helpers.d.ts +2 -0
  109. package/dist/types/web/provider/helpers.d.ts.map +1 -0
  110. package/dist/types/web/provider/index.d.ts +16 -0
  111. package/dist/types/web/provider/index.d.ts.map +1 -0
  112. package/dist/types/web/provider/model.d.ts +7 -0
  113. package/dist/types/web/provider/model.d.ts.map +1 -0
  114. package/dist/types/web.d.ts +4 -0
  115. package/dist/types/web.d.ts.map +1 -0
  116. package/package.json +23 -9
  117. package/dist/cjs/index.js +0 -8
  118. package/dist/cjs/index.js.map +0 -1
  119. package/dist/cjs/node/builder/node.js.map +0 -1
  120. package/dist/mjs/index.js +0 -3
  121. package/dist/mjs/index.js.map +0 -1
  122. package/dist/mjs/node/builder/node.js +0 -14
  123. package/dist/mjs/node/builder/node.js.map +0 -1
  124. package/dist/types/index.d.ts +0 -4
  125. package/dist/types/index.d.ts.map +0 -1
  126. package/dist/types/node/builder/node.d.ts +0 -7
  127. package/dist/types/node/builder/node.d.ts.map +0 -1
@@ -1,86 +1,144 @@
1
1
  "use strict";
2
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
+ var _, done = false;
8
+ for (var i = decorators.length - 1; i >= 0; i--) {
9
+ var context = {};
10
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
+ if (kind === "accessor") {
15
+ if (result === void 0) continue;
16
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
+ if (_ = accept(result.get)) descriptor.get = _;
18
+ if (_ = accept(result.set)) descriptor.set = _;
19
+ if (_ = accept(result.init)) initializers.unshift(_);
20
+ }
21
+ else if (_ = accept(result)) {
22
+ if (kind === "field") initializers.unshift(_);
23
+ else descriptor[key] = _;
24
+ }
25
+ }
26
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
+ done = true;
28
+ };
29
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
+ var useValue = arguments.length > 2;
31
+ for (var i = 0; i < initializers.length; i++) {
32
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
+ }
34
+ return useValue ? value : void 0;
35
+ };
36
+ var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
37
+ if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
38
+ return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
39
+ };
2
40
  Object.defineProperty(exports, "__esModule", { value: true });
3
41
  exports.NodeOmniWorkerPool = void 0;
4
42
  const _1 = require(".");
5
- class NodeOmniWorkerPool {
6
- constructor(from, options) {
7
- this._pool = [];
8
- this._count = 0;
9
- this._lastUseIdx = -1;
10
- this.isInitialized = () => this._count > 0;
11
- this.getNumOfWorkers = () => this._count;
12
- this.use = () => {
13
- if (!this.isInitialized()) {
14
- return this._from.use();
15
- }
16
- else {
17
- const lastIdx = this._lastUseIdx;
18
- // first run or maxed out
19
- if (lastIdx === this._count - 1 ||
20
- lastIdx === -1) {
21
- this._lastUseIdx = 0;
22
- return this._pool[0].use();
23
- // any other run
43
+ const helpers_1 = require("../types/helpers");
44
+ let NodeOmniWorkerPool = (() => {
45
+ let _classDecorators = [(0, helpers_1.staticImplements)()];
46
+ let _classDescriptor;
47
+ let _classExtraInitializers = [];
48
+ let _classThis;
49
+ var NodeOmniWorkerPool = _classThis = class {
50
+ constructor(from, options) {
51
+ this._pool = [];
52
+ this._count = 0;
53
+ this._lastUseIdx = -1;
54
+ this.isInitialized = () => this._count > 0;
55
+ this.getNumOfWorkers = () => this._count;
56
+ this.use = () => {
57
+ if (!this.isInitialized()) {
58
+ return this._from.use();
24
59
  }
25
60
  else {
26
- this._lastUseIdx = lastIdx + 1;
27
- return this._pool[lastIdx + 1].use();
61
+ const lastIdx = this._lastUseIdx;
62
+ // first run or maxed out
63
+ if (lastIdx === this._count - 1 ||
64
+ lastIdx === -1) {
65
+ this._lastUseIdx = 0;
66
+ return this._pool[0].use();
67
+ // any other run
68
+ }
69
+ else {
70
+ this._lastUseIdx = lastIdx + 1;
71
+ return this._pool[lastIdx + 1].use();
72
+ }
28
73
  }
29
- }
30
- };
31
- this.destroy = async () => {
32
- const fns = [];
33
- this._pool.forEach(w => {
34
- const fn = async () => {
35
- await w.destroy();
36
- };
37
- fns.push(fn);
38
- });
39
- await Promise.all(fns.map(fn => fn()));
40
- };
41
- this._from = from;
42
- this._options = options || { numOfWorkers: 1 };
43
- this.applyOptions();
44
- }
45
- /**
46
- * First builds the OmniWorkers and then launches a new NodeOmniWorker pool
47
- * @param from The (relative) file path from your project's root to the worker .ts file
48
- * @param options (Optional) Options object for launching the pool
49
- */
50
- static async buildAndLaunch(from, options) {
51
- const worker = await _1.NodeOmniWorker.build(from);
52
- const pool = NodeOmniWorkerPool.launch(worker, options);
53
- return pool;
54
- }
55
- /**
56
- * Launches a new NodeOmniWorker pool according to the options provided
57
- * @param worker An already built NodeOmniWorker
58
- * @returns A newly created pool
59
- */
60
- static launch(worker, options) {
61
- const pool = new NodeOmniWorkerPool(worker, options);
62
- return pool;
63
- }
64
- /**
65
- * Applies the options provided
66
- */
67
- applyOptions() {
68
- const { numOfWorkers } = this._options;
69
- let count = 0;
70
- if (numOfWorkers === undefined) {
71
- count = 1;
74
+ };
75
+ this.destroy = async () => {
76
+ const fns = [];
77
+ this._pool.forEach(w => {
78
+ const fn = async () => {
79
+ await w.destroy();
80
+ };
81
+ fns.push(fn);
82
+ });
83
+ await Promise.all(fns.map(fn => fn()));
84
+ };
85
+ this._from = from;
86
+ this._options = options;
87
+ this.applyOptions();
72
88
  }
73
- else {
74
- count = numOfWorkers;
89
+ /**
90
+ * First builds the OmniWorkers and then launches a new NodeOmniWorker pool
91
+ * @param from The (relative) file path from your project's root to the worker .ts file
92
+ * @param options (Optional) Options object for launching the pool
93
+ */
94
+ static async buildAndLaunch(from, options = {
95
+ numOfWorkers: 1
96
+ }) {
97
+ const worker = await _1.NodeOmniWorker.build(from);
98
+ const pool = NodeOmniWorkerPool.launch(worker, options);
99
+ return pool;
75
100
  }
76
- if (count < 1) {
77
- throw Error(`number of workers must be at least 1`);
101
+ /**
102
+ * Launches a new NodeOmniWorker pool according to the options provided
103
+ * @param worker An already built NodeOmniWorker
104
+ * @returns A newly created pool
105
+ */
106
+ static launch(worker, options = {
107
+ numOfWorkers: 1
108
+ }) {
109
+ const pool = new NodeOmniWorkerPool(worker, options);
110
+ return pool;
78
111
  }
79
- this._count = count;
80
- for (let i = 0; i <= count; i++) {
81
- this._pool = this._from.clone(count);
112
+ /**
113
+ * Applies the options provided
114
+ */
115
+ applyOptions() {
116
+ const { numOfWorkers } = this._options;
117
+ let count = 0;
118
+ if (numOfWorkers === undefined) {
119
+ count = 1;
120
+ }
121
+ else {
122
+ count = numOfWorkers;
123
+ }
124
+ if (count < 1) {
125
+ throw Error(`number of workers must be at least 1`);
126
+ }
127
+ this._count = count;
128
+ for (let i = 0; i <= count; i++) {
129
+ this._pool = this._from.clone(count);
130
+ }
82
131
  }
83
- }
84
- }
132
+ };
133
+ __setFunctionName(_classThis, "NodeOmniWorkerPool");
134
+ (() => {
135
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
136
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
137
+ NodeOmniWorkerPool = _classThis = _classDescriptor.value;
138
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
139
+ __runInitializers(_classThis, _classExtraInitializers);
140
+ })();
141
+ return NodeOmniWorkerPool = _classThis;
142
+ })();
85
143
  exports.NodeOmniWorkerPool = NodeOmniWorkerPool;
86
144
  //# sourceMappingURL=pool.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"pool.js","sourceRoot":"","sources":["../../../src/node/pool.ts"],"names":[],"mappings":";;;AAAA,wBAAmC;AAGnC,MAAa,kBAAkB;IAQ7B,YACE,IAAuB,EACvB,OAAgC;QAR1B,UAAK,GAAwB,EAAE,CAAC;QAGhC,WAAM,GAAW,CAAC,CAAC;QACnB,gBAAW,GAAW,CAAC,CAAC,CAAC;QA4D1B,kBAAa,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QAEtC,oBAAe,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC;QAEpC,QAAG,GAAG,GAAG,EAAE;YAChB,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,EAAE,CAAC;gBAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;YAC1B,CAAC;iBAAM,CAAC;gBACN,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC;gBAEjC,yBAAyB;gBACzB,IACE,OAAO,KAAK,IAAI,CAAC,MAAM,GAAG,CAAC;oBAC3B,OAAO,KAAK,CAAC,CAAC,EACd,CAAC;oBACD,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC;oBACrB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;oBAE7B,gBAAgB;gBAChB,CAAC;qBAAM,CAAC;oBACN,IAAI,CAAC,WAAW,GAAG,OAAO,GAAG,CAAC,CAAC;oBAC/B,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;gBACvC,CAAC;YACH,CAAC;QACH,CAAC,CAAA;QAEM,YAAO,GAAG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M,CAAC,MAAM,CACX,MAAyB,EACzB,UAAkC;YAChC,YAAY,EAAE,CAAC;SAChB;YAED,MAAM,IAAI,GAAG,IAAI,kBAAkB,CAAI,MAAM,EAAE,OAAO,CAAC,CAAC;YACxD,OAAO,IAAI,CAAC;QACd,CAAC;QAED;;WAEG;QACK,YAAY;YAClB,MAAM,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC,QAAQ,CAAC;YAEvC,IAAI,KAAK,GAAG,CAAC,CAAC;YACd,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;gBAC/B,KAAK,GAAG,CAAC,CAAC;YACZ,CAAC;iBAAM,CAAC;gBACN,KAAK,GAAG,YAAY,CAAC;YACvB,CAAC;YAED,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;gBACd,MAAM,KAAK,CAAC,sCAAsC,CAAC,CAAC;YACtD,CAAC;YACD,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;YACpB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;gBAChC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACvC,CAAC;QACH,CAAC;;;;;QApEH,6KA0GC;;;QA1GY,uDAAkB;;;;AAAlB,gDAAkB"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NodeOmniWorker = void 0;
4
+ var index_1 = require("./node/index");
5
+ Object.defineProperty(exports, "NodeOmniWorker", { enumerable: true, get: function () { return index_1.NodeOmniWorker; } });
6
+ //# sourceMappingURL=node.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.js","sourceRoot":"","sources":["../../src/node.ts"],"names":[],"mappings":";;;AAMA,sCAEsB;AADpB,uGAAA,cAAc,OAAA"}
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.staticImplements = staticImplements;
4
+ /**
5
+ * Applies an interface with static function definitions
6
+ * @returns The constructor of class T
7
+ */
8
+ function staticImplements() {
9
+ return (constructor) => { constructor; };
10
+ }
11
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/types/helpers.ts"],"names":[],"mappings":";;AAIA,4CAEC;AAND;;;GAGG;AACH,SAAgB,gBAAgB;IAC5B,OAAO,CAAc,WAAc,EAAE,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;AAC7D,CAAC"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=node-omni-worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-omni-worker.js","sourceRoot":"","sources":["../../../src/types/node-omni-worker.ts"],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=web-omni-worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-omni-worker.js","sourceRoot":"","sources":["../../../src/types/web-omni-worker.ts"],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertToJsUrl = convertToJsUrl;
4
+ function convertToJsUrl(tsUrl, extension) {
5
+ tsUrl.pathname = tsUrl.pathname.replace(/\.ts$/, extension);
6
+ tsUrl.href = tsUrl.href.replace(/\.ts$/, extension);
7
+ return tsUrl;
8
+ }
9
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../../src/web/builder/helpers.ts"],"names":[],"mappings":";;AAEA,wCAIC;AAJD,SAAgB,cAAc,CAAC,KAAU,EAAE,SAAsB;IAC/D,KAAK,CAAC,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IAC5D,KAAK,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IACpD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -32,23 +32,16 @@ var __importStar = (this && this.__importStar) || (function () {
32
32
  return result;
33
33
  };
34
34
  })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
35
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.buildApiNode = void 0;
40
- const Comlink = __importStar(require("comlink"));
41
- const worker_threads_1 = require("worker_threads");
42
- const node_adapter_js_1 = __importDefault(require("comlink/dist/umd/node-adapter.js"));
36
+ exports.buildWebApiAndWorker = void 0;
37
+ const Comlink = __importStar(require("comlink/dist/esm/comlink"));
43
38
  const helpers_1 = require("./helpers");
44
- const path_1 = __importDefault(require("path"));
45
- const buildApiNode = async (workerPath) => {
46
- const callerDir = (0, helpers_1.getCallerDir)();
47
- const resolvedPath = path_1.default.resolve(callerDir, workerPath);
48
- const js = await (0, helpers_1.buildWorkerCode)(resolvedPath);
49
- const worker = new worker_threads_1.Worker(js, { eval: true });
50
- const api = Comlink.wrap((0, node_adapter_js_1.default)(worker));
51
- return { api, worker };
39
+ const buildWebApiAndWorker = (tsUrl, options) => {
40
+ const extension = options.extension || '.js';
41
+ const jsUrl = (0, helpers_1.convertToJsUrl)(tsUrl, extension);
42
+ const worker = new Worker(jsUrl, { type: 'module' });
43
+ const api = Comlink.wrap(worker);
44
+ return { jsUrl, worker, api };
52
45
  };
53
- exports.buildApiNode = buildApiNode;
54
- //# sourceMappingURL=node.js.map
46
+ exports.buildWebApiAndWorker = buildWebApiAndWorker;
47
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/web/builder/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kEAAoD;AACpD,uCAA2C;AAGpC,MAAM,oBAAoB,GAAG,CAClC,KAAU,EACV,OAAqC,EACyB,EAAE;IAEhE,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,KAAK,CAAC;IAC7C,MAAM,KAAK,GAAG,IAAA,wBAAc,EAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IAE/C,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;IACrD,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,CAAI,MAAM,CAAC,CAAC;IAEpC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;AAChC,CAAC,CAAA;AAZY,QAAA,oBAAoB,wBAYhC"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=model.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model.js","sourceRoot":"","sources":["../../../../src/web/builder/model.ts"],"names":[],"mappings":""}
@@ -0,0 +1,156 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
19
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
20
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
21
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
22
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
23
+ var _, done = false;
24
+ for (var i = decorators.length - 1; i >= 0; i--) {
25
+ var context = {};
26
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
27
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
28
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
29
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
30
+ if (kind === "accessor") {
31
+ if (result === void 0) continue;
32
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
33
+ if (_ = accept(result.get)) descriptor.get = _;
34
+ if (_ = accept(result.set)) descriptor.set = _;
35
+ if (_ = accept(result.init)) initializers.unshift(_);
36
+ }
37
+ else if (_ = accept(result)) {
38
+ if (kind === "field") initializers.unshift(_);
39
+ else descriptor[key] = _;
40
+ }
41
+ }
42
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
43
+ done = true;
44
+ };
45
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
46
+ var useValue = arguments.length > 2;
47
+ for (var i = 0; i < initializers.length; i++) {
48
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
49
+ }
50
+ return useValue ? value : void 0;
51
+ };
52
+ var __importStar = (this && this.__importStar) || (function () {
53
+ var ownKeys = function(o) {
54
+ ownKeys = Object.getOwnPropertyNames || function (o) {
55
+ var ar = [];
56
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
57
+ return ar;
58
+ };
59
+ return ownKeys(o);
60
+ };
61
+ return function (mod) {
62
+ if (mod && mod.__esModule) return mod;
63
+ var result = {};
64
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
65
+ __setModuleDefault(result, mod);
66
+ return result;
67
+ };
68
+ })();
69
+ var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
70
+ if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
71
+ return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
72
+ };
73
+ Object.defineProperty(exports, "__esModule", { value: true });
74
+ exports.WebOmniWorker = void 0;
75
+ const Comlink = __importStar(require("comlink/dist/esm/comlink"));
76
+ const helpers_1 = require("../types/helpers");
77
+ const builder_1 = require("./builder");
78
+ /**
79
+ * OmniWorker for the web
80
+ */
81
+ let WebOmniWorker = (() => {
82
+ let _classDecorators = [(0, helpers_1.staticImplements)(), (0, helpers_1.staticImplements)()];
83
+ let _classDescriptor;
84
+ let _classExtraInitializers = [];
85
+ let _classThis;
86
+ var WebOmniWorker = _classThis = class {
87
+ constructor(url, options, worker, api) {
88
+ this.isInitialized = () => this._api !== undefined;
89
+ this.use = () => {
90
+ const isInitialized = this.isInitialized();
91
+ if (isInitialized) {
92
+ return this._api;
93
+ }
94
+ else {
95
+ throw Error(`worker is not yet initialized. make sure to call the build() function, first`);
96
+ }
97
+ };
98
+ this.destroy = async () => {
99
+ var _a;
100
+ (_a = this._worker) === null || _a === void 0 ? void 0 : _a.terminate();
101
+ };
102
+ this.clone = (numOfTimes) => {
103
+ const workers = [];
104
+ const url = this._url;
105
+ const options = this._options;
106
+ for (let i = 0; i <= numOfTimes; i++) {
107
+ const { worker, api } = (0, builder_1.buildWebApiAndWorker)(url, options);
108
+ workers.push(new WebOmniWorker(url, options, worker, api));
109
+ }
110
+ return workers;
111
+ };
112
+ this._url = url;
113
+ this._options = options;
114
+ this._worker = worker;
115
+ this._api = api;
116
+ return this;
117
+ }
118
+ /**
119
+ * Handles the building of the necessary resources for an OmniWorker to function.
120
+ * This usually means, building from the consumer's code, creating the comlink
121
+ * interface between the worker and the main thread as well as the worker itself.
122
+ *
123
+ * @param tsUrl A URL object linking to the worker .ts file
124
+ * @param options Builder options
125
+ * @returns A WebOmniWorker
126
+ */
127
+ static async build(tsUrl, options = {
128
+ extension: '.js'
129
+ }) {
130
+ const { jsUrl, worker, api } = (0, builder_1.buildWebApiAndWorker)(tsUrl, options);
131
+ return new WebOmniWorker(jsUrl, options, worker, api);
132
+ }
133
+ };
134
+ __setFunctionName(_classThis, "WebOmniWorker");
135
+ (() => {
136
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
137
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
138
+ WebOmniWorker = _classThis = _classDescriptor.value;
139
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
140
+ })();
141
+ /**
142
+ * Expose the functions inside the worker to the rest of the application.
143
+ * After having exposed the functions, the build step can be initiated.
144
+ * @param exposable A class, object with functions or a single function to be
145
+ * exposed to the main thread.
146
+ */
147
+ _classThis.expose = (exposable) => {
148
+ Comlink.expose(exposable);
149
+ };
150
+ (() => {
151
+ __runInitializers(_classThis, _classExtraInitializers);
152
+ })();
153
+ return WebOmniWorker = _classThis;
154
+ })();
155
+ exports.WebOmniWorker = WebOmniWorker;
156
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/web/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,kEAAoD;AACpD,8CAAoD;AACpD,uCAAiD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AC,OAAO,GAAG,MAAM,CAAC;YACtB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;YAChB,OAAO,IAAI,CAAC;QACd,CAAC;QAYD;;;;;;;;WAQG;QACI,MAAM,CAAC,KAAK,CAAC,KAAK,CACvB,KAAU,EACV,UAAwC;YACtC,SAAS,EAAE,KAAK;SACjB;YAED,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,IAAA,8BAAoB,EAAI,KAAK,EAAE,OAAO,CAAC,CAAC;YACvE,OAAO,IAAI,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;QACxD,CAAC;;;;;QA9CH,6KAyEC;;;;IAtDC;;;;;OAKG;IACW,iBAAM,GAAG,CAAI,SAAY,EAAQ,EAAE;QAC/C,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IAC5B,CAAC,AAFmB,CAEnB;;QA3BU,uDAAa;;;;AAAb,sCAAa"}
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
+ var _, done = false;
8
+ for (var i = decorators.length - 1; i >= 0; i--) {
9
+ var context = {};
10
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
+ if (kind === "accessor") {
15
+ if (result === void 0) continue;
16
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
+ if (_ = accept(result.get)) descriptor.get = _;
18
+ if (_ = accept(result.set)) descriptor.set = _;
19
+ if (_ = accept(result.init)) initializers.unshift(_);
20
+ }
21
+ else if (_ = accept(result)) {
22
+ if (kind === "field") initializers.unshift(_);
23
+ else descriptor[key] = _;
24
+ }
25
+ }
26
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
+ done = true;
28
+ };
29
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
+ var useValue = arguments.length > 2;
31
+ for (var i = 0; i < initializers.length; i++) {
32
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
+ }
34
+ return useValue ? value : void 0;
35
+ };
36
+ var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
37
+ if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
38
+ return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
39
+ };
40
+ Object.defineProperty(exports, "__esModule", { value: true });
41
+ exports.WebOmniWorkerPool = void 0;
42
+ const _1 = require(".");
43
+ const helpers_1 = require("../types/helpers");
44
+ let WebOmniWorkerPool = (() => {
45
+ let _classDecorators = [(0, helpers_1.staticImplements)()];
46
+ let _classDescriptor;
47
+ let _classExtraInitializers = [];
48
+ let _classThis;
49
+ var WebOmniWorkerPool = _classThis = class {
50
+ constructor(from, options) {
51
+ this._pool = [];
52
+ this._count = 0;
53
+ this._lastUseIdx = -1;
54
+ this.isInitialized = () => this._count > 0;
55
+ this.getNumOfWorkers = () => this._count;
56
+ this.use = () => {
57
+ if (!this.isInitialized()) {
58
+ return this._from.use();
59
+ }
60
+ else {
61
+ const lastIdx = this._lastUseIdx;
62
+ // first run or maxed out
63
+ if (lastIdx === this._count - 1 ||
64
+ lastIdx === -1) {
65
+ this._lastUseIdx = 0;
66
+ return this._pool[0].use();
67
+ // any other run
68
+ }
69
+ else {
70
+ this._lastUseIdx = lastIdx + 1;
71
+ return this._pool[lastIdx + 1].use();
72
+ }
73
+ }
74
+ };
75
+ this.destroy = async () => {
76
+ const fns = [];
77
+ this._pool.forEach(w => {
78
+ const fn = async () => {
79
+ await w.destroy();
80
+ };
81
+ fns.push(fn);
82
+ });
83
+ await Promise.all(fns.map(fn => fn()));
84
+ };
85
+ this._from = from;
86
+ this._options = options || { numOfWorkers: 1 };
87
+ this.applyOptions();
88
+ }
89
+ /**
90
+ * First builds the OmniWorkers and then launches a new WebOmniWorker pool
91
+ * @param url A URL with the relative file path to the worker .ts file
92
+ * @param options (Optional) Options object for launching the pool
93
+ */
94
+ static async buildAndLaunch(url, options = {
95
+ extension: '.js',
96
+ numOfWorkers: (navigator === null || navigator === void 0 ? void 0 : navigator.hardwareConcurrency) || 1
97
+ }) {
98
+ const worker = await _1.WebOmniWorker.build(url, options);
99
+ const pool = WebOmniWorkerPool.launch(worker, options);
100
+ return pool;
101
+ }
102
+ /**
103
+ * Launches a new WebOmniWorker pool according to the options provided
104
+ * @param worker An already built WebOmniWorker
105
+ * @returns A newly created pool
106
+ */
107
+ static launch(worker, options) {
108
+ const pool = new WebOmniWorkerPool(worker, options);
109
+ return pool;
110
+ }
111
+ /**
112
+ * Applies the options provided
113
+ */
114
+ applyOptions() {
115
+ const { numOfWorkers } = this._options;
116
+ let count = 0;
117
+ if (numOfWorkers === undefined) {
118
+ count = 1;
119
+ }
120
+ else {
121
+ count = numOfWorkers;
122
+ }
123
+ if (count < 1) {
124
+ throw Error(`number of workers must be at least 1`);
125
+ }
126
+ this._count = count;
127
+ for (let i = 0; i <= count; i++) {
128
+ this._pool = this._from.clone(count);
129
+ }
130
+ }
131
+ };
132
+ __setFunctionName(_classThis, "WebOmniWorkerPool");
133
+ (() => {
134
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
135
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
136
+ WebOmniWorkerPool = _classThis = _classDescriptor.value;
137
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
138
+ __runInitializers(_classThis, _classExtraInitializers);
139
+ })();
140
+ return WebOmniWorkerPool = _classThis;
141
+ })();
142
+ exports.WebOmniWorkerPool = WebOmniWorkerPool;
143
+ //# sourceMappingURL=pool.js.map