refactor: add subscribe / unsubscribe to alertManager

This commit is contained in:
henrygd
2025-08-24 19:48:21 -04:00
parent b64318d9e8
commit 0638ff3c21
3 changed files with 34 additions and 13 deletions

View File

@@ -537,6 +537,7 @@ export const getSystemNameFromId = (() => {
/** Helper to manage user alerts */
export const alertManager = (() => {
const collection = pb.collection<AlertRecord>("alerts")
let unsub: () => void
/** Fields to fetch from alerts collection */
const fields = "id,name,system,value,min,triggered"
@@ -597,17 +598,29 @@ export const alertManager = (() => {
}
})()
collection.subscribe("*", batchUpdate, { fields })
async function subscribe() {
unsub = await collection.subscribe("*", batchUpdate, { fields })
}
function unsubscribe() {
unsub?.()
}
async function refresh() {
const records = await fetchAlerts()
add(records)
}
return {
/** Add alerts to store */
add,
/** Remove alerts from store */
remove,
/** Subscribe to alerts */
subscribe,
/** Unsubscribe from alerts */
unsubscribe,
/** Refresh alerts with latest data from hub */
async refresh() {
const records = await fetchAlerts()
add(records)
},
refresh,
}
})()