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

Missing cleanup prototype functions #292

Closed
shanshanzhu opened this issue Sep 19, 2018 · 4 comments · Fixed by #847
Closed

Missing cleanup prototype functions #292

shanshanzhu opened this issue Sep 19, 2018 · 4 comments · Fixed by #847

Comments

@shanshanzhu
Copy link

shanshanzhu commented Sep 19, 2018

When we register a listener to 'data', 'error' events, the source code is basically overriding existing methods. There is no clean ways to unregister the listener. We'd expect a method such as off, removeListener. Even better, we can useaddListener to replace current implementation of on.

https://github.com/grpc/grpc-web/blob/master/javascript/net/grpc/web/grpcwebclientreadablestream.js#L211-L224

To clean up, in current version we have to do this:
stream.on('data', () => {})

@stanley-cheung
Copy link
Collaborator

stanley-cheung commented Sep 19, 2018

That's a fair point. Looks like gRPC Node's implementation uses EventEmitters. The .on() API can attach multiple listeners when called multiple times. And the returned stream object implemented the EventEmitters interface, which gives a removeListener method. Will definitely look into this.

loyalpartner pushed a commit to loyalpartner/grpc-web that referenced this issue Sep 4, 2020
* grpcweb: Support grpc-web-text content-type

Adds support for the application/grpc-web-text content type to the Go proxy.
Client support and tests will be added later.

Helps grpc#254.
@MihailsKuzmins
Copy link

Now there are two cleanup methods, i.e. cancel and removeListener. So I'm wondering if it's enough just to call stream.cancel on the end event, or should I remove all listeners explicitly with removeListener?
Or maybe cancel is not a cleanup method at all?

const stream = client.sayManyHellos(req, {});
stream.on("end", () => {
	stream.cancel()
})

Or more explicitly

const stream = client.sayManyHellos(req, {});
const onEnd = () => {
	stream.removeListener("end", onEnd)
	stream.cancel()
}

stream.on("end", onEnd)

@sampajano
Copy link
Collaborator

@MihailsKuzmins Thanks for the question!

As far as i understand, cancel() and removeListener() serves slightly different purposes (see the docs).

I haven't tried myself, but i do expect a few last callbacks (like end callbacks being invoked as in your example) after cancel() is called, unless you removes the listener also in your example.

Hope that makes sense :)

@MihailsKuzmins
Copy link

@sampajano thank you for explaining.

So as I understood it's better to remove the end callback rather than just relying on cancel(). I will adjust my code :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants