@trezor/utils
Advanced tools
Comparing version 9.0.5 to 9.0.6
@@ -0,1 +1,6 @@ | ||
# 9.0.6 | ||
- fix(utils): scheduleAction readonly param | ||
- feat(utils): scheduleAction with variable timeouts | ||
# 9.0.5 | ||
@@ -2,0 +7,0 @@ |
@@ -0,1 +1,2 @@ | ||
export * from './abortablePromise'; | ||
export * from './arrayDistinct'; | ||
@@ -2,0 +3,0 @@ export * from './arrayPartition'; |
@@ -30,2 +30,3 @@ "use strict"; | ||
exports.xssFilters = exports.versionUtils = exports.enumUtils = exports.bufferUtils = void 0; | ||
__exportStar(require("./abortablePromise"), exports); | ||
__exportStar(require("./arrayDistinct"), exports); | ||
@@ -32,0 +33,0 @@ __exportStar(require("./arrayPartition"), exports); |
export type ScheduledAction<T> = (signal?: AbortSignal) => Promise<T>; | ||
type AttemptParams = { | ||
timeout?: number; | ||
gap?: number; | ||
}; | ||
export type ScheduleActionParams = { | ||
delay?: number; | ||
deadline?: number; | ||
attempts?: number; | ||
timeout?: number; | ||
gap?: number; | ||
attempts?: number | readonly AttemptParams[]; | ||
signal?: AbortSignal; | ||
}; | ||
} & AttemptParams; | ||
export declare const scheduleAction: <T>(action: ScheduledAction<T>, params: ScheduleActionParams) => Promise<T>; | ||
export {}; | ||
//# sourceMappingURL=scheduleAction.d.ts.map |
@@ -13,2 +13,3 @@ "use strict"; | ||
exports.scheduleAction = void 0; | ||
const isArray = (attempts) => Array.isArray(attempts); | ||
const abortedBySignal = () => new Error('Aborted by signal'); | ||
@@ -72,10 +73,17 @@ const abortedByDeadline = () => new Error('Aborted by deadline'); | ||
break; | ||
const aborter = new AbortController(); | ||
const onClear = () => aborter.abort(); | ||
clear.addEventListener('abort', onClear); | ||
try { | ||
return yield attempt(); | ||
return yield attempt(a, aborter.signal); | ||
} | ||
catch (_a) { | ||
yield failure(); | ||
onClear(); | ||
yield failure(a); | ||
} | ||
finally { | ||
clear.removeEventListener('abort', onClear); | ||
} | ||
} | ||
return clear.aborted ? Promise.reject() : attempt(); | ||
return clear.aborted ? Promise.reject() : attempt(attempts - 1, clear); | ||
}); | ||
@@ -85,5 +93,10 @@ const scheduleAction = (action, params) => __awaiter(void 0, void 0, void 0, function* () { | ||
const deadlineMs = deadline && deadline - Date.now(); | ||
const attemptCount = attempts !== null && attempts !== void 0 ? attempts : (deadline ? Infinity : 1); | ||
const attemptCount = isArray(attempts) | ||
? attempts.length | ||
: attempts !== null && attempts !== void 0 ? attempts : (deadline ? Infinity : 1); | ||
const clearAborter = new AbortController(); | ||
const clear = clearAborter.signal; | ||
const getParams = isArray(attempts) | ||
? (attempt) => attempts[attempt] | ||
: () => ({ timeout, gap }); | ||
try { | ||
@@ -93,6 +106,6 @@ return yield Promise.race([ | ||
rejectAfterMs(deadlineMs, abortedByDeadline, clear), | ||
resolveAfterMs(delay, clear).then(() => attemptLoop(attemptCount, () => Promise.race([ | ||
rejectAfterMs(timeout, abortedByTimeout, clear), | ||
resolveAction(action, clear), | ||
]), () => resolveAfterMs(gap !== null && gap !== void 0 ? gap : 0, clear), clear)), | ||
resolveAfterMs(delay, clear).then(() => attemptLoop(attemptCount, (attempt, abort) => Promise.race([ | ||
rejectAfterMs(getParams(attempt).timeout, abortedByTimeout, clear), | ||
resolveAction(action, abort), | ||
]), attempt => { var _a; return resolveAfterMs((_a = getParams(attempt).gap) !== null && _a !== void 0 ? _a : 0, clear); }, clear)), | ||
]); | ||
@@ -99,0 +112,0 @@ } |
{ | ||
"name": "@trezor/utils", | ||
"version": "9.0.5", | ||
"version": "9.0.6", | ||
"author": "Trezor <info@trezor.io>", | ||
@@ -32,6 +32,6 @@ "homepage": "https://github.com/trezor/trezor-suite/tree/develop/packages/utils", | ||
"jest": "^26.6.3", | ||
"rimraf": "^3.0.2", | ||
"tsx": "^3.8.2", | ||
"typescript": "4.9.3" | ||
"rimraf": "^4.1.2", | ||
"tsx": "^3.12.3", | ||
"typescript": "4.9.5" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38673
68
733