It’s like “Dynamic Dispose”
- Defers
Action
to be executed during Dispose - Executes them on Dispose
- Allows cascade calls
-
create “rollback point”, pass it down the execution flow, dispose when you want to clean up
var rollback = new Rollback(); runner.StartCoroutine(ShowPopup(rollback)); //... rollback.Dispose();
-
pass down to dependent features, defer “dispose actions” in place when you make something that has side effects
IEnumerator ShowPopup(Rollback popupRollback) {
window.SetActive(true);
popupRollback.Defer(() => window.SetActive(false));
someEvent += Callback;
popupRollback.Defer(() => someEvent -= Callback);
yield return WaitOkButton();
var fxInstance = Instantiate(fxPrefab);
popupRollback.Defer(() ⇒ Destroy(fxInstance));
yield return new WaitForSeconds(fxInstance.Delay);
}
Presentation (PDF) : https://github.com/korchoon/rollback/blob/main/Readme/Rollback%20article.pdf
- Main differences to Dispose
- don’t need to store dependencies you need only in Dispose(or similar)
- you don’t need to guess
- what needs to be cleaned up, and what doesn’t (in case of some interruption, or exception)
- in which order
- you actually don’t need Dispose method to clean-up