@defer/client
Advanced tools
Comparing version 1.3.0-alpha-20230324193114-b07b151 to 1.3.0-alpha-20230410232623-44ca317
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.INTERNAL_VERSION = void 0; | ||
exports.INTERNAL_VERSION = 3; | ||
exports.INTERNAL_VERSION = 4; |
@@ -74,2 +74,11 @@ "use strict"; | ||
exports.getExecution = getExecution; | ||
function defaultRetryPolicy() { | ||
return { | ||
maxAttempts: 13, | ||
initialInterval: 0.5, | ||
randomizationFactor: 0.5, | ||
multiplier: 1.5, | ||
maxInterval: 60, | ||
}; | ||
} | ||
const defer = (fn, options) => { | ||
@@ -102,9 +111,34 @@ const ret = async (...args) => { | ||
ret.__fn = fn; | ||
let retryPolicy = 0; | ||
if (options?.retry === true) { | ||
retryPolicy = 12; | ||
let retryPolicy = defaultRetryPolicy(); | ||
switch (typeof options?.retry) { | ||
case "boolean": { | ||
if (options.retry) | ||
retryPolicy = defaultRetryPolicy(); | ||
break; | ||
} | ||
case "number": { | ||
retryPolicy.maxAttempts = options.retry; | ||
break; | ||
} | ||
case "object": { | ||
if (options.retry.maxAttempts) | ||
retryPolicy.maxAttempts = options.retry.maxAttempts; | ||
if (options.retry.initialInterval) | ||
retryPolicy.initialInterval = options.retry.initialInterval; | ||
if (options.retry.randomizationFactor) | ||
retryPolicy.randomizationFactor = options.retry.randomizationFactor; | ||
if (options.retry.multiplier) | ||
retryPolicy.multiplier = options.retry.multiplier; | ||
if (options.retry.maxInterval) | ||
retryPolicy.maxInterval = options.retry.maxInterval; | ||
break; | ||
} | ||
case "undefined": { | ||
retryPolicy.maxAttempts = 0; | ||
break; | ||
} | ||
default: { | ||
throw new Error("invalid retry options"); | ||
} | ||
} | ||
if (typeof options?.retry === "number") { | ||
retryPolicy = options.retry; | ||
} | ||
ret.__metadata = { version: constants_js_1.INTERNAL_VERSION, retry: retryPolicy }; | ||
@@ -111,0 +145,0 @@ return ret; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = "1.3.0-alpha-20230324193114-b07b151"; | ||
exports.default = "1.3.0-alpha-20230410232623-44ca317"; |
@@ -1,1 +0,1 @@ | ||
export const INTERNAL_VERSION = 3; | ||
export const INTERNAL_VERSION = 4; |
@@ -65,2 +65,11 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
} | ||
function defaultRetryPolicy() { | ||
return { | ||
maxAttempts: 13, | ||
initialInterval: 0.5, | ||
randomizationFactor: 0.5, | ||
multiplier: 1.5, | ||
maxInterval: 60, | ||
}; | ||
} | ||
export const defer = (fn, options) => { | ||
@@ -93,9 +102,34 @@ const ret = async (...args) => { | ||
ret.__fn = fn; | ||
let retryPolicy = 0; | ||
if (options?.retry === true) { | ||
retryPolicy = 12; | ||
let retryPolicy = defaultRetryPolicy(); | ||
switch (typeof options?.retry) { | ||
case "boolean": { | ||
if (options.retry) | ||
retryPolicy = defaultRetryPolicy(); | ||
break; | ||
} | ||
case "number": { | ||
retryPolicy.maxAttempts = options.retry; | ||
break; | ||
} | ||
case "object": { | ||
if (options.retry.maxAttempts) | ||
retryPolicy.maxAttempts = options.retry.maxAttempts; | ||
if (options.retry.initialInterval) | ||
retryPolicy.initialInterval = options.retry.initialInterval; | ||
if (options.retry.randomizationFactor) | ||
retryPolicy.randomizationFactor = options.retry.randomizationFactor; | ||
if (options.retry.multiplier) | ||
retryPolicy.multiplier = options.retry.multiplier; | ||
if (options.retry.maxInterval) | ||
retryPolicy.maxInterval = options.retry.maxInterval; | ||
break; | ||
} | ||
case "undefined": { | ||
retryPolicy.maxAttempts = 0; | ||
break; | ||
} | ||
default: { | ||
throw new Error("invalid retry options"); | ||
} | ||
} | ||
if (typeof options?.retry === "number") { | ||
retryPolicy = options.retry; | ||
} | ||
ret.__metadata = { version: INTERNAL_VERSION, retry: retryPolicy }; | ||
@@ -102,0 +136,0 @@ return ret; |
@@ -1,1 +0,1 @@ | ||
export default "1.3.0-alpha-20230324193114-b07b151"; | ||
export default "1.3.0-alpha-20230410232623-44ca317"; |
{ | ||
"name": "@defer/client", | ||
"version": "1.3.0-alpha-20230324193114-b07b151", | ||
"version": "1.3.0-alpha-20230410232623-44ca317", | ||
"description": "Zero infrastructure NodeJS background jobs", | ||
@@ -34,18 +34,4 @@ "dependencies": { | ||
}, | ||
"./api": { | ||
"require": { | ||
"types": "./typings/client.d.cts", | ||
"default": "./cjs/client.js" | ||
}, | ||
"import": { | ||
"types": "./typings/client.d.ts", | ||
"default": "./esm/client.js" | ||
}, | ||
"default": { | ||
"types": "./typings/client.d.ts", | ||
"default": "./esm/client.js" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
} | ||
} |
@@ -1,1 +0,1 @@ | ||
export declare const INTERNAL_VERSION = 3; | ||
export declare const INTERNAL_VERSION = 4; |
@@ -19,3 +19,2 @@ import { Units } from "parse-duration"; | ||
declare type Range<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>; | ||
export declare type RetryNumber = Range<0, 13>; | ||
export declare type Concurrency = Range<0, 51>; | ||
@@ -26,3 +25,3 @@ export interface HasDeferMetadata { | ||
cron?: string; | ||
retry?: RetryNumber; | ||
retry?: RetryPolicy; | ||
concurrency?: Concurrency; | ||
@@ -46,4 +45,11 @@ }; | ||
} | ||
export interface RetryPolicy { | ||
maxAttempts: number; | ||
initialInterval: number; | ||
randomizationFactor: number; | ||
multiplier: number; | ||
maxInterval: number; | ||
} | ||
export interface DeferOptions { | ||
retry?: boolean | RetryNumber; | ||
retry?: boolean | number | Partial<RetryPolicy>; | ||
concurrency?: Concurrency; | ||
@@ -50,0 +56,0 @@ } |
@@ -1,2 +0,2 @@ | ||
declare const _default: "1.3.0-alpha-20230324193114-b07b151"; | ||
declare const _default: "1.3.0-alpha-20230410232623-44ca317"; | ||
export default _default; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
42796
1009