-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into one-event-return
- Loading branch information
Showing
7 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,34 @@ | ||
# add-listener | ||
|
||
Utility method to add listeners to events and remove them when the signal is aborted. | ||
|
||
Currently this **requires** a `signal`. Without a `signal`, you should just use the native `addListener` method as this has no advantages over it. | ||
|
||
```js | ||
import {addListener} from 'webext-events'; | ||
|
||
addListener(chrome.tabs.onCreated, (tab) => { | ||
console.log('Hurray, a new tab was created') | ||
}, {signal: AbortSignal.timeout(1000)}); | ||
``` | ||
|
||
> [!NOTE] | ||
> Background workers are unloaded and the status of `signal`s created within them is reset. Dealing with this is outside the responsibility of this library. | ||
## Compatibility | ||
|
||
- Any browser | ||
|
||
## Permissions | ||
|
||
- No special permissions | ||
|
||
## Context | ||
|
||
- Any context | ||
|
||
## Related | ||
|
||
- [abort-utils](https://github.com/fregante/abort-utils) - Utility functions to use and combine `AbortSignal` and `AbortController` with Promises. | ||
|
||
## [Main page ⏎](../readme.md) |
This file contains 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,30 @@ | ||
import { | ||
describe, it, vi, expect, expectTypeOf, | ||
} from 'vitest'; | ||
import {addListener} from './add-listener.js'; | ||
|
||
describe('addListener', () => { | ||
it('should remove the listener when the signal is aborted', () => { | ||
const event = { | ||
addListener: vi.fn(), | ||
removeListener: vi.fn(), | ||
}; | ||
const listener = vi.fn(); | ||
const controller = new AbortController(); | ||
addListener(event, listener, {signal: controller.signal}); | ||
|
||
expect(event.addListener).toHaveBeenCalledWith(listener); | ||
|
||
controller.abort(); | ||
|
||
expect(event.removeListener).toHaveBeenCalledWith(listener); | ||
}); | ||
|
||
it('should have the correct types', () => { | ||
addListener(chrome.tabs.onMoved, (tabId, tab) => { | ||
expectTypeOf(tabId).toEqualTypeOf<number>(); | ||
expectTypeOf(tab).toEqualTypeOf<chrome.tabs.TabMoveInfo>(); | ||
}, {signal: AbortSignal.timeout(1000)}); | ||
}); | ||
}); | ||
|
This file contains 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,26 @@ | ||
type AnyFunction = (...parameters: any[]) => void; | ||
|
||
type RemovableEvent<T = (...arguments_: unknown[]) => unknown> = { | ||
removeListener(callback: T): void; | ||
addListener(callback: T): void; | ||
}; | ||
|
||
export function addListener<Event extends RemovableEvent<AnyFunction>>( | ||
event: Event, | ||
listener: (...parameters: Parameters<Parameters<Event['addListener']>[0]>) => void, | ||
{ | ||
signal, | ||
}: { | ||
signal: AbortSignal; | ||
}, | ||
): void { | ||
if (signal?.aborted) { | ||
return; | ||
} | ||
|
||
event.addListener(listener); | ||
|
||
signal.addEventListener('abort', () => { | ||
event.removeListener(listener); | ||
}, {once: true}); | ||
} |
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './on-context-invalidated.js'; | ||
export * from './on-extension-start.js'; | ||
export * from './one-event.js'; | ||
export * from './add-listener.js'; |
This file contains 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 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 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