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

It should be possible to remove event listeners #2087

Closed
1 task done
Rychu-Pawel opened this issue Jul 22, 2022 · 3 comments
Closed
1 task done

It should be possible to remove event listeners #2087

Rychu-Pawel opened this issue Jul 22, 2022 · 3 comments
Labels
enhancement This change will extend Got features ✭ help wanted ✭

Comments

@Rychu-Pawel
Copy link
Contributor

What problem are you trying to solve? Describe the feature.

Currently, there is no way to remove event listeners, e.g., for uploadProgress event.

const ongoingRequestPromise = gotChunkInstance.post(uploadUrl, {
    body: await chunkStreamProvider.getChunkStream() //this is an FS ReadStream
});

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

ongoingRequestPromise.on("uploadProgress", eventListener);

setTimeout(() => {
    // This is not possible
    ongoingRequestPromise.removeEventListener("uploadProgress", eventListener);
}, 2000);

Checklist

  • I have read the documentation and made sure this feature doesn't already exist.
@sindresorhus
Copy link
Owner

You can just add a conditional in your listener to stop handling it.

@Rychu-Pawel
Copy link
Contributor Author

Do you mean something like this?

const ongoingRequestPromise = gotChunkInstance.post(uploadUrl, {
    body: await chunkStreamProvider.getChunkStream() //this is an FS ReadStream
});

let stopListening = false;

const eventListener = (progress: Progress) => {
    if (stopListening)
        return;

    console.log(progress);
};

ongoingRequestPromise.on("uploadProgress", eventListener);

setTimeout(() => {
   stopListening = true;
}, 2000);

Yes, I can but that doesn't look perfect... I would rather prefer to just unregister the listener :)

@szmarczak szmarczak added enhancement This change will extend Got features ✭ help wanted ✭ labels Jul 22, 2022
@sindresorhus
Copy link
Owner

Fixed by #2092

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This change will extend Got features ✭ help wanted ✭
Projects
None yet
Development

No branches or pull requests

3 participants