@gby/destroyable 3.7.0 → 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
+ };
@@ -0,0 +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;
@@ -0,0 +1,10 @@
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
10
+ };
package/dist/index.cjs CHANGED
@@ -1 +1 @@
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;
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,496 +1,12 @@
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 {
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 o(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 = h(e, this, null, (t, r) => {
116
- console.error("销毁函数在串行销毁时出错", this, r, t);
117
- });
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);
486
- }
487
- }
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";
488
4
  export {
489
- l as Destroyable,
490
- T as DestroyableEventEmitter,
491
- g as DestroyableEventTarget,
492
- D as createDestroyableSubClass,
493
- o as destroyDestroyable,
494
- _ as destroyObject,
495
- d 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
496
12
  };
package/package.json CHANGED
@@ -1,14 +1,26 @@
1
1
  {
2
2
  "name": "@gby/destroyable",
3
- "version": "3.7.0",
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
- "require": "./dist/index.cjs",
10
- "types": "./dist/index.d.ts",
11
- "import": "./dist/index.js"
9
+ "./web": {
10
+ "require": "./dist/index-web.cjs",
11
+ "types": "./dist/index-web.d.ts",
12
+ "import": "./dist/index-web.js"
13
+ },
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
+ }
12
24
  },
13
25
  "type": "module",
14
26
  "sideEffects": false,