enisdenjo / graphql-ws

Coherent, zero-dependency, lazy, simple, GraphQL over WebSocket Protocol compliant server and client.
https://the-guild.dev/graphql/ws
MIT License
1.75k stars 162 forks source link

fix(server): wait for async generator cleanup to complete #474

Closed ttoohey closed 1 year ago

ttoohey commented 1 year ago

allow subscriptions to have a cleanup phase after calling .return() and ensure the iterator is 'done' before finishing

enisdenjo commented 1 year ago

Invoking the return method of an iterator should finish it immediately. Except for wrong iterator implementations - I don't think there are cases where you need to flush pending messages after completing it.

return(value) A function that accepts zero or one argument and returns an object conforming to the IteratorResult interface, typically with value equal to the value passed in and done equal to true. Calling this method tells the iterator that the caller does not intend to make any more next() calls and can perform any cleanup actions.

*The iterator protocol on MDN

Having read the return method definition on MDN, we can notice 2 things about it: