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