Cleanup is an elaborate facility to handle cleanups for multiple dependent objects even in completely dynamic environments. It is built on Destroyable, and can consume other objects, which support
Destroyable protocol: define
It is defined in
Read a background on destruction in Destructors.
Cleanup defines following API:
1 2 3 4 5 6 7 8 9 10 11
The whole system operates in a stack-like fashion reflecting common embedded states.
Constructor initializes an internal state required for
Cleanup to function. It creates a single property on an instance called
popCleanup() operate in a LIFO fashion.
resource is any resource you want to keep track of.
cleanup is an optional function, which can be used to dispose of
resource. During the cleanup phase:
cleanupwas present, it is called passing
resourceas the only parameter.
cleanupwas not present, it is assumed that
resourcesupports Destroyable protocol, and its
destroy()method will be called.
pushCleanup() returns a function object that uniquely identifies the pushed resource. It can be used later with
Additionally it can be called without parameters to clean up the resource. Please do so only after it was successfully removed with
popCleanup() is a counterpart of
pushCleanup(). It runs a cleanup code for the last pushed resource returning nothing. If
dontRun is a truthy value, the cleanup run is skipped, and the last object is simply removed from stack and the function to clean it up is returned.
If you poped a cleanup resource manually with this method passing a truthy value as
dontRun, you are responsible for cleaning it up manually. One way to do it is to call the returned value as a function with no parameters.
This method is used to remove a reference to a resource from the stack. It can be anywhere. The cleanup code for that resource is not run.
The only parameter for this method is a value returned by
The method returns a truthy value, if the resource was found, and a falsy value otherwise. If you know that resource was pushed, but it is not there, usually it means that it was already cleaned up.
If you removed a cleanup resource manually with this method, you are responsible for cleaning it up manually. One way to do it is to call
f as a function with no parameters.
Cleans up all accumulated resources removing them from the stack.
A required method for Destroyable protocol. It simply calls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46