@travetto/worker 4.1.0 → 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/package.json +2 -2
- package/src/pool.ts +1 -1
- package/src/queue.ts +1 -1
- package/src/support/barrier.ts +5 -10
- package/src/support/error.ts +1 -5
- package/src/support/timeout.ts +5 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/worker",
|
|
3
|
-
"version": "
|
|
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": "^
|
|
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
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 };
|
package/src/support/barrier.ts
CHANGED
|
@@ -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<
|
|
18
|
-
const uncaught = Util.resolvablePromise<
|
|
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
|
-
|
|
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
|
|
27
|
+
return uncaught.promise as unknown as ReturnType<(typeof Barrier)['listenForUnhandled']>;
|
|
33
28
|
}
|
|
34
29
|
|
|
35
30
|
#support: string[] = [];
|
package/src/support/error.ts
CHANGED
|
@@ -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 { }
|
package/src/support/timeout.ts
CHANGED
|
@@ -8,12 +8,12 @@ import { ExecutionError } from './error';
|
|
|
8
8
|
export class Timeout extends ExecutionError {
|
|
9
9
|
|
|
10
10
|
#timeout?: AbortController;
|
|
11
|
-
#
|
|
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.
|
|
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.#
|
|
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.#
|
|
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
|
}
|