@gby/destroyable 3.6.3 → 3.8.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,123 @@
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
+ };
@@ -0,0 +1 @@
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;
@@ -0,0 +1 @@
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;
@@ -0,0 +1,123 @@
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
+ };
@@ -0,0 +1,258 @@
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
+ };
@@ -0,0 +1 @@
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;
@@ -0,0 +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;
@@ -0,0 +1,13 @@
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
+ export * from "./DestroyableEventEmitter";
13
+ //# 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,QAAQ,CAAC;AACvB,cAAc,eAAe,CAAC;AAC9B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC"}
@@ -0,0 +1,10 @@
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
10
+ };
@@ -1 +1 @@
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;
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;
package/dist/index-web.js CHANGED
@@ -1,9 +1,10 @@
1
- import { D as a, a as t, c as r, d as o, b, t as y } from "./DestroyableEventTarget-CIMBV0bD.js";
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";
2
3
  export {
3
- a as Destroyable,
4
- t as DestroyableEventTarget,
5
- r as createDestroyableSubClass,
6
- o as destroyDestroyable,
7
- b as destroyObject,
4
+ t as Destroyable,
5
+ b as DestroyableEventTarget,
6
+ a as createDestroyableSubClass,
7
+ r as destroyDestroyable,
8
+ o as destroyObject,
8
9
  y as throwOnDestroyed
9
10
  };
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 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;
package/dist/index.js CHANGED
@@ -1,130 +1,12 @@
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 {
6
- /**
7
- * 引用计数
8
- * @remarks
9
- * 引用计数为 0 时,对象才会被销毁
10
- */
11
- refCount = 0;
12
- /**
13
- * 是否已经销毁
14
- */
15
- get isDestroyed() {
16
- return this._isDestroyed;
17
- }
18
- _isDestroyed = !1;
19
- /**
20
- * 是否可以销毁
21
- */
22
- get canDestroy() {
23
- return !this.isDestroyed && this.refCount <= 0;
24
- }
25
- /**
26
- * 销毁者
27
- */
28
- _destroyers = [];
29
- /**
30
- * 添加销毁者
31
- * @param fun
32
- * @returns 返回销毁者的顺序
33
- */
34
- disposeFun(e) {
35
- return this._destroyers.push(e), e;
36
- }
37
- /**
38
- * 取消销毁者函数
39
- * @param fun
40
- * @returns
41
- */
42
- cancelDisposeFun(e) {
43
- const s = this._destroyers.indexOf(e);
44
- return this._destroyers.splice(s, 1), e;
45
- }
46
- /**
47
- * 添加销毁对象
48
- * @param obj
49
- * @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
50
- * @returns
51
- */
52
- disposeObj(e, s) {
53
- const t = s ? function() {
54
- return e.destroySync();
55
- } : function() {
56
- return e.destroy();
57
- };
58
- return this.disposeFun(t), e.__destroyable_destroyer = t, e;
59
- }
60
- /**
61
- * 取消销毁者函数
62
- * @param fun
63
- * @returns
64
- */
65
- cancelDisposeObj(e) {
66
- const s = e.__destroyable_destroyer;
67
- return s && this.cancelDisposeFun(s), e;
68
- }
69
- dispose(e, s) {
70
- return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, s);
71
- }
72
- cancelDispose(e) {
73
- return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
74
- }
75
- /**
76
- * 自己的销毁方法
77
- * @remarks
78
- * 子类根据需要进行重载
79
- */
80
- destroyThis() {
81
- }
82
- /**
83
- * 同步销毁
84
- * @remarks
85
- * 会逆序依次同步调用所有销毁函数,并不会等待上一个销毁函数异步执行完成
86
- */
87
- destroySync() {
88
- if (!this.canDestroy) return this.isDestroyed;
89
- let e = this._destroyers.length;
90
- for (; --e >= 0; ) {
91
- const s = this._destroyers[e];
92
- try {
93
- s();
94
- } catch (t) {
95
- console.error("销毁函数在同步销毁时出错", this, s, t);
96
- }
97
- }
98
- this._destroyers.length = 0;
99
- try {
100
- this.destroyThis();
101
- } catch (s) {
102
- console.error("destroyThis 在异步销毁时出错", this, s);
103
- }
104
- return r(this), !0;
105
- }
106
- /**
107
- * 串行销毁
108
- * @remarks
109
- * 会逆序依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
110
- * @returns 如果所有的销毁函数都是同步的,那么返回 true;如果有任何一个销毁函数是异步的,那么返回一个 Promise 对象
111
- */
112
- destroy() {
113
- if (!this.canDestroy) return this.isDestroyed;
114
- const e = this._destroyers.toReversed();
115
- e.push(this.destroyThis), this._destroyers.length = 0;
116
- const s = n(e, this, null, (t, o) => {
117
- console.error("销毁函数在串行销毁时出错", this, o, t);
118
- });
119
- return s instanceof Promise ? s.then(() => (r(this), !0)) : (r(this), !0);
120
- }
121
- }
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";
122
4
  export {
123
- p as Destroyable,
124
- u as DestroyableEventEmitter,
125
- D as DestroyableEventTarget,
126
- f as createDestroyableSubClass,
127
- r as destroyDestroyable,
128
- _ as destroyObject,
129
- m as throwOnDestroyed
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
130
12
  };
package/package.json CHANGED
@@ -1,19 +1,26 @@
1
1
  {
2
2
  "name": "@gby/destroyable",
3
- "version": "3.6.3",
3
+ "version": "3.8.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": {
9
+ "./web": {
10
10
  "require": "./dist/index-web.cjs",
11
11
  "types": "./dist/index-web.d.ts",
12
12
  "import": "./dist/index-web.js"
13
13
  },
14
- "require": "./dist/index.cjs",
15
- "types": "./dist/index.d.ts",
16
- "import": "./dist/index.js"
14
+ "./node": {
15
+ "require": "./dist/index-node.cjs",
16
+ "types": "./dist/index-node.d.ts",
17
+ "import": "./dist/index-node.js"
18
+ },
19
+ ".": {
20
+ "require": "./dist/index.cjs",
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js"
23
+ }
17
24
  },
18
25
  "type": "module",
19
26
  "sideEffects": false,
@@ -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;