Symbol.dispose #7299
Replies: 3 comments 3 replies
-
I don't fully understand how Symbol.disposable works - anyone would care to elaborate? I can only find information about it on the TC39 proposal (which is hard to read for mortals), the TC39 repository, or on this article about how it will be available in Typescript 5.2 If I understood it correctly, this is useful for resources which need to be cleaned up whenever the scope of that resource is destroyed. A good example would be for file handles, from the TC39 repo: // Without Symbol.dispose
function * g() {
const handle = acquireFileHandle(); // critical resource
try {
...
}
finally {
handle.release(); // need to manually cleanup
}
}
// With Symbol.dispose
function * g() {
using handle = acquireFileHandle(); // block-scoped critical resource
...
// handle is automatically cleaned up, the runtime calls `handle[Symbol.dispose]()` once out of scope
} However, I don't see how this would work or help on RxJS regarding subscriptions. A subscription is not a "handle" of something that you would use and needs to be disposed off when no longer relevant. I find it hard to find an example where this would be useful: using mySubscription = source$.subscribe(value => /* do something with values getting pushed */); I can't find a case where I would want I can understand it for generators, or anything really where the actual resource (what you declare with Also I fail to understand how these can be composed or grouped together. |
Beta Was this translation helpful? Give feedback.
-
Yes, we're going to implement this. Although the use cases are maybe thin, they do exist. And it's a small enough add. Basically you could use it with async function addNumbersFromTwoUsers() {
let otherUserValue = 0;
using updatesFromOtherUser$.subscribe(value => {
otherUserValue = value;
})
const userValue = await getValueFromCurrentUser()
return otherUserValue + userValue;
} I mean, that's a contrived example, I suppose... but again, it's a trivial add. It also makes our |
Beta Was this translation helpful? Give feedback.
-
NodeJS is starting to add support for
Symbol.dispose
(before it lands in V8). Maybe would be a good idea to add support for this (when available) for theSubscription
class?Beta Was this translation helpful? Give feedback.
All reactions