@gby/destroyable 2.0.0 → 3.1.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.
@@ -1 +0,0 @@
1
- {"version":3,"file":"index-node.d.ts","sourceRoot":"","sources":["../src/index-node.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,eAAe,CAAC;AAC9B,cAAc,6BAA6B,CAAA;AAC3C,cAAc,mBAAmB,CAAA"}
@@ -1,143 +0,0 @@
1
- var y = Object.defineProperty;
2
- var d = (o, r, e) => r in o ? y(o, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[r] = e;
3
- var i = (o, r, e) => d(o, typeof r != "symbol" ? r + "" : r, e);
4
- import { a as n } from "./createDestroyableSubClass-Blnpqngp.js";
5
- import { D as p, c as _, d as m, t as x } from "./createDestroyableSubClass-Blnpqngp.js";
6
- import { EventEmitter as c } from "node:events";
7
- class u extends c {
8
- constructor() {
9
- super(...arguments);
10
- /**
11
- * 引用计数
12
- * @remarks
13
- * 引用计数为 0 时,对象才会被销毁
14
- */
15
- i(this, "refCount", 0);
16
- i(this, "_isDestroyed", !1);
17
- /**
18
- * 销毁者
19
- */
20
- i(this, "_destroyers", []);
21
- }
22
- /**
23
- * 是否已经销毁
24
- */
25
- get isDestroyed() {
26
- return this._isDestroyed;
27
- }
28
- /**
29
- * 是否可以销毁
30
- */
31
- get canDestroy() {
32
- return !this.isDestroyed && this.refCount <= 0;
33
- }
34
- /**
35
- * 添加销毁者
36
- * @param fun
37
- * @returns 返回销毁者的顺序
38
- */
39
- disposeFun(e) {
40
- return this._destroyers.push(e), e;
41
- }
42
- /**
43
- * 取消销毁者函数
44
- * @param fun
45
- * @returns
46
- */
47
- cancelDisposeFun(e) {
48
- const s = this._destroyers.indexOf(e);
49
- return this._destroyers.splice(s, 1), e;
50
- }
51
- /**
52
- * 添加销毁对象
53
- * @param obj
54
- * @param sync - 表示是否使用 `obj.destroy()` 方法进行销毁;默认使用 `obj.destroyAsync()` 方法进行销毁
55
- * @returns
56
- */
57
- disposeObj(e, s) {
58
- const t = s ? function() {
59
- return e.destroy();
60
- } : function() {
61
- return e.destroyAsync();
62
- };
63
- return this.disposeFun(t), e.__destroyable_destroyer = t, e;
64
- }
65
- /**
66
- * 取消销毁者函数
67
- * @param fun
68
- * @returns
69
- */
70
- cancelDisposeObj(e) {
71
- const s = e.__destroyable_destroyer;
72
- return s && this.cancelDisposeFun(s), e;
73
- }
74
- dispose(e, s) {
75
- return typeof e == "function" ? this.disposeFun(e) : this.disposeObj(e, s);
76
- }
77
- cancelDispose(e) {
78
- return typeof e == "function" ? this.cancelDisposeFun(e) : this.cancelDisposeObj(e);
79
- }
80
- /**
81
- * 自己的销毁方法
82
- * @remarks
83
- * 子类根据需要进行重载
84
- */
85
- destroyThis() {
86
- }
87
- /**
88
- * 销毁
89
- */
90
- destroy() {
91
- if (!this.canDestroy) return this.isDestroyed;
92
- let e = this._destroyers.length;
93
- for (; --e >= 0; ) {
94
- const s = this._destroyers[e];
95
- try {
96
- s();
97
- } catch (t) {
98
- console.error("销毁函数在同步销毁时出错", this, s, t);
99
- }
100
- }
101
- this._destroyers.length = 0;
102
- try {
103
- this.destroyThis();
104
- } catch (s) {
105
- console.error("destroyThis 在异步销毁时出错", this, s);
106
- }
107
- return n(this), !0;
108
- }
109
- /**
110
- * 异步销毁
111
- * @remarks
112
- * 会依次执行销毁,并且只有上一个销毁完成之后才会执行下一个销毁
113
- */
114
- async destroyAsync() {
115
- if (!this.canDestroy) return this.isDestroyed;
116
- let e = this._destroyers.length;
117
- for (; --e >= 0; ) {
118
- const s = this._destroyers[e];
119
- try {
120
- const t = s();
121
- t && t instanceof Promise && await t;
122
- } catch (t) {
123
- console.error("销毁函数在异步销毁时出错", this, s, t);
124
- }
125
- }
126
- this._destroyers.length = 0;
127
- try {
128
- const s = this.destroyThis();
129
- s && s instanceof Promise && await s;
130
- } catch (s) {
131
- console.error("destroyThis 在异步销毁时出错", this, s);
132
- }
133
- return n(this), !0;
134
- }
135
- }
136
- export {
137
- p as Destroyable,
138
- u as NodeDestroyable,
139
- _ as createDestroyableSubClass,
140
- n as destroyDestroyable,
141
- m as destroyObject,
142
- x as throwOnDestroyed
143
- };