@gby/destroyable 3.9.0 → 5.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 @@
1
+ let e=require(`com-tools`),t=require(`@gby/event`);var n=class extends t.CountEventTrigger{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){let t=this._destroyers.indexOf(e);return this._destroyers.splice(t,1),e}disposeObj(e,t){let n=t?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(n),e.__destroyable_destroyer=n,e}cancelDisposeObj(e){let 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;){let t=this._destroyers[e];try{t()}catch(e){console.error(`销毁函数在同步销毁时出错`,this,t,e)}}this._destroyers.length=0;try{this.destroyThis()}catch(e){console.error(`destroyThis 在异步销毁时出错`,this,e)}return a(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;let t=this._destroyers.toReversed();t.push(this.destroyThis),this._destroyers.length=0;let n=(0,e.serialCallAllFuns)(t,this,null,(e,t)=>{console.error(`销毁函数在串行销毁时出错`,this,t,e)});return n instanceof Promise?n.then(()=>(a(this),!0)):(a(this),!0)}};function r(){throw`已销毁`}function i(e){for(var t in e)typeof e[t]==`function`&&(e[t]=r);return e._isDestroyed=!0,!0}function a(e){for(var t in e)!(t in n.prototype)&&typeof e[t]==`function`&&(e[t]=r);return e._isDestroyed=!0,!0}Object.defineProperty(exports,`i`,{enumerable:!0,get:function(){return r}}),Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return a}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return i}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return n}});
@@ -0,0 +1,81 @@
1
+ import { serialCallAllFuns as e } from "com-tools";
2
+ import { CountEventTrigger as t } from "@gby/event";
3
+ //#region src/Destroyable.ts
4
+ var n = class extends t {
5
+ refCount = 0;
6
+ get isDestroyed() {
7
+ return this._isDestroyed;
8
+ }
9
+ _isDestroyed = !1;
10
+ get canDestroy() {
11
+ return !this.isDestroyed && this.refCount <= 0;
12
+ }
13
+ _destroyers = [];
14
+ disposeFun(e) {
15
+ return this._destroyers.push(e), e;
16
+ }
17
+ cancelDisposeFun(e) {
18
+ let t = this._destroyers.indexOf(e);
19
+ return this._destroyers.splice(t, 1), e;
20
+ }
21
+ disposeObj(e, t) {
22
+ let n = t ? function() {
23
+ return e.destroySync();
24
+ } : function() {
25
+ return e.destroy();
26
+ };
27
+ return this.disposeFun(n), e.__destroyable_destroyer = n, e;
28
+ }
29
+ cancelDisposeObj(e) {
30
+ let t = e.__destroyable_destroyer;
31
+ return t && this.cancelDisposeFun(t), e;
32
+ }
33
+ dispose(e, t) {
34
+ return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, t);
35
+ }
36
+ cancelDispose(e) {
37
+ return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
38
+ }
39
+ destroyThis() {}
40
+ destroySync() {
41
+ if (!this.canDestroy) return this.isDestroyed;
42
+ let e = this._destroyers.length;
43
+ for (; --e >= 0;) {
44
+ let t = this._destroyers[e];
45
+ try {
46
+ t();
47
+ } catch (e) {
48
+ console.error("销毁函数在同步销毁时出错", this, t, e);
49
+ }
50
+ }
51
+ this._destroyers.length = 0;
52
+ try {
53
+ this.destroyThis();
54
+ } catch (e) {
55
+ console.error("destroyThis 在异步销毁时出错", this, e);
56
+ }
57
+ return a(this), !0;
58
+ }
59
+ destroy() {
60
+ if (!this.canDestroy) return this.isDestroyed;
61
+ let t = this._destroyers.toReversed();
62
+ t.push(this.destroyThis), this._destroyers.length = 0;
63
+ let n = e(t, this, null, (e, t) => {
64
+ console.error("销毁函数在串行销毁时出错", this, t, e);
65
+ });
66
+ return n instanceof Promise ? n.then(() => (a(this), !0)) : (a(this), !0);
67
+ }
68
+ };
69
+ function r() {
70
+ throw "已销毁";
71
+ }
72
+ function i(e) {
73
+ for (var t in e) typeof e[t] == "function" && (e[t] = r);
74
+ return e._isDestroyed = !0, !0;
75
+ }
76
+ function a(e) {
77
+ for (var t in e) !(t in n.prototype) && typeof e[t] == "function" && (e[t] = r);
78
+ return e._isDestroyed = !0, !0;
79
+ }
80
+ //#endregion
81
+ export { r as i, a as n, i as r, n as t };
@@ -1,8 +1,11 @@
1
1
  import type { FunDestroyer, IDestroyable } from "./type";
2
+ import type { EventMap } from "@gby/event";
3
+ import { CountEventTrigger } from "@gby/event";
4
+ export type { EventMap };
2
5
  /**
3
6
  * 可销毁的对象,不继承任何事件系统
4
7
  */
5
- export declare class Destroyable implements IDestroyable {
8
+ export declare class Destroyable<M extends EventMap> extends CountEventTrigger<M> implements IDestroyable {
6
9
  /**
7
10
  * 引用计数
8
11
  * @remarks
@@ -1 +1 @@
1
- {"version":3,"file":"Destroyable.d.ts","sourceRoot":"","sources":["../src/Destroyable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAMzD;;GAEG;AACH,qBAAa,WAAY,YAAW,YAAY;IAE5C;;;;OAIG;IACH,QAAQ,SAAK;IAEb;;OAEG;IACH,IAAI,WAAW,YAEd;IACD,YAAY,UAAS;IAErB;;OAEG;IACH,IAAI,UAAU,YAEb;IAED;;OAEG;IACH,WAAW,EAAE,YAAY,EAAE,CAAM;IAEjC;;;;OAIG;IACH,UAAU,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAK7C;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAMnD;;;;;OAKG;IACH,UAAU,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,CAAC;IAQ7D;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IASnD;;;OAGG;IACH,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAC1C,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,EAAE,YAAY,CAAC,EAAE,OAAO,GAAG,CAAC;IAClE,OAAO,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,EAAE,YAAY,CAAC,EAAE,OAAO,GAAG,CAAC;IAStF,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAChD,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAChD,aAAa,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC;IAapE;;;;OAIG;IACH,WAAW,IAAI,GAAG;IAKlB;;;;OAIG;IACH,WAAW;IA6BX;;;;;OAKG;IACH,OAAO;CAuBV;AAKD;;GAEG;AACH,wBAAgB,gBAAgB,SAE/B;AAKD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,GAAG,WASxC;AAKD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,YAAY,WAYtD"}
1
+ {"version":3,"file":"Destroyable.d.ts","sourceRoot":"","sources":["../src/Destroyable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAEzD,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAA;AACxC,OAAO,EAAC,iBAAiB,EAAC,MAAM,YAAY,CAAA;AAE5C,YAAY,EAAC,QAAQ,EAAC,CAAA;AAItB;;GAEG;AACH,qBAAa,WAAW,CAAC,CAAC,SAAS,QAAQ,CAAG,SAAQ,iBAAiB,CAAC,CAAC,CAAE,YAAW,YAAY;IAE9F;;;;OAIG;IACH,QAAQ,SAAK;IAEb;;OAEG;IACH,IAAI,WAAW,YAEd;IACD,YAAY,UAAS;IAErB;;OAEG;IACH,IAAI,UAAU,YAEb;IAED;;OAEG;IACH,WAAW,EAAE,YAAY,EAAE,CAAM;IAEjC;;;;OAIG;IACH,UAAU,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAK7C;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAMnD;;;;;OAKG;IACH,UAAU,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,CAAC;IAQ7D;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IASnD;;;OAGG;IACH,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAC1C,OAAO,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,EAAE,YAAY,CAAC,EAAE,OAAO,GAAG,CAAC;IAClE,OAAO,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,EAAE,YAAY,CAAC,EAAE,OAAO,GAAG,CAAC;IAStF,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAChD,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAChD,aAAa,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC;IAapE;;;;OAIG;IACH,WAAW,IAAI,GAAG;IAKlB;;;;OAIG;IACH,WAAW;IA6BX;;;;;OAKG;IACH,OAAO;CAuBV;AAKD;;GAEG;AACH,wBAAgB,gBAAgB,SAE/B;AAKD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,GAAG,WASxC;AAKD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,YAAY,WAYtD"}
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./createDestroyableSubClass-CHkLsrwP.cjs"),t=require("./DestroyableEventEmitter-OgjYk1aA.cjs");exports.Destroyable=e.Destroyable;exports.createDestroyableSubClass=e.createDestroyableSubClass;exports.destroyDestroyable=e.destroyDestroyable;exports.destroyObject=e.destroyObject;exports.throwOnDestroyed=e.throwOnDestroyed;exports.DestroyableEventEmitter=t.DestroyableEventEmitter;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./Destroyable-C3et1ngw.cjs`);let t=require(`com-tools`),n=require(`node:events`);var r=class extends n.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){let t=this._destroyers.indexOf(e);return this._destroyers.splice(t,1),e}disposeObj(e,t){let n=t?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(n),e.__destroyable_destroyer=n,e}cancelDisposeObj(e){let 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 t=this._destroyers.length;for(;--t>=0;){let e=this._destroyers[t];try{e()}catch(t){console.error(`销毁函数在同步销毁时出错`,this,e,t)}}this._destroyers.length=0;try{this.destroyThis()}catch(e){console.error(`destroyThis 在异步销毁时出错`,this,e)}return e.n(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;let n=this._destroyers.toReversed();n.push(this.destroyThis),this._destroyers.length=0;let r=(0,t.serialCallAllFuns)(n,this,null,(e,t)=>{console.error(`销毁函数在串行销毁时出错`,this,t,e)});return r instanceof Promise?r.then(()=>(e.n(this),!0)):(e.n(this),!0)}};exports.DestroyableEventEmitter=r;
@@ -1,13 +1,2 @@
1
- /**
2
- * 可销毁的
3
- *
4
- * @remarks
5
- * @gby/destroyable 提供了用于构建可销毁对象的类的基类,用于使得地管理可销毁对象的生命周期
6
- *
7
- * @packageDocumentation
8
- */
9
- export * from './type';
10
- export * from './Destroyable';
11
- export * from "./createDestroyableSubClass";
12
1
  export * from "./DestroyableEventEmitter";
13
2
  //# sourceMappingURL=index-node.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index-node.d.ts","sourceRoot":"","sources":["../src/index-node.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,QAAQ,CAAC;AACvB,cAAc,eAAe,CAAC;AAC9B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index-node.d.ts","sourceRoot":"","sources":["../src/index-node.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC"}
@@ -1,10 +1,71 @@
1
- import { D as s, c as r, d as a, a as o, t as y } from "./createDestroyableSubClass-BGwNfZdZ.js";
2
- import { D as b } from "./DestroyableEventEmitter-DMAh3-8-.js";
3
- export {
4
- s as Destroyable,
5
- b as DestroyableEventEmitter,
6
- r as createDestroyableSubClass,
7
- a as destroyDestroyable,
8
- o as destroyObject,
9
- y as throwOnDestroyed
1
+ import { n as e } from "./Destroyable-mrBsNabq.js";
2
+ import { serialCallAllFuns as t } from "com-tools";
3
+ import { EventEmitter as n } from "node:events";
4
+ //#region src/DestroyableEventEmitter.ts
5
+ var r = class extends n {
6
+ refCount = 0;
7
+ get isDestroyed() {
8
+ return this._isDestroyed;
9
+ }
10
+ _isDestroyed = !1;
11
+ get canDestroy() {
12
+ return !this.isDestroyed && this.refCount <= 0;
13
+ }
14
+ _destroyers = [];
15
+ disposeFun(e) {
16
+ return this._destroyers.push(e), e;
17
+ }
18
+ cancelDisposeFun(e) {
19
+ let t = this._destroyers.indexOf(e);
20
+ return this._destroyers.splice(t, 1), e;
21
+ }
22
+ disposeObj(e, t) {
23
+ let n = t ? function() {
24
+ return e.destroySync();
25
+ } : function() {
26
+ return e.destroy();
27
+ };
28
+ return this.disposeFun(n), e.__destroyable_destroyer = n, e;
29
+ }
30
+ cancelDisposeObj(e) {
31
+ let t = e.__destroyable_destroyer;
32
+ return t && this.cancelDisposeFun(t), e;
33
+ }
34
+ dispose(e, t) {
35
+ return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, t);
36
+ }
37
+ cancelDispose(e) {
38
+ return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
39
+ }
40
+ destroyThis() {}
41
+ destroySync() {
42
+ if (!this.canDestroy) return this.isDestroyed;
43
+ let t = this._destroyers.length;
44
+ for (; --t >= 0;) {
45
+ let e = this._destroyers[t];
46
+ try {
47
+ e();
48
+ } catch (t) {
49
+ console.error("销毁函数在同步销毁时出错", this, e, t);
50
+ }
51
+ }
52
+ this._destroyers.length = 0;
53
+ try {
54
+ this.destroyThis();
55
+ } catch (e) {
56
+ console.error("destroyThis 在异步销毁时出错", this, e);
57
+ }
58
+ return e(this), !0;
59
+ }
60
+ destroy() {
61
+ if (!this.canDestroy) return this.isDestroyed;
62
+ let n = this._destroyers.toReversed();
63
+ n.push(this.destroyThis), this._destroyers.length = 0;
64
+ let r = t(n, this, null, (e, t) => {
65
+ console.error("销毁函数在串行销毁时出错", this, t, e);
66
+ });
67
+ return r instanceof Promise ? r.then(() => (e(this), !0)) : (e(this), !0);
68
+ }
10
69
  };
70
+ //#endregion
71
+ export { r as DestroyableEventEmitter };
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./createDestroyableSubClass-CHkLsrwP.cjs"),t=require("./DestroyableEventTarget-3Wyju42G.cjs");exports.Destroyable=e.Destroyable;exports.createDestroyableSubClass=e.createDestroyableSubClass;exports.destroyDestroyable=e.destroyDestroyable;exports.destroyObject=e.destroyObject;exports.throwOnDestroyed=e.throwOnDestroyed;exports.DestroyableEventTarget=t.DestroyableEventTarget;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./Destroyable-C3et1ngw.cjs`);let t=require(`com-tools`),n=require(`@gby/event-bus`);var r=class extends n.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){let t=this._destroyers.indexOf(e);return this._destroyers.splice(t,1),e}disposeObj(e,t){let n=t?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(n),e.__destroyable_destroyer=n,e}cancelDisposeObj(e){let 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 t=this._destroyers.length;for(;--t>=0;){let e=this._destroyers[t];try{e()}catch(t){console.error(`销毁函数在同步销毁时出错`,this,e,t)}}this._destroyers.length=0;try{this.destroyThis()}catch(e){console.error(`destroyThis 在异步销毁时出错`,this,e)}return e.n(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;let n=this._destroyers.toReversed();n.push(this.destroyThis),this._destroyers.length=0;let r=(0,t.serialCallAllFuns)(n,this,null,(e,t)=>{console.error(`销毁函数在串行销毁时出错`,this,t,e)});return r instanceof Promise?r.then(()=>(e.n(this),!0)):(e.n(this),!0)}};exports.DestroyableEventTarget=r;
@@ -1,13 +1,2 @@
1
- /**
2
- * 可销毁的
3
- *
4
- * @remarks
5
- * @gby/destroyable 提供了用于构建可销毁对象的类的基类,用于使得地管理可销毁对象的生命周期
6
- *
7
- * @packageDocumentation
8
- */
9
- export * from './type';
10
- export * from './Destroyable';
11
- export * from "./createDestroyableSubClass";
12
1
  export * from "./DestroyableEventTarget";
13
2
  //# sourceMappingURL=index-web.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index-web.d.ts","sourceRoot":"","sources":["../src/index-web.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,QAAQ,CAAC;AACvB,cAAc,eAAe,CAAC;AAC9B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index-web.d.ts","sourceRoot":"","sources":["../src/index-web.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC"}
package/dist/index-web.js CHANGED
@@ -1,10 +1,71 @@
1
- import { D as t, c as a, d as r, a as o, t as y } from "./createDestroyableSubClass-BGwNfZdZ.js";
2
- import { D as b } from "./DestroyableEventTarget-DxkJCQlH.js";
3
- export {
4
- t as Destroyable,
5
- b as DestroyableEventTarget,
6
- a as createDestroyableSubClass,
7
- r as destroyDestroyable,
8
- o as destroyObject,
9
- y as throwOnDestroyed
1
+ import { n as e } from "./Destroyable-mrBsNabq.js";
2
+ import { serialCallAllFuns as t } from "com-tools";
3
+ import { EventBus as n } from "@gby/event-bus";
4
+ //#region src/DestroyableEventTarget.ts
5
+ var r = class extends n {
6
+ refCount = 0;
7
+ get isDestroyed() {
8
+ return this._isDestroyed;
9
+ }
10
+ _isDestroyed = !1;
11
+ get canDestroy() {
12
+ return !this.isDestroyed && this.refCount <= 0;
13
+ }
14
+ _destroyers = [];
15
+ disposeFun(e) {
16
+ return this._destroyers.push(e), e;
17
+ }
18
+ cancelDisposeFun(e) {
19
+ let t = this._destroyers.indexOf(e);
20
+ return this._destroyers.splice(t, 1), e;
21
+ }
22
+ disposeObj(e, t) {
23
+ let n = t ? function() {
24
+ return e.destroySync();
25
+ } : function() {
26
+ return e.destroy();
27
+ };
28
+ return this.disposeFun(n), e.__destroyable_destroyer = n, e;
29
+ }
30
+ cancelDisposeObj(e) {
31
+ let t = e.__destroyable_destroyer;
32
+ return t && this.cancelDisposeFun(t), e;
33
+ }
34
+ dispose(e, t) {
35
+ return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, t);
36
+ }
37
+ cancelDispose(e) {
38
+ return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
39
+ }
40
+ destroyThis() {}
41
+ destroySync() {
42
+ if (!this.canDestroy) return this.isDestroyed;
43
+ let t = this._destroyers.length;
44
+ for (; --t >= 0;) {
45
+ let e = this._destroyers[t];
46
+ try {
47
+ e();
48
+ } catch (t) {
49
+ console.error("销毁函数在同步销毁时出错", this, e, t);
50
+ }
51
+ }
52
+ this._destroyers.length = 0;
53
+ try {
54
+ this.destroyThis();
55
+ } catch (e) {
56
+ console.error("destroyThis 在异步销毁时出错", this, e);
57
+ }
58
+ return e(this), !0;
59
+ }
60
+ destroy() {
61
+ if (!this.canDestroy) return this.isDestroyed;
62
+ let n = this._destroyers.toReversed();
63
+ n.push(this.destroyThis), this._destroyers.length = 0;
64
+ let r = t(n, this, null, (e, t) => {
65
+ console.error("销毁函数在串行销毁时出错", this, t, e);
66
+ });
67
+ return r instanceof Promise ? r.then(() => (e(this), !0)) : (e(this), !0);
68
+ }
10
69
  };
70
+ //#endregion
71
+ export { r as DestroyableEventTarget };
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./createDestroyableSubClass-CHkLsrwP.cjs"),t=require("./DestroyableEventTarget-3Wyju42G.cjs"),r=require("./DestroyableEventEmitter-OgjYk1aA.cjs");exports.Destroyable=e.Destroyable;exports.createDestroyableSubClass=e.createDestroyableSubClass;exports.destroyDestroyable=e.destroyDestroyable;exports.destroyObject=e.destroyObject;exports.throwOnDestroyed=e.throwOnDestroyed;exports.DestroyableEventTarget=t.DestroyableEventTarget;exports.DestroyableEventEmitter=r.DestroyableEventEmitter;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./Destroyable-C3et1ngw.cjs`);let t=require(`com-tools`);function n(n){class r extends n{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){let t=this._destroyers.indexOf(e);return this._destroyers.splice(t,1),e}disposeObj(e,t){let n=t?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(n),e.__destroyable_destroyer=n,e}cancelDisposeObj(e){let 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 t=this._destroyers.length;for(;--t>=0;){let e=this._destroyers[t];try{e()}catch(t){console.error(`销毁函数在同步销毁时出错`,this,e,t)}}this._destroyers.length=0;try{this.destroyThis()}catch(e){console.error(`destroyThis 在异步销毁时出错`,this,e)}return e.n(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;let n=this._destroyers.toReversed();n.push(this.destroyThis),this._destroyers.length=0;let r=(0,t.serialCallAllFuns)(n,this,null,(e,t)=>{console.error(`销毁函数在串行销毁时出错`,this,t,e)});return r instanceof Promise?r.then(()=>(e.n(this),!0)):(e.n(this),!0)}}return r}exports.Destroyable=e.t,exports.createDestroyableSubClass=n,exports.destroyDestroyable=e.n,exports.destroyObject=e.r,exports.throwOnDestroyed=e.i;
package/dist/index.d.ts CHANGED
@@ -9,6 +9,4 @@
9
9
  export * from "./type";
10
10
  export * from './Destroyable';
11
11
  export * from "./createDestroyableSubClass";
12
- export * from "./DestroyableEventTarget";
13
- export * from "./DestroyableEventEmitter";
14
12
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,QAAQ,CAAC;AACvB,cAAc,eAAe,CAAC;AAC9B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,QAAQ,CAAC;AACvB,cAAc,eAAe,CAAC;AAC9B,cAAc,6BAA6B,CAAC"}
package/dist/index.js CHANGED
@@ -1,12 +1,73 @@
1
- import { D as r, c as s, d as a, a as o, t as D } from "./createDestroyableSubClass-BGwNfZdZ.js";
2
- import { D as b } from "./DestroyableEventTarget-DxkJCQlH.js";
3
- import { D as d } from "./DestroyableEventEmitter-DMAh3-8-.js";
4
- export {
5
- r as Destroyable,
6
- d as DestroyableEventEmitter,
7
- b as DestroyableEventTarget,
8
- s as createDestroyableSubClass,
9
- a as destroyDestroyable,
10
- o as destroyObject,
11
- D as throwOnDestroyed
12
- };
1
+ import { i as e, n as t, r as n, t as r } from "./Destroyable-mrBsNabq.js";
2
+ import { serialCallAllFuns as i } from "com-tools";
3
+ //#region src/createDestroyableSubClass.ts
4
+ function a(e) {
5
+ class n extends e {
6
+ refCount = 0;
7
+ get isDestroyed() {
8
+ return this._isDestroyed;
9
+ }
10
+ _isDestroyed = !1;
11
+ get canDestroy() {
12
+ return !this.isDestroyed && this.refCount <= 0;
13
+ }
14
+ _destroyers = [];
15
+ disposeFun(e) {
16
+ return this._destroyers.push(e), e;
17
+ }
18
+ cancelDisposeFun(e) {
19
+ let t = this._destroyers.indexOf(e);
20
+ return this._destroyers.splice(t, 1), e;
21
+ }
22
+ disposeObj(e, t) {
23
+ let n = t ? function() {
24
+ return e.destroySync();
25
+ } : function() {
26
+ return e.destroy();
27
+ };
28
+ return this.disposeFun(n), e.__destroyable_destroyer = n, e;
29
+ }
30
+ cancelDisposeObj(e) {
31
+ let t = e.__destroyable_destroyer;
32
+ return t && this.cancelDisposeFun(t), e;
33
+ }
34
+ dispose(e, t) {
35
+ return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, t);
36
+ }
37
+ cancelDispose(e) {
38
+ return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
39
+ }
40
+ destroyThis() {}
41
+ destroySync() {
42
+ if (!this.canDestroy) return this.isDestroyed;
43
+ let e = this._destroyers.length;
44
+ for (; --e >= 0;) {
45
+ let t = this._destroyers[e];
46
+ try {
47
+ t();
48
+ } catch (e) {
49
+ console.error("销毁函数在同步销毁时出错", this, t, e);
50
+ }
51
+ }
52
+ this._destroyers.length = 0;
53
+ try {
54
+ this.destroyThis();
55
+ } catch (e) {
56
+ console.error("destroyThis 在异步销毁时出错", this, e);
57
+ }
58
+ return t(this), !0;
59
+ }
60
+ destroy() {
61
+ if (!this.canDestroy) return this.isDestroyed;
62
+ let e = this._destroyers.toReversed();
63
+ e.push(this.destroyThis), this._destroyers.length = 0;
64
+ let n = i(e, this, null, (e, t) => {
65
+ console.error("销毁函数在串行销毁时出错", this, t, e);
66
+ });
67
+ return n instanceof Promise ? n.then(() => (t(this), !0)) : (t(this), !0);
68
+ }
69
+ }
70
+ return n;
71
+ }
72
+ //#endregion
73
+ export { r as Destroyable, a as createDestroyableSubClass, t as destroyDestroyable, n as destroyObject, e as throwOnDestroyed };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gby/destroyable",
3
- "version": "3.9.0",
3
+ "version": "5.0.0",
4
4
  "description": "@gby/destroyable 提供了用于构建可销毁对象的类的基类,用于使得地管理可销毁对象的生命周期",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -17,11 +17,6 @@
17
17
  "import": "./dist/index-node.js"
18
18
  },
19
19
  ".": {
20
- "browser": {
21
- "require": "./dist/index-web.cjs",
22
- "types": "./dist/index-web.d.ts",
23
- "import": "./dist/index-web.js"
24
- },
25
20
  "require": "./dist/index.cjs",
26
21
  "types": "./dist/index.d.ts",
27
22
  "import": "./dist/index.js"
@@ -57,7 +52,7 @@
57
52
  "devDependencies": {
58
53
  "@microsoft/api-documenter": "latest",
59
54
  "@microsoft/api-extractor": "latest",
60
- "@types/node": "^25.3.3",
55
+ "@types/node": "^25.5.0",
61
56
  "build-tls": "latest",
62
57
  "dts-bundle-generator": "latest",
63
58
  "package-tls": "latest",
@@ -67,8 +62,9 @@
67
62
  "vite-node": "latest"
68
63
  },
69
64
  "dependencies": {
65
+ "@gby/event": "^1.0.0",
70
66
  "@gby/event-bus": "^1.3.0",
71
- "com-tools": "^2.21.4"
67
+ "com-tools": "^2.22.1"
72
68
  },
73
69
  "scripts": {
74
70
  "serve": "vite",
@@ -1,123 +0,0 @@
1
- import { d as r } from "./createDestroyableSubClass-BGwNfZdZ.js";
2
- import { EventEmitter as o } from "node:events";
3
- import { serialCallAllFuns as n } from "com-tools";
4
- class u extends o {
5
- /**
6
- * 引用计数
7
- * @remarks
8
- * 引用计数为 0 时,对象才会被销毁
9
- */
10
- refCount = 0;
11
- /**
12
- * 是否已经销毁
13
- */
14
- get isDestroyed() {
15
- return this._isDestroyed;
16
- }
17
- _isDestroyed = !1;
18
- /**
19
- * 是否可以销毁
20
- */
21
- get canDestroy() {
22
- return !this.isDestroyed && this.refCount <= 0;
23
- }
24
- /**
25
- * 销毁者
26
- */
27
- _destroyers = [];
28
- /**
29
- * 添加销毁者
30
- * @param fun
31
- * @returns 返回销毁者的顺序
32
- */
33
- disposeFun(e) {
34
- return this._destroyers.push(e), e;
35
- }
36
- /**
37
- * 取消销毁者函数
38
- * @param fun
39
- * @returns
40
- */
41
- cancelDisposeFun(e) {
42
- const s = this._destroyers.indexOf(e);
43
- return this._destroyers.splice(s, 1), e;
44
- }
45
- /**
46
- * 添加销毁对象
47
- * @param obj
48
- * @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
49
- * @returns
50
- */
51
- disposeObj(e, s) {
52
- const t = s ? function() {
53
- return e.destroySync();
54
- } : function() {
55
- return e.destroy();
56
- };
57
- return this.disposeFun(t), e.__destroyable_destroyer = t, e;
58
- }
59
- /**
60
- * 取消销毁者函数
61
- * @param fun
62
- * @returns
63
- */
64
- cancelDisposeObj(e) {
65
- const s = e.__destroyable_destroyer;
66
- return s && this.cancelDisposeFun(s), e;
67
- }
68
- dispose(e, s) {
69
- return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, s);
70
- }
71
- cancelDispose(e) {
72
- return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
73
- }
74
- /**
75
- * 自己的销毁方法
76
- * @remarks
77
- * 子类根据需要进行重载
78
- */
79
- destroyThis() {
80
- }
81
- /**
82
- * 同步销毁
83
- * @remarks
84
- * 会逆序依次同步调用所有销毁函数,并不会等待上一个销毁函数异步执行完成
85
- */
86
- destroySync() {
87
- if (!this.canDestroy) return this.isDestroyed;
88
- let e = this._destroyers.length;
89
- for (; --e >= 0; ) {
90
- const s = this._destroyers[e];
91
- try {
92
- s();
93
- } catch (t) {
94
- console.error("销毁函数在同步销毁时出错", this, s, t);
95
- }
96
- }
97
- this._destroyers.length = 0;
98
- try {
99
- this.destroyThis();
100
- } catch (s) {
101
- console.error("destroyThis 在异步销毁时出错", this, s);
102
- }
103
- return r(this), !0;
104
- }
105
- /**
106
- * 串行销毁
107
- * @remarks
108
- * 会逆序依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
109
- * @returns 如果所有的销毁函数都是同步的,那么返回 true;如果有任何一个销毁函数是异步的,那么返回一个 Promise 对象
110
- */
111
- destroy() {
112
- if (!this.canDestroy) return this.isDestroyed;
113
- const e = this._destroyers.toReversed();
114
- e.push(this.destroyThis), this._destroyers.length = 0;
115
- const s = n(e, this, null, (t, i) => {
116
- console.error("销毁函数在串行销毁时出错", this, i, t);
117
- });
118
- return s instanceof Promise ? s.then(() => (r(this), !0)) : (r(this), !0);
119
- }
120
- }
121
- export {
122
- u as D
123
- };
@@ -1 +0,0 @@
1
- "use strict";const r=require("./createDestroyableSubClass-CHkLsrwP.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 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 r.destroyDestroyable(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;const e=this._destroyers.toReversed();e.push(this.destroyThis),this._destroyers.length=0;const s=n.serialCallAllFuns(e,this,null,(t,o)=>{console.error("销毁函数在串行销毁时出错",this,o,t)});return s instanceof Promise?s.then(()=>(r.destroyDestroyable(this),!0)):(r.destroyDestroyable(this),!0)}}exports.DestroyableEventEmitter=y;
@@ -1 +0,0 @@
1
- "use strict";const r=require("./createDestroyableSubClass-CHkLsrwP.cjs"),i=require("@gby/event-bus"),n=require("com-tools");class y extends i.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 r.destroyDestroyable(this),!0}destroy(){if(!this.canDestroy)return this.isDestroyed;const e=this._destroyers.toReversed();e.push(this.destroyThis),this._destroyers.length=0;const s=n.serialCallAllFuns(e,this,null,(t,o)=>{console.error("销毁函数在串行销毁时出错",this,o,t)});return s instanceof Promise?s.then(()=>(r.destroyDestroyable(this),!0)):(r.destroyDestroyable(this),!0)}}exports.DestroyableEventTarget=y;
@@ -1,123 +0,0 @@
1
- import { d as r } from "./createDestroyableSubClass-BGwNfZdZ.js";
2
- import { EventBus as o } from "@gby/event-bus";
3
- import { serialCallAllFuns as n } from "com-tools";
4
- class u extends o {
5
- /**
6
- * 引用计数
7
- * @remarks
8
- * 引用计数为 0 时,对象才会被销毁
9
- */
10
- refCount = 0;
11
- /**
12
- * 是否已经销毁
13
- */
14
- get isDestroyed() {
15
- return this._isDestroyed;
16
- }
17
- _isDestroyed = !1;
18
- /**
19
- * 是否可以销毁
20
- */
21
- get canDestroy() {
22
- return !this.isDestroyed && this.refCount <= 0;
23
- }
24
- /**
25
- * 销毁者
26
- */
27
- _destroyers = [];
28
- /**
29
- * 添加销毁者
30
- * @param fun
31
- * @returns 返回销毁者的顺序
32
- */
33
- disposeFun(e) {
34
- return this._destroyers.push(e), e;
35
- }
36
- /**
37
- * 取消销毁者函数
38
- * @param fun
39
- * @returns
40
- */
41
- cancelDisposeFun(e) {
42
- const s = this._destroyers.indexOf(e);
43
- return this._destroyers.splice(s, 1), e;
44
- }
45
- /**
46
- * 添加销毁对象
47
- * @param obj
48
- * @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
49
- * @returns
50
- */
51
- disposeObj(e, s) {
52
- const t = s ? function() {
53
- return e.destroySync();
54
- } : function() {
55
- return e.destroy();
56
- };
57
- return this.disposeFun(t), e.__destroyable_destroyer = t, e;
58
- }
59
- /**
60
- * 取消销毁者函数
61
- * @param fun
62
- * @returns
63
- */
64
- cancelDisposeObj(e) {
65
- const s = e.__destroyable_destroyer;
66
- return s && this.cancelDisposeFun(s), e;
67
- }
68
- dispose(e, s) {
69
- return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, s);
70
- }
71
- cancelDispose(e) {
72
- return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
73
- }
74
- /**
75
- * 自己的销毁方法
76
- * @remarks
77
- * 子类根据需要进行重载
78
- */
79
- destroyThis() {
80
- }
81
- /**
82
- * 同步销毁
83
- * @remarks
84
- * 会逆序依次同步调用所有销毁函数,并不会等待上一个销毁函数异步执行完成
85
- */
86
- destroySync() {
87
- if (!this.canDestroy) return this.isDestroyed;
88
- let e = this._destroyers.length;
89
- for (; --e >= 0; ) {
90
- const s = this._destroyers[e];
91
- try {
92
- s();
93
- } catch (t) {
94
- console.error("销毁函数在同步销毁时出错", this, s, t);
95
- }
96
- }
97
- this._destroyers.length = 0;
98
- try {
99
- this.destroyThis();
100
- } catch (s) {
101
- console.error("destroyThis 在异步销毁时出错", this, s);
102
- }
103
- return r(this), !0;
104
- }
105
- /**
106
- * 串行销毁
107
- * @remarks
108
- * 会逆序依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
109
- * @returns 如果所有的销毁函数都是同步的,那么返回 true;如果有任何一个销毁函数是异步的,那么返回一个 Promise 对象
110
- */
111
- destroy() {
112
- if (!this.canDestroy) return this.isDestroyed;
113
- const e = this._destroyers.toReversed();
114
- e.push(this.destroyThis), this._destroyers.length = 0;
115
- const s = n(e, this, null, (t, i) => {
116
- console.error("销毁函数在串行销毁时出错", this, i, t);
117
- });
118
- return s instanceof Promise ? s.then(() => (r(this), !0)) : (r(this), !0);
119
- }
120
- }
121
- export {
122
- u as D
123
- };
@@ -1,258 +0,0 @@
1
- import { serialCallAllFuns as h } from "com-tools";
2
- class u {
3
- /**
4
- * 引用计数
5
- * @remarks
6
- * 引用计数为 0 时,对象才会被销毁
7
- */
8
- refCount = 0;
9
- /**
10
- * 是否已经销毁
11
- */
12
- get isDestroyed() {
13
- return this._isDestroyed;
14
- }
15
- _isDestroyed = !1;
16
- /**
17
- * 是否可以销毁
18
- */
19
- get canDestroy() {
20
- return !this.isDestroyed && this.refCount <= 0;
21
- }
22
- /**
23
- * 销毁者
24
- */
25
- _destroyers = [];
26
- /**
27
- * 添加销毁者
28
- * @param fun
29
- * @returns 返回销毁者的顺序
30
- */
31
- disposeFun(s) {
32
- return this._destroyers.push(s), s;
33
- }
34
- /**
35
- * 取消销毁者函数
36
- * @param fun
37
- * @returns
38
- */
39
- cancelDisposeFun(s) {
40
- const r = this._destroyers.indexOf(s);
41
- return this._destroyers.splice(r, 1), s;
42
- }
43
- /**
44
- * 添加销毁对象
45
- * @param obj
46
- * @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
47
- * @returns
48
- */
49
- disposeObj(s, r) {
50
- const e = r ? function() {
51
- return s.destroySync();
52
- } : function() {
53
- return s.destroy();
54
- };
55
- return this.disposeFun(e), s.__destroyable_destroyer = e, s;
56
- }
57
- /**
58
- * 取消销毁者函数
59
- * @param fun
60
- * @returns
61
- */
62
- cancelDisposeObj(s) {
63
- const r = s.__destroyable_destroyer;
64
- return r && this.cancelDisposeFun(r), s;
65
- }
66
- dispose(s, r) {
67
- return typeof s == "function" ? this.disposeFun(s) : this.disposeObj(s, r);
68
- }
69
- cancelDispose(s) {
70
- return typeof s == "function" ? this.cancelDisposeFun(s) : this.cancelDisposeObj(s);
71
- }
72
- /**
73
- * 自己的销毁方法
74
- * @remarks
75
- * 子类根据需要进行重载
76
- */
77
- destroyThis() {
78
- }
79
- /**
80
- * 同步销毁
81
- * @remarks
82
- * 会逆序依次同步调用所有销毁函数,并不会等待上一个销毁函数异步执行完成
83
- */
84
- destroySync() {
85
- if (!this.canDestroy) return this.isDestroyed;
86
- let s = this._destroyers.length;
87
- for (; --s >= 0; ) {
88
- const r = this._destroyers[s];
89
- try {
90
- r();
91
- } catch (e) {
92
- console.error("销毁函数在同步销毁时出错", this, r, e);
93
- }
94
- }
95
- this._destroyers.length = 0;
96
- try {
97
- this.destroyThis();
98
- } catch (r) {
99
- console.error("destroyThis 在异步销毁时出错", this, r);
100
- }
101
- return n(this), !0;
102
- }
103
- /**
104
- * 串行销毁
105
- * @remarks
106
- * 会逆序依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
107
- * @returns 如果所有的销毁函数都是同步的,那么返回 true;如果有任何一个销毁函数是异步的,那么返回一个 Promise 对象
108
- */
109
- destroy() {
110
- if (!this.canDestroy) return this.isDestroyed;
111
- const s = this._destroyers.toReversed();
112
- s.push(this.destroyThis), this._destroyers.length = 0;
113
- const r = h(s, this, null, (e, t) => {
114
- console.error("销毁函数在串行销毁时出错", this, t, e);
115
- });
116
- return r instanceof Promise ? r.then(() => (n(this), !0)) : (n(this), !0);
117
- }
118
- }
119
- function d() {
120
- throw "已销毁";
121
- }
122
- function l(i) {
123
- for (var s in i)
124
- typeof i[s] == "function" && (i[s] = d);
125
- return i._isDestroyed = !0, !0;
126
- }
127
- function n(i) {
128
- for (var s in i)
129
- !(s in u.prototype) && typeof i[s] == "function" && (i[s] = d);
130
- return i._isDestroyed = !0, !0;
131
- }
132
- function a(i) {
133
- class s extends i {
134
- /**
135
- * 引用计数
136
- * @remarks
137
- * 引用计数为 0 时,对象才会被销毁
138
- */
139
- refCount = 0;
140
- /**
141
- * 是否已经销毁
142
- */
143
- get isDestroyed() {
144
- return this._isDestroyed;
145
- }
146
- _isDestroyed = !1;
147
- /**
148
- * 是否可以销毁
149
- */
150
- get canDestroy() {
151
- return !this.isDestroyed && this.refCount <= 0;
152
- }
153
- /**
154
- * 销毁者
155
- */
156
- _destroyers = [];
157
- /**
158
- * 添加销毁者
159
- * @param fun
160
- * @returns 返回销毁者的顺序
161
- */
162
- disposeFun(e) {
163
- return this._destroyers.push(e), e;
164
- }
165
- /**
166
- * 取消销毁者函数
167
- * @param fun
168
- * @returns
169
- */
170
- cancelDisposeFun(e) {
171
- const t = this._destroyers.indexOf(e);
172
- return this._destroyers.splice(t, 1), e;
173
- }
174
- /**
175
- * 添加销毁对象
176
- * @param obj
177
- * @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
178
- * @returns
179
- */
180
- disposeObj(e, t) {
181
- const o = t ? function() {
182
- return e.destroySync();
183
- } : function() {
184
- return e.destroy();
185
- };
186
- return this.disposeFun(o), e.__destroyable_destroyer = o, e;
187
- }
188
- /**
189
- * 取消销毁者函数
190
- * @param fun
191
- * @returns
192
- */
193
- cancelDisposeObj(e) {
194
- const t = e.__destroyable_destroyer;
195
- return t && this.cancelDisposeFun(t), e;
196
- }
197
- dispose(e, t) {
198
- return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, t);
199
- }
200
- cancelDispose(e) {
201
- return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
202
- }
203
- /**
204
- * 自己的销毁方法
205
- * @remarks
206
- * 子类根据需要进行重载
207
- */
208
- destroyThis() {
209
- }
210
- /**
211
- * 同步销毁
212
- * @remarks
213
- * 会逆序依次同步调用所有销毁函数,并不会等待上一个销毁函数异步执行完成
214
- */
215
- destroySync() {
216
- if (!this.canDestroy) return this.isDestroyed;
217
- let e = this._destroyers.length;
218
- for (; --e >= 0; ) {
219
- const t = this._destroyers[e];
220
- try {
221
- t();
222
- } catch (o) {
223
- console.error("销毁函数在同步销毁时出错", this, t, o);
224
- }
225
- }
226
- this._destroyers.length = 0;
227
- try {
228
- this.destroyThis();
229
- } catch (t) {
230
- console.error("destroyThis 在异步销毁时出错", this, t);
231
- }
232
- return n(this), !0;
233
- }
234
- /**
235
- * 串行销毁
236
- * @remarks
237
- * 会逆序依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
238
- * @returns 如果所有的销毁函数都是同步的,那么返回 true;如果有任何一个销毁函数是异步的,那么返回一个 Promise 对象
239
- */
240
- destroy() {
241
- if (!this.canDestroy) return this.isDestroyed;
242
- const e = this._destroyers.toReversed();
243
- e.push(this.destroyThis), this._destroyers.length = 0;
244
- const t = h(e, this, null, (o, y) => {
245
- console.error("销毁函数在串行销毁时出错", this, y, o);
246
- });
247
- return t instanceof Promise ? t.then(() => (n(this), !0)) : (n(this), !0);
248
- }
249
- }
250
- return s;
251
- }
252
- export {
253
- u as D,
254
- l as a,
255
- a as c,
256
- n as d,
257
- d as t
258
- };
@@ -1 +0,0 @@
1
- "use strict";const d=require("com-tools");class h{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 r=this._destroyers.indexOf(e);return this._destroyers.splice(r,1),e}disposeObj(e,r){const s=r?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(s),e.__destroyable_destroyer=s,e}cancelDisposeObj(e){const r=e.__destroyable_destroyer;return r&&this.cancelDisposeFun(r),e}dispose(e,r){return typeof e=="function"?this.disposeFun(e):this.disposeObj(e,r)}cancelDispose(e){return typeof e=="function"?this.cancelDisposeFun(e):this.cancelDisposeObj(e)}destroyThis(){}destroySync(){if(!this.canDestroy)return this.isDestroyed;let e=this._destroyers.length;for(;--e>=0;){const r=this._destroyers[e];try{r()}catch(s){console.error("销毁函数在同步销毁时出错",this,r,s)}}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 e=this._destroyers.toReversed();e.push(this.destroyThis),this._destroyers.length=0;const r=d.serialCallAllFuns(e,this,null,(s,t)=>{console.error("销毁函数在串行销毁时出错",this,t,s)});return r instanceof Promise?r.then(()=>(o(this),!0)):(o(this),!0)}}function y(){throw"已销毁"}function c(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 h.prototype)&&typeof i[e]=="function"&&(i[e]=y);return i._isDestroyed=!0,!0}function l(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 t=this._destroyers.indexOf(s);return this._destroyers.splice(t,1),s}disposeObj(s,t){const n=t?function(){return s.destroySync()}:function(){return s.destroy()};return this.disposeFun(n),s.__destroyable_destroyer=n,s}cancelDisposeObj(s){const t=s.__destroyable_destroyer;return t&&this.cancelDisposeFun(t),s}dispose(s,t){return typeof s=="function"?this.disposeFun(s):this.disposeObj(s,t)}cancelDispose(s){return typeof s=="function"?this.cancelDisposeFun(s):this.cancelDisposeObj(s)}destroyThis(){}destroySync(){if(!this.canDestroy)return this.isDestroyed;let s=this._destroyers.length;for(;--s>=0;){const t=this._destroyers[s];try{t()}catch(n){console.error("销毁函数在同步销毁时出错",this,t,n)}}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 s=this._destroyers.toReversed();s.push(this.destroyThis),this._destroyers.length=0;const t=d.serialCallAllFuns(s,this,null,(n,u)=>{console.error("销毁函数在串行销毁时出错",this,u,n)});return t instanceof Promise?t.then(()=>(o(this),!0)):(o(this),!0)}}return e}exports.Destroyable=h;exports.createDestroyableSubClass=l;exports.destroyDestroyable=o;exports.destroyObject=c;exports.throwOnDestroyed=y;