lyb-js 1.6.25 → 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
|
-
(
|
|
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/README.md
CHANGED
|
@@ -177,6 +177,8 @@ console.log(t); //"string"
|
|
|
177
177
|
|
|
178
178
|
\- [LibJsTimeGreeting-时间问候](#LibJsTimeGreeting-时间问候)
|
|
179
179
|
|
|
180
|
+
\- [LibJsCountdown-倒计时](#LibJsCountdown-倒计时)
|
|
181
|
+
|
|
180
182
|
|
|
181
183
|
## Base-基础
|
|
182
184
|
|
|
@@ -784,7 +786,7 @@ const result3 = libJsTimeGreeting({ afternoon: "午后好" }); //自定义下午
|
|
|
784
786
|
console.log(result3); //50% 概率为 true
|
|
785
787
|
```
|
|
786
788
|
|
|
787
|
-
###
|
|
789
|
+
### LibJsCountdown-倒计时
|
|
788
790
|
|
|
789
791
|
> 以当前时间为开始时间,传递结束时间,返回年到秒的数值,以及是否结束
|
|
790
792
|
|
package/Time/LibJsCountdown.js
CHANGED
|
@@ -7,12 +7,12 @@ export const libJsCountdown = (endTime) => {
|
|
|
7
7
|
const time = dayjs.duration(diff);
|
|
8
8
|
const pad = (n) => n.toString().padStart(2, "0");
|
|
9
9
|
return {
|
|
10
|
-
years: pad(time.years()),
|
|
11
|
-
months: pad(time.months()),
|
|
12
|
-
days: pad(time.days()),
|
|
13
|
-
hours: pad(time.hours()),
|
|
14
|
-
minutes: pad(time.minutes()),
|
|
15
|
-
seconds: pad(time.seconds()),
|
|
10
|
+
years: diff <= 0 ? "00" : pad(time.years()),
|
|
11
|
+
months: diff <= 0 ? "00" : pad(time.months()),
|
|
12
|
+
days: diff <= 0 ? "00" : pad(time.days()),
|
|
13
|
+
hours: diff <= 0 ? "00" : pad(time.hours()),
|
|
14
|
+
minutes: diff <= 0 ? "00" : pad(time.minutes()),
|
|
15
|
+
seconds: diff <= 0 ? "00" : pad(time.seconds()),
|
|
16
16
|
ended: diff <= 0,
|
|
17
17
|
};
|
|
18
18
|
};
|