()
| 21 | * }) |
| 22 | */ |
| 23 | export function createCleanupObject() { |
| 24 | const cleanups = new Set<Cleanup>(); |
| 25 | |
| 26 | const cleanupObject = { |
| 27 | clean() { |
| 28 | /** |
| 29 | * We start cleaning up from last items added. It is the same as eg, react effect where children effects are cleaned up before parents. |
| 30 | * |
| 31 | * Rationale is that cleanups added last might depend on something created before and if we clean them first, they might not be able to clean up properly. |
| 32 | */ |
| 33 | const cleanupsList = [...cleanups].reverse(); |
| 34 | cleanups.clear(); |
| 35 | |
| 36 | runInAction(() => { |
| 37 | cleanupsList.forEach((cleanup) => { |
| 38 | cleanup(); |
| 39 | }); |
| 40 | }); |
| 41 | }, |
| 42 | cleanOne(cleanup: Cleanup) { |
| 43 | if (!cleanups.has(cleanup)) return false; |
| 44 | |
| 45 | cleanups.delete(cleanup); |
| 46 | |
| 47 | cleanup(); |
| 48 | |
| 49 | return true; |
| 50 | }, |
| 51 | set next(cleanupToAdd: MaybeCleanup | void) { |
| 52 | if (!cleanupToAdd) return; |
| 53 | |
| 54 | cleanups.add(cleanupToAdd); |
| 55 | }, |
| 56 | get size() { |
| 57 | return cleanups.size; |
| 58 | }, |
| 59 | }; |
| 60 | |
| 61 | return cleanupObject; |
| 62 | } |
no outgoing calls
no test coverage detected