make sure old names are removed in systemsbyname store

This commit is contained in:
henrygd
2025-09-02 19:12:39 -04:00
parent e9fb9b856f
commit 794db0ac6a
2 changed files with 17 additions and 4 deletions

View File

@@ -17,7 +17,7 @@ export const $downSystems = map<Record<string, SystemRecord>>({})
/** Map of paused systems by id */ /** Map of paused systems by id */
export const $pausedSystems = map<Record<string, SystemRecord>>({}) export const $pausedSystems = map<Record<string, SystemRecord>>({})
/** List of all system records */ /** List of all system records */
export const $systems: ReadableAtom<SystemRecord[]> = computed($allSystemsByName, Object.values) export const $systems: ReadableAtom<SystemRecord[]> = computed($allSystemsById, Object.values)
/** Map of alert records by system id and alert name */ /** Map of alert records by system id and alert name */
export const $alerts = map<AlertMap>({}) export const $alerts = map<AlertMap>({})

View File

@@ -29,7 +29,7 @@ export function init() {
initialized = true initialized = true
// sync system stores on change // sync system stores on change
$allSystemsByName.listen((newSystems, oldSystems, changedKey) => { $allSystemsById.listen((newSystems, oldSystems, changedKey) => {
const oldSystem = oldSystems[changedKey] const oldSystem = oldSystems[changedKey]
const newSystem = newSystems[changedKey] const newSystem = newSystems[changedKey]
@@ -59,6 +59,10 @@ export function init() {
$pausedSystems.setKey(newSystem.id, newSystem) $pausedSystems.setKey(newSystem.id, newSystem)
removeFromStore(newSystem, $upSystems) removeFromStore(newSystem, $upSystems)
removeFromStore(newSystem, $downSystems) removeFromStore(newSystem, $downSystems)
} else if (newStatus === SystemStatus.Pending) {
removeFromStore(newSystem, $upSystems)
removeFromStore(newSystem, $downSystems)
removeFromStore(newSystem, $pausedSystems)
} }
// run things that need to be done when systems change // run things that need to be done when systems change
@@ -100,13 +104,22 @@ async function fetchSystems(): Promise<SystemRecord[]> {
} }
} }
// Store management functions
/** Add system to both name and ID stores */ /** Add system to both name and ID stores */
export function add(system: SystemRecord) { export function add(system: SystemRecord) {
$allSystemsByName.setKey(system.name, system) $allSystemsByName.setKey(system.name, system)
$allSystemsById.setKey(system.id, system) $allSystemsById.setKey(system.id, system)
} }
/** Update system in stores */
export function update(system: SystemRecord) {
// if name changed, make sure old name is removed from the name store
const oldName = $allSystemsById.get()[system.id]?.name
if (oldName !== system.name) {
$allSystemsByName.setKey(oldName, undefined as any)
}
add(system)
}
/** Remove system from stores */ /** Remove system from stores */
export function remove(system: SystemRecord) { export function remove(system: SystemRecord) {
removeFromStore(system, $allSystemsByName) removeFromStore(system, $allSystemsByName)
@@ -125,7 +138,7 @@ function removeFromStore(system: SystemRecord, store: PreinitializedMapStore<Rec
/** Action functions for subscription */ /** Action functions for subscription */
const actionFns: Record<string, (system: SystemRecord) => void> = { const actionFns: Record<string, (system: SystemRecord) => void> = {
create: add, create: add,
update: add, update: update,
delete: remove, delete: remove,
} }