Skip to content
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

Remove event listener feature #2092

Merged
merged 5 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions documentation/1-promise.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,28 @@ Whether the promise is canceled.
### `promise.on(event, handler)`

The events are the same as in [Stream API](3-streams.md#events).

### `promise.off(event, handler)`

Removes listener registered with [`promise.on`](1-promise.md#promiseonevent-handler)

```js
import {createReadStream} from 'node:fs';
import got from 'got';

const ongoingRequestPromise = got.post(uploadUrl, {
body: createReadStream('sample.txt')
});

const eventListener = (progress: Progress) => {
console.log(progress);
};

ongoingRequestPromise.on('uploadProgress', eventListener);

setTimeout(() => {
ongoingRequestPromise.off('uploadProgress', eventListener);
}, 500);

await ongoingRequestPromise;
```
5 changes: 5 additions & 0 deletions source/as-promise/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ export default function asPromise<T>(firstRequest?: Request): CancelableRequest<
return promise;
};

promise.off = (event: string, fn: (...args: any[]) => void) => {
emitter.off(event, fn);
return promise;
};

const shortcut = <T>(responseType: Options['responseType']): CancelableRequest<T> => {
const newPromise = (async () => {
// Wait until downloading has ended
Expand Down
1 change: 1 addition & 0 deletions source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export type GotEventFunction<T> =
export interface RequestEvents<T> {
on: GotEventFunction<T>;
once: GotEventFunction<T>;
off: GotEventFunction<T>;
}

export type CacheableRequestFunction = (
Expand Down
41 changes: 41 additions & 0 deletions test/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,44 @@ test('upload progress - no body', withServer, async (t, server, got) => {
},
]);
});

test('upload progress - no events when immediatly removed listener', withServer, async (t, server, got) => {
server.post('/', uploadEndpoint);

const events: Progress[] = [];

const listener = (event: Progress) => events.push(event);

const promise = got.post('')
.on('uploadProgress', listener)
.off('uploadProgress', listener);

await promise;

t.is(events.length, 0);
});

test('upload progress - one event when removed listener', withServer, async (t, server, got) => {
server.post('/', uploadEndpoint);

const events: Progress[] = [];

const promise = got.post('');

const listener = (event: Progress) => {
events.push(event);
void promise.off('uploadProgress', listener);
};

void promise.on('uploadProgress', listener);

await promise;

t.deepEqual(events, [
{
percent: 0,
transferred: 0,
total: undefined,
},
]);
});