@gby/destroyable 3.6.3 → 3.7.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/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./DestroyableEventTarget-uZtYhcb1.cjs"),i=require("node:events"),n=require("com-tools");class y extends i.EventEmitter{refCount=0;get isDestroyed(){return this._isDestroyed}_isDestroyed=!1;get canDestroy(){return!this.isDestroyed&&this.refCount<=0}_destroyers=[];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 r=t?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(r),e.__destroyable_destroyer=r,e}cancelDisposeObj(e){const t=e.__destroyable_destroyer;return t&&this.cancelDisposeFun(t),e}dispose(e,t){return typeof e=="function"?this.disposeFun(e):this.disposeObj(e,t)}cancelDispose(e){return typeof e=="function"?this.cancelDisposeFun(e):this.cancelDisposeObj(e)}destroyThis(){}destroySync(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const t=this._destroyers[e];try{t()}catch(r){console.error("销毁函数在同步销毁时出错",this,t,r)}}this._destroyers.length=0;try{this.destroyThis()}catch(t){console.error("destroyThis 在异步销毁时出错",this,t)}return s.destroyDestroyable(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;const e=this._destroyers.toReversed();e.push(this.destroyThis),this._destroyers.length=0;const t=n.serialCallAllFuns(e,this,null,(r,o)=>{console.error("销毁函数在串行销毁时出错",this,o,r)});return t instanceof Promise?t.then(()=>(s.destroyDestroyable(this),!0)):(s.destroyDestroyable(this),!0)}}exports.Destroyable=s.Destroyable;exports.DestroyableEventTarget=s.DestroyableEventTarget;exports.createDestroyableSubClass=s.createDestroyableSubClass;exports.destroyDestroyable=s.destroyDestroyable;exports.destroyObject=s.destroyObject;exports.throwOnDestroyed=s.throwOnDestroyed;exports.DestroyableEventEmitter=y;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("com-tools"),c=require("@gby/event-bus"),l=require("node:events");class y{refCount=0;get isDestroyed(){return this._isDestroyed}_isDestroyed=!1;get canDestroy(){return!this.isDestroyed&&this.refCount<=0}_destroyers=[];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.destroySync()}:function(){return e.destroy()};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(){}destroySync(){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(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;const e=this._destroyers.toReversed();e.push(this.destroyThis),this._destroyers.length=0;const s=h.serialCallAllFuns(e,this,null,(t,r)=>{console.error("销毁函数在串行销毁时出错",this,r,t)});return s instanceof Promise?s.then(()=>(o(this),!0)):(o(this),!0)}}function d(){throw"已销毁"}function a(i){for(var e in i)typeof i[e]=="function"&&(i[e]=d);return i._isDestroyed=!0,!0}function o(i){for(var e in i)!(e in y.prototype)&&typeof i[e]=="function"&&(i[e]=d);return i._isDestroyed=!0,!0}function f(i){class e extends i{refCount=0;get isDestroyed(){return this._isDestroyed}_isDestroyed=!1;get canDestroy(){return!this.isDestroyed&&this.refCount<=0}_destroyers=[];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.destroySync()}:function(){return t.destroy()};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(){}destroySync(){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 o(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;const t=this._destroyers.toReversed();t.push(this.destroyThis),this._destroyers.length=0;const r=h.serialCallAllFuns(t,this,null,(n,u)=>{console.error("销毁函数在串行销毁时出错",this,u,n)});return r instanceof Promise?r.then(()=>(o(this),!0)):(o(this),!0)}}return e}class D extends c.EventBus{refCount=0;get isDestroyed(){return this._isDestroyed}_isDestroyed=!1;get canDestroy(){return!this.isDestroyed&&this.refCount<=0}_destroyers=[];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.destroySync()}:function(){return e.destroy()};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(){}destroySync(){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(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;const e=this._destroyers.toReversed();e.push(this.destroyThis),this._destroyers.length=0;const s=h.serialCallAllFuns(e,this,null,(t,r)=>{console.error("销毁函数在串行销毁时出错",this,r,t)});return s instanceof Promise?s.then(()=>(o(this),!0)):(o(this),!0)}}class p extends l.EventEmitter{refCount=0;get isDestroyed(){return this._isDestroyed}_isDestroyed=!1;get canDestroy(){return!this.isDestroyed&&this.refCount<=0}_destroyers=[];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.destroySync()}:function(){return e.destroy()};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(){}destroySync(){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(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;const e=this._destroyers.toReversed();e.push(this.destroyThis),this._destroyers.length=0;const s=h.serialCallAllFuns(e,this,null,(t,r)=>{console.error("销毁函数在串行销毁时出错",this,r,t)});return s instanceof Promise?s.then(()=>(o(this),!0)):(o(this),!0)}}exports.Destroyable=y;exports.DestroyableEventEmitter=p;exports.DestroyableEventTarget=D;exports.createDestroyableSubClass=f;exports.destroyDestroyable=o;exports.destroyObject=a;exports.throwOnDestroyed=d;
package/dist/index.js CHANGED
@@ -1,8 +1,7 @@
1
- import { d as r } from "./DestroyableEventTarget-CIMBV0bD.js";
2
- import { D as p, a as D, c as f, b as _, t as m } from "./DestroyableEventTarget-CIMBV0bD.js";
3
- import { EventEmitter as i } from "node:events";
4
- import { serialCallAllFuns as n } from "com-tools";
5
- class u extends i {
1
+ import { serialCallAllFuns as h } from "com-tools";
2
+ import { EventBus as c } from "@gby/event-bus";
3
+ import { EventEmitter as u } from "node:events";
4
+ class l {
6
5
  /**
7
6
  * 引用计数
8
7
  * @remarks
@@ -101,7 +100,7 @@ class u extends i {
101
100
  } catch (s) {
102
101
  console.error("destroyThis 在异步销毁时出错", this, s);
103
102
  }
104
- return r(this), !0;
103
+ return o(this), !0;
105
104
  }
106
105
  /**
107
106
  * 串行销毁
@@ -113,18 +112,385 @@ class u extends i {
113
112
  if (!this.canDestroy) return this.isDestroyed;
114
113
  const e = this._destroyers.toReversed();
115
114
  e.push(this.destroyThis), this._destroyers.length = 0;
116
- const s = n(e, this, null, (t, o) => {
117
- console.error("销毁函数在串行销毁时出错", this, o, t);
115
+ const s = h(e, this, null, (t, r) => {
116
+ console.error("销毁函数在串行销毁时出错", this, r, t);
118
117
  });
119
- return s instanceof Promise ? s.then(() => (r(this), !0)) : (r(this), !0);
118
+ return s instanceof Promise ? s.then(() => (o(this), !0)) : (o(this), !0);
119
+ }
120
+ }
121
+ function d() {
122
+ throw "已销毁";
123
+ }
124
+ function _(i) {
125
+ for (var e in i)
126
+ typeof i[e] == "function" && (i[e] = d);
127
+ return i._isDestroyed = !0, !0;
128
+ }
129
+ function o(i) {
130
+ for (var e in i)
131
+ !(e in l.prototype) && typeof i[e] == "function" && (i[e] = d);
132
+ return i._isDestroyed = !0, !0;
133
+ }
134
+ function D(i) {
135
+ class e extends i {
136
+ /**
137
+ * 引用计数
138
+ * @remarks
139
+ * 引用计数为 0 时,对象才会被销毁
140
+ */
141
+ refCount = 0;
142
+ /**
143
+ * 是否已经销毁
144
+ */
145
+ get isDestroyed() {
146
+ return this._isDestroyed;
147
+ }
148
+ _isDestroyed = !1;
149
+ /**
150
+ * 是否可以销毁
151
+ */
152
+ get canDestroy() {
153
+ return !this.isDestroyed && this.refCount <= 0;
154
+ }
155
+ /**
156
+ * 销毁者
157
+ */
158
+ _destroyers = [];
159
+ /**
160
+ * 添加销毁者
161
+ * @param fun
162
+ * @returns 返回销毁者的顺序
163
+ */
164
+ disposeFun(t) {
165
+ return this._destroyers.push(t), t;
166
+ }
167
+ /**
168
+ * 取消销毁者函数
169
+ * @param fun
170
+ * @returns
171
+ */
172
+ cancelDisposeFun(t) {
173
+ const r = this._destroyers.indexOf(t);
174
+ return this._destroyers.splice(r, 1), t;
175
+ }
176
+ /**
177
+ * 添加销毁对象
178
+ * @param obj
179
+ * @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
180
+ * @returns
181
+ */
182
+ disposeObj(t, r) {
183
+ const n = r ? function() {
184
+ return t.destroySync();
185
+ } : function() {
186
+ return t.destroy();
187
+ };
188
+ return this.disposeFun(n), t.__destroyable_destroyer = n, t;
189
+ }
190
+ /**
191
+ * 取消销毁者函数
192
+ * @param fun
193
+ * @returns
194
+ */
195
+ cancelDisposeObj(t) {
196
+ const r = t.__destroyable_destroyer;
197
+ return r && this.cancelDisposeFun(r), t;
198
+ }
199
+ dispose(t, r) {
200
+ return typeof t == "function" ? this.disposeFun(t) : this.disposeObj(t, r);
201
+ }
202
+ cancelDispose(t) {
203
+ return typeof t == "function" ? this.cancelDisposeFun(t) : this.cancelDisposeObj(t);
204
+ }
205
+ /**
206
+ * 自己的销毁方法
207
+ * @remarks
208
+ * 子类根据需要进行重载
209
+ */
210
+ destroyThis() {
211
+ }
212
+ /**
213
+ * 同步销毁
214
+ * @remarks
215
+ * 会逆序依次同步调用所有销毁函数,并不会等待上一个销毁函数异步执行完成
216
+ */
217
+ destroySync() {
218
+ if (!this.canDestroy) return this.isDestroyed;
219
+ let t = this._destroyers.length;
220
+ for (; --t >= 0; ) {
221
+ const r = this._destroyers[t];
222
+ try {
223
+ r();
224
+ } catch (n) {
225
+ console.error("销毁函数在同步销毁时出错", this, r, n);
226
+ }
227
+ }
228
+ this._destroyers.length = 0;
229
+ try {
230
+ this.destroyThis();
231
+ } catch (r) {
232
+ console.error("destroyThis 在异步销毁时出错", this, r);
233
+ }
234
+ return o(this), !0;
235
+ }
236
+ /**
237
+ * 串行销毁
238
+ * @remarks
239
+ * 会逆序依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
240
+ * @returns 如果所有的销毁函数都是同步的,那么返回 true;如果有任何一个销毁函数是异步的,那么返回一个 Promise 对象
241
+ */
242
+ destroy() {
243
+ if (!this.canDestroy) return this.isDestroyed;
244
+ const t = this._destroyers.toReversed();
245
+ t.push(this.destroyThis), this._destroyers.length = 0;
246
+ const r = h(t, this, null, (n, y) => {
247
+ console.error("销毁函数在串行销毁时出错", this, y, n);
248
+ });
249
+ return r instanceof Promise ? r.then(() => (o(this), !0)) : (o(this), !0);
250
+ }
251
+ }
252
+ return e;
253
+ }
254
+ class g extends c {
255
+ /**
256
+ * 引用计数
257
+ * @remarks
258
+ * 引用计数为 0 时,对象才会被销毁
259
+ */
260
+ refCount = 0;
261
+ /**
262
+ * 是否已经销毁
263
+ */
264
+ get isDestroyed() {
265
+ return this._isDestroyed;
266
+ }
267
+ _isDestroyed = !1;
268
+ /**
269
+ * 是否可以销毁
270
+ */
271
+ get canDestroy() {
272
+ return !this.isDestroyed && this.refCount <= 0;
273
+ }
274
+ /**
275
+ * 销毁者
276
+ */
277
+ _destroyers = [];
278
+ /**
279
+ * 添加销毁者
280
+ * @param fun
281
+ * @returns 返回销毁者的顺序
282
+ */
283
+ disposeFun(e) {
284
+ return this._destroyers.push(e), e;
285
+ }
286
+ /**
287
+ * 取消销毁者函数
288
+ * @param fun
289
+ * @returns
290
+ */
291
+ cancelDisposeFun(e) {
292
+ const s = this._destroyers.indexOf(e);
293
+ return this._destroyers.splice(s, 1), e;
294
+ }
295
+ /**
296
+ * 添加销毁对象
297
+ * @param obj
298
+ * @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
299
+ * @returns
300
+ */
301
+ disposeObj(e, s) {
302
+ const t = s ? function() {
303
+ return e.destroySync();
304
+ } : function() {
305
+ return e.destroy();
306
+ };
307
+ return this.disposeFun(t), e.__destroyable_destroyer = t, e;
308
+ }
309
+ /**
310
+ * 取消销毁者函数
311
+ * @param fun
312
+ * @returns
313
+ */
314
+ cancelDisposeObj(e) {
315
+ const s = e.__destroyable_destroyer;
316
+ return s && this.cancelDisposeFun(s), e;
317
+ }
318
+ dispose(e, s) {
319
+ return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, s);
320
+ }
321
+ cancelDispose(e) {
322
+ return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
323
+ }
324
+ /**
325
+ * 自己的销毁方法
326
+ * @remarks
327
+ * 子类根据需要进行重载
328
+ */
329
+ destroyThis() {
330
+ }
331
+ /**
332
+ * 同步销毁
333
+ * @remarks
334
+ * 会逆序依次同步调用所有销毁函数,并不会等待上一个销毁函数异步执行完成
335
+ */
336
+ destroySync() {
337
+ if (!this.canDestroy) return this.isDestroyed;
338
+ let e = this._destroyers.length;
339
+ for (; --e >= 0; ) {
340
+ const s = this._destroyers[e];
341
+ try {
342
+ s();
343
+ } catch (t) {
344
+ console.error("销毁函数在同步销毁时出错", this, s, t);
345
+ }
346
+ }
347
+ this._destroyers.length = 0;
348
+ try {
349
+ this.destroyThis();
350
+ } catch (s) {
351
+ console.error("destroyThis 在异步销毁时出错", this, s);
352
+ }
353
+ return o(this), !0;
354
+ }
355
+ /**
356
+ * 串行销毁
357
+ * @remarks
358
+ * 会逆序依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
359
+ * @returns 如果所有的销毁函数都是同步的,那么返回 true;如果有任何一个销毁函数是异步的,那么返回一个 Promise 对象
360
+ */
361
+ destroy() {
362
+ if (!this.canDestroy) return this.isDestroyed;
363
+ const e = this._destroyers.toReversed();
364
+ e.push(this.destroyThis), this._destroyers.length = 0;
365
+ const s = h(e, this, null, (t, r) => {
366
+ console.error("销毁函数在串行销毁时出错", this, r, t);
367
+ });
368
+ return s instanceof Promise ? s.then(() => (o(this), !0)) : (o(this), !0);
369
+ }
370
+ }
371
+ class T extends u {
372
+ /**
373
+ * 引用计数
374
+ * @remarks
375
+ * 引用计数为 0 时,对象才会被销毁
376
+ */
377
+ refCount = 0;
378
+ /**
379
+ * 是否已经销毁
380
+ */
381
+ get isDestroyed() {
382
+ return this._isDestroyed;
383
+ }
384
+ _isDestroyed = !1;
385
+ /**
386
+ * 是否可以销毁
387
+ */
388
+ get canDestroy() {
389
+ return !this.isDestroyed && this.refCount <= 0;
390
+ }
391
+ /**
392
+ * 销毁者
393
+ */
394
+ _destroyers = [];
395
+ /**
396
+ * 添加销毁者
397
+ * @param fun
398
+ * @returns 返回销毁者的顺序
399
+ */
400
+ disposeFun(e) {
401
+ return this._destroyers.push(e), e;
402
+ }
403
+ /**
404
+ * 取消销毁者函数
405
+ * @param fun
406
+ * @returns
407
+ */
408
+ cancelDisposeFun(e) {
409
+ const s = this._destroyers.indexOf(e);
410
+ return this._destroyers.splice(s, 1), e;
411
+ }
412
+ /**
413
+ * 添加销毁对象
414
+ * @param obj
415
+ * @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
416
+ * @returns
417
+ */
418
+ disposeObj(e, s) {
419
+ const t = s ? function() {
420
+ return e.destroySync();
421
+ } : function() {
422
+ return e.destroy();
423
+ };
424
+ return this.disposeFun(t), e.__destroyable_destroyer = t, e;
425
+ }
426
+ /**
427
+ * 取消销毁者函数
428
+ * @param fun
429
+ * @returns
430
+ */
431
+ cancelDisposeObj(e) {
432
+ const s = e.__destroyable_destroyer;
433
+ return s && this.cancelDisposeFun(s), e;
434
+ }
435
+ dispose(e, s) {
436
+ return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, s);
437
+ }
438
+ cancelDispose(e) {
439
+ return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
440
+ }
441
+ /**
442
+ * 自己的销毁方法
443
+ * @remarks
444
+ * 子类根据需要进行重载
445
+ */
446
+ destroyThis() {
447
+ }
448
+ /**
449
+ * 同步销毁
450
+ * @remarks
451
+ * 会逆序依次同步调用所有销毁函数,并不会等待上一个销毁函数异步执行完成
452
+ */
453
+ destroySync() {
454
+ if (!this.canDestroy) return this.isDestroyed;
455
+ let e = this._destroyers.length;
456
+ for (; --e >= 0; ) {
457
+ const s = this._destroyers[e];
458
+ try {
459
+ s();
460
+ } catch (t) {
461
+ console.error("销毁函数在同步销毁时出错", this, s, t);
462
+ }
463
+ }
464
+ this._destroyers.length = 0;
465
+ try {
466
+ this.destroyThis();
467
+ } catch (s) {
468
+ console.error("destroyThis 在异步销毁时出错", this, s);
469
+ }
470
+ return o(this), !0;
471
+ }
472
+ /**
473
+ * 串行销毁
474
+ * @remarks
475
+ * 会逆序依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
476
+ * @returns 如果所有的销毁函数都是同步的,那么返回 true;如果有任何一个销毁函数是异步的,那么返回一个 Promise 对象
477
+ */
478
+ destroy() {
479
+ if (!this.canDestroy) return this.isDestroyed;
480
+ const e = this._destroyers.toReversed();
481
+ e.push(this.destroyThis), this._destroyers.length = 0;
482
+ const s = h(e, this, null, (t, r) => {
483
+ console.error("销毁函数在串行销毁时出错", this, r, t);
484
+ });
485
+ return s instanceof Promise ? s.then(() => (o(this), !0)) : (o(this), !0);
120
486
  }
121
487
  }
122
488
  export {
123
- p as Destroyable,
124
- u as DestroyableEventEmitter,
125
- D as DestroyableEventTarget,
126
- f as createDestroyableSubClass,
127
- r as destroyDestroyable,
489
+ l as Destroyable,
490
+ T as DestroyableEventEmitter,
491
+ g as DestroyableEventTarget,
492
+ D as createDestroyableSubClass,
493
+ o as destroyDestroyable,
128
494
  _ as destroyObject,
129
- m as throwOnDestroyed
495
+ d as throwOnDestroyed
130
496
  };
package/package.json CHANGED
@@ -1,16 +1,11 @@
1
1
  {
2
2
  "name": "@gby/destroyable",
3
- "version": "3.6.3",
3
+ "version": "3.7.0",
4
4
  "description": "@gby/destroyable 提供了用于构建可销毁对象的类的基类,用于使得地管理可销毁对象的生命周期",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
8
  "exports": {
9
- "browser": {
10
- "require": "./dist/index-web.cjs",
11
- "types": "./dist/index-web.d.ts",
12
- "import": "./dist/index-web.js"
13
- },
14
9
  "require": "./dist/index.cjs",
15
10
  "types": "./dist/index.d.ts",
16
11
  "import": "./dist/index.js"
@@ -1,377 +0,0 @@
1
- import { serialCallAllFuns as h } from "com-tools";
2
- import { EventBus as u } from "@gby/event-bus";
3
- class c {
4
- /**
5
- * 引用计数
6
- * @remarks
7
- * 引用计数为 0 时,对象才会被销毁
8
- */
9
- refCount = 0;
10
- /**
11
- * 是否已经销毁
12
- */
13
- get isDestroyed() {
14
- return this._isDestroyed;
15
- }
16
- _isDestroyed = !1;
17
- /**
18
- * 是否可以销毁
19
- */
20
- get canDestroy() {
21
- return !this.isDestroyed && this.refCount <= 0;
22
- }
23
- /**
24
- * 销毁者
25
- */
26
- _destroyers = [];
27
- /**
28
- * 添加销毁者
29
- * @param fun
30
- * @returns 返回销毁者的顺序
31
- */
32
- disposeFun(e) {
33
- return this._destroyers.push(e), e;
34
- }
35
- /**
36
- * 取消销毁者函数
37
- * @param fun
38
- * @returns
39
- */
40
- cancelDisposeFun(e) {
41
- const t = this._destroyers.indexOf(e);
42
- return this._destroyers.splice(t, 1), e;
43
- }
44
- /**
45
- * 添加销毁对象
46
- * @param obj
47
- * @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
48
- * @returns
49
- */
50
- disposeObj(e, t) {
51
- const s = t ? function() {
52
- return e.destroySync();
53
- } : function() {
54
- return e.destroy();
55
- };
56
- return this.disposeFun(s), e.__destroyable_destroyer = s, e;
57
- }
58
- /**
59
- * 取消销毁者函数
60
- * @param fun
61
- * @returns
62
- */
63
- cancelDisposeObj(e) {
64
- const t = e.__destroyable_destroyer;
65
- return t && this.cancelDisposeFun(t), e;
66
- }
67
- dispose(e, t) {
68
- return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, t);
69
- }
70
- cancelDispose(e) {
71
- return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
72
- }
73
- /**
74
- * 自己的销毁方法
75
- * @remarks
76
- * 子类根据需要进行重载
77
- */
78
- destroyThis() {
79
- }
80
- /**
81
- * 同步销毁
82
- * @remarks
83
- * 会逆序依次同步调用所有销毁函数,并不会等待上一个销毁函数异步执行完成
84
- */
85
- destroySync() {
86
- if (!this.canDestroy) return this.isDestroyed;
87
- let e = this._destroyers.length;
88
- for (; --e >= 0; ) {
89
- const t = this._destroyers[e];
90
- try {
91
- t();
92
- } catch (s) {
93
- console.error("销毁函数在同步销毁时出错", this, t, s);
94
- }
95
- }
96
- this._destroyers.length = 0;
97
- try {
98
- this.destroyThis();
99
- } catch (t) {
100
- console.error("destroyThis 在异步销毁时出错", this, t);
101
- }
102
- return o(this), !0;
103
- }
104
- /**
105
- * 串行销毁
106
- * @remarks
107
- * 会逆序依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
108
- * @returns 如果所有的销毁函数都是同步的,那么返回 true;如果有任何一个销毁函数是异步的,那么返回一个 Promise 对象
109
- */
110
- destroy() {
111
- if (!this.canDestroy) return this.isDestroyed;
112
- const e = this._destroyers.toReversed();
113
- e.push(this.destroyThis), this._destroyers.length = 0;
114
- const t = h(e, this, null, (s, r) => {
115
- console.error("销毁函数在串行销毁时出错", this, r, s);
116
- });
117
- return t instanceof Promise ? t.then(() => (o(this), !0)) : (o(this), !0);
118
- }
119
- }
120
- function d() {
121
- throw "已销毁";
122
- }
123
- function f(i) {
124
- for (var e in i)
125
- typeof i[e] == "function" && (i[e] = d);
126
- return i._isDestroyed = !0, !0;
127
- }
128
- function o(i) {
129
- for (var e in i)
130
- !(e in c.prototype) && typeof i[e] == "function" && (i[e] = d);
131
- return i._isDestroyed = !0, !0;
132
- }
133
- function p(i) {
134
- class e extends i {
135
- /**
136
- * 引用计数
137
- * @remarks
138
- * 引用计数为 0 时,对象才会被销毁
139
- */
140
- refCount = 0;
141
- /**
142
- * 是否已经销毁
143
- */
144
- get isDestroyed() {
145
- return this._isDestroyed;
146
- }
147
- _isDestroyed = !1;
148
- /**
149
- * 是否可以销毁
150
- */
151
- get canDestroy() {
152
- return !this.isDestroyed && this.refCount <= 0;
153
- }
154
- /**
155
- * 销毁者
156
- */
157
- _destroyers = [];
158
- /**
159
- * 添加销毁者
160
- * @param fun
161
- * @returns 返回销毁者的顺序
162
- */
163
- disposeFun(s) {
164
- return this._destroyers.push(s), s;
165
- }
166
- /**
167
- * 取消销毁者函数
168
- * @param fun
169
- * @returns
170
- */
171
- cancelDisposeFun(s) {
172
- const r = this._destroyers.indexOf(s);
173
- return this._destroyers.splice(r, 1), s;
174
- }
175
- /**
176
- * 添加销毁对象
177
- * @param obj
178
- * @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
179
- * @returns
180
- */
181
- disposeObj(s, r) {
182
- const n = r ? function() {
183
- return s.destroySync();
184
- } : function() {
185
- return s.destroy();
186
- };
187
- return this.disposeFun(n), s.__destroyable_destroyer = n, s;
188
- }
189
- /**
190
- * 取消销毁者函数
191
- * @param fun
192
- * @returns
193
- */
194
- cancelDisposeObj(s) {
195
- const r = s.__destroyable_destroyer;
196
- return r && this.cancelDisposeFun(r), s;
197
- }
198
- dispose(s, r) {
199
- return typeof s == "function" ? this.disposeFun(s) : this.disposeObj(s, r);
200
- }
201
- cancelDispose(s) {
202
- return typeof s == "function" ? this.cancelDisposeFun(s) : this.cancelDisposeObj(s);
203
- }
204
- /**
205
- * 自己的销毁方法
206
- * @remarks
207
- * 子类根据需要进行重载
208
- */
209
- destroyThis() {
210
- }
211
- /**
212
- * 同步销毁
213
- * @remarks
214
- * 会逆序依次同步调用所有销毁函数,并不会等待上一个销毁函数异步执行完成
215
- */
216
- destroySync() {
217
- if (!this.canDestroy) return this.isDestroyed;
218
- let s = this._destroyers.length;
219
- for (; --s >= 0; ) {
220
- const r = this._destroyers[s];
221
- try {
222
- r();
223
- } catch (n) {
224
- console.error("销毁函数在同步销毁时出错", this, r, n);
225
- }
226
- }
227
- this._destroyers.length = 0;
228
- try {
229
- this.destroyThis();
230
- } catch (r) {
231
- console.error("destroyThis 在异步销毁时出错", this, r);
232
- }
233
- return o(this), !0;
234
- }
235
- /**
236
- * 串行销毁
237
- * @remarks
238
- * 会逆序依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
239
- * @returns 如果所有的销毁函数都是同步的,那么返回 true;如果有任何一个销毁函数是异步的,那么返回一个 Promise 对象
240
- */
241
- destroy() {
242
- if (!this.canDestroy) return this.isDestroyed;
243
- const s = this._destroyers.toReversed();
244
- s.push(this.destroyThis), this._destroyers.length = 0;
245
- const r = h(s, this, null, (n, y) => {
246
- console.error("销毁函数在串行销毁时出错", this, y, n);
247
- });
248
- return r instanceof Promise ? r.then(() => (o(this), !0)) : (o(this), !0);
249
- }
250
- }
251
- return e;
252
- }
253
- class D extends u {
254
- /**
255
- * 引用计数
256
- * @remarks
257
- * 引用计数为 0 时,对象才会被销毁
258
- */
259
- refCount = 0;
260
- /**
261
- * 是否已经销毁
262
- */
263
- get isDestroyed() {
264
- return this._isDestroyed;
265
- }
266
- _isDestroyed = !1;
267
- /**
268
- * 是否可以销毁
269
- */
270
- get canDestroy() {
271
- return !this.isDestroyed && this.refCount <= 0;
272
- }
273
- /**
274
- * 销毁者
275
- */
276
- _destroyers = [];
277
- /**
278
- * 添加销毁者
279
- * @param fun
280
- * @returns 返回销毁者的顺序
281
- */
282
- disposeFun(e) {
283
- return this._destroyers.push(e), e;
284
- }
285
- /**
286
- * 取消销毁者函数
287
- * @param fun
288
- * @returns
289
- */
290
- cancelDisposeFun(e) {
291
- const t = this._destroyers.indexOf(e);
292
- return this._destroyers.splice(t, 1), e;
293
- }
294
- /**
295
- * 添加销毁对象
296
- * @param obj
297
- * @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
298
- * @returns
299
- */
300
- disposeObj(e, t) {
301
- const s = t ? function() {
302
- return e.destroySync();
303
- } : function() {
304
- return e.destroy();
305
- };
306
- return this.disposeFun(s), e.__destroyable_destroyer = s, e;
307
- }
308
- /**
309
- * 取消销毁者函数
310
- * @param fun
311
- * @returns
312
- */
313
- cancelDisposeObj(e) {
314
- const t = e.__destroyable_destroyer;
315
- return t && this.cancelDisposeFun(t), e;
316
- }
317
- dispose(e, t) {
318
- return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, t);
319
- }
320
- cancelDispose(e) {
321
- return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
322
- }
323
- /**
324
- * 自己的销毁方法
325
- * @remarks
326
- * 子类根据需要进行重载
327
- */
328
- destroyThis() {
329
- }
330
- /**
331
- * 同步销毁
332
- * @remarks
333
- * 会逆序依次同步调用所有销毁函数,并不会等待上一个销毁函数异步执行完成
334
- */
335
- destroySync() {
336
- if (!this.canDestroy) return this.isDestroyed;
337
- let e = this._destroyers.length;
338
- for (; --e >= 0; ) {
339
- const t = this._destroyers[e];
340
- try {
341
- t();
342
- } catch (s) {
343
- console.error("销毁函数在同步销毁时出错", this, t, s);
344
- }
345
- }
346
- this._destroyers.length = 0;
347
- try {
348
- this.destroyThis();
349
- } catch (t) {
350
- console.error("destroyThis 在异步销毁时出错", this, t);
351
- }
352
- return o(this), !0;
353
- }
354
- /**
355
- * 串行销毁
356
- * @remarks
357
- * 会逆序依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
358
- * @returns 如果所有的销毁函数都是同步的,那么返回 true;如果有任何一个销毁函数是异步的,那么返回一个 Promise 对象
359
- */
360
- destroy() {
361
- if (!this.canDestroy) return this.isDestroyed;
362
- const e = this._destroyers.toReversed();
363
- e.push(this.destroyThis), this._destroyers.length = 0;
364
- const t = h(e, this, null, (s, r) => {
365
- console.error("销毁函数在串行销毁时出错", this, r, s);
366
- });
367
- return t instanceof Promise ? t.then(() => (o(this), !0)) : (o(this), !0);
368
- }
369
- }
370
- export {
371
- c as D,
372
- D as a,
373
- f as b,
374
- p as c,
375
- o as d,
376
- d as t
377
- };
@@ -1 +0,0 @@
1
- "use strict";const h=require("com-tools"),c=require("@gby/event-bus");class d{refCount=0;get isDestroyed(){return this._isDestroyed}_isDestroyed=!1;get canDestroy(){return!this.isDestroyed&&this.refCount<=0}_destroyers=[];disposeFun(e){return this._destroyers.push(e),e}cancelDisposeFun(e){const t=this._destroyers.indexOf(e);return this._destroyers.splice(t,1),e}disposeObj(e,t){const s=t?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(s),e.__destroyable_destroyer=s,e}cancelDisposeObj(e){const t=e.__destroyable_destroyer;return t&&this.cancelDisposeFun(t),e}dispose(e,t){return typeof e=="function"?this.disposeFun(e):this.disposeObj(e,t)}cancelDispose(e){return typeof e=="function"?this.cancelDisposeFun(e):this.cancelDisposeObj(e)}destroyThis(){}destroySync(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const t=this._destroyers[e];try{t()}catch(s){console.error("销毁函数在同步销毁时出错",this,t,s)}}this._destroyers.length=0;try{this.destroyThis()}catch(t){console.error("destroyThis 在异步销毁时出错",this,t)}return o(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;const e=this._destroyers.toReversed();e.push(this.destroyThis),this._destroyers.length=0;const t=h.serialCallAllFuns(e,this,null,(s,r)=>{console.error("销毁函数在串行销毁时出错",this,r,s)});return t instanceof Promise?t.then(()=>(o(this),!0)):(o(this),!0)}}function y(){throw"已销毁"}function l(i){for(var e in i)typeof i[e]=="function"&&(i[e]=y);return i._isDestroyed=!0,!0}function o(i){for(var e in i)!(e in d.prototype)&&typeof i[e]=="function"&&(i[e]=y);return i._isDestroyed=!0,!0}function a(i){class e extends i{refCount=0;get isDestroyed(){return this._isDestroyed}_isDestroyed=!1;get canDestroy(){return!this.isDestroyed&&this.refCount<=0}_destroyers=[];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 n=r?function(){return s.destroySync()}:function(){return s.destroy()};return this.disposeFun(n),s.__destroyable_destroyer=n,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(){}destroySync(){if(!this.canDestroy)return this.isDestroyed;let s=this._destroyers.length;for(;--s>=0;){const r=this._destroyers[s];try{r()}catch(n){console.error("销毁函数在同步销毁时出错",this,r,n)}}this._destroyers.length=0;try{this.destroyThis()}catch(r){console.error("destroyThis 在异步销毁时出错",this,r)}return o(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;const s=this._destroyers.toReversed();s.push(this.destroyThis),this._destroyers.length=0;const r=h.serialCallAllFuns(s,this,null,(n,u)=>{console.error("销毁函数在串行销毁时出错",this,u,n)});return r instanceof Promise?r.then(()=>(o(this),!0)):(o(this),!0)}}return e}class f extends c.EventBus{refCount=0;get isDestroyed(){return this._isDestroyed}_isDestroyed=!1;get canDestroy(){return!this.isDestroyed&&this.refCount<=0}_destroyers=[];disposeFun(e){return this._destroyers.push(e),e}cancelDisposeFun(e){const t=this._destroyers.indexOf(e);return this._destroyers.splice(t,1),e}disposeObj(e,t){const s=t?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(s),e.__destroyable_destroyer=s,e}cancelDisposeObj(e){const t=e.__destroyable_destroyer;return t&&this.cancelDisposeFun(t),e}dispose(e,t){return typeof e=="function"?this.disposeFun(e):this.disposeObj(e,t)}cancelDispose(e){return typeof e=="function"?this.cancelDisposeFun(e):this.cancelDisposeObj(e)}destroyThis(){}destroySync(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const t=this._destroyers[e];try{t()}catch(s){console.error("销毁函数在同步销毁时出错",this,t,s)}}this._destroyers.length=0;try{this.destroyThis()}catch(t){console.error("destroyThis 在异步销毁时出错",this,t)}return o(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;const e=this._destroyers.toReversed();e.push(this.destroyThis),this._destroyers.length=0;const t=h.serialCallAllFuns(e,this,null,(s,r)=>{console.error("销毁函数在串行销毁时出错",this,r,s)});return t instanceof Promise?t.then(()=>(o(this),!0)):(o(this),!0)}}exports.Destroyable=d;exports.DestroyableEventTarget=f;exports.createDestroyableSubClass=a;exports.destroyDestroyable=o;exports.destroyObject=l;exports.throwOnDestroyed=y;
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./DestroyableEventTarget-uZtYhcb1.cjs");exports.Destroyable=e.Destroyable;exports.DestroyableEventTarget=e.DestroyableEventTarget;exports.createDestroyableSubClass=e.createDestroyableSubClass;exports.destroyDestroyable=e.destroyDestroyable;exports.destroyObject=e.destroyObject;exports.throwOnDestroyed=e.throwOnDestroyed;
package/dist/index-web.js DELETED
@@ -1,9 +0,0 @@
1
- import { D as a, a as t, c as r, d as o, b, t as y } from "./DestroyableEventTarget-CIMBV0bD.js";
2
- export {
3
- a as Destroyable,
4
- t as DestroyableEventTarget,
5
- r as createDestroyableSubClass,
6
- o as destroyDestroyable,
7
- b as destroyObject,
8
- y as throwOnDestroyed
9
- };