@travetto/worker 4.1.1 → 5.0.0-rc.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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 ArcSine Technologies
3
+ Copyright (c) 2023 ArcSine Technologies
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/worker",
3
- "version": "4.1.1",
3
+ "version": "5.0.0-rc.0",
4
4
  "description": "Process management utilities, with a focus on inter-process communication",
5
5
  "keywords": [
6
6
  "exec",
@@ -25,7 +25,7 @@
25
25
  "directory": "module/worker"
26
26
  },
27
27
  "dependencies": {
28
- "@travetto/base": "^4.1.1",
28
+ "@travetto/base": "^5.0.0-rc.0",
29
29
  "generic-pool": "^3.9.0"
30
30
  },
31
31
  "travetto": {
package/src/pool.ts CHANGED
@@ -48,7 +48,7 @@ export class WorkPool {
48
48
  try {
49
49
  pendingAcquires += 1;
50
50
  const res = isWorkerFactory(worker) ? await worker() : { execute: worker };
51
- res.id ??= Util.shortHash(`${Math.random()}`);
51
+ res.id ??= Util.uuid();
52
52
 
53
53
  if (res.init) {
54
54
  await res.init();
package/src/queue.ts CHANGED
@@ -32,7 +32,7 @@ export class WorkQueue<X> implements AsyncIterator<X>, AsyncIterable<X> {
32
32
  */
33
33
  async next(): Promise<IteratorResult<X>> {
34
34
  while (!this.#done && !this.#queue.length) {
35
- await this.#ready;
35
+ await this.#ready.promise;
36
36
  this.#ready = Util.resolvablePromise();
37
37
  }
38
38
  return { value: (this.#queue.length ? this.#queue.shift() : undefined)!, done: this.#done };
@@ -14,22 +14,17 @@ export class Barrier {
14
14
  /**
15
15
  * Listen for an unhandled event, as a promise
16
16
  */
17
- static listenForUnhandled(): Promise<never> & { cancel: () => void } {
18
- const uncaught = Util.resolvablePromise<never>();
19
- const uncaughtWithCancel: typeof uncaught & { cancel?: () => void } = uncaught;
17
+ static listenForUnhandled(): Promise<unknown> & { cancel: () => void } {
18
+ const uncaught = Util.resolvablePromise<unknown>();
20
19
  const onError = (err: Error): void => { Util.queueMacroTask().then(() => uncaught.reject(err)); };
21
20
  process.on('unhandledRejection', onError).on('uncaughtException', onError);
22
21
  const cancel = (): void => {
23
22
  process.off('unhandledRejection', onError).off('unhandledException', onError);
23
+ uncaught.resolve(undefined); // Close the promise
24
24
  };
25
- uncaughtWithCancel.cancel = (): void => {
26
- cancel(); // Remove the handler
27
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
28
- uncaughtWithCancel.resolve(undefined as never); // Close the promise
29
- };
30
-
25
+ Object.defineProperty(uncaught.promise, 'cancel', { value: cancel });
31
26
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
32
- return uncaughtWithCancel as (Promise<never> & { cancel: () => void });
27
+ return uncaught.promise as unknown as ReturnType<(typeof Barrier)['listenForUnhandled']>;
33
28
  }
34
29
 
35
30
  #support: string[] = [];
@@ -3,8 +3,4 @@ import { AppError } from '@travetto/base';
3
3
  /**
4
4
  * Represents an execution error
5
5
  */
6
- export class ExecutionError extends AppError {
7
- constructor(message: string, stack?: string) {
8
- super(message, 'general', {}, stack);
9
- }
10
- }
6
+ export class ExecutionError extends AppError { }
@@ -8,12 +8,12 @@ import { ExecutionError } from './error';
8
8
  export class Timeout extends ExecutionError {
9
9
 
10
10
  #timeout?: AbortController;
11
- #promise = Util.resolvablePromise();
11
+ #resolver = Util.resolvablePromise();
12
12
  #duration: number;
13
13
 
14
14
  constructor(duration: number | TimeSpan, op: string = 'Operation') {
15
15
  super(`${op} timed out after ${duration}${typeof duration === 'number' ? 'ms' : ''}`);
16
- this.#duration = TimeUtil.timeToMs(duration);
16
+ this.#duration = TimeUtil.asMillis(duration);
17
17
  }
18
18
 
19
19
  /**
@@ -21,7 +21,7 @@ export class Timeout extends ExecutionError {
21
21
  */
22
22
  cancel(): void {
23
23
  if (this.#timeout) {
24
- this.#promise.resolve();
24
+ this.#resolver.resolve();
25
25
  this.#timeout.abort();
26
26
  this.#timeout = undefined;
27
27
  }
@@ -34,9 +34,9 @@ export class Timeout extends ExecutionError {
34
34
  if (!this.#timeout) {
35
35
  this.#timeout = new AbortController();
36
36
  timers.setTimeout(this.#duration, undefined, { ref: false, signal: this.#timeout.signal })
37
- .then(() => this.#promise.reject(this))
37
+ .then(() => this.#resolver.reject(this))
38
38
  .catch(() => false);
39
39
  }
40
- return this.#promise;
40
+ return this.#resolver.promise;
41
41
  }
42
42
  }