From 1471cc55562b0e22926c804f7cc7f5a418e3782c Mon Sep 17 00:00:00 2001 From: Phil Brockman <116004898+phil-loops@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:18:35 -0800 Subject: [PATCH 1/3] typescript update for the `DeferredFunction` --- src/backend/local.ts | 18 +++++++++--------- src/index.ts | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/backend/local.ts b/src/backend/local.ts index abfd8b17..8115b5a4 100644 --- a/src/backend/local.ts +++ b/src/backend/local.ts @@ -44,10 +44,10 @@ import version from "../version.js"; import { Counter } from "./local/counter.js"; import { KV } from "./local/kv.js"; -interface InternalExecution { +interface InternalExecution { id: string; args: string; - func: DeferableFunction; + func: F; functionId: string; functionName: string; state: ExecutionState; @@ -63,7 +63,7 @@ interface InternalExecution { } const concurrencyCounter = new Counter(); -const executionsStore = new KV(); +const executionsStore = new KV>(); const functionIdMapping = new Map(); const promisesState = new Set>(); @@ -137,7 +137,7 @@ function paginate( function isExecutionMatchFilter( filters: ExecutionFilters | undefined, - execution: InternalExecution, + execution: InternalExecution, ): boolean { if ( filters?.states && @@ -189,7 +189,7 @@ function isExecutionMatchFilter( return true; } -function buildExecution(execution: InternalExecution): Execution { +function buildExecution(execution: InternalExecution): Execution { return { id: execution.id, state: execution.state, @@ -348,7 +348,7 @@ export async function enqueue( } const now = new Date(); - const execution: InternalExecution = { + const execution: InternalExecution = { id: randomUUID(), state: "created", functionId: functionId, @@ -464,7 +464,7 @@ export async function reRunExecution( throw new ExecutionNotFound(`cannot find execution "${id}"`); const now = new Date(); - const newExecution: InternalExecution = { + const newExecution: InternalExecution = { id: randomUUID(), state: "created", functionId: execution.functionId, @@ -492,7 +492,7 @@ export async function listExecutions( for (const executionId of executionIds) { const execution = (await executionsStore.get( executionId, - )) as InternalExecution; + )) as InternalExecution; if (isExecutionMatchFilter(filters, execution)) data.set(executionId, buildExecution(execution)); @@ -512,7 +512,7 @@ export async function listExecutionAttempts( for (const executionId of executionIds) { const execution = (await executionsStore.get( executionId, - )) as InternalExecution; + )) as InternalExecution; if ( (execution.id === id || execution.retryOf === id) && diff --git a/src/index.ts b/src/index.ts index 23a2394d..930704b3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -70,7 +70,7 @@ export interface RetryPolicy { maxInterval: number; } -export type DeferableFunction = (...args: any) => Promise; +export type DeferableFunction = (...args: any[]) => Promise; export interface ExecutionOptions { delay?: Duration | Date; From 42b943bd4dc77fcf5ae594b21815bb963580d430 Mon Sep 17 00:00:00 2001 From: Phil Brockman <116004898+phil-loops@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:49:04 -0800 Subject: [PATCH 2/3] Create old-doors-explain.md --- .changeset/old-doors-explain.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/old-doors-explain.md diff --git a/.changeset/old-doors-explain.md b/.changeset/old-doors-explain.md new file mode 100644 index 00000000..9689abc4 --- /dev/null +++ b/.changeset/old-doors-explain.md @@ -0,0 +1,5 @@ +--- +"@defer/client": patch +--- + +update type definition for `DeferableFunction` From dc38d609b5bcbea33762f52e83cabaf8c7d5cb30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89mile=20R=C3=A9?= Date: Tue, 20 Feb 2024 17:14:27 +0400 Subject: [PATCH 3/3] Fix InternalExecution interface --- .changeset/long-dancers-judge.md | 5 +++++ src/backend/local.ts | 28 +++++++++++----------------- src/index.ts | 8 ++++---- 3 files changed, 20 insertions(+), 21 deletions(-) create mode 100644 .changeset/long-dancers-judge.md diff --git a/.changeset/long-dancers-judge.md b/.changeset/long-dancers-judge.md new file mode 100644 index 00000000..000d1622 --- /dev/null +++ b/.changeset/long-dancers-judge.md @@ -0,0 +1,5 @@ +--- +"@defer/client": patch +--- + +Fix InternalExecution interface diff --git a/src/backend/local.ts b/src/backend/local.ts index 8115b5a4..8785a4a7 100644 --- a/src/backend/local.ts +++ b/src/backend/local.ts @@ -47,7 +47,7 @@ import { KV } from "./local/kv.js"; interface InternalExecution { id: string; args: string; - func: F; + func: DeferredFunction; functionId: string; functionName: string; state: ExecutionState; @@ -232,9 +232,7 @@ async function loop(shouldRun: () => boolean): Promise { const execution = await executionsStore.transaction( executionId, async (execution) => { - const func = execution.func as DeferredFunction< - typeof execution.func - >; + const func = execution.func; shouldDiscard = execution.state === "created" && execution.discardAfter !== undefined && @@ -348,7 +346,7 @@ export async function enqueue( } const now = new Date(); - const execution: InternalExecution = { + const execution: InternalExecution = { id: randomUUID(), state: "created", functionId: functionId, @@ -464,7 +462,7 @@ export async function reRunExecution( throw new ExecutionNotFound(`cannot find execution "${id}"`); const now = new Date(); - const newExecution: InternalExecution = { + const newExecution: InternalExecution = { id: randomUUID(), state: "created", functionId: execution.functionId, @@ -490,12 +488,10 @@ export async function listExecutions( const data = new Map(); for (const executionId of executionIds) { - const execution = (await executionsStore.get( - executionId, - )) as InternalExecution; + const execution = await executionsStore.get(executionId); - if (isExecutionMatchFilter(filters, execution)) - data.set(executionId, buildExecution(execution)); + if (isExecutionMatchFilter(filters, execution!)) + data.set(executionId, buildExecution(execution!)); } return paginate(pageRequest, data); @@ -510,15 +506,13 @@ export async function listExecutionAttempts( const data = new Map(); for (const executionId of executionIds) { - const execution = (await executionsStore.get( - executionId, - )) as InternalExecution; + const execution = await executionsStore.get(executionId); if ( - (execution.id === id || execution.retryOf === id) && - isExecutionMatchFilter(filters, execution) + (execution!.id === id || execution!.retryOf === id) && + isExecutionMatchFilter(filters, execution!) ) - data.set(executionId, buildExecution(execution)); + data.set(executionId, buildExecution(execution!)); } return paginate(pageRequest, data); diff --git a/src/index.ts b/src/index.ts index 930704b3..ef97ee9b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -467,10 +467,10 @@ export async function listExecutions( * @throws {DeferError} when error is unknown * @returns {Promise>} */ -export function awaitResult( +export function awaitResult>( fn: DeferredFunction, -): (...args: Parameters) => Promise> { - return async function (...args: Parameters): Promise> { +): (...args: Parameters) => Promise> { + return async function (...args: Parameters): Promise> { const enqueueResponse = await enqueue(fn, ...args); await sleep(1000); @@ -493,7 +493,7 @@ export function awaitResult( throw error; } case "succeed": - return await getExecutionResult>(enqueueResponse.id); + return await getExecutionResult>(enqueueResponse.id); case "aborted": case "cancelled": case "discarded":