@gby/destroyable 1.0.2 → 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,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;
@@ -8,11 +8,21 @@ import type { FunDestroyer } from "./Destroyable";
8
8
  */
9
9
  export declare function createDestroyableSubClass<P extends new (...args: any) => Object>(ParentClass: P): {
10
10
  new (...args: any): {
11
+ /**
12
+ * 引用计数
13
+ * @remarks
14
+ * 引用计数为 0 时,对象才会被销毁
15
+ */
16
+ refCount: number;
11
17
  /**
12
18
  * 是否已经销毁
13
19
  */
14
20
  readonly isDestroyed: boolean;
15
21
  _isDestroyed: boolean;
22
+ /**
23
+ * 是否可以销毁
24
+ */
25
+ readonly canDestroy: boolean;
16
26
  /**
17
27
  * 销毁者
18
28
  */
@@ -61,13 +71,13 @@ export declare function createDestroyableSubClass<P extends new (...args: any) =
61
71
  /**
62
72
  * 销毁
63
73
  */
64
- destroy(): true | undefined;
74
+ destroy(): boolean;
65
75
  /**
66
76
  * 异步销毁
67
77
  * @remarks
68
78
  * 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
69
79
  */
70
- destroyAsync(): Promise<true | undefined>;
80
+ destroyAsync(): Promise<boolean>;
71
81
  constructor: Function;
72
82
  toString(): string;
73
83
  toLocaleString(): string;
@@ -1 +1 @@
1
- {"version":3,"file":"createDestroyableSubClass.d.ts","sourceRoot":"","sources":["../src/createDestroyableSubClass.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAGlD;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,MAAM,EAAE,WAAW,EAAE,CAAC;kBAA9B,GAAG;QAO7D;;WAEG;;;QAMH;;WAEG;qBACU,YAAY,EAAE;QAE3B;;;;WAIG;mBACQ,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;QAK7C;;;;WAIG;yBACc,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;QAMnD;;;;;WAKG;mBACQ,CAAC,8BAA8B,CAAC,SAAS,OAAO,GAAG,CAAC;QAQ/D;;;;WAIG;yBACc,CAAC,8BAA8B,CAAC,GAAG,CAAC;QASrD;;;WAGG;gBACK,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;gBAClC,CAAC,8BAA8B,CAAC,iBAAiB,OAAO,GAAG,CAAC;gBAC5D,CAAC,SAAS,iBAAiB,YAAY,YAAY,CAAC,iBAAiB,OAAO,GAAG,CAAC;sBAS1E,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;sBAClC,CAAC,8BAA8B,CAAC,GAAG,CAAC;sBACpC,CAAC,SAAS,iBAAiB,YAAY,YAAY,CAAC,GAAG,CAAC;QActE;;;;WAIG;;QAMH;;WAEG;;QAkBH;;;;WAIG;;;;;;;;;;MAiDV;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"}
1
+ {"version":3,"file":"createDestroyableSubClass.d.ts","sourceRoot":"","sources":["../src/createDestroyableSubClass.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAGlD;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,MAAM,EAAE,WAAW,EAAE,CAAC;kBAA9B,GAAG;QAQ7D;;;;WAIG;;QAGH;;WAEG;;;QAMH;;WAEG;;QAMH;;WAEG;qBACU,YAAY,EAAE;QAE3B;;;;WAIG;mBACQ,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;QAK7C;;;;WAIG;yBACc,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;QAMnD;;;;;WAKG;mBACQ,CAAC,8BAA8B,CAAC,SAAS,OAAO,GAAG,CAAC;QAQ/D;;;;WAIG;yBACc,CAAC,8BAA8B,CAAC,GAAG,CAAC;QASrD;;;WAGG;gBACK,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;gBAClC,CAAC,8BAA8B,CAAC,iBAAiB,OAAO,GAAG,CAAC;gBAC5D,CAAC,SAAS,iBAAiB,YAAY,YAAY,CAAC,iBAAiB,OAAO,GAAG,CAAC;sBAS1E,CAAC,SAAS,YAAY,OAAO,CAAC,GAAG,CAAC;sBAClC,CAAC,8BAA8B,CAAC,GAAG,CAAC;sBACpC,CAAC,SAAS,iBAAiB,YAAY,YAAY,CAAC,GAAG,CAAC;QActE;;;;WAIG;;QAMH;;WAEG;;QA8BH;;;;WAIG;;;;;;;;;;MAmCV;AAMD;;GAEG;AACH,MAAM,MAAM,mBAAmB,CAAC,WAAW,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,MAAM,IAAI,UAAU,CAAC,OAAO,yBAAyB,CAAC,WAAW,CAAC,CAAC,CAAC;AAE9I;;GAEG;AACH,MAAM,MAAM,cAAc,CAAC,WAAW,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,MAAM,IAAI,YAAY,CAAC,mBAAmB,CAAC,WAAW,CAAC,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ "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;