@gby/destroyable 2.0.0 → 3.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 +17 -22
- package/dist/Destroyable.d.ts.map +1 -1
- package/dist/{NodeDestroyable.d.ts → DestroyableEventEmitter.d.ts} +13 -13
- package/dist/DestroyableEventEmitter.d.ts.map +1 -0
- package/dist/{createDestroyableSubClass-Blnpqngp.js → DestroyableEventTarget-CI_bGgU0.js} +188 -184
- package/dist/DestroyableEventTarget-NFP7x4Ni.cjs +1 -0
- package/dist/{WebDestroyable.d.ts → DestroyableEventTarget.d.ts} +13 -13
- package/dist/DestroyableEventTarget.d.ts.map +1 -0
- package/dist/createDestroyableSubClass.d.ts +16 -14
- package/dist/createDestroyableSubClass.d.ts.map +1 -1
- package/dist/index-web.cjs +1 -1
- package/dist/index-web.d.ts +1 -1
- package/dist/index-web.d.ts.map +1 -1
- package/dist/index-web.js +6 -141
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +141 -12
- package/package.json +2 -8
- package/dist/DestroyableSub.d.ts +0 -83
- package/dist/DestroyableSub.d.ts.map +0 -1
- package/dist/NodeDestroyable.d.ts.map +0 -1
- package/dist/WebDestroyable.d.ts.map +0 -1
- package/dist/createDestroyableSubClass-Ck7j3oA0.cjs +0 -1
- package/dist/index-node.cjs +0 -1
- package/dist/index-node.d.ts +0 -12
- package/dist/index-node.d.ts.map +0 -1
- package/dist/index-node.js +0 -143
package/dist/Destroyable.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export type FunDestroyer = () => void;
|
|
|
5
5
|
/**
|
|
6
6
|
* 可销毁的对象
|
|
7
7
|
*/
|
|
8
|
-
export
|
|
8
|
+
export interface IDestroyable {
|
|
9
9
|
/**
|
|
10
10
|
* 引用计数
|
|
11
11
|
* @remarks
|
|
@@ -15,16 +15,11 @@ export declare class Destroyable {
|
|
|
15
15
|
/**
|
|
16
16
|
* 是否已经销毁
|
|
17
17
|
*/
|
|
18
|
-
|
|
19
|
-
_isDestroyed: boolean;
|
|
18
|
+
readonly isDestroyed: boolean;
|
|
20
19
|
/**
|
|
21
20
|
* 是否可以销毁
|
|
22
21
|
*/
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 销毁者
|
|
26
|
-
*/
|
|
27
|
-
_destroyers: FunDestroyer[];
|
|
22
|
+
readonly canDestroy: boolean;
|
|
28
23
|
/**
|
|
29
24
|
* 添加销毁者
|
|
30
25
|
* @param fun
|
|
@@ -40,42 +35,42 @@ export declare class Destroyable {
|
|
|
40
35
|
/**
|
|
41
36
|
* 添加销毁对象
|
|
42
37
|
* @param obj
|
|
43
|
-
* @param sync - 表示是否使用 `obj.
|
|
38
|
+
* @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
|
|
44
39
|
* @returns
|
|
45
40
|
*/
|
|
46
|
-
disposeObj<T extends
|
|
41
|
+
disposeObj<T extends IDestroyable>(obj: T, sync?: boolean): T;
|
|
47
42
|
/**
|
|
48
43
|
* 取消销毁者函数
|
|
49
44
|
* @param fun
|
|
50
45
|
* @returns
|
|
51
46
|
*/
|
|
52
|
-
cancelDisposeObj<T extends
|
|
47
|
+
cancelDisposeObj<T extends IDestroyable>(obj: T): T;
|
|
53
48
|
/**
|
|
54
49
|
* 添加销毁函数或销毁对象
|
|
55
50
|
* @param fun
|
|
56
51
|
*/
|
|
57
52
|
dispose<T extends FunDestroyer>(fun: T): T;
|
|
58
|
-
dispose<T extends
|
|
59
|
-
dispose<T extends
|
|
53
|
+
dispose<T extends IDestroyable>(obj: T, asyncDestroy?: boolean): T;
|
|
54
|
+
dispose<T extends IDestroyable | FunDestroyer>(objOrFun: T, asyncDestroy?: boolean): T;
|
|
60
55
|
cancelDispose<T extends FunDestroyer>(fun: T): T;
|
|
61
|
-
cancelDispose<T extends
|
|
62
|
-
cancelDispose<T extends
|
|
56
|
+
cancelDispose<T extends IDestroyable>(obj: T): T;
|
|
57
|
+
cancelDispose<T extends IDestroyable | FunDestroyer>(objOrFun: T): T;
|
|
63
58
|
/**
|
|
64
59
|
* 自己的销毁方法
|
|
65
60
|
* @remarks
|
|
66
61
|
* 子类根据需要进行重载
|
|
67
62
|
*/
|
|
68
|
-
destroyThis(): void;
|
|
69
|
-
/**
|
|
70
|
-
* 销毁
|
|
71
|
-
*/
|
|
72
|
-
destroy(): boolean;
|
|
63
|
+
destroyThis(): Promise<void> | void;
|
|
73
64
|
/**
|
|
74
65
|
* 异步销毁
|
|
75
66
|
* @remarks
|
|
76
67
|
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
77
68
|
*/
|
|
78
|
-
|
|
69
|
+
destroy(): Promise<boolean>;
|
|
70
|
+
/**
|
|
71
|
+
* 销毁
|
|
72
|
+
*/
|
|
73
|
+
destroySync(): boolean;
|
|
79
74
|
}
|
|
80
75
|
/**
|
|
81
76
|
* 调用已销对象的时抛出错误
|
|
@@ -92,5 +87,5 @@ export declare function destroyObject(object: any): boolean;
|
|
|
92
87
|
* @param object
|
|
93
88
|
* @returns
|
|
94
89
|
*/
|
|
95
|
-
export declare function destroyDestroyable(object:
|
|
90
|
+
export declare function destroyDestroyable(object: IDestroyable): boolean;
|
|
96
91
|
//# 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,
|
|
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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H,OAAO,IAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAE7B;;OAEG;IACH,WAAW,IAAG,OAAO,CAAC;CAUzB;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,YAAY,WAYtD"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { FunDestroyer } from "./Destroyable";
|
|
1
|
+
import type { FunDestroyer, IDestroyable } from "./Destroyable";
|
|
2
2
|
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
|
* 可销毁的对象
|
|
7
7
|
*/
|
|
8
|
-
export declare class
|
|
8
|
+
export declare class DestroyableEventEmitter<EM extends EventMap<EM> = DefaultEventMap> extends EventEmitter<EM> implements IDestroyable {
|
|
9
9
|
/**
|
|
10
10
|
* 引用计数
|
|
11
11
|
* @remarks
|
|
@@ -40,41 +40,41 @@ export declare class NodeDestroyable<EM extends EventMap<EM> = DefaultEventMap>
|
|
|
40
40
|
/**
|
|
41
41
|
* 添加销毁对象
|
|
42
42
|
* @param obj
|
|
43
|
-
* @param sync - 表示是否使用 `obj.
|
|
43
|
+
* @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
|
|
44
44
|
* @returns
|
|
45
45
|
*/
|
|
46
|
-
disposeObj<T extends
|
|
46
|
+
disposeObj<T extends IDestroyable>(obj: T, sync?: boolean): T;
|
|
47
47
|
/**
|
|
48
48
|
* 取消销毁者函数
|
|
49
49
|
* @param fun
|
|
50
50
|
* @returns
|
|
51
51
|
*/
|
|
52
|
-
cancelDisposeObj<T extends
|
|
52
|
+
cancelDisposeObj<T extends IDestroyable>(obj: T): T;
|
|
53
53
|
/**
|
|
54
54
|
* 添加销毁函数或销毁对象
|
|
55
55
|
* @param fun
|
|
56
56
|
*/
|
|
57
57
|
dispose<T extends FunDestroyer>(fun: T): T;
|
|
58
|
-
dispose<T extends
|
|
59
|
-
dispose<T extends
|
|
58
|
+
dispose<T extends IDestroyable>(obj: T, asyncDestroy?: boolean): T;
|
|
59
|
+
dispose<T extends IDestroyable | FunDestroyer>(objOrFun: T, asyncDestroy?: boolean): T;
|
|
60
60
|
cancelDispose<T extends FunDestroyer>(fun: T): T;
|
|
61
|
-
cancelDispose<T extends
|
|
62
|
-
cancelDispose<T extends
|
|
61
|
+
cancelDispose<T extends IDestroyable>(obj: T): T;
|
|
62
|
+
cancelDispose<T extends IDestroyable | FunDestroyer>(objOrFun: T): T;
|
|
63
63
|
/**
|
|
64
64
|
* 自己的销毁方法
|
|
65
65
|
* @remarks
|
|
66
66
|
* 子类根据需要进行重载
|
|
67
67
|
*/
|
|
68
|
-
destroyThis(): void;
|
|
68
|
+
destroyThis(): Promise<void> | void;
|
|
69
69
|
/**
|
|
70
70
|
* 销毁
|
|
71
71
|
*/
|
|
72
|
-
|
|
72
|
+
destroySync(): boolean;
|
|
73
73
|
/**
|
|
74
74
|
* 异步销毁
|
|
75
75
|
* @remarks
|
|
76
76
|
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
77
77
|
*/
|
|
78
|
-
|
|
78
|
+
destroy(): Promise<boolean>;
|
|
79
79
|
}
|
|
80
|
-
//# sourceMappingURL=
|
|
80
|
+
//# sourceMappingURL=DestroyableEventEmitter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DestroyableEventEmitter.d.ts","sourceRoot":"","sources":["../src/DestroyableEventEmitter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAEhE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C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C;IAChD,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAChD,aAAa,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC;IAgBpE;;;;OAIG;IACH,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;IAKnC;;OAEG;IACH,WAAW;IA6BX;;;;OAIG;IACG,OAAO;CA6BhB"}
|
|
@@ -1,149 +1,22 @@
|
|
|
1
|
-
var
|
|
2
|
-
var u = (i,
|
|
3
|
-
var y = (i,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 引用计数
|
|
8
|
-
* @remarks
|
|
9
|
-
* 引用计数为 0 时,对象才会被销毁
|
|
10
|
-
*/
|
|
11
|
-
y(this, "refCount", 0);
|
|
12
|
-
y(this, "_isDestroyed", !1);
|
|
13
|
-
/**
|
|
14
|
-
* 销毁者
|
|
15
|
-
*/
|
|
16
|
-
y(this, "_destroyers", []);
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* 是否已经销毁
|
|
20
|
-
*/
|
|
21
|
-
get isDestroyed() {
|
|
22
|
-
return this._isDestroyed;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* 是否可以销毁
|
|
26
|
-
*/
|
|
27
|
-
get canDestroy() {
|
|
28
|
-
return !this.isDestroyed && this.refCount <= 0;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* 添加销毁者
|
|
32
|
-
* @param fun
|
|
33
|
-
* @returns 返回销毁者的顺序
|
|
34
|
-
*/
|
|
35
|
-
disposeFun(s) {
|
|
36
|
-
return this._destroyers.push(s), s;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* 取消销毁者函数
|
|
40
|
-
* @param fun
|
|
41
|
-
* @returns
|
|
42
|
-
*/
|
|
43
|
-
cancelDisposeFun(s) {
|
|
44
|
-
const e = this._destroyers.indexOf(s);
|
|
45
|
-
return this._destroyers.splice(e, 1), s;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* 添加销毁对象
|
|
49
|
-
* @param obj
|
|
50
|
-
* @param sync - 表示是否使用 `obj.destroy()` 方法进行销毁;默认使用 `obj.destroyAsync()` 方法进行销毁
|
|
51
|
-
* @returns
|
|
52
|
-
*/
|
|
53
|
-
disposeObj(s, e) {
|
|
54
|
-
const o = e ? function() {
|
|
55
|
-
return s.destroy();
|
|
56
|
-
} : function() {
|
|
57
|
-
return s.destroyAsync();
|
|
58
|
-
};
|
|
59
|
-
return this.disposeFun(o), s.__destroyable_destroyer = o, s;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* 取消销毁者函数
|
|
63
|
-
* @param fun
|
|
64
|
-
* @returns
|
|
65
|
-
*/
|
|
66
|
-
cancelDisposeObj(s) {
|
|
67
|
-
const e = s.__destroyable_destroyer;
|
|
68
|
-
return e && this.cancelDisposeFun(e), s;
|
|
69
|
-
}
|
|
70
|
-
dispose(s, e) {
|
|
71
|
-
return typeof s == "function" ? this.disposeFun(s) : this.disposeObj(s, e);
|
|
72
|
-
}
|
|
73
|
-
cancelDispose(s) {
|
|
74
|
-
return typeof s == "function" ? this.cancelDisposeFun(s) : this.cancelDisposeObj(s);
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* 自己的销毁方法
|
|
78
|
-
* @remarks
|
|
79
|
-
* 子类根据需要进行重载
|
|
80
|
-
*/
|
|
81
|
-
destroyThis() {
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* 销毁
|
|
85
|
-
*/
|
|
86
|
-
destroy() {
|
|
87
|
-
if (!this.canDestroy) return this.isDestroyed;
|
|
88
|
-
let s = this._destroyers.length;
|
|
89
|
-
for (; --s >= 0; ) {
|
|
90
|
-
const e = this._destroyers[s];
|
|
91
|
-
try {
|
|
92
|
-
e();
|
|
93
|
-
} catch (o) {
|
|
94
|
-
console.error("销毁函数在同步销毁时出错", this, e, o);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
this._destroyers.length = 0;
|
|
98
|
-
try {
|
|
99
|
-
this.destroyThis();
|
|
100
|
-
} catch (e) {
|
|
101
|
-
console.error("destroyThis 在异步销毁时出错", this, e);
|
|
102
|
-
}
|
|
103
|
-
return h(this), !0;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* 异步销毁
|
|
107
|
-
* @remarks
|
|
108
|
-
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
109
|
-
*/
|
|
110
|
-
async destroyAsync() {
|
|
111
|
-
if (!this.canDestroy) return this.isDestroyed;
|
|
112
|
-
let s = this._destroyers.length;
|
|
113
|
-
for (; --s >= 0; ) {
|
|
114
|
-
const e = this._destroyers[s];
|
|
115
|
-
try {
|
|
116
|
-
const o = e();
|
|
117
|
-
o && o instanceof Promise && await o;
|
|
118
|
-
} catch (o) {
|
|
119
|
-
console.error("销毁函数在异步销毁时出错", this, e, o);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
this._destroyers.length = 0;
|
|
123
|
-
try {
|
|
124
|
-
const e = this.destroyThis();
|
|
125
|
-
e && e instanceof Promise && await e;
|
|
126
|
-
} catch (e) {
|
|
127
|
-
console.error("destroyThis 在异步销毁时出错", this, e);
|
|
128
|
-
}
|
|
129
|
-
return h(this), !0;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
function c() {
|
|
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() {
|
|
133
6
|
throw "已销毁";
|
|
134
7
|
}
|
|
135
|
-
function
|
|
136
|
-
for (var
|
|
137
|
-
typeof i[
|
|
8
|
+
function D(i) {
|
|
9
|
+
for (var o in i)
|
|
10
|
+
typeof i[o] == "function" && (i[o] = h);
|
|
138
11
|
return i._isDestroyed = !0, !0;
|
|
139
12
|
}
|
|
140
|
-
function
|
|
141
|
-
for (var
|
|
142
|
-
!(
|
|
13
|
+
function d(i) {
|
|
14
|
+
for (var o in i)
|
|
15
|
+
!(o in Destroyable.prototype) && typeof i[o] == "function" && (i[o] = h);
|
|
143
16
|
return i._isDestroyed = !0, !0;
|
|
144
17
|
}
|
|
145
|
-
function
|
|
146
|
-
class
|
|
18
|
+
function p(i) {
|
|
19
|
+
class o extends i {
|
|
147
20
|
constructor() {
|
|
148
21
|
super(...arguments);
|
|
149
22
|
/**
|
|
@@ -175,46 +48,46 @@ function D(i) {
|
|
|
175
48
|
* @param fun
|
|
176
49
|
* @returns 返回销毁者的顺序
|
|
177
50
|
*/
|
|
178
|
-
disposeFun(
|
|
179
|
-
return this._destroyers.push(
|
|
51
|
+
disposeFun(e) {
|
|
52
|
+
return this._destroyers.push(e), e;
|
|
180
53
|
}
|
|
181
54
|
/**
|
|
182
55
|
* 取消销毁者函数
|
|
183
56
|
* @param fun
|
|
184
57
|
* @returns
|
|
185
58
|
*/
|
|
186
|
-
cancelDisposeFun(
|
|
187
|
-
const r = this._destroyers.indexOf(
|
|
188
|
-
return this._destroyers.splice(r, 1),
|
|
59
|
+
cancelDisposeFun(e) {
|
|
60
|
+
const r = this._destroyers.indexOf(e);
|
|
61
|
+
return this._destroyers.splice(r, 1), e;
|
|
189
62
|
}
|
|
190
63
|
/**
|
|
191
64
|
* 添加销毁对象
|
|
192
65
|
* @param obj
|
|
193
|
-
* @param sync - 表示是否使用 `obj.
|
|
66
|
+
* @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
|
|
194
67
|
* @returns
|
|
195
68
|
*/
|
|
196
|
-
disposeObj(
|
|
69
|
+
disposeObj(e, r) {
|
|
197
70
|
const n = r ? function() {
|
|
198
|
-
return
|
|
71
|
+
return e.destroySync();
|
|
199
72
|
} : function() {
|
|
200
|
-
return
|
|
73
|
+
return e.destroy();
|
|
201
74
|
};
|
|
202
|
-
return this.disposeFun(n),
|
|
75
|
+
return this.disposeFun(n), e.__destroyable_destroyer = n, e;
|
|
203
76
|
}
|
|
204
77
|
/**
|
|
205
78
|
* 取消销毁者函数
|
|
206
79
|
* @param fun
|
|
207
80
|
* @returns
|
|
208
81
|
*/
|
|
209
|
-
cancelDisposeObj(
|
|
210
|
-
const r =
|
|
211
|
-
return r && this.cancelDisposeFun(r),
|
|
82
|
+
cancelDisposeObj(e) {
|
|
83
|
+
const r = e.__destroyable_destroyer;
|
|
84
|
+
return r && this.cancelDisposeFun(r), e;
|
|
212
85
|
}
|
|
213
|
-
dispose(
|
|
214
|
-
return typeof
|
|
86
|
+
dispose(e, r) {
|
|
87
|
+
return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, r);
|
|
215
88
|
}
|
|
216
|
-
cancelDispose(
|
|
217
|
-
return typeof
|
|
89
|
+
cancelDispose(e) {
|
|
90
|
+
return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
|
|
218
91
|
}
|
|
219
92
|
/**
|
|
220
93
|
* 自己的销毁方法
|
|
@@ -224,60 +97,191 @@ function D(i) {
|
|
|
224
97
|
destroyThis() {
|
|
225
98
|
}
|
|
226
99
|
/**
|
|
227
|
-
*
|
|
100
|
+
* 异步销毁
|
|
101
|
+
* @remarks
|
|
102
|
+
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
228
103
|
*/
|
|
229
|
-
destroy() {
|
|
104
|
+
async destroy() {
|
|
230
105
|
if (!this.canDestroy) return this.isDestroyed;
|
|
231
|
-
let
|
|
232
|
-
for (; --
|
|
233
|
-
const r = this._destroyers[
|
|
106
|
+
let e = this._destroyers.length;
|
|
107
|
+
for (; --e >= 0; ) {
|
|
108
|
+
const r = this._destroyers[e];
|
|
234
109
|
try {
|
|
235
|
-
r();
|
|
110
|
+
const n = r();
|
|
111
|
+
n && n instanceof Promise && await n;
|
|
236
112
|
} catch (n) {
|
|
237
|
-
console.error("
|
|
113
|
+
console.error("销毁函数在异步销毁时出错", this, r, n);
|
|
238
114
|
}
|
|
239
115
|
}
|
|
240
116
|
this._destroyers.length = 0;
|
|
241
117
|
try {
|
|
242
|
-
this.destroyThis();
|
|
118
|
+
const r = this.destroyThis();
|
|
119
|
+
r && r instanceof Promise && await r;
|
|
243
120
|
} catch (r) {
|
|
244
121
|
console.error("destroyThis 在异步销毁时出错", this, r);
|
|
245
122
|
}
|
|
246
|
-
return
|
|
123
|
+
return d(this), !0;
|
|
247
124
|
}
|
|
248
125
|
/**
|
|
249
|
-
*
|
|
126
|
+
* 同步销毁
|
|
250
127
|
* @remarks
|
|
251
|
-
*
|
|
128
|
+
* 会依次执行销毁函数,但并不会等待每个异步销毁函数解决
|
|
252
129
|
*/
|
|
253
|
-
|
|
130
|
+
destroySync() {
|
|
254
131
|
if (!this.canDestroy) return this.isDestroyed;
|
|
255
|
-
let
|
|
256
|
-
for (; --
|
|
257
|
-
const r = this._destroyers[
|
|
132
|
+
let e = this._destroyers.length;
|
|
133
|
+
for (; --e >= 0; ) {
|
|
134
|
+
const r = this._destroyers[e];
|
|
258
135
|
try {
|
|
259
|
-
|
|
260
|
-
n && n instanceof Promise && await n;
|
|
136
|
+
r();
|
|
261
137
|
} catch (n) {
|
|
262
|
-
console.error("
|
|
138
|
+
console.error("销毁函数在同步销毁时出错", this, r, n);
|
|
263
139
|
}
|
|
264
140
|
}
|
|
265
141
|
this._destroyers.length = 0;
|
|
266
142
|
try {
|
|
267
|
-
|
|
268
|
-
r && r instanceof Promise && await r;
|
|
143
|
+
this.destroyThis();
|
|
269
144
|
} catch (r) {
|
|
270
145
|
console.error("destroyThis 在异步销毁时出错", this, r);
|
|
271
146
|
}
|
|
272
|
-
return
|
|
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);
|
|
273
277
|
}
|
|
278
|
+
return d(this), !0;
|
|
274
279
|
}
|
|
275
|
-
return s;
|
|
276
280
|
}
|
|
277
281
|
export {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
282
|
+
_ as D,
|
|
283
|
+
d as a,
|
|
284
|
+
p as c,
|
|
285
|
+
D as d,
|
|
286
|
+
h as t
|
|
283
287
|
};
|
|
@@ -0,0 +1 @@
|
|
|
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;
|