enhancementDevicesStore.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { defineStore } from 'pinia'
  2. import { ref } from 'vue'
  3. import { loadEnhancementDevices } from '../services/enhancementDevices.js'
  4. import { fetchEquipmentDetailAggregate } from '../services/detailApi.js'
  5. import { retainEquipmentRefresh } from '../domain/detailRefresh.js'
  6. /** Local viewer ownership: never overwrite the main map's point/device selection. */
  7. export const useEnhancementDevicesStore = defineStore('enhancementDevices', () => {
  8. const source = ref({ rows: [], partial: true, loading: false })
  9. const selected = ref(null)
  10. const aggregate = ref(null)
  11. const loadingDetail = ref(false)
  12. let sourceToken = 0
  13. let detailToken = 0
  14. let fingerprint = ''
  15. let detailOptions = {}
  16. async function load(summary, options = {}) {
  17. const next = JSON.stringify([summary.batchUpdatedAt, summary.phase, summary.truncated, summary.objects])
  18. if (next === fingerprint && !options.force) return
  19. fingerprint = next
  20. const token = ++sourceToken
  21. const retainedRows = source.value.rows.map((row) => ({ ...row, retained: true }))
  22. const publish = (state, loading) => {
  23. if (token !== sourceToken) return
  24. const rows = loading || state.partial
  25. ? [...new Map([...retainedRows, ...state.rows].map((row) => [row.code, row])).values()] : state.rows
  26. source.value = { ...state, rows, loading }
  27. }
  28. source.value = { ...source.value, rows: retainedRows, partial: true, loading: true }
  29. const result = await loadEnhancementDevices(summary, { ...options, alive: () => token === sourceToken,
  30. onProgress: (state) => publish(state, true),
  31. })
  32. publish(result, false)
  33. if (token !== sourceToken || !selected.value) return
  34. const current = result.rows.find((row) => row.code === selected.value.code)
  35. if (current) await open({ ...selected.value, ...current }, { ...detailOptions, ...options })
  36. }
  37. async function open(device, options = {}) {
  38. if (!device?.code) return
  39. detailOptions = { ...detailOptions, ...options }
  40. const token = ++detailToken
  41. const sameDevice = selected.value?.code === device.code && aggregate.value?.data?.deviceCode === device.code
  42. selected.value = device
  43. if (!sameDevice) aggregate.value = null
  44. loadingDetail.value = true
  45. let result
  46. try { result = await (detailOptions.fetchDetail || fetchEquipmentDetailAggregate)(device.code, detailOptions) }
  47. catch { result = { state: 'failed', data: null } }
  48. if (token !== detailToken) return
  49. const next = result?.data?.deviceCode === device.code ? result : { state: 'failed', reason: result?.reason || '', data: null }
  50. aggregate.value = retainEquipmentRefresh(aggregate.value, next, device.code)
  51. loadingDetail.value = false
  52. }
  53. function closeDetail() {
  54. detailToken += 1
  55. detailOptions = {}
  56. selected.value = null
  57. aggregate.value = null
  58. loadingDetail.value = false
  59. }
  60. function reconcile(deviceList) {
  61. if (!selected.value) return
  62. const current = deviceList.find((device) => device.code === selected.value.code)
  63. if (current) selected.value = current
  64. // A progressive/failed read is not proof that this device left the scope.
  65. else if (!source.value.loading && !source.value.partial) closeDetail()
  66. }
  67. function invalidate() {
  68. sourceToken += 1
  69. fingerprint = ''
  70. source.value = { ...source.value, partial: true, loading: true }
  71. }
  72. function clear() {
  73. sourceToken += 1
  74. fingerprint = ''
  75. source.value = { rows: [], partial: true, loading: false }
  76. closeDetail()
  77. }
  78. return { source, selected, aggregate, loadingDetail, load, open, closeDetail, reconcile, invalidate, clear }
  79. })