Skip to content

Commit

Permalink
Fix isLoading state determination
Browse files Browse the repository at this point in the history
- Followup fix for #62
  • Loading branch information
yuriyyakym committed Dec 18, 2023
1 parent c019dc4 commit 700c0ea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/selector/asyncSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ const asyncSelector = <T extends (ReadableState | ReadableAsyncState)[], U>(
const currentPendingVersion = (lastPendingVersion + 1) % Number.MAX_SAFE_INTEGER;
lastPendingVersion = currentPendingVersion;

events.requested.emit();

const status = getAggregatedAsyncStatus(asyncStates);
const errors = asyncStates.map((state) => state.getAsync().error).filter(Boolean);

if (status === AsyncStatus.PENDING) {
isLoading = true;
}

events.requested.emit();

if (errors.length > 0) {
error = new AggregateError(errors);
value = undefined;
Expand All @@ -70,10 +74,6 @@ const asyncSelector = <T extends (ReadableState | ReadableAsyncState)[], U>(
return;
}

if (status === AsyncStatus.PENDING) {
isLoading === true;
}

if (status === AsyncStatus.FULFILLED) {
const values = states.map((state) => state.get()) as StatesValues;

Expand Down
3 changes: 2 additions & 1 deletion tests/selector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,12 @@ test('emits `requested` event when one of async dependencies is requested', asyn
expect(mergedState.get()).toEqual('Hello Noname');

setTimeout(() => {
nameState.set(delay(10).then(() => 'Awai'));
nameState.set(delay(20).then(() => 'Awai'));
}, 10);

await mergedState.events.requested;
expect(mergedState.getStatus()).toEqual(AsyncStatus.PENDING);
expect(mergedState.getAsync()).toMatchObject({ isLoading: true });
await delay(5);
expect(mergedState.events.fulfilled).resolves.toEqual('Hello Awai');
expect(mergedState.events.changed).resolves.toEqual('Hello Awai');
Expand Down

0 comments on commit 700c0ea

Please sign in to comment.