calculator2026/src/pinia/pricingPaneReload.ts
2026-03-10 17:16:45 +08:00

53 lines
1.3 KiB
TypeScript

import { defineStore } from 'pinia'
export interface PricingPaneReloadDetail {
contractId: string
serviceId: string
at: number
}
interface PricingPaneReloadState {
seq: number
lastEvent: PricingPaneReloadDetail | null
persistedSeq: number
lastPersistedEvent: PricingPaneReloadDetail | null
}
const toKey = (value: string | number) => String(value)
export const usePricingPaneReloadStore = defineStore('pricingPaneReload', {
state: (): PricingPaneReloadState => ({
seq: 0,
lastEvent: null,
persistedSeq: 0,
lastPersistedEvent: null
}),
actions: {
emit(contractId: string, serviceId: string | number) {
this.lastEvent = {
contractId: toKey(contractId),
serviceId: toKey(serviceId),
at: Date.now()
}
this.seq += 1
},
emitPersisted(contractId: string, serviceId: string | number) {
this.lastPersistedEvent = {
contractId: toKey(contractId),
serviceId: toKey(serviceId),
at: Date.now()
}
this.persistedSeq += 1
}
}
})
export const matchPricingPaneReload = (
detail: PricingPaneReloadDetail | null | undefined,
contractId: string,
serviceId: string | number
) => {
if (!detail) return false
return detail.contractId === toKey(contractId) && detail.serviceId === toKey(serviceId)
}