@gby/destroyable 3.1.0 → 3.3.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 +75 -0
- package/dist/Destroyable.d.ts.map +1 -1
- package/dist/DestroyableEventEmitter.d.ts +1 -1
- package/dist/DestroyableEventTarget-DFVzomDG.js +416 -0
- package/dist/DestroyableEventTarget-DSGfqvV0.cjs +1 -0
- package/dist/DestroyableEventTarget.d.ts +1 -1
- package/dist/index-web.cjs +1 -1
- package/dist/index-web.js +5 -4
- package/dist/index.cjs +1 -1
- package/dist/index.js +7 -6
- package/package.json +1 -1
- package/dist/DestroyableEventTarget-CI_bGgU0.js +0 -287
- package/dist/DestroyableEventTarget-NFP7x4Ni.cjs +0 -1
package/dist/Destroyable.d.ts
CHANGED
|
@@ -72,6 +72,81 @@ export interface IDestroyable {
|
|
|
72
72
|
*/
|
|
73
73
|
destroySync(): boolean;
|
|
74
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* 可销毁的对象,不继承任何事件系统
|
|
77
|
+
*/
|
|
78
|
+
export declare class Destroyable implements IDestroyable {
|
|
79
|
+
/**
|
|
80
|
+
* 引用计数
|
|
81
|
+
* @remarks
|
|
82
|
+
* 引用计数为 0 时,对象才会被销毁
|
|
83
|
+
*/
|
|
84
|
+
refCount: number;
|
|
85
|
+
/**
|
|
86
|
+
* 是否已经销毁
|
|
87
|
+
*/
|
|
88
|
+
get isDestroyed(): boolean;
|
|
89
|
+
_isDestroyed: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* 是否可以销毁
|
|
92
|
+
*/
|
|
93
|
+
get canDestroy(): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* 销毁者
|
|
96
|
+
*/
|
|
97
|
+
_destroyers: FunDestroyer[];
|
|
98
|
+
/**
|
|
99
|
+
* 添加销毁者
|
|
100
|
+
* @param fun
|
|
101
|
+
* @returns 返回销毁者的顺序
|
|
102
|
+
*/
|
|
103
|
+
disposeFun<T extends FunDestroyer>(fun: T): T;
|
|
104
|
+
/**
|
|
105
|
+
* 取消销毁者函数
|
|
106
|
+
* @param fun
|
|
107
|
+
* @returns
|
|
108
|
+
*/
|
|
109
|
+
cancelDisposeFun<T extends FunDestroyer>(fun: T): T;
|
|
110
|
+
/**
|
|
111
|
+
* 添加销毁对象
|
|
112
|
+
* @param obj
|
|
113
|
+
* @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
|
|
114
|
+
* @returns
|
|
115
|
+
*/
|
|
116
|
+
disposeObj<T extends IDestroyable>(obj: T, sync?: boolean): T;
|
|
117
|
+
/**
|
|
118
|
+
* 取消销毁者函数
|
|
119
|
+
* @param fun
|
|
120
|
+
* @returns
|
|
121
|
+
*/
|
|
122
|
+
cancelDisposeObj<T extends IDestroyable>(obj: T): T;
|
|
123
|
+
/**
|
|
124
|
+
* 添加销毁函数或销毁对象
|
|
125
|
+
* @param fun
|
|
126
|
+
*/
|
|
127
|
+
dispose<T extends FunDestroyer>(fun: T): T;
|
|
128
|
+
dispose<T extends IDestroyable>(obj: T, asyncDestroy?: boolean): T;
|
|
129
|
+
dispose<T extends IDestroyable | FunDestroyer>(objOrFun: T, asyncDestroy?: boolean): T;
|
|
130
|
+
cancelDispose<T extends FunDestroyer>(fun: T): T;
|
|
131
|
+
cancelDispose<T extends IDestroyable>(obj: T): T;
|
|
132
|
+
cancelDispose<T extends IDestroyable | FunDestroyer>(objOrFun: T): T;
|
|
133
|
+
/**
|
|
134
|
+
* 自己的销毁方法
|
|
135
|
+
* @remarks
|
|
136
|
+
* 子类根据需要进行重载
|
|
137
|
+
*/
|
|
138
|
+
destroyThis(): Promise<void> | void;
|
|
139
|
+
/**
|
|
140
|
+
* 销毁
|
|
141
|
+
*/
|
|
142
|
+
destroySync(): boolean;
|
|
143
|
+
/**
|
|
144
|
+
* 异步销毁
|
|
145
|
+
* @remarks
|
|
146
|
+
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
147
|
+
*/
|
|
148
|
+
destroy(): Promise<boolean>;
|
|
149
|
+
}
|
|
75
150
|
/**
|
|
76
151
|
* 调用已销对象的时抛出错误
|
|
77
152
|
*/
|
|
@@ -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,MAAM,WAAW,YAAY;IAEzB;;;;OAIG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,QAAQ,CAAC,WAAW,EAAC,OAAO,CAAC;IAE7B;;OAEG;IACH,QAAQ,CAAC,UAAU,EAAC,OAAO,CAAC;IAG5B;;;;OAIG;IACH,UAAU,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;IAE9C;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;IAEpD;;;;;OAKG;IACH,UAAU,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,EAAC,IAAI,CAAC,EAAC,OAAO,GAAG,CAAC,CAAC;IAE5D;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;IAEpD;;;OAGG;IACH,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;IAC3C,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,EAAC,YAAY,CAAC,EAAC,OAAO,GAAG,CAAC,CAAA;IAChE,OAAO,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,EAAC,YAAY,CAAC,EAAC,OAAO,GAAG,CAAC,CAAC;IAGrF,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;IACjD,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAA;IAChD,aAAa,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC;IASrE;;;;OAIG;IACH,WAAW,IAAG,OAAO,CAAC,IAAI,CAAC,GAAC,IAAI,CAAC;IAKjC;;;;OAIG;IACH,OAAO,IAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAE7B;;OAEG;IACH,WAAW,IAAG,OAAO,CAAC;CAUzB;
|
|
1
|
+
{"version":3,"file":"Destroyable.d.ts","sourceRoot":"","sources":["../src/Destroyable.ts"],"names":[],"mappings":"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"}
|
|
@@ -3,7 +3,7 @@ import { EventEmitter } from "node:events";
|
|
|
3
3
|
export type EventMap<T> = Record<keyof T, any[]> | DefaultEventMap;
|
|
4
4
|
export type DefaultEventMap = [never];
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* 可销毁的对象,继承自 Node 的 EventEmitter
|
|
7
7
|
*/
|
|
8
8
|
export declare class DestroyableEventEmitter<EM extends EventMap<EM> = DefaultEventMap> extends EventEmitter<EM> implements IDestroyable {
|
|
9
9
|
/**
|
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
var d = Object.defineProperty;
|
|
2
|
+
var u = (o, s, e) => s in o ? d(o, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[s] = e;
|
|
3
|
+
var h = (o, s, e) => u(o, typeof s != "symbol" ? s + "" : s, e);
|
|
4
|
+
import { EventBus as a } from "@gby/event-bus";
|
|
5
|
+
class l {
|
|
6
|
+
constructor() {
|
|
7
|
+
/**
|
|
8
|
+
* 引用计数
|
|
9
|
+
* @remarks
|
|
10
|
+
* 引用计数为 0 时,对象才会被销毁
|
|
11
|
+
*/
|
|
12
|
+
h(this, "refCount", 0);
|
|
13
|
+
h(this, "_isDestroyed", !1);
|
|
14
|
+
/**
|
|
15
|
+
* 销毁者
|
|
16
|
+
*/
|
|
17
|
+
h(this, "_destroyers", []);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 是否已经销毁
|
|
21
|
+
*/
|
|
22
|
+
get isDestroyed() {
|
|
23
|
+
return this._isDestroyed;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 是否可以销毁
|
|
27
|
+
*/
|
|
28
|
+
get canDestroy() {
|
|
29
|
+
return !this.isDestroyed && this.refCount <= 0;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 添加销毁者
|
|
33
|
+
* @param fun
|
|
34
|
+
* @returns 返回销毁者的顺序
|
|
35
|
+
*/
|
|
36
|
+
disposeFun(s) {
|
|
37
|
+
return this._destroyers.push(s), s;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 取消销毁者函数
|
|
41
|
+
* @param fun
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
cancelDisposeFun(s) {
|
|
45
|
+
const e = this._destroyers.indexOf(s);
|
|
46
|
+
return this._destroyers.splice(e, 1), s;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 添加销毁对象
|
|
50
|
+
* @param obj
|
|
51
|
+
* @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
|
|
52
|
+
* @returns
|
|
53
|
+
*/
|
|
54
|
+
disposeObj(s, e) {
|
|
55
|
+
const r = e ? function() {
|
|
56
|
+
return s.destroySync();
|
|
57
|
+
} : function() {
|
|
58
|
+
return s.destroy();
|
|
59
|
+
};
|
|
60
|
+
return this.disposeFun(r), s.__destroyable_destroyer = r, s;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 取消销毁者函数
|
|
64
|
+
* @param fun
|
|
65
|
+
* @returns
|
|
66
|
+
*/
|
|
67
|
+
cancelDisposeObj(s) {
|
|
68
|
+
const e = s.__destroyable_destroyer;
|
|
69
|
+
return e && this.cancelDisposeFun(e), s;
|
|
70
|
+
}
|
|
71
|
+
dispose(s, e) {
|
|
72
|
+
return typeof s == "function" ? this.disposeFun(s) : this.disposeObj(s, e);
|
|
73
|
+
}
|
|
74
|
+
cancelDispose(s) {
|
|
75
|
+
return typeof s == "function" ? this.cancelDisposeFun(s) : this.cancelDisposeObj(s);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* 自己的销毁方法
|
|
79
|
+
* @remarks
|
|
80
|
+
* 子类根据需要进行重载
|
|
81
|
+
*/
|
|
82
|
+
destroyThis() {
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* 销毁
|
|
86
|
+
*/
|
|
87
|
+
destroySync() {
|
|
88
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
89
|
+
let s = this._destroyers.length;
|
|
90
|
+
for (; --s >= 0; ) {
|
|
91
|
+
const e = this._destroyers[s];
|
|
92
|
+
try {
|
|
93
|
+
e();
|
|
94
|
+
} catch (r) {
|
|
95
|
+
console.error("销毁函数在同步销毁时出错", this, e, r);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
this._destroyers.length = 0;
|
|
99
|
+
try {
|
|
100
|
+
this.destroyThis();
|
|
101
|
+
} catch (e) {
|
|
102
|
+
console.error("destroyThis 在异步销毁时出错", this, e);
|
|
103
|
+
}
|
|
104
|
+
return y(this), !0;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* 异步销毁
|
|
108
|
+
* @remarks
|
|
109
|
+
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
110
|
+
*/
|
|
111
|
+
async destroy() {
|
|
112
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
113
|
+
let s = this._destroyers.length;
|
|
114
|
+
for (; --s >= 0; ) {
|
|
115
|
+
const e = this._destroyers[s];
|
|
116
|
+
try {
|
|
117
|
+
const r = e();
|
|
118
|
+
r && r instanceof Promise && await r;
|
|
119
|
+
} catch (r) {
|
|
120
|
+
console.error("销毁函数在异步销毁时出错", this, e, r);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
this._destroyers.length = 0;
|
|
124
|
+
try {
|
|
125
|
+
const e = this.destroyThis();
|
|
126
|
+
e && e instanceof Promise && await e;
|
|
127
|
+
} catch (e) {
|
|
128
|
+
console.error("destroyThis 在异步销毁时出错", this, e);
|
|
129
|
+
}
|
|
130
|
+
return y(this), !0;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function c() {
|
|
134
|
+
throw "已销毁";
|
|
135
|
+
}
|
|
136
|
+
function p(o) {
|
|
137
|
+
for (var s in o)
|
|
138
|
+
typeof o[s] == "function" && (o[s] = c);
|
|
139
|
+
return o._isDestroyed = !0, !0;
|
|
140
|
+
}
|
|
141
|
+
function y(o) {
|
|
142
|
+
for (var s in o)
|
|
143
|
+
!(s in l.prototype) && typeof o[s] == "function" && (o[s] = c);
|
|
144
|
+
return o._isDestroyed = !0, !0;
|
|
145
|
+
}
|
|
146
|
+
function D(o) {
|
|
147
|
+
class s extends o {
|
|
148
|
+
constructor() {
|
|
149
|
+
super(...arguments);
|
|
150
|
+
/**
|
|
151
|
+
* 引用计数
|
|
152
|
+
* @remarks
|
|
153
|
+
* 引用计数为 0 时,对象才会被销毁
|
|
154
|
+
*/
|
|
155
|
+
h(this, "refCount", 0);
|
|
156
|
+
h(this, "_isDestroyed", !1);
|
|
157
|
+
/**
|
|
158
|
+
* 销毁者
|
|
159
|
+
*/
|
|
160
|
+
h(this, "_destroyers", []);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* 是否已经销毁
|
|
164
|
+
*/
|
|
165
|
+
get isDestroyed() {
|
|
166
|
+
return this._isDestroyed;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* 是否可以销毁
|
|
170
|
+
*/
|
|
171
|
+
get canDestroy() {
|
|
172
|
+
return !this.isDestroyed && this.refCount <= 0;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* 添加销毁者
|
|
176
|
+
* @param fun
|
|
177
|
+
* @returns 返回销毁者的顺序
|
|
178
|
+
*/
|
|
179
|
+
disposeFun(t) {
|
|
180
|
+
return this._destroyers.push(t), t;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* 取消销毁者函数
|
|
184
|
+
* @param fun
|
|
185
|
+
* @returns
|
|
186
|
+
*/
|
|
187
|
+
cancelDisposeFun(t) {
|
|
188
|
+
const i = this._destroyers.indexOf(t);
|
|
189
|
+
return this._destroyers.splice(i, 1), t;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* 添加销毁对象
|
|
193
|
+
* @param obj
|
|
194
|
+
* @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
|
|
195
|
+
* @returns
|
|
196
|
+
*/
|
|
197
|
+
disposeObj(t, i) {
|
|
198
|
+
const n = i ? function() {
|
|
199
|
+
return t.destroySync();
|
|
200
|
+
} : function() {
|
|
201
|
+
return t.destroy();
|
|
202
|
+
};
|
|
203
|
+
return this.disposeFun(n), t.__destroyable_destroyer = n, t;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* 取消销毁者函数
|
|
207
|
+
* @param fun
|
|
208
|
+
* @returns
|
|
209
|
+
*/
|
|
210
|
+
cancelDisposeObj(t) {
|
|
211
|
+
const i = t.__destroyable_destroyer;
|
|
212
|
+
return i && this.cancelDisposeFun(i), t;
|
|
213
|
+
}
|
|
214
|
+
dispose(t, i) {
|
|
215
|
+
return typeof t == "function" ? this.disposeFun(t) : this.disposeObj(t, i);
|
|
216
|
+
}
|
|
217
|
+
cancelDispose(t) {
|
|
218
|
+
return typeof t == "function" ? this.cancelDisposeFun(t) : this.cancelDisposeObj(t);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* 自己的销毁方法
|
|
222
|
+
* @remarks
|
|
223
|
+
* 子类根据需要进行重载
|
|
224
|
+
*/
|
|
225
|
+
destroyThis() {
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* 异步销毁
|
|
229
|
+
* @remarks
|
|
230
|
+
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
231
|
+
*/
|
|
232
|
+
async destroy() {
|
|
233
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
234
|
+
let t = this._destroyers.length;
|
|
235
|
+
for (; --t >= 0; ) {
|
|
236
|
+
const i = this._destroyers[t];
|
|
237
|
+
try {
|
|
238
|
+
const n = i();
|
|
239
|
+
n && n instanceof Promise && await n;
|
|
240
|
+
} catch (n) {
|
|
241
|
+
console.error("销毁函数在异步销毁时出错", this, i, n);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
this._destroyers.length = 0;
|
|
245
|
+
try {
|
|
246
|
+
const i = this.destroyThis();
|
|
247
|
+
i && i instanceof Promise && await i;
|
|
248
|
+
} catch (i) {
|
|
249
|
+
console.error("destroyThis 在异步销毁时出错", this, i);
|
|
250
|
+
}
|
|
251
|
+
return y(this), !0;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* 同步销毁
|
|
255
|
+
* @remarks
|
|
256
|
+
* 会依次执行销毁函数,但并不会等待每个异步销毁函数解决
|
|
257
|
+
*/
|
|
258
|
+
destroySync() {
|
|
259
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
260
|
+
let t = this._destroyers.length;
|
|
261
|
+
for (; --t >= 0; ) {
|
|
262
|
+
const i = this._destroyers[t];
|
|
263
|
+
try {
|
|
264
|
+
i();
|
|
265
|
+
} catch (n) {
|
|
266
|
+
console.error("销毁函数在同步销毁时出错", this, i, n);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
this._destroyers.length = 0;
|
|
270
|
+
try {
|
|
271
|
+
this.destroyThis();
|
|
272
|
+
} catch (i) {
|
|
273
|
+
console.error("destroyThis 在异步销毁时出错", this, i);
|
|
274
|
+
}
|
|
275
|
+
return y(this), !0;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return s;
|
|
279
|
+
}
|
|
280
|
+
class g extends a {
|
|
281
|
+
constructor() {
|
|
282
|
+
super(...arguments);
|
|
283
|
+
/**
|
|
284
|
+
* 引用计数
|
|
285
|
+
* @remarks
|
|
286
|
+
* 引用计数为 0 时,对象才会被销毁
|
|
287
|
+
*/
|
|
288
|
+
h(this, "refCount", 0);
|
|
289
|
+
h(this, "_isDestroyed", !1);
|
|
290
|
+
/**
|
|
291
|
+
* 销毁者
|
|
292
|
+
*/
|
|
293
|
+
h(this, "_destroyers", []);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* 是否已经销毁
|
|
297
|
+
*/
|
|
298
|
+
get isDestroyed() {
|
|
299
|
+
return this._isDestroyed;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* 是否可以销毁
|
|
303
|
+
*/
|
|
304
|
+
get canDestroy() {
|
|
305
|
+
return !this.isDestroyed && this.refCount <= 0;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* 添加销毁者
|
|
309
|
+
* @param fun
|
|
310
|
+
* @returns 返回销毁者的顺序
|
|
311
|
+
*/
|
|
312
|
+
disposeFun(e) {
|
|
313
|
+
return this._destroyers.push(e), e;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* 取消销毁者函数
|
|
317
|
+
* @param fun
|
|
318
|
+
* @returns
|
|
319
|
+
*/
|
|
320
|
+
cancelDisposeFun(e) {
|
|
321
|
+
const r = this._destroyers.indexOf(e);
|
|
322
|
+
return this._destroyers.splice(r, 1), e;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* 添加销毁对象
|
|
326
|
+
* @param obj
|
|
327
|
+
* @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
|
|
328
|
+
* @returns
|
|
329
|
+
*/
|
|
330
|
+
disposeObj(e, r) {
|
|
331
|
+
const t = r ? function() {
|
|
332
|
+
return e.destroySync();
|
|
333
|
+
} : function() {
|
|
334
|
+
return e.destroy();
|
|
335
|
+
};
|
|
336
|
+
return this.disposeFun(t), e.__destroyable_destroyer = t, e;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* 取消销毁者函数
|
|
340
|
+
* @param fun
|
|
341
|
+
* @returns
|
|
342
|
+
*/
|
|
343
|
+
cancelDisposeObj(e) {
|
|
344
|
+
const r = e.__destroyable_destroyer;
|
|
345
|
+
return r && this.cancelDisposeFun(r), e;
|
|
346
|
+
}
|
|
347
|
+
dispose(e, r) {
|
|
348
|
+
return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, r);
|
|
349
|
+
}
|
|
350
|
+
cancelDispose(e) {
|
|
351
|
+
return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* 自己的销毁方法
|
|
355
|
+
* @remarks
|
|
356
|
+
* 子类根据需要进行重载
|
|
357
|
+
*/
|
|
358
|
+
destroyThis() {
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* 销毁
|
|
362
|
+
*/
|
|
363
|
+
destroySync() {
|
|
364
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
365
|
+
let e = this._destroyers.length;
|
|
366
|
+
for (; --e >= 0; ) {
|
|
367
|
+
const r = this._destroyers[e];
|
|
368
|
+
try {
|
|
369
|
+
r();
|
|
370
|
+
} catch (t) {
|
|
371
|
+
console.error("销毁函数在同步销毁时出错", this, r, t);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
this._destroyers.length = 0;
|
|
375
|
+
try {
|
|
376
|
+
this.destroyThis();
|
|
377
|
+
} catch (r) {
|
|
378
|
+
console.error("destroyThis 在异步销毁时出错", this, r);
|
|
379
|
+
}
|
|
380
|
+
return y(this), !0;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* 异步销毁
|
|
384
|
+
* @remarks
|
|
385
|
+
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
386
|
+
*/
|
|
387
|
+
async destroy() {
|
|
388
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
389
|
+
let e = this._destroyers.length;
|
|
390
|
+
for (; --e >= 0; ) {
|
|
391
|
+
const r = this._destroyers[e];
|
|
392
|
+
try {
|
|
393
|
+
const t = r();
|
|
394
|
+
t && t instanceof Promise && await t;
|
|
395
|
+
} catch (t) {
|
|
396
|
+
console.error("销毁函数在异步销毁时出错", this, r, t);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
this._destroyers.length = 0;
|
|
400
|
+
try {
|
|
401
|
+
const r = this.destroyThis();
|
|
402
|
+
r && r instanceof Promise && await r;
|
|
403
|
+
} catch (r) {
|
|
404
|
+
console.error("destroyThis 在异步销毁时出错", this, r);
|
|
405
|
+
}
|
|
406
|
+
return y(this), !0;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
export {
|
|
410
|
+
l as D,
|
|
411
|
+
y as a,
|
|
412
|
+
g as b,
|
|
413
|
+
D as c,
|
|
414
|
+
p as d,
|
|
415
|
+
c as t
|
|
416
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var u=Object.defineProperty;var a=(o,s,e)=>s in o?u(o,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[s]=e;var y=(o,s,e)=>a(o,typeof s!="symbol"?s+"":s,e);const l=require("@gby/event-bus");class d{constructor(){y(this,"refCount",0);y(this,"_isDestroyed",!1);y(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 e=this._destroyers.indexOf(s);return this._destroyers.splice(e,1),s}disposeObj(s,e){const r=e?function(){return s.destroySync()}:function(){return s.destroy()};return this.disposeFun(r),s.__destroyable_destroyer=r,s}cancelDisposeObj(s){const e=s.__destroyable_destroyer;return e&&this.cancelDisposeFun(e),s}dispose(s,e){return typeof s=="function"?this.disposeFun(s):this.disposeObj(s,e)}cancelDispose(s){return typeof s=="function"?this.cancelDisposeFun(s):this.cancelDisposeObj(s)}destroyThis(){}destroySync(){if(!this.canDestroy)return this.isDestroyed;let s=this._destroyers.length;for(;--s>=0;){const e=this._destroyers[s];try{e()}catch(r){console.error("销毁函数在同步销毁时出错",this,e,r)}}this._destroyers.length=0;try{this.destroyThis()}catch(e){console.error("destroyThis 在异步销毁时出错",this,e)}return c(this),!0}async destroy(){if(!this.canDestroy)return this.isDestroyed;let s=this._destroyers.length;for(;--s>=0;){const e=this._destroyers[s];try{const r=e();r&&r instanceof Promise&&await r}catch(r){console.error("销毁函数在异步销毁时出错",this,e,r)}}this._destroyers.length=0;try{const e=this.destroyThis();e&&e instanceof Promise&&await e}catch(e){console.error("destroyThis 在异步销毁时出错",this,e)}return c(this),!0}}function h(){throw"已销毁"}function f(o){for(var s in o)typeof o[s]=="function"&&(o[s]=h);return o._isDestroyed=!0,!0}function c(o){for(var s in o)!(s in d.prototype)&&typeof o[s]=="function"&&(o[s]=h);return o._isDestroyed=!0,!0}function D(o){class s extends o{constructor(){super(...arguments);y(this,"refCount",0);y(this,"_isDestroyed",!1);y(this,"_destroyers",[])}get isDestroyed(){return this._isDestroyed}get canDestroy(){return!this.isDestroyed&&this.refCount<=0}disposeFun(t){return this._destroyers.push(t),t}cancelDisposeFun(t){const i=this._destroyers.indexOf(t);return this._destroyers.splice(i,1),t}disposeObj(t,i){const n=i?function(){return t.destroySync()}:function(){return t.destroy()};return this.disposeFun(n),t.__destroyable_destroyer=n,t}cancelDisposeObj(t){const i=t.__destroyable_destroyer;return i&&this.cancelDisposeFun(i),t}dispose(t,i){return typeof t=="function"?this.disposeFun(t):this.disposeObj(t,i)}cancelDispose(t){return typeof t=="function"?this.cancelDisposeFun(t):this.cancelDisposeObj(t)}destroyThis(){}async destroy(){if(!this.canDestroy)return this.isDestroyed;let t=this._destroyers.length;for(;--t>=0;){const i=this._destroyers[t];try{const n=i();n&&n instanceof Promise&&await n}catch(n){console.error("销毁函数在异步销毁时出错",this,i,n)}}this._destroyers.length=0;try{const i=this.destroyThis();i&&i instanceof Promise&&await i}catch(i){console.error("destroyThis 在异步销毁时出错",this,i)}return c(this),!0}destroySync(){if(!this.canDestroy)return this.isDestroyed;let t=this._destroyers.length;for(;--t>=0;){const i=this._destroyers[t];try{i()}catch(n){console.error("销毁函数在同步销毁时出错",this,i,n)}}this._destroyers.length=0;try{this.destroyThis()}catch(i){console.error("destroyThis 在异步销毁时出错",this,i)}return c(this),!0}}return s}class _ extends l.EventBus{constructor(){super(...arguments);y(this,"refCount",0);y(this,"_isDestroyed",!1);y(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 r=this._destroyers.indexOf(e);return this._destroyers.splice(r,1),e}disposeObj(e,r){const t=r?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(t),e.__destroyable_destroyer=t,e}cancelDisposeObj(e){const r=e.__destroyable_destroyer;return r&&this.cancelDisposeFun(r),e}dispose(e,r){return typeof e=="function"?this.disposeFun(e):this.disposeObj(e,r)}cancelDispose(e){return typeof e=="function"?this.cancelDisposeFun(e):this.cancelDisposeObj(e)}destroyThis(){}destroySync(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const r=this._destroyers[e];try{r()}catch(t){console.error("销毁函数在同步销毁时出错",this,r,t)}}this._destroyers.length=0;try{this.destroyThis()}catch(r){console.error("destroyThis 在异步销毁时出错",this,r)}return c(this),!0}async destroy(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const r=this._destroyers[e];try{const t=r();t&&t instanceof Promise&&await t}catch(t){console.error("销毁函数在异步销毁时出错",this,r,t)}}this._destroyers.length=0;try{const r=this.destroyThis();r&&r instanceof Promise&&await r}catch(r){console.error("destroyThis 在异步销毁时出错",this,r)}return c(this),!0}}exports.Destroyable=d;exports.DestroyableEventTarget=_;exports.createDestroyableSubClass=D;exports.destroyDestroyable=c;exports.destroyObject=f;exports.throwOnDestroyed=h;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { FunDestroyer, IDestroyable } from "./Destroyable";
|
|
2
2
|
import { EventBus } from "@gby/event-bus";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* 可销毁的对象,继承自 浏览器中的 的 EventTarget
|
|
5
5
|
*/
|
|
6
6
|
export declare class DestroyableEventTarget<EM extends Record<string, any> = Record<string, any>> extends EventBus<EM> implements IDestroyable {
|
|
7
7
|
/**
|
package/dist/index-web.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./DestroyableEventTarget-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./DestroyableEventTarget-DSGfqvV0.cjs");exports.Destroyable=e.Destroyable;exports.DestroyableEventTarget=e.DestroyableEventTarget;exports.createDestroyableSubClass=e.createDestroyableSubClass;exports.destroyDestroyable=e.destroyDestroyable;exports.destroyObject=e.destroyObject;exports.throwOnDestroyed=e.throwOnDestroyed;
|
package/dist/index-web.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { D as t, c as
|
|
1
|
+
import { D as a, b as t, c as r, a as o, d as b, t as y } from "./DestroyableEventTarget-DFVzomDG.js";
|
|
2
2
|
export {
|
|
3
|
+
a as Destroyable,
|
|
3
4
|
t as DestroyableEventTarget,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
r as createDestroyableSubClass,
|
|
6
|
+
o as destroyDestroyable,
|
|
7
|
+
b as destroyObject,
|
|
7
8
|
y as throwOnDestroyed
|
|
8
9
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var y=Object.defineProperty;var d=(i,r,e)=>r in i?y(i,r,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[r]=e;var n=(i,r,e)=>d(i,typeof r!="symbol"?r+"":r,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./DestroyableEventTarget-
|
|
1
|
+
"use strict";var y=Object.defineProperty;var d=(i,r,e)=>r in i?y(i,r,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[r]=e;var n=(i,r,e)=>d(i,typeof r!="symbol"?r+"":r,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./DestroyableEventTarget-DSGfqvV0.cjs"),c=require("node:events");class h extends c.EventEmitter{constructor(){super(...arguments);n(this,"refCount",0);n(this,"_isDestroyed",!1);n(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 s=t?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(s),e.__destroyable_destroyer=s,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(){}destroySync(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const t=this._destroyers[e];try{t()}catch(s){console.error("销毁函数在同步销毁时出错",this,t,s)}}this._destroyers.length=0;try{this.destroyThis()}catch(t){console.error("destroyThis 在异步销毁时出错",this,t)}return o.destroyDestroyable(this),!0}async destroy(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const t=this._destroyers[e];try{const s=t();s&&s instanceof Promise&&await s}catch(s){console.error("销毁函数在异步销毁时出错",this,t,s)}}this._destroyers.length=0;try{const t=this.destroyThis();t&&t instanceof Promise&&await t}catch(t){console.error("destroyThis 在异步销毁时出错",this,t)}return o.destroyDestroyable(this),!0}}exports.Destroyable=o.Destroyable;exports.DestroyableEventTarget=o.DestroyableEventTarget;exports.createDestroyableSubClass=o.createDestroyableSubClass;exports.destroyDestroyable=o.destroyDestroyable;exports.destroyObject=o.destroyObject;exports.throwOnDestroyed=o.throwOnDestroyed;exports.DestroyableEventEmitter=h;
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
var y = Object.defineProperty;
|
|
2
2
|
var c = (o, r, e) => r in o ? y(o, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[r] = e;
|
|
3
3
|
var i = (o, r, e) => c(o, typeof r != "symbol" ? r + "" : r, e);
|
|
4
|
-
import { a as n } from "./DestroyableEventTarget-
|
|
5
|
-
import { D as p,
|
|
4
|
+
import { a as n } from "./DestroyableEventTarget-DFVzomDG.js";
|
|
5
|
+
import { D as p, b as _, c as m, d as g, t as x } from "./DestroyableEventTarget-DFVzomDG.js";
|
|
6
6
|
import { EventEmitter as d } from "node:events";
|
|
7
7
|
class u extends d {
|
|
8
8
|
constructor() {
|
|
@@ -134,10 +134,11 @@ class u extends d {
|
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
136
|
export {
|
|
137
|
+
p as Destroyable,
|
|
137
138
|
u as DestroyableEventEmitter,
|
|
138
|
-
|
|
139
|
-
|
|
139
|
+
_ as DestroyableEventTarget,
|
|
140
|
+
m as createDestroyableSubClass,
|
|
140
141
|
n as destroyDestroyable,
|
|
141
|
-
|
|
142
|
-
|
|
142
|
+
g as destroyObject,
|
|
143
|
+
x as throwOnDestroyed
|
|
143
144
|
};
|
package/package.json
CHANGED
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
var c = Object.defineProperty;
|
|
2
|
-
var u = (i, o, s) => o in i ? c(i, o, { enumerable: !0, configurable: !0, writable: !0, value: s }) : i[o] = s;
|
|
3
|
-
var y = (i, o, s) => u(i, typeof o != "symbol" ? o + "" : o, s);
|
|
4
|
-
import { EventBus as a } from "@gby/event-bus";
|
|
5
|
-
function h() {
|
|
6
|
-
throw "已销毁";
|
|
7
|
-
}
|
|
8
|
-
function D(i) {
|
|
9
|
-
for (var o in i)
|
|
10
|
-
typeof i[o] == "function" && (i[o] = h);
|
|
11
|
-
return i._isDestroyed = !0, !0;
|
|
12
|
-
}
|
|
13
|
-
function d(i) {
|
|
14
|
-
for (var o in i)
|
|
15
|
-
!(o in Destroyable.prototype) && typeof i[o] == "function" && (i[o] = h);
|
|
16
|
-
return i._isDestroyed = !0, !0;
|
|
17
|
-
}
|
|
18
|
-
function p(i) {
|
|
19
|
-
class o extends i {
|
|
20
|
-
constructor() {
|
|
21
|
-
super(...arguments);
|
|
22
|
-
/**
|
|
23
|
-
* 引用计数
|
|
24
|
-
* @remarks
|
|
25
|
-
* 引用计数为 0 时,对象才会被销毁
|
|
26
|
-
*/
|
|
27
|
-
y(this, "refCount", 0);
|
|
28
|
-
y(this, "_isDestroyed", !1);
|
|
29
|
-
/**
|
|
30
|
-
* 销毁者
|
|
31
|
-
*/
|
|
32
|
-
y(this, "_destroyers", []);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* 是否已经销毁
|
|
36
|
-
*/
|
|
37
|
-
get isDestroyed() {
|
|
38
|
-
return this._isDestroyed;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* 是否可以销毁
|
|
42
|
-
*/
|
|
43
|
-
get canDestroy() {
|
|
44
|
-
return !this.isDestroyed && this.refCount <= 0;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* 添加销毁者
|
|
48
|
-
* @param fun
|
|
49
|
-
* @returns 返回销毁者的顺序
|
|
50
|
-
*/
|
|
51
|
-
disposeFun(e) {
|
|
52
|
-
return this._destroyers.push(e), e;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* 取消销毁者函数
|
|
56
|
-
* @param fun
|
|
57
|
-
* @returns
|
|
58
|
-
*/
|
|
59
|
-
cancelDisposeFun(e) {
|
|
60
|
-
const r = this._destroyers.indexOf(e);
|
|
61
|
-
return this._destroyers.splice(r, 1), e;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* 添加销毁对象
|
|
65
|
-
* @param obj
|
|
66
|
-
* @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
|
|
67
|
-
* @returns
|
|
68
|
-
*/
|
|
69
|
-
disposeObj(e, r) {
|
|
70
|
-
const n = r ? function() {
|
|
71
|
-
return e.destroySync();
|
|
72
|
-
} : function() {
|
|
73
|
-
return e.destroy();
|
|
74
|
-
};
|
|
75
|
-
return this.disposeFun(n), e.__destroyable_destroyer = n, e;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* 取消销毁者函数
|
|
79
|
-
* @param fun
|
|
80
|
-
* @returns
|
|
81
|
-
*/
|
|
82
|
-
cancelDisposeObj(e) {
|
|
83
|
-
const r = e.__destroyable_destroyer;
|
|
84
|
-
return r && this.cancelDisposeFun(r), e;
|
|
85
|
-
}
|
|
86
|
-
dispose(e, r) {
|
|
87
|
-
return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, r);
|
|
88
|
-
}
|
|
89
|
-
cancelDispose(e) {
|
|
90
|
-
return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* 自己的销毁方法
|
|
94
|
-
* @remarks
|
|
95
|
-
* 子类根据需要进行重载
|
|
96
|
-
*/
|
|
97
|
-
destroyThis() {
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* 异步销毁
|
|
101
|
-
* @remarks
|
|
102
|
-
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
103
|
-
*/
|
|
104
|
-
async destroy() {
|
|
105
|
-
if (!this.canDestroy) return this.isDestroyed;
|
|
106
|
-
let e = this._destroyers.length;
|
|
107
|
-
for (; --e >= 0; ) {
|
|
108
|
-
const r = this._destroyers[e];
|
|
109
|
-
try {
|
|
110
|
-
const n = r();
|
|
111
|
-
n && n instanceof Promise && await n;
|
|
112
|
-
} catch (n) {
|
|
113
|
-
console.error("销毁函数在异步销毁时出错", this, r, n);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
this._destroyers.length = 0;
|
|
117
|
-
try {
|
|
118
|
-
const r = this.destroyThis();
|
|
119
|
-
r && r instanceof Promise && await r;
|
|
120
|
-
} catch (r) {
|
|
121
|
-
console.error("destroyThis 在异步销毁时出错", this, r);
|
|
122
|
-
}
|
|
123
|
-
return d(this), !0;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* 同步销毁
|
|
127
|
-
* @remarks
|
|
128
|
-
* 会依次执行销毁函数,但并不会等待每个异步销毁函数解决
|
|
129
|
-
*/
|
|
130
|
-
destroySync() {
|
|
131
|
-
if (!this.canDestroy) return this.isDestroyed;
|
|
132
|
-
let e = this._destroyers.length;
|
|
133
|
-
for (; --e >= 0; ) {
|
|
134
|
-
const r = this._destroyers[e];
|
|
135
|
-
try {
|
|
136
|
-
r();
|
|
137
|
-
} catch (n) {
|
|
138
|
-
console.error("销毁函数在同步销毁时出错", this, r, n);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
this._destroyers.length = 0;
|
|
142
|
-
try {
|
|
143
|
-
this.destroyThis();
|
|
144
|
-
} catch (r) {
|
|
145
|
-
console.error("destroyThis 在异步销毁时出错", this, r);
|
|
146
|
-
}
|
|
147
|
-
return d(this), !0;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
return o;
|
|
151
|
-
}
|
|
152
|
-
class _ extends a {
|
|
153
|
-
constructor() {
|
|
154
|
-
super(...arguments);
|
|
155
|
-
/**
|
|
156
|
-
* 引用计数
|
|
157
|
-
* @remarks
|
|
158
|
-
* 引用计数为 0 时,对象才会被销毁
|
|
159
|
-
*/
|
|
160
|
-
y(this, "refCount", 0);
|
|
161
|
-
y(this, "_isDestroyed", !1);
|
|
162
|
-
/**
|
|
163
|
-
* 销毁者
|
|
164
|
-
*/
|
|
165
|
-
y(this, "_destroyers", []);
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* 是否已经销毁
|
|
169
|
-
*/
|
|
170
|
-
get isDestroyed() {
|
|
171
|
-
return this._isDestroyed;
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* 是否可以销毁
|
|
175
|
-
*/
|
|
176
|
-
get canDestroy() {
|
|
177
|
-
return !this.isDestroyed && this.refCount <= 0;
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* 添加销毁者
|
|
181
|
-
* @param fun
|
|
182
|
-
* @returns 返回销毁者的顺序
|
|
183
|
-
*/
|
|
184
|
-
disposeFun(s) {
|
|
185
|
-
return this._destroyers.push(s), s;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* 取消销毁者函数
|
|
189
|
-
* @param fun
|
|
190
|
-
* @returns
|
|
191
|
-
*/
|
|
192
|
-
cancelDisposeFun(s) {
|
|
193
|
-
const t = this._destroyers.indexOf(s);
|
|
194
|
-
return this._destroyers.splice(t, 1), s;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* 添加销毁对象
|
|
198
|
-
* @param obj
|
|
199
|
-
* @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
|
|
200
|
-
* @returns
|
|
201
|
-
*/
|
|
202
|
-
disposeObj(s, t) {
|
|
203
|
-
const e = t ? function() {
|
|
204
|
-
return s.destroySync();
|
|
205
|
-
} : function() {
|
|
206
|
-
return s.destroy();
|
|
207
|
-
};
|
|
208
|
-
return this.disposeFun(e), s.__destroyable_destroyer = e, s;
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* 取消销毁者函数
|
|
212
|
-
* @param fun
|
|
213
|
-
* @returns
|
|
214
|
-
*/
|
|
215
|
-
cancelDisposeObj(s) {
|
|
216
|
-
const t = s.__destroyable_destroyer;
|
|
217
|
-
return t && this.cancelDisposeFun(t), s;
|
|
218
|
-
}
|
|
219
|
-
dispose(s, t) {
|
|
220
|
-
return typeof s == "function" ? this.disposeFun(s) : this.disposeObj(s, t);
|
|
221
|
-
}
|
|
222
|
-
cancelDispose(s) {
|
|
223
|
-
return typeof s == "function" ? this.cancelDisposeFun(s) : this.cancelDisposeObj(s);
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* 自己的销毁方法
|
|
227
|
-
* @remarks
|
|
228
|
-
* 子类根据需要进行重载
|
|
229
|
-
*/
|
|
230
|
-
destroyThis() {
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* 销毁
|
|
234
|
-
*/
|
|
235
|
-
destroySync() {
|
|
236
|
-
if (!this.canDestroy) return this.isDestroyed;
|
|
237
|
-
let s = this._destroyers.length;
|
|
238
|
-
for (; --s >= 0; ) {
|
|
239
|
-
const t = this._destroyers[s];
|
|
240
|
-
try {
|
|
241
|
-
t();
|
|
242
|
-
} catch (e) {
|
|
243
|
-
console.error("销毁函数在同步销毁时出错", this, t, e);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
this._destroyers.length = 0;
|
|
247
|
-
try {
|
|
248
|
-
this.destroyThis();
|
|
249
|
-
} catch (t) {
|
|
250
|
-
console.error("destroyThis 在异步销毁时出错", this, t);
|
|
251
|
-
}
|
|
252
|
-
return d(this), !0;
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* 异步销毁
|
|
256
|
-
* @remarks
|
|
257
|
-
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
258
|
-
*/
|
|
259
|
-
async destroy() {
|
|
260
|
-
if (!this.canDestroy) return this.isDestroyed;
|
|
261
|
-
let s = this._destroyers.length;
|
|
262
|
-
for (; --s >= 0; ) {
|
|
263
|
-
const t = this._destroyers[s];
|
|
264
|
-
try {
|
|
265
|
-
const e = t();
|
|
266
|
-
e && e instanceof Promise && await e;
|
|
267
|
-
} catch (e) {
|
|
268
|
-
console.error("销毁函数在异步销毁时出错", this, t, e);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
this._destroyers.length = 0;
|
|
272
|
-
try {
|
|
273
|
-
const t = this.destroyThis();
|
|
274
|
-
t && t instanceof Promise && await t;
|
|
275
|
-
} catch (t) {
|
|
276
|
-
console.error("destroyThis 在异步销毁时出错", this, t);
|
|
277
|
-
}
|
|
278
|
-
return d(this), !0;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
export {
|
|
282
|
-
_ as D,
|
|
283
|
-
d as a,
|
|
284
|
-
p as c,
|
|
285
|
-
D as d,
|
|
286
|
-
h as t
|
|
287
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var h=Object.defineProperty;var u=(i,o,s)=>o in i?h(i,o,{enumerable:!0,configurable:!0,writable:!0,value:s}):i[o]=s;var y=(i,o,s)=>u(i,typeof o!="symbol"?o+"":o,s);const a=require("@gby/event-bus");function d(){throw"已销毁"}function l(i){for(var o in i)typeof i[o]=="function"&&(i[o]=d);return i._isDestroyed=!0,!0}function c(i){for(var o in i)!(o in Destroyable.prototype)&&typeof i[o]=="function"&&(i[o]=d);return i._isDestroyed=!0,!0}function f(i){class o extends i{constructor(){super(...arguments);y(this,"refCount",0);y(this,"_isDestroyed",!1);y(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 r=this._destroyers.indexOf(e);return this._destroyers.splice(r,1),e}disposeObj(e,r){const n=r?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(n),e.__destroyable_destroyer=n,e}cancelDisposeObj(e){const r=e.__destroyable_destroyer;return r&&this.cancelDisposeFun(r),e}dispose(e,r){return typeof e=="function"?this.disposeFun(e):this.disposeObj(e,r)}cancelDispose(e){return typeof e=="function"?this.cancelDisposeFun(e):this.cancelDisposeObj(e)}destroyThis(){}async destroy(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const r=this._destroyers[e];try{const n=r();n&&n instanceof Promise&&await n}catch(n){console.error("销毁函数在异步销毁时出错",this,r,n)}}this._destroyers.length=0;try{const r=this.destroyThis();r&&r instanceof Promise&&await r}catch(r){console.error("destroyThis 在异步销毁时出错",this,r)}return c(this),!0}destroySync(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const r=this._destroyers[e];try{r()}catch(n){console.error("销毁函数在同步销毁时出错",this,r,n)}}this._destroyers.length=0;try{this.destroyThis()}catch(r){console.error("destroyThis 在异步销毁时出错",this,r)}return c(this),!0}}return o}class D extends a.EventBus{constructor(){super(...arguments);y(this,"refCount",0);y(this,"_isDestroyed",!1);y(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 t=this._destroyers.indexOf(s);return this._destroyers.splice(t,1),s}disposeObj(s,t){const e=t?function(){return s.destroySync()}:function(){return s.destroy()};return this.disposeFun(e),s.__destroyable_destroyer=e,s}cancelDisposeObj(s){const t=s.__destroyable_destroyer;return t&&this.cancelDisposeFun(t),s}dispose(s,t){return typeof s=="function"?this.disposeFun(s):this.disposeObj(s,t)}cancelDispose(s){return typeof s=="function"?this.cancelDisposeFun(s):this.cancelDisposeObj(s)}destroyThis(){}destroySync(){if(!this.canDestroy)return this.isDestroyed;let s=this._destroyers.length;for(;--s>=0;){const t=this._destroyers[s];try{t()}catch(e){console.error("销毁函数在同步销毁时出错",this,t,e)}}this._destroyers.length=0;try{this.destroyThis()}catch(t){console.error("destroyThis 在异步销毁时出错",this,t)}return c(this),!0}async destroy(){if(!this.canDestroy)return this.isDestroyed;let s=this._destroyers.length;for(;--s>=0;){const t=this._destroyers[s];try{const e=t();e&&e instanceof Promise&&await e}catch(e){console.error("销毁函数在异步销毁时出错",this,t,e)}}this._destroyers.length=0;try{const t=this.destroyThis();t&&t instanceof Promise&&await t}catch(t){console.error("destroyThis 在异步销毁时出错",this,t)}return c(this),!0}}exports.DestroyableEventTarget=D;exports.createDestroyableSubClass=f;exports.destroyDestroyable=c;exports.destroyObject=l;exports.throwOnDestroyed=d;
|