observable icon indicating copy to clipboard operation
observable copied to clipboard

Make Observable disposable

Open jasnell opened this issue 1 year ago • 1 comments

It would be useful for the Observable class to support the upcoming Symbol.dispose or Symbol.asyncDispose mechanism (TC39 Explicit Resource Management proposal) such that when the Observable is disposed while still active, any outstanding subscriptions and closed and outstanding promises are rejected.

using obs1 = eventTarget.on('foo');
using obs2 = Observable.from(asyncIterable);

This is particularly helpful in cases where a block of code may wish to only temporarily attach itself to a particular event source (like an EventTarget that long outlives the Observable)

jasnell avatar Jun 10 '24 23:06 jasnell

IMO, it isn't ideal to include a Symbol.dispose method on an observable. This makes it impossible for you to reason about the disposed-ness of an observable if it's passed to any other function. A better alternative would be to return a tuple [observable, disposalCallback] from the static methods that create observables, e.g. element.when.

Supporting Symbol.dispose means that any observable, when passed to another function, can always come back disposed, and you have no way (other than overriding the item stored at Symbol.dispose, if that will be allowed) to prevent this :/

rbalicki2 avatar Feb 28 '25 01:02 rbalicki2