@wowistudio/grit 0.0.1 → 0.0.2
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/README.md +10 -1
- package/dist/builder.d.ts +3 -3
- package/dist/grit.js +8 -10
- package/dist/types.d.ts +3 -2
- package/dist/utils.js +7 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,10 +112,19 @@ await Grit.retry(0)
|
|
|
112
112
|
.fallback(slowerDbFetch)
|
|
113
113
|
.attempt(fastCacheLookup)
|
|
114
114
|
|
|
115
|
+
// Access the error within fallback
|
|
116
|
+
const result = await Grit.retry(0)
|
|
117
|
+
.withTimeout(5000)
|
|
118
|
+
.withFallback(async (error, attempts) => {
|
|
119
|
+
console.log(`Failed after ${attempts} attempts with error: ${error.message}`);
|
|
120
|
+
return slowerDbFetch()
|
|
121
|
+
})
|
|
122
|
+
.attempt(fastCacheLookup);
|
|
123
|
+
|
|
115
124
|
// Attempt return type is typesafe and will be a union of return type .fallback & .attempt
|
|
116
125
|
const result: number | string = await Grit.retry(0)
|
|
117
126
|
.withTimeout(100)
|
|
118
|
-
.
|
|
127
|
+
.withFallback(() => 1)
|
|
119
128
|
.attempt(() => 'a')
|
|
120
129
|
```
|
|
121
130
|
|
package/dist/builder.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GritError } from "./exceptions.js";
|
|
2
|
-
import type { DelayConfig, FunctionToExecute, TimeoutConfig } from "./types.js";
|
|
2
|
+
import type { DelayConfig, FallbackFunction, FunctionToExecute, TimeoutConfig } from "./types.js";
|
|
3
3
|
declare class GritBuilder<FallbackType = never> {
|
|
4
4
|
#private;
|
|
5
5
|
private retryCount?;
|
|
@@ -8,7 +8,7 @@ declare class GritBuilder<FallbackType = never> {
|
|
|
8
8
|
fn: FunctionToExecute<any> | undefined;
|
|
9
9
|
$delay: DelayConfig | undefined;
|
|
10
10
|
$timeout: TimeoutConfig | undefined;
|
|
11
|
-
$fallback:
|
|
11
|
+
$fallback: FallbackFunction<FallbackType> | undefined;
|
|
12
12
|
beforeRetryFn?: (retryCount: number) => void;
|
|
13
13
|
logging: boolean;
|
|
14
14
|
constructor(retryCount: number);
|
|
@@ -18,7 +18,7 @@ declare class GritBuilder<FallbackType = never> {
|
|
|
18
18
|
withDelay(config: DelayConfig): this;
|
|
19
19
|
withTimeout(timeout: TimeoutConfig): this;
|
|
20
20
|
beforeRetry(fn: (retryCount: number) => void): this;
|
|
21
|
-
withFallback<T>(fallback:
|
|
21
|
+
withFallback<T>(fallback: FallbackFunction<T>): GritBuilder<T>;
|
|
22
22
|
attempt<T = FallbackType>(fn: FunctionToExecute<T>): Promise<FallbackType extends never ? T : T | FallbackType>;
|
|
23
23
|
}
|
|
24
24
|
export { GritBuilder };
|
package/dist/grit.js
CHANGED
|
@@ -74,25 +74,24 @@ _Grit_instances = new WeakSet(), _Grit_currentDelay_get = function _Grit_current
|
|
|
74
74
|
}, _Grit_withTimeout = function _Grit_withTimeout(promise) {
|
|
75
75
|
let timer = undefined;
|
|
76
76
|
const wrappedPromise = new Promise((resolve, reject) => {
|
|
77
|
-
//
|
|
78
|
-
//
|
|
77
|
+
// promise.then(resolve, (error) => {
|
|
78
|
+
// clearTimeout(timer);
|
|
79
|
+
// timer = undefined;
|
|
80
|
+
// reject(error);
|
|
81
|
+
// })
|
|
79
82
|
promise.then(resolve, reject);
|
|
80
83
|
if (this.timeout === Number.POSITIVE_INFINITY)
|
|
81
84
|
return;
|
|
82
|
-
// We create the error outside of `setTimeout` to preserve the stack trace.
|
|
83
85
|
const message = `Promise timed out after ${this.timeout} milliseconds`;
|
|
84
86
|
const timeoutError = new TimeoutError(message);
|
|
85
87
|
timer = setTimeout(() => {
|
|
88
|
+
console.log('timeout joe joe joe');
|
|
86
89
|
reject(timeoutError);
|
|
87
90
|
}, this.timeout);
|
|
88
91
|
});
|
|
89
|
-
|
|
90
|
-
wrappedPromise.clear = () => {
|
|
92
|
+
const cancelablePromise = wrappedPromise.finally(() => {
|
|
91
93
|
clearTimeout(timer);
|
|
92
94
|
timer = undefined;
|
|
93
|
-
};
|
|
94
|
-
const cancelablePromise = wrappedPromise.finally(() => {
|
|
95
|
-
cancelablePromise.clear();
|
|
96
95
|
});
|
|
97
96
|
return cancelablePromise;
|
|
98
97
|
}, _Grit_processError = function _Grit_processError(error) {
|
|
@@ -108,7 +107,6 @@ _Grit_instances = new WeakSet(), _Grit_currentDelay_get = function _Grit_current
|
|
|
108
107
|
return __awaiter(this, void 0, void 0, function* () {
|
|
109
108
|
try {
|
|
110
109
|
__classPrivateFieldGet(this, _Grit_instances, "m", _Grit_handleBeforeRetry).call(this);
|
|
111
|
-
console.log('this.timeout', this.timeout);
|
|
112
110
|
if (this.timeout)
|
|
113
111
|
return yield __classPrivateFieldGet(this, _Grit_instances, "m", _Grit_withTimeout).call(this, Promise.resolve(fn(this.attempts)));
|
|
114
112
|
return yield fn(this.attempts);
|
|
@@ -116,7 +114,7 @@ _Grit_instances = new WeakSet(), _Grit_currentDelay_get = function _Grit_current
|
|
|
116
114
|
catch (error) {
|
|
117
115
|
if (this.retries >= this.retryCount) {
|
|
118
116
|
if (this.fallback)
|
|
119
|
-
return this.fallback(this.attempts);
|
|
117
|
+
return this.fallback(error, this.attempts);
|
|
120
118
|
throw error;
|
|
121
119
|
}
|
|
122
120
|
__classPrivateFieldGet(this, _Grit_instances, "m", _Grit_processError).call(this, error);
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export type GritProps = {
|
|
2
2
|
retryCount?: number;
|
|
3
3
|
fn?: FunctionToExecute<any>;
|
|
4
|
-
fallback?:
|
|
4
|
+
fallback?: FallbackFunction<any>;
|
|
5
5
|
onlyErrors?: (typeof Error)[];
|
|
6
6
|
skipErrors?: (typeof Error)[];
|
|
7
7
|
delay?: DelayConfig;
|
|
@@ -18,4 +18,5 @@ export type DelayConfig = number | number[] | {
|
|
|
18
18
|
minDelay: number;
|
|
19
19
|
maxDelay: number;
|
|
20
20
|
};
|
|
21
|
-
export type FunctionToExecute<T> = (attempts
|
|
21
|
+
export type FunctionToExecute<T> = (attempts: number) => (Promise<T> | T);
|
|
22
|
+
export type FallbackFunction<T> = (error: Error, attempts: number) => (Promise<T> | T);
|
package/dist/utils.js
CHANGED
|
@@ -6,10 +6,15 @@ export const isPromise = (value) => {
|
|
|
6
6
|
return value !== null && typeof value === "object" && typeof value.then === "function";
|
|
7
7
|
};
|
|
8
8
|
export const validateGritConfig = (config, retryCount) => {
|
|
9
|
+
if (retryCount !== 0 && !retryCount)
|
|
10
|
+
throw new GritError("Missing retry config (Grit.retry(<count>))");
|
|
11
|
+
if (typeof config === "number") {
|
|
12
|
+
if (config < 0)
|
|
13
|
+
throw new GritError("delay must be greater than 0");
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
9
16
|
if (!config)
|
|
10
17
|
return;
|
|
11
|
-
if (!retryCount)
|
|
12
|
-
throw new GritError("Missing retry config (Grit.retry(<count>))");
|
|
13
18
|
// validate array config
|
|
14
19
|
if (Array.isArray(config)) {
|
|
15
20
|
if (config.length !== retryCount)
|
|
@@ -20,11 +25,6 @@ export const validateGritConfig = (config, retryCount) => {
|
|
|
20
25
|
}
|
|
21
26
|
return;
|
|
22
27
|
}
|
|
23
|
-
if (typeof config === "number") {
|
|
24
|
-
if (config <= 0)
|
|
25
|
-
throw new GritError("delay must be greater than 0");
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
28
|
if (!isObject(config))
|
|
29
29
|
throw new GritError("Invalid backoff config");
|
|
30
30
|
// validate delay config
|