readiness-manager 1.1.3 → 1.2.0-rc.x1a2475dd8
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/CHANGELOG.md +11 -1
- package/README.md +14 -79
- package/dist/src/error.d.ts +16 -0
- package/dist/src/error.d.ts.map +1 -0
- package/dist/src/error.js +23 -0
- package/dist/src/error.js.map +1 -0
- package/dist/src/index.d.ts +15 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +126 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/types.d.ts +17 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +3 -0
- package/dist/src/types.js.map +1 -0
- package/dist/tests/mock.d.ts +8 -0
- package/dist/tests/mock.d.ts.map +1 -0
- package/dist/tests/mock.js +39 -0
- package/dist/tests/mock.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/jest.config.ts +8 -0
- package/package.json +16 -27
- package/project.json +16 -0
- package/src/error.ts +46 -0
- package/src/index.ts +230 -0
- package/src/spec.ts +250 -0
- package/src/types.ts +38 -0
- package/tests/mock.ts +57 -0
- package/tsconfig.json +10 -0
- package/LICENSE +0 -8
- package/index.js +0 -1
- package/src/constants.js +0 -14
- package/src/error.js +0 -42
- package/src/index.js +0 -208
- package/src/types.d.ts +0 -40
- package/tests/mock.d.ts +0 -9
- package/tests/mock.js +0 -36
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 1.2.0 (19 January 2026)
|
|
4
|
+
|
|
5
|
+
- Migrated to platform sphinx
|
|
6
|
+
- Refactor to typescript(no breaking changes to the API)
|
|
7
|
+
- Add `debug` flag to analyze how long actions take to run.
|
|
8
|
+
|
|
9
|
+
## 1.1.4 (7 April 2022)
|
|
10
|
+
|
|
11
|
+
- Fix beacon initial execution, attempt was passed as index of the `forEach`.
|
|
12
|
+
|
|
3
13
|
## 1.1.3 (2 August 2021)
|
|
4
14
|
|
|
5
15
|
### Bug Fixes
|
|
@@ -29,4 +39,4 @@
|
|
|
29
39
|
|
|
30
40
|
### Improvements
|
|
31
41
|
|
|
32
|
-
- Adding `chain` option to manager api methods.
|
|
42
|
+
- Adding `chain` option to manager api methods.
|
package/README.md
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
Controlling node application readiness state.
|
|
3
|
-
|
|
4
1
|
## Motivation
|
|
5
2
|
|
|
6
3
|
In many cases node application depends on external resources that should be available before truly starting up.
|
|
@@ -24,7 +21,7 @@ ReadinessManager.register('translations', () => axios.get('https://www.translati
|
|
|
24
21
|
ReadinessManager.run();
|
|
25
22
|
|
|
26
23
|
// Will be triggered once all registered actions are successfully resolved.
|
|
27
|
-
ReadinessManager.onReady(() => console.log('App is ready!
|
|
24
|
+
ReadinessManager.onReady(() => console.log('App is ready! o/'));
|
|
28
25
|
```
|
|
29
26
|
|
|
30
27
|
This will allow you to set up an health check route, checking if you app readiness state as following:
|
|
@@ -33,102 +30,40 @@ This will allow you to set up an health check route, checking if you app readine
|
|
|
33
30
|
// routes.health.js
|
|
34
31
|
|
|
35
32
|
if (ReadinessManager.ready) {
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
res.status(200).end(`App is ready and running well`);
|
|
34
|
+
return;
|
|
38
35
|
} else {
|
|
39
|
-
|
|
36
|
+
res.status(503).end('App is not ready');
|
|
40
37
|
}
|
|
41
38
|
```
|
|
42
39
|
|
|
43
|
-
##
|
|
44
|
-
|
|
45
|
-
### ready: boolean
|
|
46
|
-
|
|
47
|
-
Returns `true` if all the registered actions has been ran without exceptions/rejections.
|
|
48
|
-
|
|
49
|
-
### register(name: string, action: ReadyAction): void
|
|
50
|
-
|
|
51
|
-
Registers a given name and action under `ReadinessManager`, registered actions will be observed once ran in order to determine the process readiness state.
|
|
52
|
-
|
|
53
|
-
```ts
|
|
54
|
-
type ReadyAction = () => void|Promise<void>;
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### run(): void
|
|
58
|
-
|
|
59
|
-
Runs all registered actions, once all actions are resolved successfully your'e app state will be determined as `ready`.
|
|
60
|
-
|
|
61
|
-
### onReady(callback: ReadyCallback): void
|
|
62
|
-
|
|
63
|
-
Registers a given callback on the global manager ready event.
|
|
64
|
-
|
|
65
|
-
```ts
|
|
66
|
-
type ReadyCallback = () => void;
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
### onActionReady(name: string callback: ReadyCallback): void
|
|
70
|
-
|
|
71
|
-
```ts
|
|
72
|
-
type ReadyCallback = () => void;
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
Registers a given callback on a specific action completion event.
|
|
76
|
-
|
|
77
|
-
### onError(errorHandler: ActionErrorHandler): void
|
|
78
|
-
|
|
79
|
-
```ts
|
|
80
|
-
interface ActionExecutionError extends Error {
|
|
81
|
-
name: string;
|
|
82
|
-
stack: string;
|
|
83
|
-
attempt: number;
|
|
84
|
-
failReason: string;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
type ActionRetry = () => Promise<void>;
|
|
88
|
-
|
|
89
|
-
type ActionErrorHandler = (error: ActionExecutionError, retry: ActionRetry) => void;
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
Registers given error handler under the `ReadinessManager`. Any error that will be thrown from execution one of the registered actions will trigger this handler.
|
|
93
|
-
|
|
94
|
-
> You can use the `retry` method provided to your error handler according to any failure strategy you like.
|
|
95
|
-
|
|
96
|
-
### status(): ActionsStatus
|
|
97
|
-
|
|
98
|
-
Returns a status report for current registered actions.
|
|
99
|
-
|
|
100
|
-
```ts
|
|
101
|
-
type ActionsStatus = {
|
|
102
|
-
[status in ActionStatus]: string[];
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
type ActionStatus = 'not_started' | 'pending' | 'resolved' | 'rejected';
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
### Errors handling
|
|
40
|
+
## Errors handling
|
|
109
41
|
|
|
110
42
|
You can register an error handler over the manager, which will be triggered from any thrown error from it's registered actions.
|
|
111
43
|
Once an error rises, the manager will trigger the error handler with both occurred error and a `retry` method used to re-execute the failing action if desired.
|
|
112
44
|
|
|
113
|
-
> The default error handle will just log errors to console and thus strongly recommended to register your
|
|
45
|
+
> The default error handle will just log errors to console and thus strongly recommended to register your own handler.
|
|
114
46
|
|
|
115
47
|
```js
|
|
116
48
|
// app.index.js
|
|
117
49
|
const ReadinessManager = require('readiness-manager');
|
|
118
50
|
|
|
119
51
|
let count = 0;
|
|
120
|
-
const unstableAction = () =>
|
|
52
|
+
const unstableAction = () =>
|
|
53
|
+
new Promise((resolve, reject) => {
|
|
121
54
|
count++;
|
|
122
|
-
if (counter <= 1) {
|
|
55
|
+
if (counter <= 1) {
|
|
56
|
+
return reject(new Error('Not yet'));
|
|
57
|
+
}
|
|
123
58
|
|
|
124
59
|
resolve('Ok');
|
|
125
|
-
});
|
|
60
|
+
});
|
|
126
61
|
|
|
127
62
|
ReadinessManager.register('action', unstableAction);
|
|
128
63
|
|
|
129
64
|
ReadinessManager.onError((error, retry) => {
|
|
130
|
-
|
|
131
|
-
|
|
65
|
+
console.log(error);
|
|
66
|
+
if (error.attempt <= 1) retry();
|
|
132
67
|
});
|
|
133
68
|
|
|
134
69
|
ReadinessManager.run();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const ERRORS: {
|
|
2
|
+
readonly BEACON_ALREADY_EXISTS: "Beacon is already exists, please ensure to use a unique name per beacon";
|
|
3
|
+
readonly BEACON_DOES_NOT_EXISTS: "Beacon does not exists, make sure to call `register` before trying to set beacon ready hook.";
|
|
4
|
+
readonly BEACON_EXECUTION_FAILED: "Beacon execution failed";
|
|
5
|
+
};
|
|
6
|
+
export type ActionRetry = () => Promise<unknown>;
|
|
7
|
+
export type ActionErrorHandler = (error: ActionExecutionError, retry: ActionRetry) => void;
|
|
8
|
+
export declare class ActionExecutionError extends Error {
|
|
9
|
+
name: string;
|
|
10
|
+
stack: string;
|
|
11
|
+
attempt: number;
|
|
12
|
+
failReason: string;
|
|
13
|
+
constructor(name: string, attempt: number, error: Error);
|
|
14
|
+
}
|
|
15
|
+
export declare const defaultErrorHandler: ActionErrorHandler;
|
|
16
|
+
//# sourceMappingURL=error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/error.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,MAAM;;;;CAKT,CAAC;AAMX,MAAM,MAAM,WAAW,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;AAKjD,MAAM,MAAM,kBAAkB,GAAG,CAAC,KAAK,EAAE,oBAAoB,EAAE,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;AAK3F,qBAAa,oBAAqB,SAAQ,KAAK;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;gBAEP,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK;CAS1D;AAKD,eAAO,MAAM,mBAAmB,EAAE,kBAA+D,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultErrorHandler = exports.ActionExecutionError = exports.ERRORS = void 0;
|
|
4
|
+
const obs_1 = require("@fiverr-private/obs");
|
|
5
|
+
exports.ERRORS = {
|
|
6
|
+
BEACON_ALREADY_EXISTS: 'Beacon is already exists, please ensure to use a unique name per beacon',
|
|
7
|
+
BEACON_DOES_NOT_EXISTS: 'Beacon does not exists, make sure to call `register` before trying to set beacon ready hook.',
|
|
8
|
+
BEACON_EXECUTION_FAILED: 'Beacon execution failed',
|
|
9
|
+
};
|
|
10
|
+
class ActionExecutionError extends Error {
|
|
11
|
+
constructor(name, attempt, error) {
|
|
12
|
+
super();
|
|
13
|
+
this.message = exports.ERRORS.BEACON_EXECUTION_FAILED;
|
|
14
|
+
this.name = name;
|
|
15
|
+
this.attempt = attempt;
|
|
16
|
+
this.stack = error.stack || '';
|
|
17
|
+
this.failReason = error.message;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.ActionExecutionError = ActionExecutionError;
|
|
21
|
+
const defaultErrorHandler = (beaconError) => obs_1.logger.error(beaconError);
|
|
22
|
+
exports.defaultErrorHandler = defaultErrorHandler;
|
|
23
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/error.ts"],"names":[],"mappings":";;;AAAA,6CAA6C;AAIhC,QAAA,MAAM,GAAG;IAClB,qBAAqB,EAAE,yEAAyE;IAChG,sBAAsB,EAClB,8FAA8F;IAClG,uBAAuB,EAAE,yBAAyB;CAC5C,CAAC;AAgBX,MAAa,oBAAqB,SAAQ,KAAK;IAM3C,YAAY,IAAY,EAAE,OAAe,EAAE,KAAY;QACnD,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,OAAO,GAAG,cAAM,CAAC,uBAAuB,CAAC;QAC9C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC;QAC/B,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC;IACpC,CAAC;CACJ;AAfD,oDAeC;AAKM,MAAM,mBAAmB,GAAuB,CAAC,WAAW,EAAE,EAAE,CAAC,YAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;AAArF,QAAA,mBAAmB,uBAAkE"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ActionExecutionError, ActionErrorHandler } from './error';
|
|
2
|
+
import type { ReadyAction, ReadyCallback, ActionsStatus } from './types';
|
|
3
|
+
export interface ReadinessManager {
|
|
4
|
+
register: (name: string, action: ReadyAction, debug?: boolean) => void;
|
|
5
|
+
run: () => ReadinessManager;
|
|
6
|
+
onReady: (callback: ReadyCallback) => ReadinessManager;
|
|
7
|
+
onActionReady: (name: string, callback: ReadyCallback) => ReadinessManager;
|
|
8
|
+
onError: (errorHandler: ActionErrorHandler) => ReadinessManager;
|
|
9
|
+
status: () => ActionsStatus;
|
|
10
|
+
ready: boolean;
|
|
11
|
+
}
|
|
12
|
+
declare const manager: ReadinessManager;
|
|
13
|
+
export default manager;
|
|
14
|
+
export { ActionExecutionError, ActionErrorHandler };
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAU,oBAAoB,EAAuB,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAChG,OAAO,KAAK,EAAgB,WAAW,EAAE,aAAa,EAAE,aAAa,EAAsB,MAAM,SAAS,CAAC;AAO3G,MAAM,WAAW,gBAAgB;IAC7B,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,KAAK,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IACvE,GAAG,EAAE,MAAM,gBAAgB,CAAC;IAC5B,OAAO,EAAE,CAAC,QAAQ,EAAE,aAAa,KAAK,gBAAgB,CAAC;IACvD,aAAa,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,KAAK,gBAAgB,CAAC;IAC3E,OAAO,EAAE,CAAC,YAAY,EAAE,kBAAkB,KAAK,gBAAgB,CAAC;IAChE,MAAM,EAAE,MAAM,aAAa,CAAC;IAC5B,KAAK,EAAE,OAAO,CAAC;CAClB;AAiND,QAAA,MAAM,OAAO,EAAE,gBAA6C,CAAC;AAE7D,eAAe,OAAO,CAAC;AACvB,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,CAAC"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _ReadinessManagerImpl_instances, _ReadinessManagerImpl_beacons, _ReadinessManagerImpl_callbacks, _ReadinessManagerImpl_errorHandler, _ReadinessManagerImpl_execute, _ReadinessManagerImpl_updateBeacon, _ReadinessManagerImpl_trackBeaconUpdate;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.ActionExecutionError = void 0;
|
|
16
|
+
const obs_1 = require("@fiverr-private/obs");
|
|
17
|
+
const error_1 = require("./error");
|
|
18
|
+
Object.defineProperty(exports, "ActionExecutionError", { enumerable: true, get: function () { return error_1.ActionExecutionError; } });
|
|
19
|
+
let readyState = false;
|
|
20
|
+
class ReadinessManagerImpl {
|
|
21
|
+
constructor() {
|
|
22
|
+
_ReadinessManagerImpl_instances.add(this);
|
|
23
|
+
_ReadinessManagerImpl_beacons.set(this, void 0);
|
|
24
|
+
_ReadinessManagerImpl_callbacks.set(this, void 0);
|
|
25
|
+
_ReadinessManagerImpl_errorHandler.set(this, void 0);
|
|
26
|
+
__classPrivateFieldSet(this, _ReadinessManagerImpl_beacons, {}, "f");
|
|
27
|
+
__classPrivateFieldSet(this, _ReadinessManagerImpl_callbacks, [], "f");
|
|
28
|
+
__classPrivateFieldSet(this, _ReadinessManagerImpl_errorHandler, error_1.defaultErrorHandler, "f");
|
|
29
|
+
}
|
|
30
|
+
get ready() {
|
|
31
|
+
return readyState;
|
|
32
|
+
}
|
|
33
|
+
status() {
|
|
34
|
+
return Object.values(__classPrivateFieldGet(this, _ReadinessManagerImpl_beacons, "f")).reduce((acc, { name, status }) => {
|
|
35
|
+
const current = acc[status] || [];
|
|
36
|
+
return Object.assign(acc, { [status]: [...current, name] });
|
|
37
|
+
}, {});
|
|
38
|
+
}
|
|
39
|
+
register(name, action, debug = false) {
|
|
40
|
+
if (this.ready) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (__classPrivateFieldGet(this, _ReadinessManagerImpl_beacons, "f")[name]) {
|
|
44
|
+
throw new Error(error_1.ERRORS.BEACON_ALREADY_EXISTS);
|
|
45
|
+
}
|
|
46
|
+
const beacon = {
|
|
47
|
+
name,
|
|
48
|
+
action,
|
|
49
|
+
callbacks: [],
|
|
50
|
+
status: 'not_started',
|
|
51
|
+
debug,
|
|
52
|
+
};
|
|
53
|
+
Object.assign(__classPrivateFieldGet(this, _ReadinessManagerImpl_beacons, "f"), { [name]: beacon });
|
|
54
|
+
}
|
|
55
|
+
run() {
|
|
56
|
+
readyState = false;
|
|
57
|
+
Object.values(__classPrivateFieldGet(this, _ReadinessManagerImpl_beacons, "f")).forEach((beacon) => __classPrivateFieldGet(this, _ReadinessManagerImpl_instances, "m", _ReadinessManagerImpl_execute).call(this, beacon));
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
onReady(callback) {
|
|
61
|
+
if (readyState) {
|
|
62
|
+
callback();
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
__classPrivateFieldGet(this, _ReadinessManagerImpl_callbacks, "f").push(callback);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
onActionReady(name, callback) {
|
|
69
|
+
const beacon = __classPrivateFieldGet(this, _ReadinessManagerImpl_beacons, "f")[name];
|
|
70
|
+
if (!beacon) {
|
|
71
|
+
throw new Error(error_1.ERRORS.BEACON_DOES_NOT_EXISTS);
|
|
72
|
+
}
|
|
73
|
+
if (beacon.status === 'resolved') {
|
|
74
|
+
callback();
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
__classPrivateFieldGet(this, _ReadinessManagerImpl_beacons, "f")[name].callbacks.push(callback);
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
onError(errorHandler) {
|
|
81
|
+
__classPrivateFieldSet(this, _ReadinessManagerImpl_errorHandler, errorHandler, "f");
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
_ReadinessManagerImpl_beacons = new WeakMap(), _ReadinessManagerImpl_callbacks = new WeakMap(), _ReadinessManagerImpl_errorHandler = new WeakMap(), _ReadinessManagerImpl_instances = new WeakSet(), _ReadinessManagerImpl_execute = async function _ReadinessManagerImpl_execute(beacon, attempt = 1) {
|
|
86
|
+
const { name, action, debug } = beacon;
|
|
87
|
+
const update = (status) => __classPrivateFieldGet(this, _ReadinessManagerImpl_instances, "m", _ReadinessManagerImpl_updateBeacon).call(this, name, status);
|
|
88
|
+
update('pending');
|
|
89
|
+
const startTime = debug ? Date.now() : null;
|
|
90
|
+
try {
|
|
91
|
+
const result = action();
|
|
92
|
+
if (result instanceof Promise) {
|
|
93
|
+
await result;
|
|
94
|
+
}
|
|
95
|
+
if (debug && startTime !== null) {
|
|
96
|
+
const duration = Date.now() - startTime;
|
|
97
|
+
obs_1.logger.debug(`[ReadinessManager] Action "${name}" completed in ${duration}ms`);
|
|
98
|
+
}
|
|
99
|
+
update('resolved');
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
if (debug && startTime !== null) {
|
|
103
|
+
const duration = Date.now() - startTime;
|
|
104
|
+
obs_1.logger.debug(`[ReadinessManager] Action "${name}" failed after ${duration}ms`);
|
|
105
|
+
}
|
|
106
|
+
update('rejected');
|
|
107
|
+
__classPrivateFieldGet(this, _ReadinessManagerImpl_errorHandler, "f").call(this, new error_1.ActionExecutionError(name, attempt, error), () => __classPrivateFieldGet(this, _ReadinessManagerImpl_instances, "m", _ReadinessManagerImpl_execute).call(this, beacon, attempt + 1));
|
|
108
|
+
}
|
|
109
|
+
}, _ReadinessManagerImpl_updateBeacon = function _ReadinessManagerImpl_updateBeacon(name, status) {
|
|
110
|
+
Object.assign(__classPrivateFieldGet(this, _ReadinessManagerImpl_beacons, "f")[name], { status });
|
|
111
|
+
if (status === 'resolved') {
|
|
112
|
+
__classPrivateFieldGet(this, _ReadinessManagerImpl_beacons, "f")[name].callbacks.forEach((callback) => callback());
|
|
113
|
+
}
|
|
114
|
+
__classPrivateFieldGet(this, _ReadinessManagerImpl_instances, "m", _ReadinessManagerImpl_trackBeaconUpdate).call(this);
|
|
115
|
+
}, _ReadinessManagerImpl_trackBeaconUpdate = function _ReadinessManagerImpl_trackBeaconUpdate() {
|
|
116
|
+
if (readyState) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
readyState = Object.values(__classPrivateFieldGet(this, _ReadinessManagerImpl_beacons, "f")).every((beacon) => beacon.status === 'resolved');
|
|
120
|
+
if (readyState) {
|
|
121
|
+
__classPrivateFieldGet(this, _ReadinessManagerImpl_callbacks, "f").forEach((callback) => callback());
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
const manager = new ReadinessManagerImpl();
|
|
125
|
+
exports.default = manager;
|
|
126
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index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EAAE,MAAoB;IAC5C,MAAM,CAAC,MAAM,CAAC,uBAAA,IAAI,qCAAS,CAAC,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;IAG/C,IAAI,MAAM,KAAK,UAAU,EAAE,CAAC;QACxB,uBAAA,IAAI,qCAAS,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC;IACpE,CAAC;IAED,uBAAA,IAAI,gFAAmB,MAAvB,IAAI,CAAqB,CAAC;AAC9B,CAAC;IAQG,IAAI,UAAU,EAAE,CAAC;QACb,OAAO;IACX,CAAC;IAED,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,uBAAA,IAAI,qCAAS,CAAC,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,KAAK,UAAU,CAAC,CAAC;IAE1F,IAAI,UAAU,EAAE,CAAC;QACb,uBAAA,IAAI,uCAAW,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC;IACtD,CAAC;AACL,CAAC;AAGL,MAAM,OAAO,GAAqB,IAAI,oBAAoB,EAAE,CAAC;AAE7D,kBAAe,OAAO,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type ActionStatus = 'not_started' | 'pending' | 'resolved' | 'rejected';
|
|
2
|
+
export type ReadyCallback = () => void;
|
|
3
|
+
export type ReadyAction = () => unknown | Promise<unknown>;
|
|
4
|
+
export type ActionsStatus = {
|
|
5
|
+
[key in ActionStatus]?: string[];
|
|
6
|
+
};
|
|
7
|
+
export interface Beacon {
|
|
8
|
+
name: string;
|
|
9
|
+
action: ReadyAction;
|
|
10
|
+
status: ActionStatus;
|
|
11
|
+
callbacks: ReadyCallback[];
|
|
12
|
+
debug?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface BeaconsMap {
|
|
15
|
+
[name: string]: Beacon;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,YAAY,GAAG,aAAa,GAAG,SAAS,GAAG,UAAU,GAAG,UAAU,CAAC;AAK/E,MAAM,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC;AAKvC,MAAM,MAAM,WAAW,GAAG,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAK3D,MAAM,MAAM,aAAa,GAAG;KACvB,GAAG,IAAI,YAAY,CAAC,CAAC,EAAE,MAAM,EAAE;CACnC,CAAC;AAKF,MAAM,WAAW,MAAM;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,WAAW,CAAC;IACpB,MAAM,EAAE,YAAY,CAAC;IACrB,SAAS,EAAE,aAAa,EAAE,CAAC;IAC3B,KAAK,CAAC,EAAE,OAAO,CAAC;CACnB;AAID,MAAM,WAAW,UAAU;IACvB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;CAC1B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ReadinessManager } from '../src/index';
|
|
2
|
+
export interface ReadinessManagerMock extends ReadinessManager {
|
|
3
|
+
setReady: (isReady: boolean) => void;
|
|
4
|
+
orchestrate: () => void;
|
|
5
|
+
}
|
|
6
|
+
declare const manager: ReadinessManagerMock;
|
|
7
|
+
export default manager;
|
|
8
|
+
//# sourceMappingURL=mock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock.d.ts","sourceRoot":"","sources":["../../tests/mock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAIhD,MAAM,WAAW,oBAAqB,SAAQ,gBAAgB;IAC1D,QAAQ,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;IACrC,WAAW,EAAE,MAAM,IAAI,CAAC;CAC3B;AA+CD,QAAA,MAAM,OAAO,EAAE,oBAAqD,CAAC;AAErE,eAAe,OAAO,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class ReadinessManagerMockImpl {
|
|
4
|
+
constructor() {
|
|
5
|
+
this._ready = false;
|
|
6
|
+
}
|
|
7
|
+
get ready() {
|
|
8
|
+
return this._ready;
|
|
9
|
+
}
|
|
10
|
+
status() {
|
|
11
|
+
return {};
|
|
12
|
+
}
|
|
13
|
+
setReady(isReady) {
|
|
14
|
+
this._ready = isReady;
|
|
15
|
+
}
|
|
16
|
+
register(_name, _action, _debug) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
run() {
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
orchestrate() {
|
|
23
|
+
this._ready = true;
|
|
24
|
+
}
|
|
25
|
+
onReady(callback) {
|
|
26
|
+
callback();
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
onActionReady(_name, callback) {
|
|
30
|
+
callback();
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
onError(_errorHandler) {
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const manager = new ReadinessManagerMockImpl();
|
|
38
|
+
exports.default = manager;
|
|
39
|
+
//# sourceMappingURL=mock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock.js","sourceRoot":"","sources":["../../tests/mock.ts"],"names":[],"mappings":";;AAYA,MAAM,wBAAwB;IAA9B;QACY,WAAM,GAAG,KAAK,CAAC;IAuC3B,CAAC;IArCG,IAAI,KAAK;QACL,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAED,MAAM;QACF,OAAO,EAAmB,CAAC;IAC/B,CAAC;IAED,QAAQ,CAAC,OAAgB;QACrB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,QAAQ,CAAC,KAAa,EAAE,OAAoB,EAAE,MAAgB;QAC1D,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,GAAG;QACC,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,WAAW;QACP,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;IACvB,CAAC;IAED,OAAO,CAAC,QAAuB;QAC3B,QAAQ,EAAE,CAAC;QACX,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,aAAa,CAAC,KAAa,EAAE,QAAuB;QAChD,QAAQ,EAAE,CAAC;QACX,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,OAAO,CAAC,aAAiC;QACrC,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AAED,MAAM,OAAO,GAAyB,IAAI,wBAAwB,EAAE,CAAC;AAErE,kBAAe,OAAO,CAAC"}
|