lyb-js 1.6.26 → 1.6.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,8 @@ export declare class LibJsClassObservable<T extends Record<string, any>> {
6
6
  private data;
7
7
  /** 监听器 */
8
8
  private listeners;
9
+ /** 全局监听器(监听所有键的更改) */
10
+ private globalListeners;
9
11
  /** 按id映射监听索引 */
10
12
  private listenerIds;
11
13
  /** 监听函数索引ID */
@@ -37,6 +39,8 @@ export declare class LibJsClassObservable<T extends Record<string, any>> {
37
39
  onValue<K extends keyof T>(key: K, callback: (newValue: T[K], oldValue: T[K]) => void, immediately?: boolean): () => void;
38
40
  /** 新增:通过id监听(若id存在则覆盖旧监听) */
39
41
  onValueById<K extends keyof T>(id: string, key: K, callback: (newValue: T[K], oldValue: T[K]) => void, immediately?: boolean): void;
42
+ /** 监听所有键的变化 */
43
+ onAnyValue(callback: (key: keyof T, newValue: any, oldValue: any) => void): () => void;
40
44
  /** 新增:通过id移除监听 */
41
45
  offValueById(...ids: (string | string[])[]): void;
42
46
  /** @description 触发某个键名的所有回调函数,适用于同时监听了多个值的场景,用于切换要显示的值,或者延迟在特定时机通知监听器
@@ -9,6 +9,8 @@ export class LibJsClassObservable {
9
9
  constructor(initialData) {
10
10
  /** 监听器 */
11
11
  this.listeners = new Map();
12
+ /** 全局监听器(监听所有键的更改) */
13
+ this.globalListeners = new Map();
12
14
  /** 按id映射监听索引 */
13
15
  this.listenerIds = new Map();
14
16
  /** 监听函数索引ID */
@@ -38,8 +40,10 @@ export class LibJsClassObservable {
38
40
  if (oldValue === value)
39
41
  return value;
40
42
  this.data[key] = value;
41
- immediately &&
42
- ((_a = this.listeners.get(key)) === null || _a === void 0 ? void 0 : _a.forEach((fn) => fn(value, oldValue)));
43
+ if (immediately) {
44
+ (_a = this.listeners.get(key)) === null || _a === void 0 ? void 0 : _a.forEach((fn) => fn(value, oldValue));
45
+ this.globalListeners.forEach((fn) => fn(key, value, oldValue));
46
+ }
43
47
  return value;
44
48
  }
45
49
  /** @description 监听值
@@ -69,6 +73,12 @@ export class LibJsClassObservable {
69
73
  // 存储移除函数
70
74
  this.listenerIds.set(`${id}_remove`, remove);
71
75
  }
76
+ /** 监听所有键的变化 */
77
+ onAnyValue(callback) {
78
+ const id = this.index++;
79
+ this.globalListeners.set(id, callback);
80
+ return () => this.globalListeners.delete(id);
81
+ }
72
82
  /** 新增:通过id移除监听 */
73
83
  offValueById(...ids) {
74
84
  const flatIds = ids.flat();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.6.26",
3
+ "version": "1.6.27",
4
4
  "description": "自用JS方法库",
5
5
  "license": "ISC",
6
6
  "type": "module",