@defer.run/client
Advanced tools
Comparing version 0.1.0 to 0.2.0-alpha-20221221102447-045af78
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -6,5 +9,6 @@ exports.poolForExecutionResult = exports.serializeBackgroundFunctionArguments = exports.executeBackgroundFunction = void 0; | ||
const constants_js_1 = require("./constants.js"); | ||
function executeBackgroundFunction(fnName, args, fetcher, debug = false) { | ||
const parse_duration_1 = __importDefault(require("parse-duration")); | ||
function executeBackgroundFunction(fnName, args, fetcher, debug = false, executionOptions) { | ||
return new Promise((resolve, reject) => { | ||
const body = serializeBackgroundFunctionArguments(fnName, args); | ||
const body = serializeBackgroundFunctionArguments(fnName, args, executionOptions); | ||
if (!body) { | ||
@@ -42,3 +46,12 @@ throw new Error(`[defer.run][${fnName}] failed to serialize arguments`); | ||
exports.executeBackgroundFunction = executeBackgroundFunction; | ||
function serializeBackgroundFunctionArguments(fnName, args) { | ||
const delayOptionsToScheduleForValue = (delay) => { | ||
if (delay instanceof Date) { | ||
return delay.toISOString(); | ||
} | ||
else { | ||
const ms = (0, parse_duration_1.default)(delay); | ||
return new Date(Date.now() + ms).toISOString(); | ||
} | ||
}; | ||
function serializeBackgroundFunctionArguments(fnName, args, executionOptions) { | ||
let body = "[]"; | ||
@@ -49,2 +62,7 @@ try { | ||
arguments: args, | ||
...(executionOptions && executionOptions.delay | ||
? { | ||
schedule_for: delayOptionsToScheduleForValue(executionOptions.delay), | ||
} | ||
: {}), | ||
}); | ||
@@ -51,0 +69,0 @@ return body; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.defer = exports.isDeferExecution = exports.init = void 0; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
const constants_js_1 = require("./constants.js"); | ||
@@ -41,7 +40,3 @@ const execute_js_1 = require("./execute.js"); | ||
ret.__version = constants_js_1.INTERNAL_VERSION; | ||
return ret; | ||
}; | ||
exports.defer = defer; | ||
exports.defer.await = (fn) => { | ||
const ret = async (...args) => { | ||
ret.await = async (...args) => { | ||
const executionResult = await (0, exports.defer)(fn)(...args); | ||
@@ -55,5 +50,48 @@ // an exception is raised in case of failed execution creation, the below code becoming unreachable | ||
}; | ||
ret.__fn = fn; | ||
ret.__version = constants_js_1.INTERNAL_VERSION; | ||
ret.delayed = (...args) => { | ||
if (debug) { | ||
console.log(`[defer.run][${fn.name}] invoked.`); | ||
} | ||
if (token && fetcher) { | ||
return (0, execute_js_1.executeBackgroundFunction)(fn.name, args, fetcher, debug); | ||
} | ||
else { | ||
if (debug) { | ||
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
} | ||
// try to serialize arguments for develpment warning purposes | ||
(0, execute_js_1.serializeBackgroundFunctionArguments)(fn.name, args); | ||
// FIX: do better | ||
return fn(...args); | ||
} | ||
}; | ||
return ret; | ||
}; | ||
exports.defer = defer; | ||
// EXAMPLES: | ||
// | ||
// interface Contact { | ||
// id: string; | ||
// name: string; | ||
// } | ||
// | ||
// const importContacts = (companyId: string, contacts: Contact[]) => { | ||
// return new Promise<{ imported: number; companyId: string }>((resolve) => { | ||
// console.log(`Start importing contacts for company#${companyId}`); | ||
// setTimeout(() => { | ||
// console.log(contacts); | ||
// console.log("Done."); | ||
// resolve({ imported: 10000, companyId }); | ||
// }, 5000); | ||
// }); | ||
// }; | ||
// | ||
// const importContactsD = defer(importContacts); | ||
// | ||
// async function test() { | ||
// await importContactsD("1", []); // fire and forget | ||
// | ||
// await importContactsD.await("1", []); // wait for execution result | ||
// | ||
// await importContactsD.delayed("1", [], { delay: "2 days" }); // scheduled | ||
// } |
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { FN_EXECUTION_POLLING_INTERVAL_SECS } from "./constants.js"; | ||
export function executeBackgroundFunction(fnName, args, fetcher, debug = false) { | ||
import parseDuration from "parse-duration"; | ||
export function executeBackgroundFunction(fnName, args, fetcher, debug = false, executionOptions) { | ||
return new Promise((resolve, reject) => { | ||
const body = serializeBackgroundFunctionArguments(fnName, args); | ||
const body = serializeBackgroundFunctionArguments(fnName, args, executionOptions); | ||
if (!body) { | ||
@@ -37,3 +38,12 @@ throw new Error(`[defer.run][${fnName}] failed to serialize arguments`); | ||
} | ||
export function serializeBackgroundFunctionArguments(fnName, args) { | ||
const delayOptionsToScheduleForValue = (delay) => { | ||
if (delay instanceof Date) { | ||
return delay.toISOString(); | ||
} | ||
else { | ||
const ms = parseDuration(delay); | ||
return new Date(Date.now() + ms).toISOString(); | ||
} | ||
}; | ||
export function serializeBackgroundFunctionArguments(fnName, args, executionOptions) { | ||
let body = "[]"; | ||
@@ -44,2 +54,7 @@ try { | ||
arguments: args, | ||
...(executionOptions && executionOptions.delay | ||
? { | ||
schedule_for: delayOptionsToScheduleForValue(executionOptions.delay), | ||
} | ||
: {}), | ||
}); | ||
@@ -46,0 +61,0 @@ return body; |
@@ -1,2 +0,1 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { DOMAIN, INTERNAL_VERSION, PATH, TOKEN_ENV_NAME } from "./constants.js"; | ||
@@ -36,6 +35,3 @@ import { executeBackgroundFunction, poolForExecutionResult, serializeBackgroundFunctionArguments, } from "./execute.js"; | ||
ret.__version = INTERNAL_VERSION; | ||
return ret; | ||
}; | ||
defer.await = (fn) => { | ||
const ret = async (...args) => { | ||
ret.await = async (...args) => { | ||
const executionResult = await defer(fn)(...args); | ||
@@ -49,5 +45,47 @@ // an exception is raised in case of failed execution creation, the below code becoming unreachable | ||
}; | ||
ret.__fn = fn; | ||
ret.__version = INTERNAL_VERSION; | ||
ret.delayed = (...args) => { | ||
if (debug) { | ||
console.log(`[defer.run][${fn.name}] invoked.`); | ||
} | ||
if (token && fetcher) { | ||
return executeBackgroundFunction(fn.name, args, fetcher, debug); | ||
} | ||
else { | ||
if (debug) { | ||
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
} | ||
// try to serialize arguments for develpment warning purposes | ||
serializeBackgroundFunctionArguments(fn.name, args); | ||
// FIX: do better | ||
return fn(...args); | ||
} | ||
}; | ||
return ret; | ||
}; | ||
// EXAMPLES: | ||
// | ||
// interface Contact { | ||
// id: string; | ||
// name: string; | ||
// } | ||
// | ||
// const importContacts = (companyId: string, contacts: Contact[]) => { | ||
// return new Promise<{ imported: number; companyId: string }>((resolve) => { | ||
// console.log(`Start importing contacts for company#${companyId}`); | ||
// setTimeout(() => { | ||
// console.log(contacts); | ||
// console.log("Done."); | ||
// resolve({ imported: 10000, companyId }); | ||
// }, 5000); | ||
// }); | ||
// }; | ||
// | ||
// const importContactsD = defer(importContacts); | ||
// | ||
// async function test() { | ||
// await importContactsD("1", []); // fire and forget | ||
// | ||
// await importContactsD.await("1", []); // wait for execution result | ||
// | ||
// await importContactsD.delayed("1", [], { delay: "2 days" }); // scheduled | ||
// } |
{ | ||
"name": "@defer.run/client", | ||
"version": "0.1.0", | ||
"version": "0.2.0-alpha-20221221102447-045af78", | ||
"description": "cua JavaScript client", | ||
"dependencies": { | ||
"@whatwg-node/fetch": "^0.2.9" | ||
"@whatwg-node/fetch": "^0.2.9", | ||
"parse-duration": "^1.0.2" | ||
}, | ||
@@ -8,0 +9,0 @@ "repository": "git@github.com:charlypoly/cua-client.git", |
import type { DeferConfiguredFetcher } from "./fetcher.js"; | ||
import type { DeferExecutionOptions } from "./index.js"; | ||
export interface DeferExecuteResponse { | ||
@@ -7,4 +8,4 @@ id?: string; | ||
} | ||
export declare function executeBackgroundFunction(fnName: string, args: any[], fetcher: DeferConfiguredFetcher, debug?: boolean): Promise<DeferExecuteResponse>; | ||
export declare function serializeBackgroundFunctionArguments(fnName: string, args: any[]): string | false; | ||
export declare function executeBackgroundFunction(fnName: string, args: any[], fetcher: DeferConfiguredFetcher, debug?: boolean, executionOptions?: DeferExecutionOptions): Promise<DeferExecuteResponse>; | ||
export declare function serializeBackgroundFunctionArguments(fnName: string, args: any[], executionOptions?: DeferExecutionOptions): string | false; | ||
export interface DeferExecutionResponse { | ||
@@ -11,0 +12,0 @@ id: string; |
@@ -0,1 +1,2 @@ | ||
import type { Units } from "parse-duration"; | ||
import { DeferExecuteResponse } from "./execute.js"; | ||
@@ -10,2 +11,7 @@ export type { DeferExecuteResponse } from "./execute.js"; | ||
declare type UnPromise<F> = F extends Promise<infer R> ? R : F; | ||
export declare type DelayString = `${string}${Units}`; | ||
export interface DeferExecutionOptions { | ||
delay: DelayString | Date; | ||
} | ||
declare type DeferRetFnParameters<F extends (...args: any | undefined) => Promise<any>> = [...first: Parameters<F>, options: DeferExecutionOptions]; | ||
interface DeferRetFn<F extends (...args: any | undefined) => Promise<any>> { | ||
@@ -15,13 +21,12 @@ (...args: Parameters<F>): ReturnType<F>; | ||
__version: number; | ||
await: DeferAwaitRetFn<F>; | ||
delayed: (...args: DeferRetFnParameters<F>) => ReturnType<F>; | ||
} | ||
interface DeferAwaitRetFn<F extends (...args: any | undefined) => Promise<any>> { | ||
(...args: Parameters<F>): Promise<UnPromise<ReturnType<F>>>; | ||
__fn: F; | ||
__version: number; | ||
} | ||
interface Defer { | ||
<F extends (...args: any | undefined) => Promise<any>>(fn: F): DeferRetFn<F>; | ||
await: <F extends (...args: any | undefined) => Promise<any>>(fn: F) => DeferAwaitRetFn<F>; | ||
} | ||
export declare const isDeferExecution: (obj: any) => obj is DeferExecuteResponse; | ||
export declare const defer: Defer; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
24646
483
2
+ Addedparse-duration@^1.0.2
+ Addedparse-duration@1.1.1(transitive)