Skip to content

Commit

Permalink
Add .off() method for events (#2092)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rychu-Pawel authored Jul 28, 2022
1 parent c41a98a commit 88056be
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
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,
},
]);
});

0 comments on commit 88056be

Please sign in to comment.