@gby/destroyable 3.8.0 → 4.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.
- package/dist/{DestroyableEventEmitter-DMAh3-8-.js → Destroyable-BqvuD4gL.js} +50 -36
- package/dist/Destroyable-DllTvY6v.cjs +1 -0
- package/dist/index-node.cjs +1 -1
- package/dist/index-node.d.ts +0 -11
- package/dist/index-node.d.ts.map +1 -1
- package/dist/index-node.js +121 -8
- package/dist/index-web.cjs +1 -1
- package/dist/index-web.d.ts +0 -11
- package/dist/index-web.d.ts.map +1 -1
- package/dist/index-web.js +121 -8
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +127 -9
- package/package.json +1 -1
- package/dist/DestroyableEventEmitter-OgjYk1aA.cjs +0 -1
- package/dist/DestroyableEventTarget-3Wyju42G.cjs +0 -1
- package/dist/DestroyableEventTarget-DxkJCQlH.js +0 -123
- package/dist/createDestroyableSubClass-BGwNfZdZ.js +0 -258
- package/dist/createDestroyableSubClass-CHkLsrwP.cjs +0 -1
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { serialCallAllFuns as n } from "com-tools";
|
|
4
|
-
class u extends o {
|
|
1
|
+
import { serialCallAllFuns as d } from "com-tools";
|
|
2
|
+
class h {
|
|
5
3
|
/**
|
|
6
4
|
* 引用计数
|
|
7
5
|
* @remarks
|
|
@@ -30,17 +28,17 @@ class u extends o {
|
|
|
30
28
|
* @param fun
|
|
31
29
|
* @returns 返回销毁者的顺序
|
|
32
30
|
*/
|
|
33
|
-
disposeFun(
|
|
34
|
-
return this._destroyers.push(
|
|
31
|
+
disposeFun(s) {
|
|
32
|
+
return this._destroyers.push(s), s;
|
|
35
33
|
}
|
|
36
34
|
/**
|
|
37
35
|
* 取消销毁者函数
|
|
38
36
|
* @param fun
|
|
39
37
|
* @returns
|
|
40
38
|
*/
|
|
41
|
-
cancelDisposeFun(
|
|
42
|
-
const
|
|
43
|
-
return this._destroyers.splice(
|
|
39
|
+
cancelDisposeFun(s) {
|
|
40
|
+
const e = this._destroyers.indexOf(s);
|
|
41
|
+
return this._destroyers.splice(e, 1), s;
|
|
44
42
|
}
|
|
45
43
|
/**
|
|
46
44
|
* 添加销毁对象
|
|
@@ -48,28 +46,28 @@ class u extends o {
|
|
|
48
46
|
* @param sync - 表示是否使用 `obj.destroySync()` 方法进行销毁;默认使用 `obj.destroy()` 方法进行销毁
|
|
49
47
|
* @returns
|
|
50
48
|
*/
|
|
51
|
-
disposeObj(
|
|
52
|
-
const
|
|
53
|
-
return
|
|
49
|
+
disposeObj(s, e) {
|
|
50
|
+
const r = e ? function() {
|
|
51
|
+
return s.destroySync();
|
|
54
52
|
} : function() {
|
|
55
|
-
return
|
|
53
|
+
return s.destroy();
|
|
56
54
|
};
|
|
57
|
-
return this.disposeFun(
|
|
55
|
+
return this.disposeFun(r), s.__destroyable_destroyer = r, s;
|
|
58
56
|
}
|
|
59
57
|
/**
|
|
60
58
|
* 取消销毁者函数
|
|
61
59
|
* @param fun
|
|
62
60
|
* @returns
|
|
63
61
|
*/
|
|
64
|
-
cancelDisposeObj(
|
|
65
|
-
const
|
|
66
|
-
return
|
|
62
|
+
cancelDisposeObj(s) {
|
|
63
|
+
const e = s.__destroyable_destroyer;
|
|
64
|
+
return e && this.cancelDisposeFun(e), s;
|
|
67
65
|
}
|
|
68
|
-
dispose(
|
|
69
|
-
return typeof
|
|
66
|
+
dispose(s, e) {
|
|
67
|
+
return typeof s == "function" ? this.disposeFun(s) : this.disposeObj(s, e);
|
|
70
68
|
}
|
|
71
|
-
cancelDispose(
|
|
72
|
-
return typeof
|
|
69
|
+
cancelDispose(s) {
|
|
70
|
+
return typeof s == "function" ? this.cancelDisposeFun(s) : this.cancelDisposeObj(s);
|
|
73
71
|
}
|
|
74
72
|
/**
|
|
75
73
|
* 自己的销毁方法
|
|
@@ -85,22 +83,22 @@ class u extends o {
|
|
|
85
83
|
*/
|
|
86
84
|
destroySync() {
|
|
87
85
|
if (!this.canDestroy) return this.isDestroyed;
|
|
88
|
-
let
|
|
89
|
-
for (; --
|
|
90
|
-
const
|
|
86
|
+
let s = this._destroyers.length;
|
|
87
|
+
for (; --s >= 0; ) {
|
|
88
|
+
const e = this._destroyers[s];
|
|
91
89
|
try {
|
|
92
|
-
|
|
93
|
-
} catch (
|
|
94
|
-
console.error("销毁函数在同步销毁时出错", this,
|
|
90
|
+
e();
|
|
91
|
+
} catch (r) {
|
|
92
|
+
console.error("销毁函数在同步销毁时出错", this, e, r);
|
|
95
93
|
}
|
|
96
94
|
}
|
|
97
95
|
this._destroyers.length = 0;
|
|
98
96
|
try {
|
|
99
97
|
this.destroyThis();
|
|
100
|
-
} catch (
|
|
101
|
-
console.error("destroyThis 在异步销毁时出错", this,
|
|
98
|
+
} catch (e) {
|
|
99
|
+
console.error("destroyThis 在异步销毁时出错", this, e);
|
|
102
100
|
}
|
|
103
|
-
return
|
|
101
|
+
return i(this), !0;
|
|
104
102
|
}
|
|
105
103
|
/**
|
|
106
104
|
* 串行销毁
|
|
@@ -110,14 +108,30 @@ class u extends o {
|
|
|
110
108
|
*/
|
|
111
109
|
destroy() {
|
|
112
110
|
if (!this.canDestroy) return this.isDestroyed;
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
console.error("销毁函数在串行销毁时出错", this,
|
|
111
|
+
const s = this._destroyers.toReversed();
|
|
112
|
+
s.push(this.destroyThis), this._destroyers.length = 0;
|
|
113
|
+
const e = d(s, this, null, (r, n) => {
|
|
114
|
+
console.error("销毁函数在串行销毁时出错", this, n, r);
|
|
117
115
|
});
|
|
118
|
-
return
|
|
116
|
+
return e instanceof Promise ? e.then(() => (i(this), !0)) : (i(this), !0);
|
|
119
117
|
}
|
|
120
118
|
}
|
|
119
|
+
function o() {
|
|
120
|
+
throw "已销毁";
|
|
121
|
+
}
|
|
122
|
+
function y(t) {
|
|
123
|
+
for (var s in t)
|
|
124
|
+
typeof t[s] == "function" && (t[s] = o);
|
|
125
|
+
return t._isDestroyed = !0, !0;
|
|
126
|
+
}
|
|
127
|
+
function i(t) {
|
|
128
|
+
for (var s in t)
|
|
129
|
+
!(s in h.prototype) && typeof t[s] == "function" && (t[s] = o);
|
|
130
|
+
return t._isDestroyed = !0, !0;
|
|
131
|
+
}
|
|
121
132
|
export {
|
|
122
|
-
|
|
133
|
+
h as D,
|
|
134
|
+
y as a,
|
|
135
|
+
i as d,
|
|
136
|
+
o as t
|
|
123
137
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const d=require("com-tools");class 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){const s=this._destroyers.indexOf(e);return this._destroyers.splice(s,1),e}disposeObj(e,s){const r=s?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(r),e.__destroyable_destroyer=r,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(r){console.error("销毁函数在同步销毁时出错",this,s,r)}}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=d.serialCallAllFuns(e,this,null,(r,y)=>{console.error("销毁函数在串行销毁时出错",this,y,r)});return s instanceof Promise?s.then(()=>(o(this),!0)):(o(this),!0)}}function i(){throw"已销毁"}function u(t){for(var e in t)typeof t[e]=="function"&&(t[e]=i);return t._isDestroyed=!0,!0}function o(t){for(var e in t)!(e in n.prototype)&&typeof t[e]=="function"&&(t[e]=i);return t._isDestroyed=!0,!0}exports.Destroyable=n;exports.destroyDestroyable=o;exports.destroyObject=u;exports.throwOnDestroyed=i;
|
package/dist/index-node.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./Destroyable-DllTvY6v.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;
|
package/dist/index-node.d.ts
CHANGED
|
@@ -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
|
package/dist/index-node.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index-node.d.ts","sourceRoot":"","sources":["../src/index-node.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index-node.d.ts","sourceRoot":"","sources":["../src/index-node.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC"}
|
package/dist/index-node.js
CHANGED
|
@@ -1,10 +1,123 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { d as r } from "./Destroyable-BqvuD4gL.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
|
+
}
|
|
3
121
|
export {
|
|
4
|
-
|
|
5
|
-
b as DestroyableEventEmitter,
|
|
6
|
-
r as createDestroyableSubClass,
|
|
7
|
-
a as destroyDestroyable,
|
|
8
|
-
o as destroyObject,
|
|
9
|
-
y as throwOnDestroyed
|
|
122
|
+
u as DestroyableEventEmitter
|
|
10
123
|
};
|
package/dist/index-web.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./Destroyable-DllTvY6v.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;
|
package/dist/index-web.d.ts
CHANGED
|
@@ -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
|
package/dist/index-web.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index-web.d.ts","sourceRoot":"","sources":["../src/index-web.ts"],"names":[],"mappings":"AAAA
|
|
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,123 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { d as r } from "./Destroyable-BqvuD4gL.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
|
+
}
|
|
3
121
|
export {
|
|
4
|
-
|
|
5
|
-
b as DestroyableEventTarget,
|
|
6
|
-
a as createDestroyableSubClass,
|
|
7
|
-
r as destroyDestroyable,
|
|
8
|
-
o as destroyObject,
|
|
9
|
-
y as throwOnDestroyed
|
|
122
|
+
u as DestroyableEventTarget
|
|
10
123
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("./Destroyable-DllTvY6v.cjs"),y=require("com-tools");function d(o){class i extends o{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 r=s?function(){return e.destroySync()}:function(){return e.destroy()};return this.disposeFun(r),e.__destroyable_destroyer=r,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(r){console.error("销毁函数在同步销毁时出错",this,s,r)}}this._destroyers.length=0;try{this.destroyThis()}catch(s){console.error("destroyThis 在异步销毁时出错",this,s)}return t.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=y.serialCallAllFuns(e,this,null,(r,n)=>{console.error("销毁函数在串行销毁时出错",this,n,r)});return s instanceof Promise?s.then(()=>(t.destroyDestroyable(this),!0)):(t.destroyDestroyable(this),!0)}}return i}exports.Destroyable=t.Destroyable;exports.destroyDestroyable=t.destroyDestroyable;exports.destroyObject=t.destroyObject;exports.throwOnDestroyed=t.throwOnDestroyed;exports.createDestroyableSubClass=d;
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -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
|
|
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,130 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { D as
|
|
3
|
-
import {
|
|
1
|
+
import { d as r } from "./Destroyable-BqvuD4gL.js";
|
|
2
|
+
import { D as f, a as p, t as D } from "./Destroyable-BqvuD4gL.js";
|
|
3
|
+
import { serialCallAllFuns as d } from "com-tools";
|
|
4
|
+
function u(o) {
|
|
5
|
+
class i extends o {
|
|
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 = d(e, this, null, (t, n) => {
|
|
117
|
+
console.error("销毁函数在串行销毁时出错", this, n, t);
|
|
118
|
+
});
|
|
119
|
+
return s instanceof Promise ? s.then(() => (r(this), !0)) : (r(this), !0);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return i;
|
|
123
|
+
}
|
|
4
124
|
export {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
a as destroyDestroyable,
|
|
10
|
-
o as destroyObject,
|
|
125
|
+
f as Destroyable,
|
|
126
|
+
u as createDestroyableSubClass,
|
|
127
|
+
r as destroyDestroyable,
|
|
128
|
+
p as destroyObject,
|
|
11
129
|
D as throwOnDestroyed
|
|
12
130
|
};
|
package/package.json
CHANGED
|
@@ -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;
|