Compositional Resource Management Pattern for Dependent Lifetimes in TypeScript
How to model dependent resource lifetimes compositionally (not imperatively) to avoid callback hell while ensuring proper cleanup?
I'm processing a chain of resources where each step depends on the previous output (e.g.,
1. Automatic cleanup of intermediate resources
2. Composability for operations like
Example of the problem:
Desired Pseudo-Code
I'm processing a chain of resources where each step depends on the previous output (e.g.,
src → processedMat → paddedMat →), and each resource requires cleanup. Using nested acquireUseRelease blocks quickly becomes unwieldy. What pattern or abstraction can I use to manage these dependent resources linearly (without deep nesting) while ensuring: 1. Automatic cleanup of intermediate resources
2. Composability for operations like
cv.cvtColor or padImage that produce new resources (if they allocate internally). Example of the problem:
Desired Pseudo-Code
