-
Notifications
You must be signed in to change notification settings - Fork 337
fix: handle late resolving promises with promise cancelation #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 34 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
6cc5451
fix: handle late resolving promises with promise cancelation
sarahdayan f01de26
feat: inline cancelable-promise
sarahdayan 00526a3
chore: remove dependency
sarahdayan d40c06a
feat: drop unused methods
sarahdayan 244c0db
feat: use functions instead of classes
sarahdayan 7610dcc
refactor: rename file
sarahdayan a8d9b3e
fix: fix types
sarahdayan 65a9483
fix: fix inconsistent behavior
sarahdayan 46468ff
refactor: rename type
sarahdayan 9ad3000
test: update tests
sarahdayan 516875c
build: bump bundle size
sarahdayan 8e2f05a
style: lint
sarahdayan 9702ecf
refactor: move things around
sarahdayan 7f484a7
feat: remove onCancel handler
sarahdayan 1eb4980
refactor: rename type
sarahdayan 4bf6c0e
fix: simplify code by narrowing type
sarahdayan a60e0d8
refactor: further simplify
sarahdayan 6e633fd
build: adjust bundlesize
sarahdayan 00b9cd0
build: drop yarn.lock changes
sarahdayan 511d7e3
refactor: alphabetize
sarahdayan 467ac44
test: clean up tests
sarahdayan 14a70d4
feat: catch in promise consumer to avoid unhandled promises
sarahdayan 494eb7c
chore: rename type and generic
sarahdayan 84a259f
test: use terser descriptions
sarahdayan 24e2a66
test: better descriptions
sarahdayan cef48a0
test: make the test more exhaustive
sarahdayan aedb8c2
refactor: rename internal parameter
sarahdayan 267b700
refactor: rename type
sarahdayan a431e71
test: refactor test
sarahdayan dcc7d23
test: further test concurrency behavior
sarahdayan 971eeb0
refactor: inline item removal
sarahdayan d68de50
feat: remove runWhenCanceled option
sarahdayan 3d65e25
feat: clear callbacks once triggered
sarahdayan 5163e7d
test: add missing test in both suites
sarahdayan de4a205
fix: s/running/pending/
sarahdayan ffd16fe
refactor: always pass all parameters and avoid defaults
sarahdayan c14844c
fix: avoid swallowing errors and return promise chain instead
sarahdayan 6038735
fix: apply suggestions from code review
sarahdayan d6b9d64
build: adjust bundlesize
sarahdayan 5735c23
chore(cancelable): simplify code (#876)
Haroenv 4fc760a
build: adjust bundlesize
sarahdayan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/autocomplete-core/src/__tests__/debouncing.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { noop } from '@algolia/autocomplete-shared'; | ||
| import userEvent from '@testing-library/user-event'; | ||
|
|
||
| import { createAutocomplete, InternalAutocompleteSource } from '..'; | ||
| import { createPlayground, createSource, defer } from '../../../../test/utils'; | ||
|
|
||
| type Source = InternalAutocompleteSource<{ label: string }>; | ||
|
|
||
| const delay = 10; | ||
|
|
||
| const debounced = debouncePromise<Source[][], Source[]>( | ||
| (items) => Promise.resolve(items), | ||
| delay | ||
| ); | ||
|
|
||
| describe('debouncing', () => { | ||
| test('only submits the final query', async () => { | ||
| const onStateChange = jest.fn(); | ||
| const getItems = jest.fn(({ query }) => [{ label: query }]); | ||
| const { inputElement } = createPlayground(createAutocomplete, { | ||
| onStateChange, | ||
| getSources: () => debounced([createSource({ getItems })]), | ||
| }); | ||
|
|
||
| userEvent.type(inputElement, 'abc'); | ||
|
|
||
| await defer(noop, delay); | ||
|
|
||
| expect(getItems).toHaveBeenCalledTimes(1); | ||
| expect(onStateChange).toHaveBeenLastCalledWith( | ||
| expect.objectContaining({ | ||
| state: expect.objectContaining({ | ||
| status: 'idle', | ||
| isOpen: true, | ||
| collections: expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| items: [{ __autocomplete_id: 0, label: 'abc' }], | ||
| }), | ||
| ]), | ||
| }), | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| test('triggers subsequent queries after reopening the panel', async () => { | ||
| const onStateChange = jest.fn(); | ||
| const getItems = jest.fn(({ query }) => [{ label: query }]); | ||
| const { inputElement } = createPlayground(createAutocomplete, { | ||
| onStateChange, | ||
| getSources: () => debounced([createSource({ getItems })]), | ||
| }); | ||
|
|
||
| userEvent.type(inputElement, 'abc{esc}'); | ||
|
|
||
| expect(onStateChange).toHaveBeenLastCalledWith( | ||
| expect.objectContaining({ | ||
| state: expect.objectContaining({ | ||
| status: 'idle', | ||
| isOpen: false, | ||
| }), | ||
| }) | ||
| ); | ||
|
|
||
| userEvent.type(inputElement, 'def'); | ||
|
|
||
| await defer(noop, delay); | ||
|
|
||
| expect(onStateChange).toHaveBeenLastCalledWith( | ||
| expect.objectContaining({ | ||
| state: expect.objectContaining({ | ||
| collections: expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| items: [{ __autocomplete_id: 0, label: 'abcdef' }], | ||
| }), | ||
| ]), | ||
| status: 'idle', | ||
| isOpen: true, | ||
| }), | ||
| }) | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| function debouncePromise<TParams extends unknown[], TResponse>( | ||
| fn: (...params: TParams) => Promise<TResponse>, | ||
| time: number | ||
| ) { | ||
| let timerId: ReturnType<typeof setTimeout> | undefined = undefined; | ||
|
|
||
| return function (...args: TParams) { | ||
| if (timerId) { | ||
| clearTimeout(timerId); | ||
| } | ||
|
|
||
| return new Promise<TResponse>((resolve) => { | ||
| timerId = setTimeout(() => resolve(fn(...args)), time); | ||
| }); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.