@gby/destroyable 1.0.1 → 1.1.0
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/dist/Destroyable.d.ts +96 -254
- package/dist/Destroyable.d.ts.map +1 -1
- package/dist/DestroyableSub.d.ts +22 -4
- package/dist/DestroyableSub.d.ts.map +1 -1
- package/dist/createDestroyableSubClass.d.ts +22 -5
- package/dist/createDestroyableSubClass.d.ts.map +1 -1
- package/dist/index.iife.js +1 -0
- package/dist/index.js +141 -82
- package/dist/index.umd.cjs +1 -1
- package/doc/api/destroyable.createdestroyablesubclass.md +9 -2
- package/doc/api/destroyable.destroyablesub.md +1 -1
- package/doc/api/destroyable.destroyablesubclass.md +1 -1
- package/doc/api/destroyable.nodedestroyable.md +7 -0
- package/doc/api/destroyable.webdestroyable.md +7 -0
- package/package.json +3 -2
- package/dist/tsdoc-metadata.json +0 -11
package/dist/Destroyable.d.ts
CHANGED
|
@@ -1,254 +1,96 @@
|
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
*
|
|
88
|
-
*/
|
|
89
|
-
export declare
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
*/
|
|
98
|
-
_destroyers: FunDestroyer[];
|
|
99
|
-
/**
|
|
100
|
-
* 添加销毁者
|
|
101
|
-
* @param fun
|
|
102
|
-
* @returns 返回销毁者的顺序
|
|
103
|
-
*/
|
|
104
|
-
disposeFun<T extends FunDestroyer>(fun: T): T;
|
|
105
|
-
/**
|
|
106
|
-
* 取消销毁者函数
|
|
107
|
-
* @param fun
|
|
108
|
-
* @returns
|
|
109
|
-
*/
|
|
110
|
-
cancelDisposeFun<T extends FunDestroyer>(fun: T): T;
|
|
111
|
-
/**
|
|
112
|
-
* 添加销毁对象
|
|
113
|
-
* @param obj
|
|
114
|
-
* @param sync - 表示是否使用 `obj.destroy()` 方法进行销毁;默认使用 `obj.destroyAsync()` 方法进行销毁
|
|
115
|
-
* @returns
|
|
116
|
-
*/
|
|
117
|
-
disposeObj<T extends Destroyable>(obj: T, sync?: boolean): T;
|
|
118
|
-
/**
|
|
119
|
-
* 取消销毁者函数
|
|
120
|
-
* @param fun
|
|
121
|
-
* @returns
|
|
122
|
-
*/
|
|
123
|
-
cancelDisposeObj<T extends Destroyable>(obj: T): T;
|
|
124
|
-
/**
|
|
125
|
-
* 添加销毁函数或销毁对象
|
|
126
|
-
* @param fun
|
|
127
|
-
*/
|
|
128
|
-
dispose<T extends FunDestroyer>(fun: T): T;
|
|
129
|
-
dispose<T extends Destroyable>(obj: T, asyncDestroy?: boolean): T;
|
|
130
|
-
dispose<T extends Destroyable | FunDestroyer>(objOrFun: T, asyncDestroy?: boolean): T;
|
|
131
|
-
cancelDispose<T extends FunDestroyer>(fun: T): T;
|
|
132
|
-
cancelDispose<T extends Destroyable>(obj: T): T;
|
|
133
|
-
cancelDispose<T extends Destroyable | FunDestroyer>(objOrFun: T): T;
|
|
134
|
-
/**
|
|
135
|
-
* 自己的销毁方法
|
|
136
|
-
* @remarks
|
|
137
|
-
* 子类根据需要进行重载
|
|
138
|
-
*/
|
|
139
|
-
destroyThis(): void;
|
|
140
|
-
/**
|
|
141
|
-
* 销毁
|
|
142
|
-
*/
|
|
143
|
-
destroy(): true | undefined;
|
|
144
|
-
/**
|
|
145
|
-
* 异步销毁
|
|
146
|
-
* @remarks
|
|
147
|
-
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
148
|
-
*/
|
|
149
|
-
destroyAsync(): Promise<true | undefined>;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* createDestroyableSubClass 创建的类的实例类型
|
|
154
|
-
*/
|
|
155
|
-
export declare type DestroyableSub<ParentClass extends new (...args: any) => any> = InstanceType<DestroyableSubClass<ParentClass>>;
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* createDestroyableSubClass 创建的类的类型
|
|
159
|
-
*/
|
|
160
|
-
export declare type DestroyableSubClass<ParentClass extends new (...args: any) => any> = ReturnType<typeof createDestroyableSubClass<ParentClass>>;
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* 销毁 Destroyable 对象
|
|
164
|
-
* @param object
|
|
165
|
-
* @returns
|
|
166
|
-
*/
|
|
167
|
-
export declare function destroyDestroyable(object: Destroyable): boolean;
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* 销毁对象
|
|
171
|
-
* @param object
|
|
172
|
-
* @returns
|
|
173
|
-
*/
|
|
174
|
-
export declare function destroyObject(object: any): boolean;
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* 函数销毁者
|
|
178
|
-
*/
|
|
179
|
-
export declare type FunDestroyer = () => void;
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* NodeDestroyable 类对象
|
|
183
|
-
*/
|
|
184
|
-
export declare const NodeDestroyable: {
|
|
185
|
-
new (...args: any): {
|
|
186
|
-
readonly isDestroyed: boolean;
|
|
187
|
-
_isDestroyed: boolean;
|
|
188
|
-
_destroyers: FunDestroyer_2[];
|
|
189
|
-
disposeFun<T extends FunDestroyer_2>(fun: T): T;
|
|
190
|
-
cancelDisposeFun<T extends FunDestroyer_2>(fun: T): T;
|
|
191
|
-
disposeObj<T extends /*elided*/ any>(obj: T, sync?: boolean): T;
|
|
192
|
-
cancelDisposeObj<T extends /*elided*/ any>(obj: T): T;
|
|
193
|
-
dispose<T extends FunDestroyer_2>(fun: T): T;
|
|
194
|
-
dispose<T extends /*elided*/ any>(obj: T, asyncDestroy?: boolean): T;
|
|
195
|
-
dispose<T extends FunDestroyer_2 | /*elided*/ any>(objOrFun: T, asyncDestroy?: boolean): T;
|
|
196
|
-
cancelDispose<T extends FunDestroyer_2>(fun: T): T;
|
|
197
|
-
cancelDispose<T extends /*elided*/ any>(obj: T): T;
|
|
198
|
-
cancelDispose<T extends FunDestroyer_2 | /*elided*/ any>(objOrFun: T): T;
|
|
199
|
-
destroyThis(): void;
|
|
200
|
-
destroy(): true | undefined;
|
|
201
|
-
destroyAsync(): Promise<true | undefined>;
|
|
202
|
-
};
|
|
203
|
-
} & typeof EventEmitter;
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* NodeDestroyable 实例的类型
|
|
207
|
-
*/
|
|
208
|
-
export declare type NodeDestroyable = InstanceType<NodeDestroyableClass>;
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* NodeDestroyable 类的类型
|
|
212
|
-
*/
|
|
213
|
-
export declare type NodeDestroyableClass = typeof NodeDestroyable;
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* 调用已销对象的时抛出错误
|
|
217
|
-
*/
|
|
218
|
-
export declare function throwOnDestroyed(): void;
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* WebDestroyable 类对象
|
|
222
|
-
*/
|
|
223
|
-
export declare const WebDestroyable: {
|
|
224
|
-
new (...args: any): {
|
|
225
|
-
readonly isDestroyed: boolean;
|
|
226
|
-
_isDestroyed: boolean;
|
|
227
|
-
_destroyers: FunDestroyer_2[];
|
|
228
|
-
disposeFun<T extends FunDestroyer_2>(fun: T): T;
|
|
229
|
-
cancelDisposeFun<T extends FunDestroyer_2>(fun: T): T;
|
|
230
|
-
disposeObj<T extends /*elided*/ any>(obj: T, sync?: boolean): T;
|
|
231
|
-
cancelDisposeObj<T extends /*elided*/ any>(obj: T): T;
|
|
232
|
-
dispose<T extends FunDestroyer_2>(fun: T): T;
|
|
233
|
-
dispose<T extends /*elided*/ any>(obj: T, asyncDestroy?: boolean): T;
|
|
234
|
-
dispose<T extends FunDestroyer_2 | /*elided*/ any>(objOrFun: T, asyncDestroy?: boolean): T;
|
|
235
|
-
cancelDispose<T extends FunDestroyer_2>(fun: T): T;
|
|
236
|
-
cancelDispose<T extends /*elided*/ any>(obj: T): T;
|
|
237
|
-
cancelDispose<T extends FunDestroyer_2 | /*elided*/ any>(objOrFun: T): T;
|
|
238
|
-
destroyThis(): void;
|
|
239
|
-
destroy(): true | undefined;
|
|
240
|
-
destroyAsync(): Promise<true | undefined>;
|
|
241
|
-
};
|
|
242
|
-
} & typeof EventBus;
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* WebDestroyable 实例的类型
|
|
246
|
-
*/
|
|
247
|
-
export declare type WebDestroyable = InstanceType<WebDestroyableClass>;
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* WebDestroyable 类的类型
|
|
251
|
-
*/
|
|
252
|
-
export declare type WebDestroyableClass = typeof WebDestroyable;
|
|
253
|
-
|
|
254
|
-
export { }
|
|
1
|
+
/**
|
|
2
|
+
* 函数销毁者
|
|
3
|
+
*/
|
|
4
|
+
export type FunDestroyer = () => void;
|
|
5
|
+
/**
|
|
6
|
+
* 可销毁的对象
|
|
7
|
+
*/
|
|
8
|
+
export declare class Destroyable {
|
|
9
|
+
/**
|
|
10
|
+
* 引用计数
|
|
11
|
+
* @remarks
|
|
12
|
+
* 引用计数为 0 时,对象才会被销毁
|
|
13
|
+
*/
|
|
14
|
+
refCount: number;
|
|
15
|
+
/**
|
|
16
|
+
* 是否已经销毁
|
|
17
|
+
*/
|
|
18
|
+
get isDestroyed(): boolean;
|
|
19
|
+
_isDestroyed: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* 是否可以销毁
|
|
22
|
+
*/
|
|
23
|
+
get canDestroy(): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* 销毁者
|
|
26
|
+
*/
|
|
27
|
+
_destroyers: FunDestroyer[];
|
|
28
|
+
/**
|
|
29
|
+
* 添加销毁者
|
|
30
|
+
* @param fun
|
|
31
|
+
* @returns 返回销毁者的顺序
|
|
32
|
+
*/
|
|
33
|
+
disposeFun<T extends FunDestroyer>(fun: T): T;
|
|
34
|
+
/**
|
|
35
|
+
* 取消销毁者函数
|
|
36
|
+
* @param fun
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
39
|
+
cancelDisposeFun<T extends FunDestroyer>(fun: T): T;
|
|
40
|
+
/**
|
|
41
|
+
* 添加销毁对象
|
|
42
|
+
* @param obj
|
|
43
|
+
* @param sync - 表示是否使用 `obj.destroy()` 方法进行销毁;默认使用 `obj.destroyAsync()` 方法进行销毁
|
|
44
|
+
* @returns
|
|
45
|
+
*/
|
|
46
|
+
disposeObj<T extends Destroyable>(obj: T, sync?: boolean): T;
|
|
47
|
+
/**
|
|
48
|
+
* 取消销毁者函数
|
|
49
|
+
* @param fun
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
cancelDisposeObj<T extends Destroyable>(obj: T): T;
|
|
53
|
+
/**
|
|
54
|
+
* 添加销毁函数或销毁对象
|
|
55
|
+
* @param fun
|
|
56
|
+
*/
|
|
57
|
+
dispose<T extends FunDestroyer>(fun: T): T;
|
|
58
|
+
dispose<T extends Destroyable>(obj: T, asyncDestroy?: boolean): T;
|
|
59
|
+
dispose<T extends Destroyable | FunDestroyer>(objOrFun: T, asyncDestroy?: boolean): T;
|
|
60
|
+
cancelDispose<T extends FunDestroyer>(fun: T): T;
|
|
61
|
+
cancelDispose<T extends Destroyable>(obj: T): T;
|
|
62
|
+
cancelDispose<T extends Destroyable | FunDestroyer>(objOrFun: T): T;
|
|
63
|
+
/**
|
|
64
|
+
* 自己的销毁方法
|
|
65
|
+
* @remarks
|
|
66
|
+
* 子类根据需要进行重载
|
|
67
|
+
*/
|
|
68
|
+
destroyThis(): void;
|
|
69
|
+
/**
|
|
70
|
+
* 销毁
|
|
71
|
+
*/
|
|
72
|
+
destroy(): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* 异步销毁
|
|
75
|
+
* @remarks
|
|
76
|
+
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
77
|
+
*/
|
|
78
|
+
destroyAsync(): Promise<boolean>;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 调用已销对象的时抛出错误
|
|
82
|
+
*/
|
|
83
|
+
export declare function throwOnDestroyed(): void;
|
|
84
|
+
/**
|
|
85
|
+
* 销毁对象
|
|
86
|
+
* @param object
|
|
87
|
+
* @returns
|
|
88
|
+
*/
|
|
89
|
+
export declare function destroyObject(object: any): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* 销毁 Destroyable 对象
|
|
92
|
+
* @param object
|
|
93
|
+
* @returns
|
|
94
|
+
*/
|
|
95
|
+
export declare function destroyDestroyable(object: any): boolean;
|
|
96
|
+
//# sourceMappingURL=Destroyable.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Destroyable.d.ts","sourceRoot":"","sources":["../src/Destroyable.ts"],"names":[],"mappings":"AACA;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC;AAGtC;;GAEG;AACH,qBAAa,WAAW;IAEpB;;OAEG;IACH,IAAI,WAAW,YAEd;IACD,YAAY,UAAS;IAErB;;OAEG;IACH,WAAW,EAAE,YAAY,EAAE,CAAM;IAEjC;;;;OAIG;IACH,UAAU,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAK7C;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAMnD;;;;;OAKG;IACH,UAAU,CAAC,CAAC,SAAS,WAAW,EAAE,GAAG,EAAE,CAAC,EAAC,IAAI,CAAC,EAAC,OAAO,GAAG,CAAC;IAQ1D;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,WAAW,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IASlD;;;OAGG;IACH,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAC1C,OAAO,CAAC,CAAC,SAAS,WAAW,EAAE,GAAG,EAAE,CAAC,EAAC,YAAY,CAAC,EAAC,OAAO,GAAG,CAAC;IAC/D,OAAO,CAAC,CAAC,SAAS,WAAW,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,EAAC,YAAY,CAAC,EAAC,OAAO,GAAG,CAAC;IASnF,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAChD,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAC/C,aAAa,CAAC,CAAC,SAAS,WAAW,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC;IAcnE;;;;OAIG;IACH,WAAW;IAKX;;OAEG;IACH,OAAO;
|
|
1
|
+
{"version":3,"file":"Destroyable.d.ts","sourceRoot":"","sources":["../src/Destroyable.ts"],"names":[],"mappings":"AACA;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC;AAGtC;;GAEG;AACH,qBAAa,WAAW;IAEpB;;;;OAIG;IACH,QAAQ,SAAK;IAEb;;OAEG;IACH,IAAI,WAAW,YAEd;IACD,YAAY,UAAS;IAErB;;OAEG;IACH,IAAI,UAAU,YAEb;IAED;;OAEG;IACH,WAAW,EAAE,YAAY,EAAE,CAAM;IAEjC;;;;OAIG;IACH,UAAU,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAK7C;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAMnD;;;;;OAKG;IACH,UAAU,CAAC,CAAC,SAAS,WAAW,EAAE,GAAG,EAAE,CAAC,EAAC,IAAI,CAAC,EAAC,OAAO,GAAG,CAAC;IAQ1D;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,WAAW,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IASlD;;;OAGG;IACH,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAC1C,OAAO,CAAC,CAAC,SAAS,WAAW,EAAE,GAAG,EAAE,CAAC,EAAC,YAAY,CAAC,EAAC,OAAO,GAAG,CAAC;IAC/D,OAAO,CAAC,CAAC,SAAS,WAAW,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,EAAC,YAAY,CAAC,EAAC,OAAO,GAAG,CAAC;IASnF,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAChD,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAC/C,aAAa,CAAC,CAAC,SAAS,WAAW,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC;IAcnE;;;;OAIG;IACH,WAAW;IAKX;;OAEG;IACH,OAAO;IA6BP;;;;OAIG;IACG,YAAY;CA6BrB;AAOD;;GAEG;AACH,wBAAgB,gBAAgB,SAE/B;AAKD;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,GAAG,WASxC;AAKD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,GAAG,WAY7C"}
|
package/dist/DestroyableSub.d.ts
CHANGED
|
@@ -5,8 +5,10 @@ import { EventEmitter } from "node:events";
|
|
|
5
5
|
*/
|
|
6
6
|
export declare const WebDestroyable: {
|
|
7
7
|
new (...args: any): {
|
|
8
|
+
refCount: number;
|
|
8
9
|
readonly isDestroyed: boolean;
|
|
9
10
|
_isDestroyed: boolean;
|
|
11
|
+
readonly canDestroy: boolean;
|
|
10
12
|
_destroyers: import("./Destroyable").FunDestroyer[];
|
|
11
13
|
disposeFun<T extends import("./Destroyable").FunDestroyer>(fun: T): T;
|
|
12
14
|
cancelDisposeFun<T extends import("./Destroyable").FunDestroyer>(fun: T): T;
|
|
@@ -19,8 +21,15 @@ export declare const WebDestroyable: {
|
|
|
19
21
|
cancelDispose<T extends /*elided*/ any>(obj: T): T;
|
|
20
22
|
cancelDispose<T extends import("./Destroyable").FunDestroyer | /*elided*/ any>(objOrFun: T): T;
|
|
21
23
|
destroyThis(): void;
|
|
22
|
-
destroy():
|
|
23
|
-
destroyAsync(): Promise<
|
|
24
|
+
destroy(): boolean;
|
|
25
|
+
destroyAsync(): Promise<boolean>;
|
|
26
|
+
constructor: Function;
|
|
27
|
+
toString(): string;
|
|
28
|
+
toLocaleString(): string;
|
|
29
|
+
valueOf(): Object;
|
|
30
|
+
hasOwnProperty(v: PropertyKey): boolean;
|
|
31
|
+
isPrototypeOf(v: Object): boolean;
|
|
32
|
+
propertyIsEnumerable(v: PropertyKey): boolean;
|
|
24
33
|
};
|
|
25
34
|
} & typeof EventBus;
|
|
26
35
|
/**
|
|
@@ -36,8 +45,10 @@ export type WebDestroyable = InstanceType<WebDestroyableClass>;
|
|
|
36
45
|
*/
|
|
37
46
|
export declare const NodeDestroyable: {
|
|
38
47
|
new (...args: any): {
|
|
48
|
+
refCount: number;
|
|
39
49
|
readonly isDestroyed: boolean;
|
|
40
50
|
_isDestroyed: boolean;
|
|
51
|
+
readonly canDestroy: boolean;
|
|
41
52
|
_destroyers: import("./Destroyable").FunDestroyer[];
|
|
42
53
|
disposeFun<T extends import("./Destroyable").FunDestroyer>(fun: T): T;
|
|
43
54
|
cancelDisposeFun<T extends import("./Destroyable").FunDestroyer>(fun: T): T;
|
|
@@ -50,8 +61,15 @@ export declare const NodeDestroyable: {
|
|
|
50
61
|
cancelDispose<T extends /*elided*/ any>(obj: T): T;
|
|
51
62
|
cancelDispose<T extends import("./Destroyable").FunDestroyer | /*elided*/ any>(objOrFun: T): T;
|
|
52
63
|
destroyThis(): void;
|
|
53
|
-
destroy():
|
|
54
|
-
destroyAsync(): Promise<
|
|
64
|
+
destroy(): boolean;
|
|
65
|
+
destroyAsync(): Promise<boolean>;
|
|
66
|
+
constructor: Function;
|
|
67
|
+
toString(): string;
|
|
68
|
+
toLocaleString(): string;
|
|
69
|
+
valueOf(): Object;
|
|
70
|
+
hasOwnProperty(v: PropertyKey): boolean;
|
|
71
|
+
isPrototypeOf(v: Object): boolean;
|
|
72
|
+
propertyIsEnumerable(v: PropertyKey): boolean;
|
|
55
73
|
};
|
|
56
74
|
} & typeof EventEmitter;
|
|
57
75
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DestroyableSub.d.ts","sourceRoot":"","sources":["../src/DestroyableSub.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAEzC;;GAEG;AACH,eAAO,MAAM,cAAc
|
|
1
|
+
{"version":3,"file":"DestroyableSub.d.ts","sourceRoot":"","sources":["../src/DestroyableSub.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAEzC;;GAEG;AACH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAAsC,CAAC;AAElE;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,OAAO,cAAc,CAAC;AAExD;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,YAAY,CAAC,mBAAmB,CAAC,CAAC;AAE/D;;GAEG;AACH,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;uBAA0C,CAAC;AAEvE;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,OAAO,eAAe,CAAC;AAE1D;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,YAAY,CAAC,oBAAoB,CAAC,CAAC"}
|
|
@@ -6,13 +6,23 @@ import type { FunDestroyer } from "./Destroyable";
|
|
|
6
6
|
* @param ParentClass
|
|
7
7
|
* @returns
|
|
8
8
|
*/
|
|
9
|
-
export declare function createDestroyableSubClass<P extends new (...args: any) =>
|
|
9
|
+
export declare function createDestroyableSubClass<P extends new (...args: any) => Object>(ParentClass: P): {
|
|
10
10
|
new (...args: any): {
|
|
11
|
+
/**
|
|
12
|
+
* 引用计数
|
|
13
|
+
* @remarks
|
|
14
|
+
* 引用计数为 0 时,对象才会被销毁
|
|
15
|
+
*/
|
|
16
|
+
refCount: number;
|
|
11
17
|
/**
|
|
12
18
|
* 是否已经销毁
|
|
13
19
|
*/
|
|
14
20
|
readonly isDestroyed: boolean;
|
|
15
21
|
_isDestroyed: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* 是否可以销毁
|
|
24
|
+
*/
|
|
25
|
+
readonly canDestroy: boolean;
|
|
16
26
|
/**
|
|
17
27
|
* 销毁者
|
|
18
28
|
*/
|
|
@@ -61,21 +71,28 @@ export declare function createDestroyableSubClass<P extends new (...args: any) =
|
|
|
61
71
|
/**
|
|
62
72
|
* 销毁
|
|
63
73
|
*/
|
|
64
|
-
destroy():
|
|
74
|
+
destroy(): boolean;
|
|
65
75
|
/**
|
|
66
76
|
* 异步销毁
|
|
67
77
|
* @remarks
|
|
68
78
|
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
69
79
|
*/
|
|
70
|
-
destroyAsync(): Promise<
|
|
80
|
+
destroyAsync(): Promise<boolean>;
|
|
81
|
+
constructor: Function;
|
|
82
|
+
toString(): string;
|
|
83
|
+
toLocaleString(): string;
|
|
84
|
+
valueOf(): Object;
|
|
85
|
+
hasOwnProperty(v: PropertyKey): boolean;
|
|
86
|
+
isPrototypeOf(v: Object): boolean;
|
|
87
|
+
propertyIsEnumerable(v: PropertyKey): boolean;
|
|
71
88
|
};
|
|
72
89
|
} & P;
|
|
73
90
|
/**
|
|
74
91
|
* createDestroyableSubClass 创建的类的类型
|
|
75
92
|
*/
|
|
76
|
-
export type DestroyableSubClass<ParentClass extends new (...args: any) =>
|
|
93
|
+
export type DestroyableSubClass<ParentClass extends new (...args: any) => Object> = ReturnType<typeof createDestroyableSubClass<ParentClass>>;
|
|
77
94
|
/**
|
|
78
95
|
* createDestroyableSubClass 创建的类的实例类型
|
|
79
96
|
*/
|
|
80
|
-
export type DestroyableSub<ParentClass extends new (...args: any) =>
|
|
97
|
+
export type DestroyableSub<ParentClass extends new (...args: any) => Object> = InstanceType<DestroyableSubClass<ParentClass>>;
|
|
81
98
|
//# sourceMappingURL=createDestroyableSubClass.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDestroyableSubClass.d.ts","sourceRoot":"","sources":["../src/createDestroyableSubClass.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAGlD;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,
|
|
1
|
+
{"version":3,"file":"createDestroyableSubClass.d.ts","sourceRoot":"","sources":["../src/createDestroyableSubClass.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAGlD;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,MAAM,EAAE,WAAW,EAAE,CAAC;kBAA9B,GAAG;QAQ7D;;;;WAIG;;QAGH;;WAEG;;;QAMH;;WAEG;;QAMH;;WAEG;qBACU,YAAY,EAAE;QAE3B;;;;WAIG;mBACQ,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;QAK7C;;;;WAIG;yBACc,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;QAMnD;;;;;WAKG;mBACQ,CAAC,8BAA8B,CAAC,SAAS,OAAO,GAAG,CAAC;QAQ/D;;;;WAIG;yBACc,CAAC,8BAA8B,CAAC,GAAG,CAAC;QASrD;;;WAGG;gBACK,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;gBAClC,CAAC,8BAA8B,CAAC,iBAAiB,OAAO,GAAG,CAAC;gBAC5D,CAAC,SAAS,iBAAiB,YAAY,YAAY,CAAC,iBAAiB,OAAO,GAAG,CAAC;sBAS1E,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;sBAClC,CAAC,8BAA8B,CAAC,GAAG,CAAC;sBACpC,CAAC,SAAS,iBAAiB,YAAY,YAAY,CAAC,GAAG,CAAC;QActE;;;;WAIG;;QAMH;;WAEG;;QA8BH;;;;WAIG;;;;;;;;;;MAmCV;AAMD;;GAEG;AACH,MAAM,MAAM,mBAAmB,CAAC,WAAW,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,MAAM,IAAI,UAAU,CAAC,OAAO,yBAAyB,CAAC,WAAW,CAAC,CAAC,CAAC;AAE9I;;GAEG;AACH,MAAM,MAAM,cAAc,CAAC,WAAW,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,MAAM,IAAI,YAAY,CAAC,mBAAmB,CAAC,WAAW,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var Destroyable=function(o,c){"use strict";var m=Object.defineProperty;var w=(o,c,y)=>c in o?m(o,c,{enumerable:!0,configurable:!0,writable:!0,value:y}):o[c]=y;var h=(o,c,y)=>w(o,typeof c!="symbol"?c+"":c,y);class y{constructor(){h(this,"refCount",0);h(this,"_isDestroyed",!1);h(this,"_destroyers",[])}get isDestroyed(){return this._isDestroyed}get canDestroy(){return!this.isDestroyed&&this.refCount<=0}disposeFun(e){return this._destroyers.push(e),e}cancelDisposeFun(e){const t=this._destroyers.indexOf(e);return this._destroyers.splice(t,1),e}disposeObj(e,t){const n=t?function(){return e.destroy()}:function(){return e.destroyAsync()};return this.disposeFun(n),e.__destroyable_destroyer=n,e}cancelDisposeObj(e){const t=e.__destroyable_destroyer;return t&&this.cancelDisposeFun(t),e}dispose(e,t){return typeof e=="function"?this.disposeFun(e):this.disposeObj(e,t)}cancelDispose(e){return typeof e=="function"?this.cancelDisposeFun(e):this.cancelDisposeObj(e)}destroyThis(){}destroy(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const t=this._destroyers[e];try{t()}catch(n){console.error("销毁函数在同步销毁时出错",this,t,n)}}this._destroyers.length=0;try{this.destroyThis()}catch(t){console.error("destroyThis 在异步销毁时出错",this,t)}return u(this),!0}async destroyAsync(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const t=this._destroyers[e];try{const n=t();n&&n instanceof Promise&&await n}catch(n){console.error("销毁函数在异步销毁时出错",this,t,n)}}this._destroyers.length=0;try{const t=this.destroyThis();t&&t instanceof Promise&&await t}catch(t){console.error("destroyThis 在异步销毁时出错",this,t)}return u(this),!0}}function a(){throw"已销毁"}function f(i){for(var e in i)typeof i[e]=="function"&&(i[e]=a);return i._isDestroyed=!0,!0}function u(i){for(var e in i)!(e in y.prototype)&&typeof i[e]=="function"&&(i[e]=a);return i._isDestroyed=!0,!0}function l(i){class e extends i{constructor(){super(...arguments);h(this,"refCount",0);h(this,"_isDestroyed",!1);h(this,"_destroyers",[])}get isDestroyed(){return this._isDestroyed}get canDestroy(){return!this.isDestroyed&&this.refCount<=0}disposeFun(s){return this._destroyers.push(s),s}cancelDisposeFun(s){const r=this._destroyers.indexOf(s);return this._destroyers.splice(r,1),s}disposeObj(s,r){const d=r?function(){return s.destroy()}:function(){return s.destroyAsync()};return this.disposeFun(d),s.__destroyable_destroyer=d,s}cancelDisposeObj(s){const r=s.__destroyable_destroyer;return r&&this.cancelDisposeFun(r),s}dispose(s,r){return typeof s=="function"?this.disposeFun(s):this.disposeObj(s,r)}cancelDispose(s){return typeof s=="function"?this.cancelDisposeFun(s):this.cancelDisposeObj(s)}destroyThis(){}destroy(){if(!this.canDestroy)return this.isDestroyed;let s=this._destroyers.length;for(;--s>=0;){const r=this._destroyers[s];try{r()}catch(d){console.error("销毁函数在同步销毁时出错",this,r,d)}}this._destroyers.length=0;try{this.destroyThis()}catch(r){console.error("destroyThis 在异步销毁时出错",this,r)}return u(this),!0}async destroyAsync(){if(!this.canDestroy)return this.isDestroyed;let s=this._destroyers.length;for(;--s>=0;){const r=this._destroyers[s];try{const d=r();d&&d instanceof Promise&&await d}catch(d){console.error("销毁函数在异步销毁时出错",this,r,d)}}this._destroyers.length=0;try{const r=this.destroyThis();r&&r instanceof Promise&&await r}catch(r){console.error("destroyThis 在异步销毁时出错",this,r)}return u(this),!0}}return e}var D=Object.defineProperty,p=(i,e,t)=>e in i?D(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t,_=(i,e,t)=>(p(i,e+"",t),t);if(!globalThis.CustomEvent){class i extends Event{constructor(t,n){super(t,n),_(this,"detail"),this.detail=n==null?void 0:n.detail}}globalThis.CustomEvent=i}class v extends EventTarget{addEventListener(e,t,n){const s=n==null?void 0:n.times;if(s){const r=typeof t=="function"?t:t==null?void 0:t.handleEvent;if(r)return this.multipleListen(e,r,s)}return super.addEventListener(e,t,n),()=>{this.removeEventListener(e,t,n)}}dispatchEvent(e,t){const n=e instanceof Event?e:new CustomEvent(e,{detail:t,bubbles:!1,cancelable:!0,composed:!1});return super.dispatchEvent(n)}onceListen(e,t,n){return this.addEventListener(e,t,{...n,once:!0})}multipleListen(e,t,n){let s=0;const r=new AbortController,d=r.signal;return super.addEventListener(e,E=>{++s>=n&&r.abort(),t.call(this,E)},{signal:d}),function(){r.abort()}}}const b=l(v),g=l(c.EventEmitter);return o.Destroyable=y,o.NodeDestroyable=g,o.WebDestroyable=b,o.createDestroyableSubClass=l,o.destroyDestroyable=u,o.destroyObject=f,o.throwOnDestroyed=a,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"}),o}({},node_events);
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { EventBus as
|
|
5
|
-
import { EventEmitter as
|
|
6
|
-
class
|
|
1
|
+
var u = Object.defineProperty;
|
|
2
|
+
var a = (i, e, s) => e in i ? u(i, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : i[e] = s;
|
|
3
|
+
var y = (i, e, s) => a(i, typeof e != "symbol" ? e + "" : e, s);
|
|
4
|
+
import { EventBus as l } from "@gby/event-bus";
|
|
5
|
+
import { EventEmitter as f } from "node:events";
|
|
6
|
+
class p {
|
|
7
7
|
constructor() {
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* 引用计数
|
|
10
|
+
* @remarks
|
|
11
|
+
* 引用计数为 0 时,对象才会被销毁
|
|
12
|
+
*/
|
|
13
|
+
y(this, "refCount", 0);
|
|
14
|
+
y(this, "_isDestroyed", !1);
|
|
9
15
|
/**
|
|
10
16
|
* 销毁者
|
|
11
17
|
*/
|
|
12
|
-
|
|
18
|
+
y(this, "_destroyers", []);
|
|
13
19
|
}
|
|
14
20
|
/**
|
|
15
21
|
* 是否已经销毁
|
|
@@ -17,6 +23,12 @@ class _ {
|
|
|
17
23
|
get isDestroyed() {
|
|
18
24
|
return this._isDestroyed;
|
|
19
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* 是否可以销毁
|
|
28
|
+
*/
|
|
29
|
+
get canDestroy() {
|
|
30
|
+
return !this.isDestroyed && this.refCount <= 0;
|
|
31
|
+
}
|
|
20
32
|
/**
|
|
21
33
|
* 添加销毁者
|
|
22
34
|
* @param fun
|
|
@@ -31,8 +43,8 @@ class _ {
|
|
|
31
43
|
* @returns
|
|
32
44
|
*/
|
|
33
45
|
cancelDisposeFun(e) {
|
|
34
|
-
const
|
|
35
|
-
return this._destroyers.splice(
|
|
46
|
+
const s = this._destroyers.indexOf(e);
|
|
47
|
+
return this._destroyers.splice(s, 1), e;
|
|
36
48
|
}
|
|
37
49
|
/**
|
|
38
50
|
* 添加销毁对象
|
|
@@ -40,13 +52,13 @@ class _ {
|
|
|
40
52
|
* @param sync - 表示是否使用 `obj.destroy()` 方法进行销毁;默认使用 `obj.destroyAsync()` 方法进行销毁
|
|
41
53
|
* @returns
|
|
42
54
|
*/
|
|
43
|
-
disposeObj(e,
|
|
44
|
-
const
|
|
55
|
+
disposeObj(e, s) {
|
|
56
|
+
const o = s ? function() {
|
|
45
57
|
return e.destroy();
|
|
46
58
|
} : function() {
|
|
47
59
|
return e.destroyAsync();
|
|
48
60
|
};
|
|
49
|
-
return this.disposeFun(
|
|
61
|
+
return this.disposeFun(o), e.__destroyable_destroyer = o, e;
|
|
50
62
|
}
|
|
51
63
|
/**
|
|
52
64
|
* 取消销毁者函数
|
|
@@ -54,11 +66,11 @@ class _ {
|
|
|
54
66
|
* @returns
|
|
55
67
|
*/
|
|
56
68
|
cancelDisposeObj(e) {
|
|
57
|
-
const
|
|
58
|
-
return
|
|
69
|
+
const s = e.__destroyable_destroyer;
|
|
70
|
+
return s && this.cancelDisposeFun(s), e;
|
|
59
71
|
}
|
|
60
|
-
dispose(e,
|
|
61
|
-
return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e,
|
|
72
|
+
dispose(e, s) {
|
|
73
|
+
return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, s);
|
|
62
74
|
}
|
|
63
75
|
cancelDispose(e) {
|
|
64
76
|
return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
|
|
@@ -74,14 +86,23 @@ class _ {
|
|
|
74
86
|
* 销毁
|
|
75
87
|
*/
|
|
76
88
|
destroy() {
|
|
77
|
-
if (this.
|
|
89
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
78
90
|
let e = this._destroyers.length;
|
|
79
|
-
for (; --e >= 0; )
|
|
91
|
+
for (; --e >= 0; ) {
|
|
92
|
+
const s = this._destroyers[e];
|
|
80
93
|
try {
|
|
81
|
-
|
|
82
|
-
} catch {
|
|
94
|
+
s();
|
|
95
|
+
} catch (o) {
|
|
96
|
+
console.error("销毁函数在同步销毁时出错", this, s, o);
|
|
83
97
|
}
|
|
84
|
-
|
|
98
|
+
}
|
|
99
|
+
this._destroyers.length = 0;
|
|
100
|
+
try {
|
|
101
|
+
this.destroyThis();
|
|
102
|
+
} catch (s) {
|
|
103
|
+
console.error("destroyThis 在异步销毁时出错", this, s);
|
|
104
|
+
}
|
|
105
|
+
return c(this), !0;
|
|
85
106
|
}
|
|
86
107
|
/**
|
|
87
108
|
* 异步销毁
|
|
@@ -89,38 +110,55 @@ class _ {
|
|
|
89
110
|
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
90
111
|
*/
|
|
91
112
|
async destroyAsync() {
|
|
92
|
-
if (this.
|
|
113
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
93
114
|
let e = this._destroyers.length;
|
|
94
|
-
for (; --e >= 0; )
|
|
115
|
+
for (; --e >= 0; ) {
|
|
116
|
+
const s = this._destroyers[e];
|
|
95
117
|
try {
|
|
96
|
-
|
|
97
|
-
|
|
118
|
+
const o = s();
|
|
119
|
+
o && o instanceof Promise && await o;
|
|
120
|
+
} catch (o) {
|
|
121
|
+
console.error("销毁函数在异步销毁时出错", this, s, o);
|
|
98
122
|
}
|
|
99
|
-
|
|
123
|
+
}
|
|
124
|
+
this._destroyers.length = 0;
|
|
125
|
+
try {
|
|
126
|
+
const s = this.destroyThis();
|
|
127
|
+
s && s instanceof Promise && await s;
|
|
128
|
+
} catch (s) {
|
|
129
|
+
console.error("destroyThis 在异步销毁时出错", this, s);
|
|
130
|
+
}
|
|
131
|
+
return c(this), !0;
|
|
100
132
|
}
|
|
101
133
|
}
|
|
102
|
-
function
|
|
134
|
+
function h() {
|
|
103
135
|
throw "已销毁";
|
|
104
136
|
}
|
|
105
|
-
function
|
|
106
|
-
for (var e in
|
|
107
|
-
typeof
|
|
108
|
-
return
|
|
137
|
+
function w(i) {
|
|
138
|
+
for (var e in i)
|
|
139
|
+
typeof i[e] == "function" && (i[e] = h);
|
|
140
|
+
return i._isDestroyed = !0, !0;
|
|
109
141
|
}
|
|
110
|
-
function
|
|
111
|
-
for (var e in
|
|
112
|
-
!(e in
|
|
113
|
-
return
|
|
142
|
+
function c(i) {
|
|
143
|
+
for (var e in i)
|
|
144
|
+
!(e in p.prototype) && typeof i[e] == "function" && (i[e] = h);
|
|
145
|
+
return i._isDestroyed = !0, !0;
|
|
114
146
|
}
|
|
115
|
-
function
|
|
116
|
-
class e extends
|
|
147
|
+
function d(i) {
|
|
148
|
+
class e extends i {
|
|
117
149
|
constructor() {
|
|
118
150
|
super(...arguments);
|
|
119
|
-
|
|
151
|
+
/**
|
|
152
|
+
* 引用计数
|
|
153
|
+
* @remarks
|
|
154
|
+
* 引用计数为 0 时,对象才会被销毁
|
|
155
|
+
*/
|
|
156
|
+
y(this, "refCount", 0);
|
|
157
|
+
y(this, "_isDestroyed", !1);
|
|
120
158
|
/**
|
|
121
159
|
* 销毁者
|
|
122
160
|
*/
|
|
123
|
-
|
|
161
|
+
y(this, "_destroyers", []);
|
|
124
162
|
}
|
|
125
163
|
/**
|
|
126
164
|
* 是否已经销毁
|
|
@@ -128,22 +166,28 @@ function c(t) {
|
|
|
128
166
|
get isDestroyed() {
|
|
129
167
|
return this._isDestroyed;
|
|
130
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* 是否可以销毁
|
|
171
|
+
*/
|
|
172
|
+
get canDestroy() {
|
|
173
|
+
return !this.isDestroyed && this.refCount <= 0;
|
|
174
|
+
}
|
|
131
175
|
/**
|
|
132
176
|
* 添加销毁者
|
|
133
177
|
* @param fun
|
|
134
178
|
* @returns 返回销毁者的顺序
|
|
135
179
|
*/
|
|
136
|
-
disposeFun(
|
|
137
|
-
return this._destroyers.push(
|
|
180
|
+
disposeFun(t) {
|
|
181
|
+
return this._destroyers.push(t), t;
|
|
138
182
|
}
|
|
139
183
|
/**
|
|
140
184
|
* 取消销毁者函数
|
|
141
185
|
* @param fun
|
|
142
186
|
* @returns
|
|
143
187
|
*/
|
|
144
|
-
cancelDisposeFun(
|
|
145
|
-
const
|
|
146
|
-
return this._destroyers.splice(
|
|
188
|
+
cancelDisposeFun(t) {
|
|
189
|
+
const r = this._destroyers.indexOf(t);
|
|
190
|
+
return this._destroyers.splice(r, 1), t;
|
|
147
191
|
}
|
|
148
192
|
/**
|
|
149
193
|
* 添加销毁对象
|
|
@@ -151,28 +195,28 @@ function c(t) {
|
|
|
151
195
|
* @param sync - 表示是否使用 `obj.destroy()` 方法进行销毁;默认使用 `obj.destroyAsync()` 方法进行销毁
|
|
152
196
|
* @returns
|
|
153
197
|
*/
|
|
154
|
-
disposeObj(
|
|
155
|
-
const
|
|
156
|
-
return
|
|
198
|
+
disposeObj(t, r) {
|
|
199
|
+
const n = r ? function() {
|
|
200
|
+
return t.destroy();
|
|
157
201
|
} : function() {
|
|
158
|
-
return
|
|
202
|
+
return t.destroyAsync();
|
|
159
203
|
};
|
|
160
|
-
return this.disposeFun(
|
|
204
|
+
return this.disposeFun(n), t.__destroyable_destroyer = n, t;
|
|
161
205
|
}
|
|
162
206
|
/**
|
|
163
207
|
* 取消销毁者函数
|
|
164
208
|
* @param fun
|
|
165
209
|
* @returns
|
|
166
210
|
*/
|
|
167
|
-
cancelDisposeObj(
|
|
168
|
-
const
|
|
169
|
-
return
|
|
211
|
+
cancelDisposeObj(t) {
|
|
212
|
+
const r = t.__destroyable_destroyer;
|
|
213
|
+
return r && this.cancelDisposeFun(r), t;
|
|
170
214
|
}
|
|
171
|
-
dispose(
|
|
172
|
-
return typeof
|
|
215
|
+
dispose(t, r) {
|
|
216
|
+
return typeof t == "function" ? this.disposeFun(t) : this.disposeObj(t, r);
|
|
173
217
|
}
|
|
174
|
-
cancelDispose(
|
|
175
|
-
return typeof
|
|
218
|
+
cancelDispose(t) {
|
|
219
|
+
return typeof t == "function" ? this.cancelDisposeFun(t) : this.cancelDisposeObj(t);
|
|
176
220
|
}
|
|
177
221
|
/**
|
|
178
222
|
* 自己的销毁方法
|
|
@@ -185,14 +229,23 @@ function c(t) {
|
|
|
185
229
|
* 销毁
|
|
186
230
|
*/
|
|
187
231
|
destroy() {
|
|
188
|
-
if (this.
|
|
189
|
-
let
|
|
190
|
-
for (; --
|
|
232
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
233
|
+
let t = this._destroyers.length;
|
|
234
|
+
for (; --t >= 0; ) {
|
|
235
|
+
const r = this._destroyers[t];
|
|
191
236
|
try {
|
|
192
|
-
|
|
193
|
-
} catch {
|
|
237
|
+
r();
|
|
238
|
+
} catch (n) {
|
|
239
|
+
console.error("销毁函数在同步销毁时出错", this, r, n);
|
|
194
240
|
}
|
|
195
|
-
|
|
241
|
+
}
|
|
242
|
+
this._destroyers.length = 0;
|
|
243
|
+
try {
|
|
244
|
+
this.destroyThis();
|
|
245
|
+
} catch (r) {
|
|
246
|
+
console.error("destroyThis 在异步销毁时出错", this, r);
|
|
247
|
+
}
|
|
248
|
+
return c(this), !0;
|
|
196
249
|
}
|
|
197
250
|
/**
|
|
198
251
|
* 异步销毁
|
|
@@ -200,30 +253,36 @@ function c(t) {
|
|
|
200
253
|
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
201
254
|
*/
|
|
202
255
|
async destroyAsync() {
|
|
203
|
-
if (this.
|
|
204
|
-
let
|
|
205
|
-
for (; --
|
|
256
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
257
|
+
let t = this._destroyers.length;
|
|
258
|
+
for (; --t >= 0; ) {
|
|
259
|
+
const r = this._destroyers[t];
|
|
206
260
|
try {
|
|
207
|
-
|
|
208
|
-
|
|
261
|
+
const n = r();
|
|
262
|
+
n && n instanceof Promise && await n;
|
|
263
|
+
} catch (n) {
|
|
264
|
+
console.error("销毁函数在异步销毁时出错", this, r, n);
|
|
209
265
|
}
|
|
210
|
-
|
|
266
|
+
}
|
|
267
|
+
this._destroyers.length = 0;
|
|
268
|
+
try {
|
|
269
|
+
const r = this.destroyThis();
|
|
270
|
+
r && r instanceof Promise && await r;
|
|
271
|
+
} catch (r) {
|
|
272
|
+
console.error("destroyThis 在异步销毁时出错", this, r);
|
|
273
|
+
}
|
|
274
|
+
return c(this), !0;
|
|
211
275
|
}
|
|
212
276
|
}
|
|
213
|
-
function r(n) {
|
|
214
|
-
for (var d in n)
|
|
215
|
-
!(d in e.prototype) && typeof n[d] == "function" && (n[d] = y);
|
|
216
|
-
return n._isDestroyed = !0, !0;
|
|
217
|
-
}
|
|
218
277
|
return e;
|
|
219
278
|
}
|
|
220
|
-
const
|
|
279
|
+
const x = d(l), T = d(f);
|
|
221
280
|
export {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
281
|
+
p as Destroyable,
|
|
282
|
+
T as NodeDestroyable,
|
|
283
|
+
x as WebDestroyable,
|
|
284
|
+
d as createDestroyableSubClass,
|
|
285
|
+
c as destroyDestroyable,
|
|
286
|
+
w as destroyObject,
|
|
287
|
+
h as throwOnDestroyed
|
|
229
288
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(i,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("@gby/event-bus"),require("node:events")):typeof define=="function"&&define.amd?define(["exports","@gby/event-bus","node:events"],o):(i=typeof globalThis<"u"?globalThis:i||self,o(i.Destroyable={},i.eventBus,i.node_events))})(this,function(i,o,h){"use strict";var b=Object.defineProperty;var g=(i,o,h)=>o in i?b(i,o,{enumerable:!0,configurable:!0,writable:!0,value:h}):i[o]=h;var c=(i,o,h)=>g(i,typeof o!="symbol"?o+"":o,h);class l{constructor(){c(this,"refCount",0);c(this,"_isDestroyed",!1);c(this,"_destroyers",[])}get isDestroyed(){return this._isDestroyed}get canDestroy(){return!this.isDestroyed&&this.refCount<=0}disposeFun(e){return this._destroyers.push(e),e}cancelDisposeFun(e){const t=this._destroyers.indexOf(e);return this._destroyers.splice(t,1),e}disposeObj(e,t){const y=t?function(){return e.destroy()}:function(){return e.destroyAsync()};return this.disposeFun(y),e.__destroyable_destroyer=y,e}cancelDisposeObj(e){const t=e.__destroyable_destroyer;return t&&this.cancelDisposeFun(t),e}dispose(e,t){return typeof e=="function"?this.disposeFun(e):this.disposeObj(e,t)}cancelDispose(e){return typeof e=="function"?this.cancelDisposeFun(e):this.cancelDisposeObj(e)}destroyThis(){}destroy(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const t=this._destroyers[e];try{t()}catch(y){console.error("销毁函数在同步销毁时出错",this,t,y)}}this._destroyers.length=0;try{this.destroyThis()}catch(t){console.error("destroyThis 在异步销毁时出错",this,t)}return u(this),!0}async destroyAsync(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const t=this._destroyers[e];try{const y=t();y&&y instanceof Promise&&await y}catch(y){console.error("销毁函数在异步销毁时出错",this,t,y)}}this._destroyers.length=0;try{const t=this.destroyThis();t&&t instanceof Promise&&await t}catch(t){console.error("destroyThis 在异步销毁时出错",this,t)}return u(this),!0}}function a(){throw"已销毁"}function D(n){for(var e in n)typeof n[e]=="function"&&(n[e]=a);return n._isDestroyed=!0,!0}function u(n){for(var e in n)!(e in l.prototype)&&typeof n[e]=="function"&&(n[e]=a);return n._isDestroyed=!0,!0}function f(n){class e extends n{constructor(){super(...arguments);c(this,"refCount",0);c(this,"_isDestroyed",!1);c(this,"_destroyers",[])}get isDestroyed(){return this._isDestroyed}get canDestroy(){return!this.isDestroyed&&this.refCount<=0}disposeFun(s){return this._destroyers.push(s),s}cancelDisposeFun(s){const r=this._destroyers.indexOf(s);return this._destroyers.splice(r,1),s}disposeObj(s,r){const d=r?function(){return s.destroy()}:function(){return s.destroyAsync()};return this.disposeFun(d),s.__destroyable_destroyer=d,s}cancelDisposeObj(s){const r=s.__destroyable_destroyer;return r&&this.cancelDisposeFun(r),s}dispose(s,r){return typeof s=="function"?this.disposeFun(s):this.disposeObj(s,r)}cancelDispose(s){return typeof s=="function"?this.cancelDisposeFun(s):this.cancelDisposeObj(s)}destroyThis(){}destroy(){if(!this.canDestroy)return this.isDestroyed;let s=this._destroyers.length;for(;--s>=0;){const r=this._destroyers[s];try{r()}catch(d){console.error("销毁函数在同步销毁时出错",this,r,d)}}this._destroyers.length=0;try{this.destroyThis()}catch(r){console.error("destroyThis 在异步销毁时出错",this,r)}return u(this),!0}async destroyAsync(){if(!this.canDestroy)return this.isDestroyed;let s=this._destroyers.length;for(;--s>=0;){const r=this._destroyers[s];try{const d=r();d&&d instanceof Promise&&await d}catch(d){console.error("销毁函数在异步销毁时出错",this,r,d)}}this._destroyers.length=0;try{const r=this.destroyThis();r&&r instanceof Promise&&await r}catch(r){console.error("destroyThis 在异步销毁时出错",this,r)}return u(this),!0}}return e}const _=f(o.EventBus),p=f(h.EventEmitter);i.Destroyable=l,i.NodeDestroyable=p,i.WebDestroyable=_,i.createDestroyableSubClass=f,i.destroyDestroyable=u,i.destroyObject=D,i.throwOnDestroyed=a,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
**Signature:**
|
|
10
10
|
|
|
11
11
|
```typescript
|
|
12
|
-
export declare function createDestroyableSubClass<P extends new (...args: any) =>
|
|
12
|
+
export declare function createDestroyableSubClass<P extends new (...args: any) => Object>(ParentClass: P): {
|
|
13
13
|
new (...args: any): {
|
|
14
14
|
readonly isDestroyed: boolean;
|
|
15
15
|
_isDestroyed: boolean;
|
|
@@ -27,6 +27,13 @@ export declare function createDestroyableSubClass<P extends new (...args: any) =
|
|
|
27
27
|
destroyThis(): void;
|
|
28
28
|
destroy(): true | undefined;
|
|
29
29
|
destroyAsync(): Promise<true | undefined>;
|
|
30
|
+
constructor: Function;
|
|
31
|
+
toString(): string;
|
|
32
|
+
toLocaleString(): string;
|
|
33
|
+
valueOf(): Object;
|
|
34
|
+
hasOwnProperty(v: PropertyKey): boolean;
|
|
35
|
+
isPrototypeOf(v: Object): boolean;
|
|
36
|
+
propertyIsEnumerable(v: PropertyKey): boolean;
|
|
30
37
|
};
|
|
31
38
|
} & P;
|
|
32
39
|
```
|
|
@@ -66,7 +73,7 @@ P
|
|
|
66
73
|
</tbody></table>
|
|
67
74
|
**Returns:**
|
|
68
75
|
|
|
69
|
-
{ new (...args: any): { readonly isDestroyed: boolean; \_isDestroyed: boolean; \_destroyers: [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->\[\]; disposeFun<T extends [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->>(fun: T): T; cancelDisposeFun<T extends [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->>(fun: T): T; disposeObj<T extends /\*elided\*/ any>(obj: T, sync?: boolean): T; cancelDisposeObj<T extends /\*elided\*/ any>(obj: T): T; dispose<T extends [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->>(fun: T): T; dispose<T extends /\*elided\*/ any>(obj: T, asyncDestroy?: boolean): T; dispose<T extends /\*elided\*/ any \| [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->>(objOrFun: T, asyncDestroy?: boolean): T; cancelDispose<T extends [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->>(fun: T): T; cancelDispose<T extends /\*elided\*/ any>(obj: T): T; cancelDispose<T extends /\*elided\*/ any \| [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->>(objOrFun: T): T; destroyThis(): void; destroy(): true \| undefined; destroyAsync(): Promise<true \| undefined>; }; } & P
|
|
76
|
+
{ new (...args: any): { readonly isDestroyed: boolean; \_isDestroyed: boolean; \_destroyers: [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->\[\]; disposeFun<T extends [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->>(fun: T): T; cancelDisposeFun<T extends [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->>(fun: T): T; disposeObj<T extends /\*elided\*/ any>(obj: T, sync?: boolean): T; cancelDisposeObj<T extends /\*elided\*/ any>(obj: T): T; dispose<T extends [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->>(fun: T): T; dispose<T extends /\*elided\*/ any>(obj: T, asyncDestroy?: boolean): T; dispose<T extends /\*elided\*/ any \| [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->>(objOrFun: T, asyncDestroy?: boolean): T; cancelDispose<T extends [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->>(fun: T): T; cancelDispose<T extends /\*elided\*/ any>(obj: T): T; cancelDispose<T extends /\*elided\*/ any \| [FunDestroyer](./destroyable.fundestroyer.md)<!-- -->>(objOrFun: T): T; destroyThis(): void; destroy(): true \| undefined; destroyAsync(): Promise<true \| undefined>; constructor: Function; toString(): string; toLocaleString(): string; valueOf(): Object; hasOwnProperty(v: PropertyKey): boolean; isPrototypeOf(v: Object): boolean; propertyIsEnumerable(v: PropertyKey): boolean; }; } & P
|
|
70
77
|
|
|
71
78
|
|
|
72
79
|
## Remarks
|
|
@@ -9,7 +9,7 @@ createDestroyableSubClass 创建的类的实例类型
|
|
|
9
9
|
**Signature:**
|
|
10
10
|
|
|
11
11
|
```typescript
|
|
12
|
-
export type DestroyableSub<ParentClass extends new (...args: any) =>
|
|
12
|
+
export type DestroyableSub<ParentClass extends new (...args: any) => Object> = InstanceType<DestroyableSubClass<ParentClass>>;
|
|
13
13
|
```
|
|
14
14
|
**References:** [DestroyableSubClass](./destroyable.destroyablesubclass.md)
|
|
15
15
|
|
|
@@ -9,5 +9,5 @@ createDestroyableSubClass 创建的类的类型
|
|
|
9
9
|
**Signature:**
|
|
10
10
|
|
|
11
11
|
```typescript
|
|
12
|
-
export type DestroyableSubClass<ParentClass extends new (...args: any) =>
|
|
12
|
+
export type DestroyableSubClass<ParentClass extends new (...args: any) => Object> = ReturnType<typeof createDestroyableSubClass<ParentClass>>;
|
|
13
13
|
```
|
|
@@ -27,6 +27,13 @@ NodeDestroyable: {
|
|
|
27
27
|
destroyThis(): void;
|
|
28
28
|
destroy(): true | undefined;
|
|
29
29
|
destroyAsync(): Promise<true | undefined>;
|
|
30
|
+
constructor: Function;
|
|
31
|
+
toString(): string;
|
|
32
|
+
toLocaleString(): string;
|
|
33
|
+
valueOf(): Object;
|
|
34
|
+
hasOwnProperty(v: PropertyKey): boolean;
|
|
35
|
+
isPrototypeOf(v: Object): boolean;
|
|
36
|
+
propertyIsEnumerable(v: PropertyKey): boolean;
|
|
30
37
|
};
|
|
31
38
|
} & typeof EventEmitter
|
|
32
39
|
```
|
|
@@ -27,6 +27,13 @@ WebDestroyable: {
|
|
|
27
27
|
destroyThis(): void;
|
|
28
28
|
destroy(): true | undefined;
|
|
29
29
|
destroyAsync(): Promise<true | undefined>;
|
|
30
|
+
constructor: Function;
|
|
31
|
+
toString(): string;
|
|
32
|
+
toLocaleString(): string;
|
|
33
|
+
valueOf(): Object;
|
|
34
|
+
hasOwnProperty(v: PropertyKey): boolean;
|
|
35
|
+
isPrototypeOf(v: Object): boolean;
|
|
36
|
+
propertyIsEnumerable(v: PropertyKey): boolean;
|
|
30
37
|
};
|
|
31
38
|
} & typeof EventBus
|
|
32
39
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gby/destroyable",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "@gby/destroyable 提供了用于构建可销毁对象的类的基类,用于使得地管理可销毁对象的生命周期",
|
|
5
5
|
"main": "./dist/index.umd.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"exports": {
|
|
9
9
|
"require": "./dist/index.umd.cjs",
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
|
-
"import": "./dist/index.js"
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
12
13
|
},
|
|
13
14
|
"type": "module",
|
|
14
15
|
"sideEffects": false,
|
package/dist/tsdoc-metadata.json
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
-
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
-
{
|
|
4
|
-
"tsdocVersion": "0.12",
|
|
5
|
-
"toolPackages": [
|
|
6
|
-
{
|
|
7
|
-
"packageName": "@microsoft/api-extractor",
|
|
8
|
-
"packageVersion": "7.52.8"
|
|
9
|
-
}
|
|
10
|
-
]
|
|
11
|
-
}
|