import { defineStore } from 'pinia' import { ref } from 'vue' import { loadEnhancementDevices } from '../services/enhancementDevices.js' import { fetchEquipmentDetailAggregate } from '../services/detailApi.js' import { retainEquipmentRefresh } from '../domain/detailRefresh.js' /** Local viewer ownership: never overwrite the main map's point/device selection. */ export const useEnhancementDevicesStore = defineStore('enhancementDevices', () => { const source = ref({ rows: [], partial: true, loading: false }) const selected = ref(null) const aggregate = ref(null) const loadingDetail = ref(false) let sourceToken = 0 let detailToken = 0 let fingerprint = '' let detailOptions = {} async function load(summary, options = {}) { const next = JSON.stringify([summary.batchUpdatedAt, summary.phase, summary.truncated, summary.objects]) if (next === fingerprint && !options.force) return fingerprint = next const token = ++sourceToken const retainedRows = source.value.rows.map((row) => ({ ...row, retained: true })) const publish = (state, loading) => { if (token !== sourceToken) return const rows = loading || state.partial ? [...new Map([...retainedRows, ...state.rows].map((row) => [row.code, row])).values()] : state.rows source.value = { ...state, rows, loading } } source.value = { ...source.value, rows: retainedRows, partial: true, loading: true } const result = await loadEnhancementDevices(summary, { ...options, alive: () => token === sourceToken, onProgress: (state) => publish(state, true), }) publish(result, false) if (token !== sourceToken || !selected.value) return const current = result.rows.find((row) => row.code === selected.value.code) if (current) await open({ ...selected.value, ...current }, { ...detailOptions, ...options }) } async function open(device, options = {}) { if (!device?.code) return detailOptions = { ...detailOptions, ...options } const token = ++detailToken const sameDevice = selected.value?.code === device.code && aggregate.value?.data?.deviceCode === device.code selected.value = device if (!sameDevice) aggregate.value = null loadingDetail.value = true let result try { result = await (detailOptions.fetchDetail || fetchEquipmentDetailAggregate)(device.code, detailOptions) } catch { result = { state: 'failed', data: null } } if (token !== detailToken) return const next = result?.data?.deviceCode === device.code ? result : { state: 'failed', reason: result?.reason || '', data: null } aggregate.value = retainEquipmentRefresh(aggregate.value, next, device.code) loadingDetail.value = false } function closeDetail() { detailToken += 1 detailOptions = {} selected.value = null aggregate.value = null loadingDetail.value = false } function reconcile(deviceList) { if (!selected.value) return const current = deviceList.find((device) => device.code === selected.value.code) if (current) selected.value = current // A progressive/failed read is not proof that this device left the scope. else if (!source.value.loading && !source.value.partial) closeDetail() } function invalidate() { sourceToken += 1 fingerprint = '' source.value = { ...source.value, partial: true, loading: true } } function clear() { sourceToken += 1 fingerprint = '' source.value = { rows: [], partial: true, loading: false } closeDetail() } return { source, selected, aggregate, loadingDetail, load, open, closeDetail, reconcile, invalidate, clear } })