@gby/destroyable 1.1.0 → 3.0.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 +11 -16
- package/dist/Destroyable.d.ts.map +1 -1
- package/dist/DestroyableEventEmitter.d.ts +80 -0
- package/dist/DestroyableEventEmitter.d.ts.map +1 -0
- package/dist/DestroyableEventTarget-3_XyRRfB.js +285 -0
- package/dist/DestroyableEventTarget-B26KR0Ul.cjs +1 -0
- package/dist/DestroyableEventTarget.d.ts +78 -0
- package/dist/DestroyableEventTarget.d.ts.map +1 -0
- package/dist/createDestroyableSubClass.d.ts +7 -7
- package/dist/createDestroyableSubClass.d.ts.map +1 -1
- package/dist/index-web.cjs +1 -0
- package/dist/index-web.d.ts +12 -0
- package/dist/index-web.d.ts.map +1 -0
- package/dist/index-web.js +8 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +27 -172
- package/package.json +9 -5
- package/dist/DestroyableSub.d.ts +0 -83
- package/dist/DestroyableSub.d.ts.map +0 -1
- package/dist/index.iife.js +0 -1
- package/dist/index.umd.cjs +0 -1
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
|
|
@@ -43,29 +38,29 @@ export declare class Destroyable {
|
|
|
43
38
|
* @param sync - 表示是否使用 `obj.destroy()` 方法进行销毁;默认使用 `obj.destroyAsync()` 方法进行销毁
|
|
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():
|
|
63
|
+
destroyThis(): any;
|
|
69
64
|
/**
|
|
70
65
|
* 销毁
|
|
71
66
|
*/
|
|
@@ -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;;;;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,GAAG,CAAC;IAGlB;;OAEG;IACH,OAAO,IAAG,OAAO,CAAC;IAOlB;;;;OAIG;IACH,YAAY,IAAK,OAAO,CAAC,OAAO,CAAC,CAAC;CAGrC;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"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { FunDestroyer, IDestroyable } from "./Destroyable";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
export type EventMap<T> = Record<keyof T, any[]> | DefaultEventMap;
|
|
4
|
+
export type DefaultEventMap = [never];
|
|
5
|
+
/**
|
|
6
|
+
* 可销毁的对象
|
|
7
|
+
*/
|
|
8
|
+
export declare class DestroyableEventEmitter<EM extends EventMap<EM> = DefaultEventMap> extends EventEmitter<EM> implements IDestroyable {
|
|
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 IDestroyable>(obj: T, sync?: boolean): T;
|
|
47
|
+
/**
|
|
48
|
+
* 取消销毁者函数
|
|
49
|
+
* @param fun
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
cancelDisposeObj<T extends IDestroyable>(obj: T): T;
|
|
53
|
+
/**
|
|
54
|
+
* 添加销毁函数或销毁对象
|
|
55
|
+
* @param fun
|
|
56
|
+
*/
|
|
57
|
+
dispose<T extends FunDestroyer>(fun: T): T;
|
|
58
|
+
dispose<T extends IDestroyable>(obj: T, asyncDestroy?: boolean): T;
|
|
59
|
+
dispose<T extends IDestroyable | FunDestroyer>(objOrFun: T, asyncDestroy?: boolean): T;
|
|
60
|
+
cancelDispose<T extends FunDestroyer>(fun: T): T;
|
|
61
|
+
cancelDispose<T extends IDestroyable>(obj: T): T;
|
|
62
|
+
cancelDispose<T extends IDestroyable | 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
|
+
//# 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,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,eAAe,CAAC;AACnE,MAAM,MAAM,eAAe,GAAG,CAAC,KAAK,CAAC,CAAC;AAEtC;;GAEG;AACH,qBAAa,uBAAuB,CAAC,EAAE,SAAS,QAAQ,CAAC,EAAE,CAAC,GAAG,eAAe,CAAE,SAAQ,YAAY,CAAC,EAAE,CAAG,YAAW,YAAY;IAE7H;;;;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,YAAY,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,CAAC;IAQ7D;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IASnD;;;OAGG;IACH,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAC1C,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,EAAE,YAAY,CAAC,EAAE,OAAO,GAAG,CAAC;IAClE,OAAO,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,EAAE,YAAY,CAAC,EAAE,OAAO,GAAG,CAAC;IAStF,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;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;IAcpE;;;;OAIG;IACH,WAAW;IAKX;;OAEG;IACH,OAAO;IA6BP;;;;OAIG;IACG,YAAY;CA6BrB"}
|
|
@@ -0,0 +1,285 @@
|
|
|
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.destroy()` 方法进行销毁;默认使用 `obj.destroyAsync()` 方法进行销毁
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
disposeObj(e, r) {
|
|
70
|
+
const n = r ? function() {
|
|
71
|
+
return e.destroy();
|
|
72
|
+
} : function() {
|
|
73
|
+
return e.destroyAsync();
|
|
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
|
+
*/
|
|
102
|
+
destroy() {
|
|
103
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
104
|
+
let e = this._destroyers.length;
|
|
105
|
+
for (; --e >= 0; ) {
|
|
106
|
+
const r = this._destroyers[e];
|
|
107
|
+
try {
|
|
108
|
+
r();
|
|
109
|
+
} catch (n) {
|
|
110
|
+
console.error("销毁函数在同步销毁时出错", this, r, n);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
this._destroyers.length = 0;
|
|
114
|
+
try {
|
|
115
|
+
this.destroyThis();
|
|
116
|
+
} catch (r) {
|
|
117
|
+
console.error("destroyThis 在异步销毁时出错", this, r);
|
|
118
|
+
}
|
|
119
|
+
return d(this), !0;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 异步销毁
|
|
123
|
+
* @remarks
|
|
124
|
+
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
125
|
+
*/
|
|
126
|
+
async destroyAsync() {
|
|
127
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
128
|
+
let e = this._destroyers.length;
|
|
129
|
+
for (; --e >= 0; ) {
|
|
130
|
+
const r = this._destroyers[e];
|
|
131
|
+
try {
|
|
132
|
+
const n = r();
|
|
133
|
+
n && n instanceof Promise && await n;
|
|
134
|
+
} catch (n) {
|
|
135
|
+
console.error("销毁函数在异步销毁时出错", this, r, n);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
this._destroyers.length = 0;
|
|
139
|
+
try {
|
|
140
|
+
const r = this.destroyThis();
|
|
141
|
+
r && r instanceof Promise && await r;
|
|
142
|
+
} catch (r) {
|
|
143
|
+
console.error("destroyThis 在异步销毁时出错", this, r);
|
|
144
|
+
}
|
|
145
|
+
return d(this), !0;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return o;
|
|
149
|
+
}
|
|
150
|
+
class _ extends a {
|
|
151
|
+
constructor() {
|
|
152
|
+
super(...arguments);
|
|
153
|
+
/**
|
|
154
|
+
* 引用计数
|
|
155
|
+
* @remarks
|
|
156
|
+
* 引用计数为 0 时,对象才会被销毁
|
|
157
|
+
*/
|
|
158
|
+
y(this, "refCount", 0);
|
|
159
|
+
y(this, "_isDestroyed", !1);
|
|
160
|
+
/**
|
|
161
|
+
* 销毁者
|
|
162
|
+
*/
|
|
163
|
+
y(this, "_destroyers", []);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* 是否已经销毁
|
|
167
|
+
*/
|
|
168
|
+
get isDestroyed() {
|
|
169
|
+
return this._isDestroyed;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* 是否可以销毁
|
|
173
|
+
*/
|
|
174
|
+
get canDestroy() {
|
|
175
|
+
return !this.isDestroyed && this.refCount <= 0;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* 添加销毁者
|
|
179
|
+
* @param fun
|
|
180
|
+
* @returns 返回销毁者的顺序
|
|
181
|
+
*/
|
|
182
|
+
disposeFun(s) {
|
|
183
|
+
return this._destroyers.push(s), s;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* 取消销毁者函数
|
|
187
|
+
* @param fun
|
|
188
|
+
* @returns
|
|
189
|
+
*/
|
|
190
|
+
cancelDisposeFun(s) {
|
|
191
|
+
const t = this._destroyers.indexOf(s);
|
|
192
|
+
return this._destroyers.splice(t, 1), s;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* 添加销毁对象
|
|
196
|
+
* @param obj
|
|
197
|
+
* @param sync - 表示是否使用 `obj.destroy()` 方法进行销毁;默认使用 `obj.destroyAsync()` 方法进行销毁
|
|
198
|
+
* @returns
|
|
199
|
+
*/
|
|
200
|
+
disposeObj(s, t) {
|
|
201
|
+
const e = t ? function() {
|
|
202
|
+
return s.destroy();
|
|
203
|
+
} : function() {
|
|
204
|
+
return s.destroyAsync();
|
|
205
|
+
};
|
|
206
|
+
return this.disposeFun(e), s.__destroyable_destroyer = e, s;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* 取消销毁者函数
|
|
210
|
+
* @param fun
|
|
211
|
+
* @returns
|
|
212
|
+
*/
|
|
213
|
+
cancelDisposeObj(s) {
|
|
214
|
+
const t = s.__destroyable_destroyer;
|
|
215
|
+
return t && this.cancelDisposeFun(t), s;
|
|
216
|
+
}
|
|
217
|
+
dispose(s, t) {
|
|
218
|
+
return typeof s == "function" ? this.disposeFun(s) : this.disposeObj(s, t);
|
|
219
|
+
}
|
|
220
|
+
cancelDispose(s) {
|
|
221
|
+
return typeof s == "function" ? this.cancelDisposeFun(s) : this.cancelDisposeObj(s);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* 自己的销毁方法
|
|
225
|
+
* @remarks
|
|
226
|
+
* 子类根据需要进行重载
|
|
227
|
+
*/
|
|
228
|
+
destroyThis() {
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* 销毁
|
|
232
|
+
*/
|
|
233
|
+
destroy() {
|
|
234
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
235
|
+
let s = this._destroyers.length;
|
|
236
|
+
for (; --s >= 0; ) {
|
|
237
|
+
const t = this._destroyers[s];
|
|
238
|
+
try {
|
|
239
|
+
t();
|
|
240
|
+
} catch (e) {
|
|
241
|
+
console.error("销毁函数在同步销毁时出错", this, t, e);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
this._destroyers.length = 0;
|
|
245
|
+
try {
|
|
246
|
+
this.destroyThis();
|
|
247
|
+
} catch (t) {
|
|
248
|
+
console.error("destroyThis 在异步销毁时出错", this, t);
|
|
249
|
+
}
|
|
250
|
+
return d(this), !0;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* 异步销毁
|
|
254
|
+
* @remarks
|
|
255
|
+
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
256
|
+
*/
|
|
257
|
+
async destroyAsync() {
|
|
258
|
+
if (!this.canDestroy) return this.isDestroyed;
|
|
259
|
+
let s = this._destroyers.length;
|
|
260
|
+
for (; --s >= 0; ) {
|
|
261
|
+
const t = this._destroyers[s];
|
|
262
|
+
try {
|
|
263
|
+
const e = t();
|
|
264
|
+
e && e instanceof Promise && await e;
|
|
265
|
+
} catch (e) {
|
|
266
|
+
console.error("销毁函数在异步销毁时出错", this, t, e);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
this._destroyers.length = 0;
|
|
270
|
+
try {
|
|
271
|
+
const t = this.destroyThis();
|
|
272
|
+
t && t instanceof Promise && await t;
|
|
273
|
+
} catch (t) {
|
|
274
|
+
console.error("destroyThis 在异步销毁时出错", this, t);
|
|
275
|
+
}
|
|
276
|
+
return d(this), !0;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
export {
|
|
280
|
+
_ as D,
|
|
281
|
+
d as a,
|
|
282
|
+
p as c,
|
|
283
|
+
D as d,
|
|
284
|
+
h as t
|
|
285
|
+
};
|
|
@@ -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.destroy()}:function(){return e.destroyAsync()};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(){}destroy(){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}async destroyAsync(){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}}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.destroy()}:function(){return s.destroyAsync()};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(){}destroy(){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 destroyAsync(){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;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { FunDestroyer, IDestroyable } from "./Destroyable";
|
|
2
|
+
import { EventBus } from "@gby/event-bus";
|
|
3
|
+
/**
|
|
4
|
+
* 可销毁的对象
|
|
5
|
+
*/
|
|
6
|
+
export declare class DestroyableEventTarget<EM extends Record<string, any> = Record<string, any>> extends EventBus<EM> implements IDestroyable {
|
|
7
|
+
/**
|
|
8
|
+
* 引用计数
|
|
9
|
+
* @remarks
|
|
10
|
+
* 引用计数为 0 时,对象才会被销毁
|
|
11
|
+
*/
|
|
12
|
+
refCount: number;
|
|
13
|
+
/**
|
|
14
|
+
* 是否已经销毁
|
|
15
|
+
*/
|
|
16
|
+
get isDestroyed(): boolean;
|
|
17
|
+
_isDestroyed: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* 是否可以销毁
|
|
20
|
+
*/
|
|
21
|
+
get canDestroy(): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* 销毁者
|
|
24
|
+
*/
|
|
25
|
+
_destroyers: FunDestroyer[];
|
|
26
|
+
/**
|
|
27
|
+
* 添加销毁者
|
|
28
|
+
* @param fun
|
|
29
|
+
* @returns 返回销毁者的顺序
|
|
30
|
+
*/
|
|
31
|
+
disposeFun<T extends FunDestroyer>(fun: T): T;
|
|
32
|
+
/**
|
|
33
|
+
* 取消销毁者函数
|
|
34
|
+
* @param fun
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
cancelDisposeFun<T extends FunDestroyer>(fun: T): T;
|
|
38
|
+
/**
|
|
39
|
+
* 添加销毁对象
|
|
40
|
+
* @param obj
|
|
41
|
+
* @param sync - 表示是否使用 `obj.destroy()` 方法进行销毁;默认使用 `obj.destroyAsync()` 方法进行销毁
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
disposeObj<T extends IDestroyable>(obj: T, sync?: boolean): T;
|
|
45
|
+
/**
|
|
46
|
+
* 取消销毁者函数
|
|
47
|
+
* @param fun
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
cancelDisposeObj<T extends IDestroyable>(obj: T): T;
|
|
51
|
+
/**
|
|
52
|
+
* 添加销毁函数或销毁对象
|
|
53
|
+
* @param fun
|
|
54
|
+
*/
|
|
55
|
+
dispose<T extends FunDestroyer>(fun: T): T;
|
|
56
|
+
dispose<T extends IDestroyable>(obj: T, asyncDestroy?: boolean): T;
|
|
57
|
+
dispose<T extends IDestroyable | FunDestroyer>(objOrFun: T, asyncDestroy?: boolean): T;
|
|
58
|
+
cancelDispose<T extends FunDestroyer>(fun: T): T;
|
|
59
|
+
cancelDispose<T extends IDestroyable>(obj: T): T;
|
|
60
|
+
cancelDispose<T extends IDestroyable | FunDestroyer>(objOrFun: T): T;
|
|
61
|
+
/**
|
|
62
|
+
* 自己的销毁方法
|
|
63
|
+
* @remarks
|
|
64
|
+
* 子类根据需要进行重载
|
|
65
|
+
*/
|
|
66
|
+
destroyThis(): void;
|
|
67
|
+
/**
|
|
68
|
+
* 销毁
|
|
69
|
+
*/
|
|
70
|
+
destroy(): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* 异步销毁
|
|
73
|
+
* @remarks
|
|
74
|
+
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
75
|
+
*/
|
|
76
|
+
destroyAsync(): Promise<boolean>;
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=DestroyableEventTarget.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DestroyableEventTarget.d.ts","sourceRoot":"","sources":["../src/DestroyableEventTarget.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAEhE,OAAO,EAAC,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AAGxC;;GAEG;AACH,qBAAa,sBAAsB,CAAC,EAAE,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAE,SAAQ,QAAQ,CAAC,EAAE,CAAE,YAAW,YAAY;IAElI;;;;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,YAAY,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,CAAC;IAQ7D;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IASnD;;;OAGG;IACH,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAC1C,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,EAAE,YAAY,CAAC,EAAE,OAAO,GAAG,CAAC;IAClE,OAAO,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,EAAE,YAAY,CAAC,EAAE,OAAO,GAAG,CAAC;IAStF,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;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;IAcpE;;;;OAIG;IACH,WAAW;IAKX;;OAEG;IACH,OAAO;IA6BP;;;;OAIG;IACG,YAAY;CA6BrB"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FunDestroyer } from "./Destroyable";
|
|
1
|
+
import type { FunDestroyer, IDestroyable } from "./Destroyable";
|
|
2
2
|
/**
|
|
3
3
|
* 创建可销毁的子类
|
|
4
4
|
* @remarks
|
|
@@ -45,23 +45,23 @@ export declare function createDestroyableSubClass<P extends new (...args: any) =
|
|
|
45
45
|
* @param sync - 表示是否使用 `obj.destroy()` 方法进行销毁;默认使用 `obj.destroyAsync()` 方法进行销毁
|
|
46
46
|
* @returns
|
|
47
47
|
*/
|
|
48
|
-
disposeObj<T extends
|
|
48
|
+
disposeObj<T extends IDestroyable>(obj: T, sync?: boolean): T;
|
|
49
49
|
/**
|
|
50
50
|
* 取消销毁者函数
|
|
51
51
|
* @param fun
|
|
52
52
|
* @returns
|
|
53
53
|
*/
|
|
54
|
-
cancelDisposeObj<T extends
|
|
54
|
+
cancelDisposeObj<T extends IDestroyable>(obj: T): T;
|
|
55
55
|
/**
|
|
56
56
|
* 添加销毁函数或销毁对象
|
|
57
57
|
* @param fun
|
|
58
58
|
*/
|
|
59
59
|
dispose<T extends FunDestroyer>(fun: T): T;
|
|
60
|
-
dispose<T extends
|
|
61
|
-
dispose<T extends
|
|
60
|
+
dispose<T extends IDestroyable>(obj: T, asyncDestroy?: boolean): T;
|
|
61
|
+
dispose<T extends IDestroyable | FunDestroyer>(objOrFun: T, asyncDestroy?: boolean): T;
|
|
62
62
|
cancelDispose<T extends FunDestroyer>(fun: T): T;
|
|
63
|
-
cancelDispose<T extends
|
|
64
|
-
cancelDispose<T extends
|
|
63
|
+
cancelDispose<T extends IDestroyable>(obj: T): T;
|
|
64
|
+
cancelDispose<T extends IDestroyable | FunDestroyer>(objOrFun: T): T;
|
|
65
65
|
/**
|
|
66
66
|
* 自己的销毁方法
|
|
67
67
|
* @remarks
|
|
@@ -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;
|
|
1
|
+
{"version":3,"file":"createDestroyableSubClass.d.ts","sourceRoot":"","sources":["../src/createDestroyableSubClass.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAC,YAAY,EAAE,MAAM,eAAe,CAAC;AAG/D;;;;;;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,SAAS,YAAY,OAAO,CAAC,SAAS,OAAO,GAAG,CAAC;QAQ7D;;;;WAIG;yBACc,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;QASnD;;;WAGG;gBACK,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;gBAClC,CAAC,SAAS,YAAY,OAAO,CAAC,iBAAiB,OAAO,GAAG,CAAC;gBAC1D,CAAC,SAAS,YAAY,GAAG,YAAY,YAAY,CAAC,iBAAiB,OAAO,GAAG,CAAC;sBASxE,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;sBAClC,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;sBAClC,CAAC,SAAS,YAAY,GAAG,YAAY,YAAY,CAAC,GAAG,CAAC;QAcpE;;;;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
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./DestroyableEventTarget-B26KR0Ul.cjs");exports.DestroyableEventTarget=e.DestroyableEventTarget;exports.createDestroyableSubClass=e.createDestroyableSubClass;exports.destroyDestroyable=e.destroyDestroyable;exports.destroyObject=e.destroyObject;exports.throwOnDestroyed=e.throwOnDestroyed;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 可销毁的
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* @gby/destroyable 提供了用于构建可销毁对象的类的基类,用于使得地管理可销毁对象的生命周期
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
export * from './Destroyable';
|
|
10
|
+
export * from "./createDestroyableSubClass";
|
|
11
|
+
export * from "./DestroyableEventTarget";
|
|
12
|
+
//# sourceMappingURL=index-web.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-web.d.ts","sourceRoot":"","sources":["../src/index-web.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,eAAe,CAAC;AAC9B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +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-B26KR0Ul.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.destroy()}:function(){return e.destroyAsync()};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(){}destroy(){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 destroyAsync(){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.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.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,eAAe,CAAC;AAC9B,cAAc,6BAA6B,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,eAAe,CAAC;AAC9B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
1
|
+
var y = Object.defineProperty;
|
|
2
|
+
var c = (o, r, e) => r in o ? y(o, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[r] = e;
|
|
3
|
+
var i = (o, r, e) => c(o, typeof r != "symbol" ? r + "" : r, e);
|
|
4
|
+
import { a as n } from "./DestroyableEventTarget-3_XyRRfB.js";
|
|
5
|
+
import { D as p, c as _, d as m, t as g } from "./DestroyableEventTarget-3_XyRRfB.js";
|
|
6
|
+
import { EventEmitter as d } from "node:events";
|
|
7
|
+
class u extends d {
|
|
7
8
|
constructor() {
|
|
9
|
+
super(...arguments);
|
|
8
10
|
/**
|
|
9
11
|
* 引用计数
|
|
10
12
|
* @remarks
|
|
11
13
|
* 引用计数为 0 时,对象才会被销毁
|
|
12
14
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
i(this, "refCount", 0);
|
|
16
|
+
i(this, "_isDestroyed", !1);
|
|
15
17
|
/**
|
|
16
18
|
* 销毁者
|
|
17
19
|
*/
|
|
18
|
-
|
|
20
|
+
i(this, "_destroyers", []);
|
|
19
21
|
}
|
|
20
22
|
/**
|
|
21
23
|
* 是否已经销毁
|
|
@@ -53,12 +55,12 @@ class p {
|
|
|
53
55
|
* @returns
|
|
54
56
|
*/
|
|
55
57
|
disposeObj(e, s) {
|
|
56
|
-
const
|
|
58
|
+
const t = s ? function() {
|
|
57
59
|
return e.destroy();
|
|
58
60
|
} : function() {
|
|
59
61
|
return e.destroyAsync();
|
|
60
62
|
};
|
|
61
|
-
return this.disposeFun(
|
|
63
|
+
return this.disposeFun(t), e.__destroyable_destroyer = t, e;
|
|
62
64
|
}
|
|
63
65
|
/**
|
|
64
66
|
* 取消销毁者函数
|
|
@@ -92,8 +94,8 @@ class p {
|
|
|
92
94
|
const s = this._destroyers[e];
|
|
93
95
|
try {
|
|
94
96
|
s();
|
|
95
|
-
} catch (
|
|
96
|
-
console.error("销毁函数在同步销毁时出错", this, s,
|
|
97
|
+
} catch (t) {
|
|
98
|
+
console.error("销毁函数在同步销毁时出错", this, s, t);
|
|
97
99
|
}
|
|
98
100
|
}
|
|
99
101
|
this._destroyers.length = 0;
|
|
@@ -102,7 +104,7 @@ class p {
|
|
|
102
104
|
} catch (s) {
|
|
103
105
|
console.error("destroyThis 在异步销毁时出错", this, s);
|
|
104
106
|
}
|
|
105
|
-
return
|
|
107
|
+
return n(this), !0;
|
|
106
108
|
}
|
|
107
109
|
/**
|
|
108
110
|
* 异步销毁
|
|
@@ -115,10 +117,10 @@ class p {
|
|
|
115
117
|
for (; --e >= 0; ) {
|
|
116
118
|
const s = this._destroyers[e];
|
|
117
119
|
try {
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
} catch (
|
|
121
|
-
console.error("销毁函数在异步销毁时出错", this, s,
|
|
120
|
+
const t = s();
|
|
121
|
+
t && t instanceof Promise && await t;
|
|
122
|
+
} catch (t) {
|
|
123
|
+
console.error("销毁函数在异步销毁时出错", this, s, t);
|
|
122
124
|
}
|
|
123
125
|
}
|
|
124
126
|
this._destroyers.length = 0;
|
|
@@ -128,161 +130,14 @@ class p {
|
|
|
128
130
|
} catch (s) {
|
|
129
131
|
console.error("destroyThis 在异步销毁时出错", this, s);
|
|
130
132
|
}
|
|
131
|
-
return
|
|
133
|
+
return n(this), !0;
|
|
132
134
|
}
|
|
133
135
|
}
|
|
134
|
-
function h() {
|
|
135
|
-
throw "已销毁";
|
|
136
|
-
}
|
|
137
|
-
function w(i) {
|
|
138
|
-
for (var e in i)
|
|
139
|
-
typeof i[e] == "function" && (i[e] = h);
|
|
140
|
-
return i._isDestroyed = !0, !0;
|
|
141
|
-
}
|
|
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;
|
|
146
|
-
}
|
|
147
|
-
function d(i) {
|
|
148
|
-
class e extends i {
|
|
149
|
-
constructor() {
|
|
150
|
-
super(...arguments);
|
|
151
|
-
/**
|
|
152
|
-
* 引用计数
|
|
153
|
-
* @remarks
|
|
154
|
-
* 引用计数为 0 时,对象才会被销毁
|
|
155
|
-
*/
|
|
156
|
-
y(this, "refCount", 0);
|
|
157
|
-
y(this, "_isDestroyed", !1);
|
|
158
|
-
/**
|
|
159
|
-
* 销毁者
|
|
160
|
-
*/
|
|
161
|
-
y(this, "_destroyers", []);
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* 是否已经销毁
|
|
165
|
-
*/
|
|
166
|
-
get isDestroyed() {
|
|
167
|
-
return this._isDestroyed;
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* 是否可以销毁
|
|
171
|
-
*/
|
|
172
|
-
get canDestroy() {
|
|
173
|
-
return !this.isDestroyed && this.refCount <= 0;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* 添加销毁者
|
|
177
|
-
* @param fun
|
|
178
|
-
* @returns 返回销毁者的顺序
|
|
179
|
-
*/
|
|
180
|
-
disposeFun(t) {
|
|
181
|
-
return this._destroyers.push(t), t;
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* 取消销毁者函数
|
|
185
|
-
* @param fun
|
|
186
|
-
* @returns
|
|
187
|
-
*/
|
|
188
|
-
cancelDisposeFun(t) {
|
|
189
|
-
const r = this._destroyers.indexOf(t);
|
|
190
|
-
return this._destroyers.splice(r, 1), t;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* 添加销毁对象
|
|
194
|
-
* @param obj
|
|
195
|
-
* @param sync - 表示是否使用 `obj.destroy()` 方法进行销毁;默认使用 `obj.destroyAsync()` 方法进行销毁
|
|
196
|
-
* @returns
|
|
197
|
-
*/
|
|
198
|
-
disposeObj(t, r) {
|
|
199
|
-
const n = r ? function() {
|
|
200
|
-
return t.destroy();
|
|
201
|
-
} : function() {
|
|
202
|
-
return t.destroyAsync();
|
|
203
|
-
};
|
|
204
|
-
return this.disposeFun(n), t.__destroyable_destroyer = n, t;
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* 取消销毁者函数
|
|
208
|
-
* @param fun
|
|
209
|
-
* @returns
|
|
210
|
-
*/
|
|
211
|
-
cancelDisposeObj(t) {
|
|
212
|
-
const r = t.__destroyable_destroyer;
|
|
213
|
-
return r && this.cancelDisposeFun(r), t;
|
|
214
|
-
}
|
|
215
|
-
dispose(t, r) {
|
|
216
|
-
return typeof t == "function" ? this.disposeFun(t) : this.disposeObj(t, r);
|
|
217
|
-
}
|
|
218
|
-
cancelDispose(t) {
|
|
219
|
-
return typeof t == "function" ? this.cancelDisposeFun(t) : this.cancelDisposeObj(t);
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* 自己的销毁方法
|
|
223
|
-
* @remarks
|
|
224
|
-
* 子类根据需要进行重载
|
|
225
|
-
*/
|
|
226
|
-
destroyThis() {
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* 销毁
|
|
230
|
-
*/
|
|
231
|
-
destroy() {
|
|
232
|
-
if (!this.canDestroy) return this.isDestroyed;
|
|
233
|
-
let t = this._destroyers.length;
|
|
234
|
-
for (; --t >= 0; ) {
|
|
235
|
-
const r = this._destroyers[t];
|
|
236
|
-
try {
|
|
237
|
-
r();
|
|
238
|
-
} catch (n) {
|
|
239
|
-
console.error("销毁函数在同步销毁时出错", this, r, n);
|
|
240
|
-
}
|
|
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;
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* 异步销毁
|
|
252
|
-
* @remarks
|
|
253
|
-
* 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
|
|
254
|
-
*/
|
|
255
|
-
async destroyAsync() {
|
|
256
|
-
if (!this.canDestroy) return this.isDestroyed;
|
|
257
|
-
let t = this._destroyers.length;
|
|
258
|
-
for (; --t >= 0; ) {
|
|
259
|
-
const r = this._destroyers[t];
|
|
260
|
-
try {
|
|
261
|
-
const n = r();
|
|
262
|
-
n && n instanceof Promise && await n;
|
|
263
|
-
} catch (n) {
|
|
264
|
-
console.error("销毁函数在异步销毁时出错", this, r, n);
|
|
265
|
-
}
|
|
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;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return e;
|
|
278
|
-
}
|
|
279
|
-
const x = d(l), T = d(f);
|
|
280
136
|
export {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
h as throwOnDestroyed
|
|
137
|
+
u as DestroyableEventEmitter,
|
|
138
|
+
p as DestroyableEventTarget,
|
|
139
|
+
_ as createDestroyableSubClass,
|
|
140
|
+
n as destroyDestroyable,
|
|
141
|
+
m as destroyObject,
|
|
142
|
+
g as throwOnDestroyed
|
|
288
143
|
};
|
package/package.json
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gby/destroyable",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "@gby/destroyable 提供了用于构建可销毁对象的类的基类,用于使得地管理可销毁对象的生命周期",
|
|
5
|
-
"main": "./dist/index.
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
|
-
"
|
|
9
|
+
"browser": {
|
|
10
|
+
"require": "./dist/index-web.cjs",
|
|
11
|
+
"types": "./dist/index-web.d.ts",
|
|
12
|
+
"import": "./dist/index-web.js"
|
|
13
|
+
},
|
|
14
|
+
"require": "./dist/index.cjs",
|
|
10
15
|
"types": "./dist/index.d.ts",
|
|
11
|
-
"import": "./dist/index.js"
|
|
12
|
-
"default": "./dist/index.js"
|
|
16
|
+
"import": "./dist/index.js"
|
|
13
17
|
},
|
|
14
18
|
"type": "module",
|
|
15
19
|
"sideEffects": false,
|
package/dist/DestroyableSub.d.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { EventBus } from "@gby/event-bus";
|
|
2
|
-
import { EventEmitter } from "node:events";
|
|
3
|
-
/**
|
|
4
|
-
* WebDestroyable 类对象
|
|
5
|
-
*/
|
|
6
|
-
export declare const WebDestroyable: {
|
|
7
|
-
new (...args: any): {
|
|
8
|
-
refCount: number;
|
|
9
|
-
readonly isDestroyed: boolean;
|
|
10
|
-
_isDestroyed: boolean;
|
|
11
|
-
readonly canDestroy: boolean;
|
|
12
|
-
_destroyers: import("./Destroyable").FunDestroyer[];
|
|
13
|
-
disposeFun<T extends import("./Destroyable").FunDestroyer>(fun: T): T;
|
|
14
|
-
cancelDisposeFun<T extends import("./Destroyable").FunDestroyer>(fun: T): T;
|
|
15
|
-
disposeObj<T extends /*elided*/ any>(obj: T, sync?: boolean): T;
|
|
16
|
-
cancelDisposeObj<T extends /*elided*/ any>(obj: T): T;
|
|
17
|
-
dispose<T extends import("./Destroyable").FunDestroyer>(fun: T): T;
|
|
18
|
-
dispose<T extends /*elided*/ any>(obj: T, asyncDestroy?: boolean): T;
|
|
19
|
-
dispose<T extends import("./Destroyable").FunDestroyer | /*elided*/ any>(objOrFun: T, asyncDestroy?: boolean): T;
|
|
20
|
-
cancelDispose<T extends import("./Destroyable").FunDestroyer>(fun: T): T;
|
|
21
|
-
cancelDispose<T extends /*elided*/ any>(obj: T): T;
|
|
22
|
-
cancelDispose<T extends import("./Destroyable").FunDestroyer | /*elided*/ any>(objOrFun: T): T;
|
|
23
|
-
destroyThis(): void;
|
|
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;
|
|
33
|
-
};
|
|
34
|
-
} & typeof EventBus;
|
|
35
|
-
/**
|
|
36
|
-
* WebDestroyable 类的类型
|
|
37
|
-
*/
|
|
38
|
-
export type WebDestroyableClass = typeof WebDestroyable;
|
|
39
|
-
/**
|
|
40
|
-
* WebDestroyable 实例的类型
|
|
41
|
-
*/
|
|
42
|
-
export type WebDestroyable = InstanceType<WebDestroyableClass>;
|
|
43
|
-
/**
|
|
44
|
-
* NodeDestroyable 类对象
|
|
45
|
-
*/
|
|
46
|
-
export declare const NodeDestroyable: {
|
|
47
|
-
new (...args: any): {
|
|
48
|
-
refCount: number;
|
|
49
|
-
readonly isDestroyed: boolean;
|
|
50
|
-
_isDestroyed: boolean;
|
|
51
|
-
readonly canDestroy: boolean;
|
|
52
|
-
_destroyers: import("./Destroyable").FunDestroyer[];
|
|
53
|
-
disposeFun<T extends import("./Destroyable").FunDestroyer>(fun: T): T;
|
|
54
|
-
cancelDisposeFun<T extends import("./Destroyable").FunDestroyer>(fun: T): T;
|
|
55
|
-
disposeObj<T extends /*elided*/ any>(obj: T, sync?: boolean): T;
|
|
56
|
-
cancelDisposeObj<T extends /*elided*/ any>(obj: T): T;
|
|
57
|
-
dispose<T extends import("./Destroyable").FunDestroyer>(fun: T): T;
|
|
58
|
-
dispose<T extends /*elided*/ any>(obj: T, asyncDestroy?: boolean): T;
|
|
59
|
-
dispose<T extends import("./Destroyable").FunDestroyer | /*elided*/ any>(objOrFun: T, asyncDestroy?: boolean): T;
|
|
60
|
-
cancelDispose<T extends import("./Destroyable").FunDestroyer>(fun: T): T;
|
|
61
|
-
cancelDispose<T extends /*elided*/ any>(obj: T): T;
|
|
62
|
-
cancelDispose<T extends import("./Destroyable").FunDestroyer | /*elided*/ any>(objOrFun: T): T;
|
|
63
|
-
destroyThis(): void;
|
|
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;
|
|
73
|
-
};
|
|
74
|
-
} & typeof EventEmitter;
|
|
75
|
-
/**
|
|
76
|
-
* NodeDestroyable 类的类型
|
|
77
|
-
*/
|
|
78
|
-
export type NodeDestroyableClass = typeof NodeDestroyable;
|
|
79
|
-
/**
|
|
80
|
-
* NodeDestroyable 实例的类型
|
|
81
|
-
*/
|
|
82
|
-
export type NodeDestroyable = InstanceType<NodeDestroyableClass>;
|
|
83
|
-
//# sourceMappingURL=DestroyableSub.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/index.iife.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
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.umd.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"})});
|