Patch Changes
-
Fix collection cleanup to fire status:change event with 'cleaned-up' status (#714)
Previously, when a collection was garbage collected, event handlers were removed before the status was changed to 'cleaned-up'. This prevented listeners from receiving the status:change event, breaking the collection factory pattern where collections listen for cleanup to remove themselves from a cache.
Now, the cleanup process:
- Cleans up sync, state, changes, and indexes
- Sets status to 'cleaned-up' (fires the event)
- Finally cleans up event handlers
This enables the collection factory pattern:
const cache = new Map<string, ReturnType<typeof createCollection>>() const getTodoCollection = (id: string) => { if (!cache.has(id)) { const collection = createCollection(/* ... */) collection.on("status:change", ({ status }) => { if (status === "cleaned-up") { cache.delete(id) // This now works! } }) cache.set(id, collection) } return cache.get(id)! }