To prevent programmer errors like using an object after it has been disposed, use assertNotDisposed guards. This is especially useful when a helper manages internal state (like a reference counter) rather than an external resource.
In Evolu, it is a convention to call assertNotDisposed(disposables) inside synchronous methods of a returned object to ensure the underlying DisposableStack is still active. For async operations, if an operation is aborted due to disposal, use assertNotAborted to distinguish lifecycle-related aborts from ordinary control flow.
const createRefCount = (): RefCount => {
using disposer = new DisposableStack();
let count = 0;
const disposables = disposer.move();
return {
increment: () => {
assertNotDisposed(disposables);
count += 1;
return count;
},
getCount: () => {
assertNotDisposed(disposables);
return count;
},
[Symbol.dispose]: () => disposables.dispose(),
};
};