lyb-js 1.6.19 → 1.6.20

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.
@@ -7,6 +7,8 @@ type Listener = (w: number, h: number, s: number) => void;
7
7
  export declare class LibJsResizeWatcher {
8
8
  /** 存储所有监听器及其是否需要立即执行的标志 */
9
9
  private _listeners;
10
+ /** 按id存储监听器 */
11
+ private _listenerMap;
10
12
  /** 当前适配模式 */
11
13
  private _mode;
12
14
  constructor(mode?: "h" | "v" | "hv");
@@ -17,6 +19,10 @@ export declare class LibJsResizeWatcher {
17
19
  * @returns 一个函数,用于移除该监听器
18
20
  */
19
21
  on(cb: Listener, immediate?: boolean): () => void;
22
+ /** 新增方法:通过id注册监听器(若id已存在会先移除旧的) */
23
+ onById(id: string, cb: Listener, immediate?: boolean): void;
24
+ /** 通过id移除监听器 */
25
+ offById(...ids: (string | string[])[]): void;
20
26
  /** @description 内部 resize 回调函数,调用所有注册的监听器 */
21
27
  private _handleResize;
22
28
  }
@@ -3,6 +3,8 @@ export class LibJsResizeWatcher {
3
3
  constructor(mode = "hv") {
4
4
  /** 存储所有监听器及其是否需要立即执行的标志 */
5
5
  this._listeners = [];
6
+ /** 按id存储监听器 */
7
+ this._listenerMap = new Map();
6
8
  /** @description 内部 resize 回调函数,调用所有注册的监听器 */
7
9
  this._handleResize = () => {
8
10
  const w = window.innerWidth;
@@ -57,4 +59,23 @@ export class LibJsResizeWatcher {
57
59
  this._listeners = this._listeners.filter((l) => l !== item);
58
60
  };
59
61
  }
62
+ /** 新增方法:通过id注册监听器(若id已存在会先移除旧的) */
63
+ onById(id, cb, immediate = true) {
64
+ this.offById(id);
65
+ const remove = this.on(cb, immediate);
66
+ this._listenerMap.set(id, cb);
67
+ // 存储移除函数
68
+ this._listenerMap.set(`${id}_remove`, remove);
69
+ }
70
+ /** 通过id移除监听器 */
71
+ offById(...ids) {
72
+ const flatIds = ids.flat();
73
+ for (const id of flatIds) {
74
+ const remove = this._listenerMap.get(`${id}_remove`);
75
+ if (typeof remove === "function")
76
+ remove();
77
+ this._listenerMap.delete(id);
78
+ this._listenerMap.delete(`${id}_remove`);
79
+ }
80
+ }
60
81
  }
@@ -6,6 +6,8 @@ export declare class LibJsClassObservable<T extends Record<string, any>> {
6
6
  private data;
7
7
  /** 监听器 */
8
8
  private listeners;
9
+ /** 按id映射监听索引 */
10
+ private listenerIds;
9
11
  /** 监听函数索引ID */
10
12
  private index;
11
13
  /**
@@ -33,6 +35,10 @@ export declare class LibJsClassObservable<T extends Record<string, any>> {
33
35
  * @returns 取消监听的函数
34
36
  */
35
37
  onValue<K extends keyof T>(key: K, callback: (newValue: T[K], oldValue: T[K]) => void, immediately?: boolean): () => void;
38
+ /** 新增:通过id监听(若id存在则覆盖旧监听) */
39
+ onValueById<K extends keyof T>(id: string, key: K, callback: (newValue: T[K], oldValue: T[K]) => void, immediately?: boolean): void;
40
+ /** 新增:通过id移除监听 */
41
+ offValueById(...ids: (string | string[])[]): void;
36
42
  /** @description 触发某个键名的所有回调函数,适用于同时监听了多个值的场景,用于切换要显示的值,或者延迟在特定时机通知监听器
37
43
  * @param key 要触发的键名
38
44
  * @returns 触发的键值
@@ -9,6 +9,8 @@ export class LibJsClassObservable {
9
9
  constructor(initialData) {
10
10
  /** 监听器 */
11
11
  this.listeners = new Map();
12
+ /** 按id映射监听索引 */
13
+ this.listenerIds = new Map();
12
14
  /** 监听函数索引ID */
13
15
  this.index = 0;
14
16
  this.data = Object.assign({}, initialData);
@@ -57,6 +59,27 @@ export class LibJsClassObservable {
57
59
  this.listeners.get(key).delete(id);
58
60
  };
59
61
  }
62
+ /** 新增:通过id监听(若id存在则覆盖旧监听) */
63
+ onValueById(id, key, callback, immediately = true) {
64
+ // 移除旧监听
65
+ this.offValueById(id);
66
+ const remove = this.onValue(key, callback, immediately);
67
+ const index = this.index - 1;
68
+ this.listenerIds.set(id, index);
69
+ // 存储移除函数
70
+ this.listenerIds.set(`${id}_remove`, remove);
71
+ }
72
+ /** 新增:通过id移除监听 */
73
+ offValueById(...ids) {
74
+ const flatIds = ids.flat();
75
+ for (const id of flatIds) {
76
+ const remove = this.listenerIds.get(`${id}_remove`);
77
+ if (typeof remove === "function")
78
+ remove();
79
+ this.listenerIds.delete(id);
80
+ this.listenerIds.delete(`${id}_remove`);
81
+ }
82
+ }
60
83
  /** @description 触发某个键名的所有回调函数,适用于同时监听了多个值的场景,用于切换要显示的值,或者延迟在特定时机通知监听器
61
84
  * @param key 要触发的键名
62
85
  * @returns 触发的键值
@@ -97,5 +120,6 @@ export class LibJsClassObservable {
97
120
  /** @description 清空所有监听 */
98
121
  clear() {
99
122
  this.listeners.clear();
123
+ this.listenerIds.clear();
100
124
  }
101
125
  }
@@ -0,0 +1,8 @@
1
+ type Listener = () => void;
2
+ /** @description 关闭监听 */
3
+ export declare class LibJsEmitterClose {
4
+ private static listeners;
5
+ static on(eventName: string, listener: Listener): void;
6
+ static emit(eventName: string): void;
7
+ }
8
+ export {};
@@ -0,0 +1,17 @@
1
+ /** @description 关闭监听 */
2
+ export class LibJsEmitterClose {
3
+ static on(eventName, listener) {
4
+ if (!this.listeners.has(eventName))
5
+ this.listeners.set(eventName, new Set());
6
+ this.listeners.get(eventName).add(listener);
7
+ }
8
+ static emit(eventName) {
9
+ const set = this.listeners.get(eventName);
10
+ if (!set)
11
+ return;
12
+ for (const listener of set)
13
+ listener();
14
+ this.listeners.delete(eventName);
15
+ }
16
+ }
17
+ LibJsEmitterClose.listeners = new Map();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.6.19",
3
+ "version": "1.6.20",
4
4
  "description": "自用JS方法库",
5
5
  "license": "ISC",
6
6
  "type": "module",