-
Notifications
You must be signed in to change notification settings - Fork 58
feat(common): use addEventListener
instead of onabort
in async utils onAbortPromise
#645
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
Closed
douglascayers
wants to merge
10
commits into
powersync-ja:main
from
douglascayers:common-async-onabortpromise
Closed
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c740eb1
Refactor onAbortPromise to use addEventListener
douglascayers 599a603
Add changeset entry
douglascayers a0b8b61
Update .changeset/moody-hats-sleep.md
douglascayers 3808477
Remove test that doesn't test the SDK. The logic is explained in the …
douglascayers 01e3403
Avoid memory leaks by removing event listener after race.
douglascayers fcd53c9
Update changeset entry
douglascayers 7a6407d
Avoid memory leak by ensuring abort handler is removed
douglascayers d08cc08
Consolidate logic by calling abortHandler() if signal is already aborted
douglascayers f9561d2
Remove unused import of 'abort'
douglascayers 74a9a95
Merge remote-tracking branch 'origin/main' into common-async-onabortp…
douglascayers 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@powersync/common': patch | ||
--- | ||
|
||
Use addEventListener instead of overwriting the onabort property, which can lead to bugs and race conditions. | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { onAbortPromise } from '../../src/utils/async'; | ||
|
||
describe('onAbortPromise', () => { | ||
it('should resolve when signal is aborted', async () => { | ||
const controller = new AbortController(); | ||
const promise = onAbortPromise(controller.signal); | ||
|
||
// Abort the signal after a short delay | ||
setTimeout(() => controller.abort(), 10); | ||
|
||
// The promise should resolve | ||
await expect(promise).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('should resolve immediately if signal is already aborted', async () => { | ||
const controller = new AbortController(); | ||
controller.abort(); // Abort before creating promise | ||
|
||
const promise = onAbortPromise(controller.signal); | ||
|
||
// Should resolve immediately | ||
await expect(promise).resolves.toBeUndefined(); | ||
}); | ||
|
||
// This test emphasizes why not to use the 'onabort' property. | ||
it('should demonstrate that overwriting onabort property supersedes previous onabort logic', async () => { | ||
const controller = new AbortController(); | ||
const signal = controller.signal; | ||
|
||
// Set up first onabort handler | ||
const firstHandler = vi.fn(); | ||
signal.onabort = firstHandler; | ||
|
||
// Overwrite onabort property with a second handler | ||
const secondHandler = vi.fn(); | ||
signal.onabort = secondHandler; | ||
|
||
// Abort the signal | ||
controller.abort(); | ||
|
||
// Only the second handler should be called (first one was overwritten) | ||
expect(firstHandler).not.toHaveBeenCalled(); | ||
expect(secondHandler).toHaveBeenCalledTimes(1); | ||
}); | ||
douglascayers marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
it('should show that onAbortPromise does not interfere with onabort property or other event listeners', async () => { | ||
const controller = new AbortController(); | ||
const signal = controller.signal; | ||
|
||
// Set up onabort property handler | ||
const onabortHandler = vi.fn(); | ||
signal.onabort = onabortHandler; | ||
|
||
// Set up another event listener | ||
const eventListenerHandler = vi.fn(); | ||
signal.addEventListener('abort', eventListenerHandler); | ||
|
||
// Create onAbortPromise (which uses addEventListener internally) | ||
const promise = onAbortPromise(signal); | ||
|
||
// Abort the signal | ||
controller.abort(); | ||
|
||
// All handlers should be called | ||
expect(onabortHandler).toHaveBeenCalledTimes(1); | ||
expect(eventListenerHandler).toHaveBeenCalledTimes(1); | ||
|
||
// The promise should also resolve | ||
await expect(promise).resolves.toBeUndefined(); | ||
}); | ||
}); |
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.