Skip to content

Commit

Permalink
More async action fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonmade committed May 29, 2024
1 parent df94c4d commit 9c50a0e
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions packages/async/source/AsyncAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,13 @@ export class AsyncAction<Data = unknown, Input = unknown> {
return this.latest.updatedAt;
}

#hasRun = false;
readonly #hasChanged: (
this: AsyncAction<Data, Input>,
input?: Input,
lastInput?: Input,
) => boolean;

#hasRun = false;
readonly #latestCalls = signal<AsyncActionResults<Data, Input>>({});

constructor(
fetchFunction: AsyncActionFunction<Data, Input>,
{
Expand All @@ -137,7 +135,7 @@ export class AsyncAction<Data = unknown, Input = unknown> {
input?: Input,
{signal}: {signal?: AbortSignal} = {},
): AsyncActionPromise<Data, Input> => {
const wasRunning = this.#latestCalls.peek().running;
const wasRunning = this.#running.peek();

const actionRun =
!this.#hasRun && this.initial.status === 'pending'
Expand All @@ -146,15 +144,10 @@ export class AsyncAction<Data = unknown, Input = unknown> {
finally: this.#finalizeAction,
});

this.#hasRun = true;
actionRun.start(input, {signal});

batch(() => {
this.#latestCalls.value = {
...this.#latestCalls.peek(),
running: actionRun,
};

this.#hasRun = true;
actionRun.start(input, {signal});
this.#running.value = actionRun;
wasRunning?.abort();
});

Expand All @@ -175,7 +168,7 @@ export class AsyncAction<Data = unknown, Input = unknown> {
};

hasChanged = (input?: Input) => {
return this.#hasChanged.call(this, input, this.latest.input);
return this.#hasChanged.call(this, input, this.#latestPeek().input);
};

#finalizeAction = (actionRun: AsyncActionRun<Data, Input>) => {
Expand All @@ -199,12 +192,6 @@ function defaultHasChanged(input?: unknown, lastInput?: unknown) {
return input !== lastInput && !dequal(input, lastInput);
}

interface AsyncActionResults<Data, Input> {
finished?: AsyncActionRun<Data, Input>;
resolved?: AsyncActionRun<Data, Input>;
running?: AsyncActionRun<Data, Input>;
}

export class AsyncActionRun<Data = unknown, Input = unknown> {
readonly promise: AsyncActionPromise<Data, Input>;
readonly action: AsyncAction<Data, Input>;
Expand Down Expand Up @@ -321,7 +308,7 @@ export class AsyncActionRun<Data = unknown, Input = unknown> {
}
}

abort = (reason?: any) => {
abort = (reason: any = new AsyncActionAbortError()) => {
this.#abortController.abort(reason);
};

Expand Down Expand Up @@ -370,6 +357,13 @@ export class AsyncActionRun<Data = unknown, Input = unknown> {
}
}

class AsyncActionAbortError extends Error {
constructor() {
super('The async action was aborted');
this.name = 'AsyncActionAbortError';
}
}

export interface AsyncActionPromise<Data, Input> extends Promise<Data> {
readonly status: AsyncActionStatus;
readonly value?: Data;
Expand Down

0 comments on commit 9c50a0e

Please sign in to comment.