vivth 1.4.8 → 1.4.10

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vivth",
3
- "version": "1.4.8",
3
+ "version": "1.4.10",
4
4
  "description": "library primitives",
5
5
  "main": "index.mjs",
6
6
  "types": "./types/index.d.mts",
@@ -151,7 +151,6 @@ export class WorkerMainThread {
151
151
  * @returns {Promise<void>}
152
152
  */
153
153
  static async #workerFilehandler(handler, options, worker, listener) {
154
- let resolvedPath;
155
154
  const pathValidator = WorkerMainThread.pathValidator;
156
155
  const [resolvedPath_, error] = await TryAsync(async () => {
157
156
  if (Paths.root === undefined) {
@@ -170,7 +169,7 @@ export class WorkerMainThread {
170
169
  });
171
170
  return;
172
171
  }
173
- resolvedPath = resolvedPath_;
172
+ const resolvedPath = resolvedPath_;
174
173
  const runtime = GetRuntime();
175
174
  const workerClass = WorkerMainThread.workerClass;
176
175
  if (
@@ -185,15 +184,14 @@ export class WorkerMainThread {
185
184
  if (runtime !== 'browser') {
186
185
  throw new Error('not a browser');
187
186
  }
188
- let worker_;
189
- worker_ = worker.#worker.value = new workerClass(
187
+ const worker_ = (worker.#worker.value = new workerClass(
190
188
  resolvedPath,
191
189
  // @ts-expect-error
192
190
  {
193
191
  ...options,
194
192
  ...WorkerMainThread.#options,
195
193
  }
196
- );
194
+ ));
197
195
  if ('onmessage' in worker_ === false) {
198
196
  throw new Error('not a browser');
199
197
  }
@@ -205,18 +203,19 @@ export class WorkerMainThread {
205
203
  }
206
204
  },
207
205
  nonBrowser: async () => {
208
- const worker_ = (worker.#worker.value = new workerClass(
209
- resolvedPath,
210
- // @ts-expect-error
211
- {
212
- ...options,
213
- ...WorkerMainThread.#options,
214
- }
215
- ));
206
+ if (workerClass !== (await import('node:worker_threads')).Worker) {
207
+ throw "Worker are not impored from 'node:worker_threads'";
208
+ }
209
+ const worker_ = (worker.#worker.value = new workerClass(resolvedPath, {
210
+ ...options,
211
+ ...WorkerMainThread.#options,
212
+ }));
216
213
  if ('addEventListener' in worker_) {
214
+ // @ts-expect-error
217
215
  worker_.addEventListener('message', listener);
218
216
  if (SafeExit.instance) {
219
217
  SafeExit.instance.addCallback(async () => {
218
+ // @ts-expect-error
220
219
  worker_.removeEventListener('message', listener);
221
220
  });
222
221
  }
@@ -236,14 +235,12 @@ export class WorkerMainThread {
236
235
  Console.error(errorCreatingWorker);
237
236
  return;
238
237
  }
239
- if (SafeExit.instance) {
240
- SafeExit.instance.addCallback(async () => {
241
- if (worker.#worker.value) {
242
- worker.#worker.value.postMessage(closeWorkerThreadEventObject, []);
243
- }
244
- worker.terminate();
245
- });
238
+ if (!SafeExit.instance) {
239
+ return;
246
240
  }
241
+ SafeExit.instance.addCallback(async () => {
242
+ worker.terminate();
243
+ });
247
244
  }
248
245
  /**
249
246
  * lazyly generated because node version need to await
@@ -269,6 +266,7 @@ export class WorkerMainThread {
269
266
  * @return {void}
270
267
  */
271
268
  terminate = () => {
269
+ this.postMessage(closeWorkerThreadEventObject);
272
270
  /**
273
271
  * this is more for browser, as most of this are automatically cleaned with `SafeExit`;
274
272
  */
@@ -100,9 +100,8 @@ export class WorkerMainThread {
100
100
  * @returns {Promise<void>}
101
101
  */
102
102
  static async #workerFilehandler(handler, options, worker, listener) {
103
- let resolvedPath;
104
103
  WorkerMainThread.#options.eval = true;
105
- resolvedPath = (await FSInline.vivthFSInlineFile(handler)).toString('utf-8');
104
+ const resolvedPath = (await FSInline.vivthFSInlineFile(handler)).toString('utf-8');
106
105
  const runtime = GetRuntime();
107
106
  const workerClass = WorkerMainThread.workerClass;
108
107
  if (
@@ -117,16 +116,15 @@ export class WorkerMainThread {
117
116
  if (runtime !== 'browser') {
118
117
  throw new Error('not a browser');
119
118
  }
120
- let worker_;
121
119
  const inlineURL = Base64URL(handler, 'application/javascript', btoa);
122
- worker_ = worker.#worker.value = new workerClass(
120
+ const worker_ = (worker.#worker.value = new workerClass(
123
121
  inlineURL,
124
122
  // @ts-expect-error
125
123
  {
126
124
  ...options,
127
125
  ...WorkerMainThread.#options,
128
126
  }
129
- );
127
+ ));
130
128
  if ('onmessage' in worker_ === false) {
131
129
  throw new Error('not a browser');
132
130
  }
@@ -138,18 +136,19 @@ export class WorkerMainThread {
138
136
  }
139
137
  },
140
138
  nonBrowser: async () => {
141
- const worker_ = (worker.#worker.value = new workerClass(
142
- resolvedPath,
143
- // @ts-expect-error
144
- {
145
- ...options,
146
- ...WorkerMainThread.#options,
147
- }
148
- ));
139
+ if (workerClass !== (await import('node:worker_threads')).Worker) {
140
+ throw "Worker are not impored from 'node:worker_threads'";
141
+ }
142
+ const worker_ = (worker.#worker.value = new workerClass(resolvedPath, {
143
+ ...options,
144
+ ...WorkerMainThread.#options,
145
+ }));
149
146
  if ('addEventListener' in worker_) {
147
+ // @ts-expect-error
150
148
  worker_.addEventListener('message', listener);
151
149
  if (SafeExit.instance) {
152
150
  SafeExit.instance.addCallback(async () => {
151
+ // @ts-expect-error
153
152
  worker_.removeEventListener('message', listener);
154
153
  });
155
154
  }
@@ -169,14 +168,12 @@ export class WorkerMainThread {
169
168
  Console.error(errorCreatingWorker);
170
169
  return;
171
170
  }
172
- if (SafeExit.instance) {
173
- SafeExit.instance.addCallback(async () => {
174
- if (worker.#worker.value) {
175
- worker.#worker.value.postMessage(closeWorkerThreadEventObject, []);
176
- }
177
- await worker.terminate();
178
- });
171
+ if (!SafeExit.instance) {
172
+ return;
179
173
  }
174
+ SafeExit.instance.addCallback(async () => {
175
+ worker.terminate();
176
+ });
180
177
  }
181
178
  /**
182
179
  * lazyly generated because node version need to await
@@ -200,6 +197,7 @@ export class WorkerMainThread {
200
197
  * @return {void}
201
198
  */
202
199
  terminate = () => {
200
+ this.postMessage(closeWorkerThreadEventObject);
203
201
  /**
204
202
  * this is more for browser, as most of this are automatically cleaned with `SafeExit`;
205
203
  */
@@ -129,6 +129,14 @@ export class Dev {
129
129
  * }, 'vivthDevCodeBlock');
130
130
  */
131
131
  static vivthDevCodeBlock = async (callback, _closing) => {
132
+ if (
133
+ /**
134
+ * just in case Bundler doesn't properly clearup `vivthDevCodeBlock`
135
+ */
136
+ !Dev.isDev
137
+ ) {
138
+ return;
139
+ }
132
140
  await callback({ test: Dev.#test });
133
141
  };
134
142
  }