lyb-js 1.6.18 → 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.
- package/Base/LibJsResizeWatcher.d.ts +6 -0
- package/Base/LibJsResizeWatcher.js +21 -0
- package/Misc/LibJsClassObservable.d.ts +9 -1
- package/Misc/LibJsClassObservable.js +29 -1
- package/Misc/LibJsEmitterClose.d.ts +8 -0
- package/Misc/LibJsEmitterClose.js +17 -0
- package/Time/LibJsTimeAgo.d.ts +1 -1
- package/Time/LibJsTimeAgo.js +7 -6
- package/package.json +1 -1
|
@@ -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,13 +6,17 @@ 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
|
/**
|
|
12
14
|
* @param initialData 监听的数据
|
|
13
15
|
*/
|
|
14
16
|
constructor(initialData: T);
|
|
15
|
-
/** @description
|
|
17
|
+
/** @description 获取所有数据 */
|
|
18
|
+
getData(): T;
|
|
19
|
+
/** @description 获取值
|
|
16
20
|
* @param key 要获取的键
|
|
17
21
|
* @returns 对应的值
|
|
18
22
|
*/
|
|
@@ -31,6 +35,10 @@ export declare class LibJsClassObservable<T extends Record<string, any>> {
|
|
|
31
35
|
* @returns 取消监听的函数
|
|
32
36
|
*/
|
|
33
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;
|
|
34
42
|
/** @description 触发某个键名的所有回调函数,适用于同时监听了多个值的场景,用于切换要显示的值,或者延迟在特定时机通知监听器
|
|
35
43
|
* @param key 要触发的键名
|
|
36
44
|
* @returns 触发的键值
|
|
@@ -9,11 +9,17 @@ 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);
|
|
15
17
|
}
|
|
16
|
-
/** @description
|
|
18
|
+
/** @description 获取所有数据 */
|
|
19
|
+
getData() {
|
|
20
|
+
return this.data;
|
|
21
|
+
}
|
|
22
|
+
/** @description 获取值
|
|
17
23
|
* @param key 要获取的键
|
|
18
24
|
* @returns 对应的值
|
|
19
25
|
*/
|
|
@@ -53,6 +59,27 @@ export class LibJsClassObservable {
|
|
|
53
59
|
this.listeners.get(key).delete(id);
|
|
54
60
|
};
|
|
55
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
|
+
}
|
|
56
83
|
/** @description 触发某个键名的所有回调函数,适用于同时监听了多个值的场景,用于切换要显示的值,或者延迟在特定时机通知监听器
|
|
57
84
|
* @param key 要触发的键名
|
|
58
85
|
* @returns 触发的键值
|
|
@@ -93,5 +120,6 @@ export class LibJsClassObservable {
|
|
|
93
120
|
/** @description 清空所有监听 */
|
|
94
121
|
clear() {
|
|
95
122
|
this.listeners.clear();
|
|
123
|
+
this.listenerIds.clear();
|
|
96
124
|
}
|
|
97
125
|
}
|
|
@@ -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/Time/LibJsTimeAgo.d.ts
CHANGED
package/Time/LibJsTimeAgo.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @description
|
|
1
|
+
/** @description 时间差计算(支持未来时间:多久后)
|
|
2
2
|
* @param timestamp 毫秒时间戳
|
|
3
3
|
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsTimeAgo-中文时间差
|
|
4
4
|
*/
|
|
@@ -11,12 +11,13 @@ export const libJsTimeAgo = (timestamp) => {
|
|
|
11
11
|
{ unit: "小时", milliseconds: 60 * 60 * 1000 },
|
|
12
12
|
{ unit: "分钟", milliseconds: 60 * 1000 },
|
|
13
13
|
];
|
|
14
|
-
const
|
|
15
|
-
const
|
|
14
|
+
const now = Date.now();
|
|
15
|
+
const diff = timestamp - now;
|
|
16
|
+
const absDiff = Math.abs(diff);
|
|
16
17
|
for (const { unit, milliseconds } of timeUnits) {
|
|
17
|
-
if (
|
|
18
|
-
const count = Math.floor(
|
|
19
|
-
return `${count} ${unit}前`;
|
|
18
|
+
if (absDiff >= milliseconds) {
|
|
19
|
+
const count = Math.floor(absDiff / milliseconds);
|
|
20
|
+
return diff > 0 ? `${count} ${unit}后` : `${count} ${unit}前`;
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
return "刚刚";
|