Skip to content

Commit

Permalink
Tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Nov 17, 2024
1 parent 1334ccd commit 8d36744
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
34 changes: 34 additions & 0 deletions source/add-listener.md
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)
23 changes: 23 additions & 0 deletions source/add-listener.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
describe, it, vi, expect,
} 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);
});
});

2 changes: 2 additions & 0 deletions source/on-context-invalidated.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ fetch('/api', {signal: onContextInvalidated.signal})
- any context

Some contexts like the background page/worker and standalone tabs will be closed by the browser automatically so the event doesn't apply there.

## [Main page ⏎](../readme.md)
2 changes: 2 additions & 0 deletions source/on-extension-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ onExtensionStart.addListener(listener);
- background worker
- background page
- event page (not in Chrome)

## [Main page ⏎](../readme.md)
2 changes: 2 additions & 0 deletions source/one-event.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ if (timeout.aborted) {
## Related
- [one-event](https://github.com/fregante/one-event) - The same thing, but for regular browser events.
## [Main page ⏎](../readme.md)

0 comments on commit 8d36744

Please sign in to comment.