You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using Firestore streams with `where` clauses, documents can be removed from your local cache if they no longer match the query filters. This happens when a document's data changes and no longer satisfies the stream's conditions.
207
207
208
-
> hint: returning `undefined` will discard the document change and do nothing with the local cache store
208
+
You can prevent this automatic removal by returning `undefined` from the `removed` hook. This keeps the document in your local cache even when it no longer matches the stream's where clause.
209
+
210
+
**Real-world example**: In a Pokemon game, you might have a stream showing only Pokemon that are "available for battle" (not fainted, not in the PC, etc.). But you want to keep legendary Pokemon visible in the UI even if they temporarily become unavailable, so players can see their stats and plan their team.
211
+
212
+
```js
213
+
constpokedexModule=magnetar.collection('pokedex')
214
+
215
+
// Stream only Pokemon available for battle, but keep ALL in cache when they faint
216
+
pokedexModule
217
+
.where('status', '==', 'available')
218
+
.where('hp', '>', 0)
219
+
.orderBy('level', 'desc')
220
+
.stream(undefined, {
221
+
modifyReadResponseOn: {
222
+
removed: (payload) => {
223
+
// Keep ALL Pokemon in cache even if they become unavailable
224
+
// This allows us to see their stats and plan our team
225
+
returnundefined// This prevents removal from local cache
226
+
},
227
+
},
228
+
})
229
+
230
+
// Later, when any Pokemon faints (hp becomes 0):
231
+
// - Firestore removes it from the stream because hp <= 0
232
+
// - Our removed hook prevents it from being removed from local cache
233
+
// - The Pokemon stays visible in the UI for reference and team planning
234
+
// - We can still see their stats, moves, and other information
235
+
```
209
236
210
237
### Accessing Metadata when Reading Data
211
238
212
-
> documentation WIP
239
+
When reading data from your remote store, you can access metadata as a second parameter in your `modifyReadResponseOn` functions. This metadata contains the full Firestore DocumentSnapshot, which includes the document ID and other server-side information.
213
240
214
-
> hint: you can access the metadata as second param
241
+
**Real-world example**: You might want to add the document ID to your data in the frontend cache, but not store it as a field in the Firestore document itself. This is useful when you want to keep your database documents clean while still having easy access to the ID in your frontend code.
Magnetar provides a global event system that you can use to listen to various actions happening throughout your app. This is useful for showing toasts, logging, analytics, or any other side effects.
219
274
220
-
on
275
+
### Global Events
221
276
222
-
> hint: use case: toasts
277
+
You can set up global events when you instantiate Magnetar. These events will be triggered for all modules and actions.
223
278
224
-
> hint: use case: developer logging (see setup for now)
0 commit comments