Skip to content

Commit

Permalink
Fix selector requested event emission
Browse files Browse the repository at this point in the history
Closes #61
  • Loading branch information
yuriyyakym committed Dec 18, 2023
1 parent 2e4f866 commit 30be1fc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/selector/asyncSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,17 @@ const asyncSelector = <T extends (ReadableState | ReadableAsyncState)[], U>(
scenario(
async () => {
const abortController = new AbortController();
await Promise.race(states.map((state) => state.events.changed.abortable(abortController)));
await Promise.race(
states.flatMap((state) => {
return isReadableAsyncState(state)
? [
state.events.requested.abortable(abortController),
state.events.rejected.abortable(abortController),
state.events.fulfilled.abortable(abortController),
]
: state.events.changed.abortable(abortController);
}),
);
abortController.abort();
},
determineNextVersion,
Expand Down
3 changes: 2 additions & 1 deletion tests/selector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ test('emits `requested` event when one of async dependencies is requested', asyn
nameState.set(delay(10).then(() => 'Awai'));
}, 10);

expect(mergedState.events.requested).resolves.toBeUndefined();
await mergedState.events.requested;
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 30be1fc

Please sign in to comment.