Registry.ts

// Registry.ts

/**
 * 通用键值注册表(缓存 / 注册中心)
 */
export class Registry<T> {
  protected store = new Map<string, T>()

  set(key: string, value: T): void {
    this.store.set(key, value)
  }

  get(key: string): T | undefined {
    return this.store.get(key)
  }

  has(key: string): boolean {
    return this.store.has(key)
  }

  delete(key: string): boolean {
    return this.store.delete(key)
  }

  clear(): void {
    this.store.clear()
  }

  keys(): IterableIterator<string> {
    return this.store.keys()
  }

  values(): IterableIterator<T> {
    return this.store.values()
  }

  entries(): IterableIterator<[string, T]> {
    return this.store.entries()
  }

  size(): number {
    return this.store.size
  }

  /**
   * 调用存储的回调函数(仅当 T 为函数类型时有效)
   */
  invoke(key: string, ...args: any[]): void {
    const fn = this.store.get(key)
    if (typeof fn === 'function') {
      fn(...args)
    }
  }
}
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
上次更新:
(adsbygoogle = window.adsbygoogle || []).push({});