SharedStateBridgeSingleton.ts

// SharedStateBridgeSingleton.ts

import { Registry } from './Registry'
import { SharedDataSubscriber, SharedStateBridge } from './SharedStateBridge'

// 单例子类
class SharedStateBridgeSingleton extends SharedStateBridge {
  private static instance: SharedStateBridgeSingleton

  private constructor() {
    super()
  }

  public static getInstance(): SharedStateBridgeSingleton {
    if (!SharedStateBridgeSingleton.instance) {
      SharedStateBridgeSingleton.instance = new SharedStateBridgeSingleton()
    }
    return this.instance
  }
}

// 获取单例
const singleton = SharedStateBridgeSingleton.getInstance()

// ========== 缓存注册表 ==========
const componentRegistry = new Registry<any>()

// ========== 类型导出 ==========
export type { SharedDataSubscriber }

export type ResourceUpdateCallback = (resourceType: string) => void

// ========== 共享数据(发布订阅) ==========
export const publishSharedData = singleton.publishSharedData.bind(singleton)
export const getSharedData = singleton.getSharedData.bind(singleton)
export const clearSharedData = singleton.clearSharedData.bind(singleton)
export const subscribeSharedData = singleton.subscribeSharedData.bind(singleton)
export const unsubscribeSharedData =
  singleton.unsubscribeSharedData.bind(singleton)

// ========== 工单组件缓存 ==========
export const getCachedServiceRequest =
  componentRegistry.get.bind(componentRegistry)
export const setCachedServiceRequest =
  componentRegistry.set.bind(componentRegistry)

// ========== 表单数据回调 ==========
export type GetFormDataCallback = () => Promise<{
  jsSn: string
  workOrderForm: any
  subWorkOrderDTOList?: Array<{ jsSn: string; workOrderForm: any }>
} | null>
const formCallbackRegistry = new Registry<GetFormDataCallback>()
export const registerFormDataCallback =
  formCallbackRegistry.set.bind(formCallbackRegistry)
export const getFormDataCallback =
  formCallbackRegistry.get.bind(formCallbackRegistry)
export const unregisterFormDataCallback =
  formCallbackRegistry.delete.bind(formCallbackRegistry)

// ========== 资源更新回调(单回调) ==========
const resourceCallbackRegistry = new Registry<ResourceUpdateCallback>()
const RESOURCE_KEY = 'update'
export const registerResourceUpdateCallback = (
  callback: ResourceUpdateCallback
) => resourceCallbackRegistry.set(RESOURCE_KEY, callback)
export const unregisterResourceUpdateCallback = () =>
  resourceCallbackRegistry.delete(RESOURCE_KEY)
export const triggerResourceAdd = (resourceType: string) => {
  const callback = resourceCallbackRegistry.get(RESOURCE_KEY)
  if (callback) {
    callback(resourceType)
  } else {
    console.warn(
      `No resource update callback registered, cannot add ${resourceType}`
    )
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
上次更新:
(adsbygoogle = window.adsbygoogle || []).push({});