Skip to content

Commit

Permalink
Fix InternalExecution interface
Browse files Browse the repository at this point in the history
  • Loading branch information
codenem committed Feb 20, 2024
1 parent 42b943b commit dc38d60
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-dancers-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@defer/client": patch
---

Fix InternalExecution interface
28 changes: 11 additions & 17 deletions src/backend/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { KV } from "./local/kv.js";
interface InternalExecution<F extends DeferableFunction> {
id: string;
args: string;
func: F;
func: DeferredFunction<F>;
functionId: string;
functionName: string;
state: ExecutionState;
Expand Down Expand Up @@ -232,9 +232,7 @@ async function loop(shouldRun: () => boolean): Promise<void> {
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 &&
Expand Down Expand Up @@ -348,7 +346,7 @@ export async function enqueue<F extends DeferableFunction>(
}

const now = new Date();
const execution: InternalExecution<any> = {
const execution: InternalExecution<F> = {
id: randomUUID(),
state: "created",
functionId: functionId,
Expand Down Expand Up @@ -464,7 +462,7 @@ export async function reRunExecution(
throw new ExecutionNotFound(`cannot find execution "${id}"`);

const now = new Date();
const newExecution: InternalExecution<any> = {
const newExecution: InternalExecution<typeof execution.func.__fn> = {
id: randomUUID(),
state: "created",
functionId: execution.functionId,
Expand All @@ -490,12 +488,10 @@ export async function listExecutions(
const data = new Map<string, Execution>();

for (const executionId of executionIds) {
const execution = (await executionsStore.get(
executionId,
)) as InternalExecution<any>;
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);
Expand All @@ -510,15 +506,13 @@ export async function listExecutionAttempts(
const data = new Map<string, Execution>();

for (const executionId of executionIds) {
const execution = (await executionsStore.get(
executionId,
)) as InternalExecution<any>;
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);
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,10 @@ export async function listExecutions(
* @throws {DeferError} when error is unknown
* @returns {Promise<Awaited<F>>}
*/
export function awaitResult<F extends DeferableFunction>(
export function awaitResult<F extends DeferableFunction, R = ReturnType<F>>(
fn: DeferredFunction<F>,
): (...args: Parameters<F>) => Promise<Awaited<F>> {
return async function (...args: Parameters<F>): Promise<Awaited<F>> {
): (...args: Parameters<F>) => Promise<Awaited<R>> {
return async function (...args: Parameters<F>): Promise<Awaited<R>> {
const enqueueResponse = await enqueue(fn, ...args);
await sleep(1000);

Expand All @@ -493,7 +493,7 @@ export function awaitResult<F extends DeferableFunction>(
throw error;
}
case "succeed":
return await getExecutionResult<Awaited<F>>(enqueueResponse.id);
return await getExecutionResult<Awaited<R>>(enqueueResponse.id);
case "aborted":
case "cancelled":
case "discarded":
Expand Down

0 comments on commit dc38d60

Please sign in to comment.