@idlebox/common 1.4.1 → 1.4.3

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.
Files changed (261) hide show
  1. package/lib/cjs/array/arrayUnique.cjs +2 -4
  2. package/lib/cjs/array/arrayUnique.cjs.map +1 -1
  3. package/lib/cjs/array/normalizeArray.cjs +2 -4
  4. package/lib/cjs/array/normalizeArray.cjs.map +1 -1
  5. package/lib/cjs/array/sortAlpha.cjs +2 -4
  6. package/lib/cjs/array/sortAlpha.cjs.map +1 -1
  7. package/lib/cjs/date/isInvalid.cjs +1 -1
  8. package/lib/cjs/date/isInvalid.cjs.map +1 -1
  9. package/lib/cjs/date/timeString.cjs +8 -10
  10. package/lib/cjs/date/timeString.cjs.map +1 -1
  11. package/lib/cjs/debugging/serializable.cjs +2 -2
  12. package/lib/cjs/debugging/serializable.cjs.map +1 -1
  13. package/lib/cjs/debugging/tryInspect.cjs +7 -9
  14. package/lib/cjs/debugging/tryInspect.cjs.map +1 -1
  15. package/lib/cjs/error/convertUnknown.cjs +2 -4
  16. package/lib/cjs/error/convertUnknown.cjs.map +1 -1
  17. package/lib/cjs/error/getFrame.cjs +2 -2
  18. package/lib/cjs/error/getFrame.cjs.map +1 -1
  19. package/lib/cjs/error/known.cjs.map +1 -1
  20. package/lib/cjs/error/pretty.cjs +22 -30
  21. package/lib/cjs/error/pretty.cjs.map +1 -1
  22. package/lib/cjs/function/asyncCallbackList.cjs.map +1 -1
  23. package/lib/cjs/function/callbackList.cjs.map +1 -1
  24. package/lib/cjs/function/delayCallbackList.cjs +3 -3
  25. package/lib/cjs/function/delayCallbackList.cjs.map +1 -1
  26. package/lib/cjs/lifecycle/dispose/disposableEvent.cjs +1 -3
  27. package/lib/cjs/lifecycle/dispose/disposableEvent.cjs.map +1 -1
  28. package/lib/cjs/lifecycle/dispose/lifecycle.async.cjs.map +1 -1
  29. package/lib/cjs/lifecycle/dispose/lifecycle.global.cjs +4 -8
  30. package/lib/cjs/lifecycle/dispose/lifecycle.global.cjs.map +1 -1
  31. package/lib/cjs/lifecycle/dispose/lifecycle.sync.cjs +1 -1
  32. package/lib/cjs/lifecycle/dispose/lifecycle.sync.cjs.map +1 -1
  33. package/lib/cjs/lifecycle/event/event.cjs +61 -6
  34. package/lib/cjs/lifecycle/event/event.cjs.map +1 -1
  35. package/lib/cjs/lifecycle/event/memorized.cjs +1 -1
  36. package/lib/cjs/lifecycle/event/memorized.cjs.map +1 -1
  37. package/lib/cjs/lifecycle/promise/cancellationToken/driver.common.cjs.map +1 -1
  38. package/lib/cjs/lifecycle/promise/deferredPromise.cjs +2 -2
  39. package/lib/cjs/lifecycle/promise/deferredPromise.cjs.map +1 -1
  40. package/lib/cjs/lifecycle/timeout/timeout.cjs +1 -3
  41. package/lib/cjs/lifecycle/timeout/timeout.cjs.map +1 -1
  42. package/lib/cjs/lifecycle/timeout/timeoutError.cjs.map +1 -1
  43. package/lib/cjs/log/logger.cjs +4 -6
  44. package/lib/cjs/log/logger.cjs.map +1 -1
  45. package/lib/cjs/mapSet/customSet.cjs +3 -7
  46. package/lib/cjs/mapSet/customSet.cjs.map +1 -1
  47. package/lib/cjs/mapSet/extendMap.cjs +5 -9
  48. package/lib/cjs/mapSet/extendMap.cjs.map +1 -1
  49. package/lib/cjs/misc/assertNotNull.cjs +1 -3
  50. package/lib/cjs/misc/assertNotNull.cjs.map +1 -1
  51. package/lib/cjs/misc/package.json.cjs +1 -1
  52. package/lib/cjs/misc/package.json.cjs.map +1 -1
  53. package/lib/cjs/object/initOnRead.cjs.map +1 -1
  54. package/lib/cjs/object/objectSame.cjs +2 -1
  55. package/lib/cjs/object/objectSame.cjs.map +1 -1
  56. package/lib/cjs/path/normalizePath.cjs +7 -9
  57. package/lib/cjs/path/normalizePath.cjs.map +1 -1
  58. package/lib/cjs/path/pathArray.cjs +1 -1
  59. package/lib/cjs/path/pathArray.cjs.map +1 -1
  60. package/lib/cjs/platform/globalSingleton.cjs +1 -1
  61. package/lib/cjs/platform/globalSymbol.cjs +6 -8
  62. package/lib/cjs/platform/globalSymbol.cjs.map +1 -1
  63. package/lib/cjs/promise/promiseCollection.cjs +3 -3
  64. package/lib/cjs/promise/promiseCollection.cjs.map +1 -1
  65. package/lib/cjs/reflection/classes/singleton.cjs +1 -3
  66. package/lib/cjs/reflection/classes/singleton.cjs.map +1 -1
  67. package/lib/cjs/reflection/methods/bind.cjs +1 -3
  68. package/lib/cjs/reflection/methods/bind.cjs.map +1 -1
  69. package/lib/cjs/reflection/methods/initOnRead.cjs +2 -2
  70. package/lib/cjs/reflection/methods/initOnRead.cjs.map +1 -1
  71. package/lib/cjs/string/castCase.cjs +5 -5
  72. package/lib/cjs/string/castCase.cjs.map +1 -1
  73. package/lib/cjs/string/pad2.cjs +2 -4
  74. package/lib/cjs/string/pad2.cjs.map +1 -1
  75. package/lib/cjs/string/sizeString.cjs +23 -27
  76. package/lib/cjs/string/sizeString.cjs.map +1 -1
  77. package/lib/esm/array/arrayUnique.d.ts +1 -3
  78. package/lib/esm/array/arrayUnique.d.ts.map +1 -1
  79. package/lib/esm/array/arrayUnique.js +2 -4
  80. package/lib/esm/array/arrayUnique.js.map +1 -1
  81. package/lib/esm/array/normalizeArray.js +2 -4
  82. package/lib/esm/array/normalizeArray.js.map +1 -1
  83. package/lib/esm/array/sortAlpha.js +2 -4
  84. package/lib/esm/array/sortAlpha.js.map +1 -1
  85. package/lib/esm/date/isInvalid.js +1 -1
  86. package/lib/esm/date/isInvalid.js.map +1 -1
  87. package/lib/esm/date/timeString.d.ts +1 -3
  88. package/lib/esm/date/timeString.d.ts.map +1 -1
  89. package/lib/esm/date/timeString.js +8 -10
  90. package/lib/esm/date/timeString.js.map +1 -1
  91. package/lib/esm/debugging/serializable.d.ts +1 -1
  92. package/lib/esm/debugging/serializable.d.ts.map +1 -1
  93. package/lib/esm/debugging/serializable.js +2 -2
  94. package/lib/esm/debugging/serializable.js.map +1 -1
  95. package/lib/esm/debugging/tryInspect.d.ts.map +1 -1
  96. package/lib/esm/debugging/tryInspect.js +7 -9
  97. package/lib/esm/debugging/tryInspect.js.map +1 -1
  98. package/lib/esm/error/convertUnknown.d.ts.map +1 -1
  99. package/lib/esm/error/convertUnknown.js +2 -4
  100. package/lib/esm/error/convertUnknown.js.map +1 -1
  101. package/lib/esm/error/getFrame.js +2 -2
  102. package/lib/esm/error/getFrame.js.map +1 -1
  103. package/lib/esm/error/known.d.ts.map +1 -1
  104. package/lib/esm/error/known.js.map +1 -1
  105. package/lib/esm/error/pretty.d.ts.map +1 -1
  106. package/lib/esm/error/pretty.js +22 -30
  107. package/lib/esm/error/pretty.js.map +1 -1
  108. package/lib/esm/function/asyncCallbackList.d.ts +1 -1
  109. package/lib/esm/function/asyncCallbackList.d.ts.map +1 -1
  110. package/lib/esm/function/asyncCallbackList.js.map +1 -1
  111. package/lib/esm/function/callbackList.d.ts.map +1 -1
  112. package/lib/esm/function/callbackList.js.map +1 -1
  113. package/lib/esm/function/delayCallbackList.d.ts.map +1 -1
  114. package/lib/esm/function/delayCallbackList.js +3 -3
  115. package/lib/esm/function/delayCallbackList.js.map +1 -1
  116. package/lib/esm/lifecycle/dispose/disposableEvent.d.ts +1 -1
  117. package/lib/esm/lifecycle/dispose/disposableEvent.d.ts.map +1 -1
  118. package/lib/esm/lifecycle/dispose/disposableEvent.js +1 -3
  119. package/lib/esm/lifecycle/dispose/disposableEvent.js.map +1 -1
  120. package/lib/esm/lifecycle/dispose/lifecycle.async.d.ts +2 -2
  121. package/lib/esm/lifecycle/dispose/lifecycle.async.d.ts.map +1 -1
  122. package/lib/esm/lifecycle/dispose/lifecycle.async.js.map +1 -1
  123. package/lib/esm/lifecycle/dispose/lifecycle.d.ts +1 -1
  124. package/lib/esm/lifecycle/dispose/lifecycle.d.ts.map +1 -1
  125. package/lib/esm/lifecycle/dispose/lifecycle.global.d.ts +1 -1
  126. package/lib/esm/lifecycle/dispose/lifecycle.global.d.ts.map +1 -1
  127. package/lib/esm/lifecycle/dispose/lifecycle.global.js +4 -8
  128. package/lib/esm/lifecycle/dispose/lifecycle.global.js.map +1 -1
  129. package/lib/esm/lifecycle/dispose/lifecycle.sync.d.ts +2 -2
  130. package/lib/esm/lifecycle/dispose/lifecycle.sync.d.ts.map +1 -1
  131. package/lib/esm/lifecycle/dispose/lifecycle.sync.js +1 -1
  132. package/lib/esm/lifecycle/dispose/lifecycle.sync.js.map +1 -1
  133. package/lib/esm/lifecycle/event/event.d.ts +15 -7
  134. package/lib/esm/lifecycle/event/event.d.ts.map +1 -1
  135. package/lib/esm/lifecycle/event/event.js +61 -6
  136. package/lib/esm/lifecycle/event/event.js.map +1 -1
  137. package/lib/esm/lifecycle/event/memorized.d.ts +2 -2
  138. package/lib/esm/lifecycle/event/memorized.d.ts.map +1 -1
  139. package/lib/esm/lifecycle/event/memorized.js +1 -1
  140. package/lib/esm/lifecycle/event/memorized.js.map +1 -1
  141. package/lib/esm/lifecycle/promise/cancellationToken/driver.common.js.map +1 -1
  142. package/lib/esm/lifecycle/promise/cancellationToken/source.d.ts +2 -2
  143. package/lib/esm/lifecycle/promise/cancellationToken/source.d.ts.map +1 -1
  144. package/lib/esm/lifecycle/promise/deferredPromise.js +2 -2
  145. package/lib/esm/lifecycle/promise/deferredPromise.js.map +1 -1
  146. package/lib/esm/lifecycle/timeout/timeout.js +1 -3
  147. package/lib/esm/lifecycle/timeout/timeout.js.map +1 -1
  148. package/lib/esm/lifecycle/timeout/timeoutError.d.ts.map +1 -1
  149. package/lib/esm/lifecycle/timeout/timeoutError.js.map +1 -1
  150. package/lib/esm/log/logger.d.ts.map +1 -1
  151. package/lib/esm/log/logger.js +4 -6
  152. package/lib/esm/log/logger.js.map +1 -1
  153. package/lib/esm/mapSet/customSet.d.ts +1 -3
  154. package/lib/esm/mapSet/customSet.d.ts.map +1 -1
  155. package/lib/esm/mapSet/customSet.js +3 -7
  156. package/lib/esm/mapSet/customSet.js.map +1 -1
  157. package/lib/esm/mapSet/extendMap.d.ts.map +1 -1
  158. package/lib/esm/mapSet/extendMap.js +5 -9
  159. package/lib/esm/mapSet/extendMap.js.map +1 -1
  160. package/lib/esm/misc/assertNotNull.d.ts.map +1 -1
  161. package/lib/esm/misc/assertNotNull.js +1 -3
  162. package/lib/esm/misc/assertNotNull.js.map +1 -1
  163. package/lib/esm/misc/package.json.js +1 -1
  164. package/lib/esm/misc/package.json.js.map +1 -1
  165. package/lib/esm/object/initOnRead.d.ts +1 -3
  166. package/lib/esm/object/initOnRead.d.ts.map +1 -1
  167. package/lib/esm/object/initOnRead.js.map +1 -1
  168. package/lib/esm/object/objectSame.js +2 -1
  169. package/lib/esm/object/objectSame.js.map +1 -1
  170. package/lib/esm/path/normalizePath.d.ts.map +1 -1
  171. package/lib/esm/path/normalizePath.js +7 -9
  172. package/lib/esm/path/normalizePath.js.map +1 -1
  173. package/lib/esm/path/pathArray.js +1 -1
  174. package/lib/esm/path/pathArray.js.map +1 -1
  175. package/lib/esm/platform/globalSingleton.js +1 -1
  176. package/lib/esm/platform/globalSymbol.d.ts.map +1 -1
  177. package/lib/esm/platform/globalSymbol.js +6 -8
  178. package/lib/esm/platform/globalSymbol.js.map +1 -1
  179. package/lib/esm/promise/promiseCollection.js +3 -3
  180. package/lib/esm/promise/promiseCollection.js.map +1 -1
  181. package/lib/esm/reflection/classes/hookClass.d.ts +1 -1
  182. package/lib/esm/reflection/classes/hookClass.d.ts.map +1 -1
  183. package/lib/esm/reflection/classes/singleton.d.ts.map +1 -1
  184. package/lib/esm/reflection/classes/singleton.js +1 -3
  185. package/lib/esm/reflection/classes/singleton.js.map +1 -1
  186. package/lib/esm/reflection/methods/bind.d.ts.map +1 -1
  187. package/lib/esm/reflection/methods/bind.js +1 -3
  188. package/lib/esm/reflection/methods/bind.js.map +1 -1
  189. package/lib/esm/reflection/methods/initOnRead.d.ts +1 -1
  190. package/lib/esm/reflection/methods/initOnRead.d.ts.map +1 -1
  191. package/lib/esm/reflection/methods/initOnRead.js +2 -2
  192. package/lib/esm/reflection/methods/initOnRead.js.map +1 -1
  193. package/lib/esm/string/castCase.js +5 -5
  194. package/lib/esm/string/castCase.js.map +1 -1
  195. package/lib/esm/string/pad2.d.ts.map +1 -1
  196. package/lib/esm/string/pad2.js +2 -4
  197. package/lib/esm/string/pad2.js.map +1 -1
  198. package/lib/esm/string/sizeString.d.ts.map +1 -1
  199. package/lib/esm/string/sizeString.js +23 -27
  200. package/lib/esm/string/sizeString.js.map +1 -1
  201. package/lib/esm/typingHelper/deep.partial.d.ts +1 -1
  202. package/lib/esm/typingHelper/deep.partial.d.ts.map +1 -1
  203. package/lib/esm/typingHelper/deep.readonly.d.ts +1 -1
  204. package/lib/esm/typingHelper/deep.readonly.d.ts.map +1 -1
  205. package/lib/esm/typingHelper/deep.required.d.ts +1 -1
  206. package/lib/esm/typingHelper/deep.required.d.ts.map +1 -1
  207. package/lib/esm/typingHelper/deep.writable.d.ts +1 -1
  208. package/lib/esm/typingHelper/deep.writable.d.ts.map +1 -1
  209. package/package.json +3 -3
  210. package/src/array/arrayUnique.ts +3 -6
  211. package/src/array/normalizeArray.ts +3 -3
  212. package/src/array/sortAlpha.ts +3 -3
  213. package/src/date/isInvalid.ts +1 -1
  214. package/src/date/timeString.ts +9 -15
  215. package/src/debugging/serializable.ts +8 -8
  216. package/src/debugging/tryInspect.ts +12 -8
  217. package/src/error/convertUnknown.ts +2 -3
  218. package/src/error/getFrame.ts +2 -2
  219. package/src/error/known.ts +2 -2
  220. package/src/error/pretty.ts +29 -34
  221. package/src/function/asyncCallbackList.ts +3 -3
  222. package/src/function/callbackList.ts +2 -2
  223. package/src/function/delayCallbackList.ts +4 -4
  224. package/src/lifecycle/dispose/disposableEvent.ts +2 -3
  225. package/src/lifecycle/dispose/disposedError.ts +1 -1
  226. package/src/lifecycle/dispose/lifecycle.async.ts +2 -2
  227. package/src/lifecycle/dispose/lifecycle.global.ts +6 -7
  228. package/src/lifecycle/dispose/lifecycle.sync.ts +3 -3
  229. package/src/lifecycle/dispose/lifecycle.ts +1 -1
  230. package/src/lifecycle/event/event.ts +66 -13
  231. package/src/lifecycle/event/memorized.ts +4 -4
  232. package/src/lifecycle/promise/cancellationToken/driver.browser.ts +1 -1
  233. package/src/lifecycle/promise/cancellationToken/driver.common.ts +2 -2
  234. package/src/lifecycle/promise/cancellationToken/source.ts +2 -2
  235. package/src/lifecycle/promise/deferredPromise.ts +2 -2
  236. package/src/lifecycle/timeout/timeout.ts +1 -2
  237. package/src/lifecycle/timeout/timeoutError.ts +1 -1
  238. package/src/log/logger.ts +8 -9
  239. package/src/mapSet/customSet.ts +4 -8
  240. package/src/mapSet/extendMap.ts +6 -7
  241. package/src/misc/assertNotNull.ts +1 -2
  242. package/src/misc/package.json.ts +1 -1
  243. package/src/object/initOnRead.ts +1 -3
  244. package/src/object/objectSame.ts +2 -2
  245. package/src/path/normalizePath.ts +14 -17
  246. package/src/path/pathArray.ts +2 -2
  247. package/src/platform/globalSingleton.ts +1 -1
  248. package/src/platform/globalSymbol.ts +8 -9
  249. package/src/promise/promiseCollection.ts +3 -3
  250. package/src/reflection/classes/hookClass.ts +1 -1
  251. package/src/reflection/classes/singleton.ts +3 -4
  252. package/src/reflection/methods/bind.ts +1 -2
  253. package/src/reflection/methods/initOnRead.ts +3 -3
  254. package/src/string/castCase.ts +5 -5
  255. package/src/string/pad2.ts +2 -3
  256. package/src/string/sizeString.ts +33 -25
  257. package/src/tsconfig.json +1 -1
  258. package/src/typingHelper/deep.partial.ts +7 -7
  259. package/src/typingHelper/deep.readonly.ts +7 -7
  260. package/src/typingHelper/deep.required.ts +7 -7
  261. package/src/typingHelper/deep.writable.ts +7 -7
@@ -1,12 +1,10 @@
1
- import { IDisposable } from '../dispose/lifecycle.js';
1
+ import type { IDisposable } from '../dispose/lifecycle.js';
2
2
 
3
- export interface EventHandler<T> {
4
- (data: T): void;
5
- }
3
+ export type EventHandler<T> = (data: T) => void;
6
4
 
7
- export interface EventRegister<T> {
8
- (callback: EventHandler<T>): IDisposable;
9
- }
5
+ export type EventRegister<T> = (callback: EventHandler<T>) => IDisposable;
6
+
7
+ type DeferFn = () => void;
10
8
 
11
9
  /**
12
10
  * 事件注册对象
@@ -14,6 +12,7 @@ export interface EventRegister<T> {
14
12
  */
15
13
  export class Emitter<T> implements IDisposable {
16
14
  protected readonly _callbacks: EventHandler<T>[] = [];
15
+ private executing = false;
17
16
 
18
17
  constructor() {
19
18
  this.handle = this.handle.bind(this);
@@ -31,8 +30,15 @@ export class Emitter<T> implements IDisposable {
31
30
  * @param data 回调数据
32
31
  */
33
32
  public fire(data: T) {
34
- for (const callback of this._callbacks) {
35
- callback(data);
33
+ this.requireNotExecuting();
34
+ this.executing = true;
35
+ try {
36
+ for (const callback of this._callbacks) {
37
+ callback(data);
38
+ }
39
+ } finally {
40
+ this.doDefer();
41
+ this.executing = false;
36
42
  }
37
43
  }
38
44
 
@@ -40,6 +46,8 @@ export class Emitter<T> implements IDisposable {
40
46
  * 与 `fire()`相同,但是忽略任何错误,并且即便出错也继续执行全部callback
41
47
  */
42
48
  public fireNoError(data: T) {
49
+ this.requireNotExecuting();
50
+ this.executing = true;
43
51
  for (const callback of this._callbacks) {
44
52
  try {
45
53
  callback(data);
@@ -47,6 +55,8 @@ export class Emitter<T> implements IDisposable {
47
55
  console.error('Error ignored: ', e);
48
56
  }
49
57
  }
58
+ this.doDefer();
59
+ this.executing = false;
50
60
  }
51
61
 
52
62
  /**
@@ -61,18 +71,55 @@ export class Emitter<T> implements IDisposable {
61
71
  * @param callback 回调函数
62
72
  */
63
73
  handle(callback: EventHandler<T>): IDisposable {
64
- this._callbacks.unshift(callback);
74
+ this.requireNotExecuting();
75
+ let disposed = false;
76
+ this._callbacks.unshift((e) => {
77
+ if (!disposed) callback(e);
78
+ });
79
+ const realDispose = () => {
80
+ const index = this._callbacks.indexOf(callback);
81
+ if (index !== -1) {
82
+ this._callbacks.splice(index, 1);
83
+ }
84
+ };
65
85
  return {
66
86
  dispose: () => {
67
- const index = this._callbacks.indexOf(callback);
68
- if (index !== -1) {
69
- this._callbacks.splice(index, 1);
87
+ disposed = true;
88
+ if (this.executing) {
89
+ this.defer(realDispose);
90
+ } else {
91
+ realDispose();
70
92
  }
71
93
  },
72
94
  };
73
95
  }
74
96
 
97
+ /**
98
+ * 注册一次性回调
99
+ * @param callback 回调函数
100
+ */
101
+ once(callback: EventHandler<T>): IDisposable {
102
+ this.requireNotExecuting();
103
+ const disposable = this.handle((data) => {
104
+ callback(data);
105
+ disposable.dispose();
106
+ });
107
+ return disposable;
108
+ }
109
+
110
+ private defers: DeferFn[] = [];
111
+ public defer(fn: DeferFn) {
112
+ this.defers.push(fn);
113
+ }
114
+ private doDefer() {
115
+ for (const fn of this.defers) {
116
+ fn();
117
+ }
118
+ this.defers.length = 0;
119
+ }
120
+
75
121
  dispose() {
122
+ this.requireNotExecuting();
76
123
  this._callbacks.length = 0;
77
124
  this.fireNoError =
78
125
  this.fire =
@@ -81,4 +128,10 @@ export class Emitter<T> implements IDisposable {
81
128
  throw new Error('Event is disposed');
82
129
  };
83
130
  }
131
+
132
+ private requireNotExecuting() {
133
+ if (this.executing) {
134
+ throw new Error('conflict state, emitter is firing');
135
+ }
136
+ }
84
137
  }
@@ -1,5 +1,5 @@
1
- import { IDisposable } from '../dispose/lifecycle.js';
2
- import { Emitter, EventHandler } from './event.js';
1
+ import type { IDisposable } from '../dispose/lifecycle.js';
2
+ import { Emitter, type EventHandler } from './event.js';
3
3
 
4
4
  /**
5
5
  * 会记住上次fire的内容,并在每个新的handler注册时立即调用一次的Emitter
@@ -8,7 +8,7 @@ import { Emitter, EventHandler } from './event.js';
8
8
  */
9
9
  export class MemorizedEmitter<T> extends Emitter<T> {
10
10
  private _memo?: T;
11
- private _is_memo: boolean = false;
11
+ private _is_memo = false;
12
12
 
13
13
  public override fire(data: T) {
14
14
  this._memo = data;
@@ -28,7 +28,7 @@ export class MemorizedEmitter<T> extends Emitter<T> {
28
28
  }
29
29
 
30
30
  public forget() {
31
- delete this._memo;
31
+ this._memo = undefined;
32
32
  this._is_memo = false;
33
33
  }
34
34
 
@@ -1,4 +1,4 @@
1
- import { EventHandler } from '../../event/event.js';
1
+ import type { EventHandler } from '../../event/event.js';
2
2
 
3
3
  import type { CancellationDriver, __CancellationToken } from './source.js';
4
4
 
@@ -1,5 +1,5 @@
1
- import { IDisposable } from '../../dispose/lifecycle.js';
2
- import { Emitter, EventHandler } from '../../event/event.js';
1
+ import type { IDisposable } from '../../dispose/lifecycle.js';
2
+ import { Emitter, type EventHandler } from '../../event/event.js';
3
3
 
4
4
  import type { CancellationDriver, __CancellationToken } from './source.js';
5
5
 
@@ -1,7 +1,7 @@
1
1
  import { globalObject } from '../../../platform/globalObject.js';
2
- import { IDisposable } from '../../dispose/lifecycle.js';
2
+ import type { IDisposable } from '../../dispose/lifecycle.js';
3
3
  import { DisposableOnce } from '../../dispose/lifecycle.sync.js';
4
- import { EventHandler } from '../../event/event.js';
4
+ import type { EventHandler } from '../../event/event.js';
5
5
  import { CancellationDriverBrowser } from './driver.browser.js';
6
6
  import { CancellationDriverCommon } from './driver.common.js';
7
7
 
@@ -33,7 +33,7 @@ export class DeferredPromise<T, PT = any> {
33
33
  );
34
34
  this.p
35
35
  .finally(() => {
36
- delete this._progressList;
36
+ this._progressList = undefined;
37
37
  })
38
38
  .catch(() => void 0);
39
39
  }
@@ -46,7 +46,7 @@ export class DeferredPromise<T, PT = any> {
46
46
  }
47
47
 
48
48
  progress(fn: ProgressCallback<PT>): void {
49
- this._progressList!.push(fn);
49
+ this._progressList?.push(fn);
50
50
  }
51
51
 
52
52
  get completed(): boolean {
@@ -42,7 +42,6 @@ export function timeoutPromise<T>(ms: number, message: string | T, p?: T): T {
42
42
  }
43
43
  if (p instanceof DeferredPromise) {
44
44
  return Promise.race([p.p, timeout(ms, msg)]).then(() => p) as any;
45
- } else {
46
- return Promise.race([p, timeout(ms, msg)]).then(() => p) as any;
47
45
  }
46
+ return Promise.race([p, timeout(ms, msg)]).then(() => p) as any;
48
47
  }
@@ -5,7 +5,7 @@ import { humanDate } from '../../date/timeString.js';
5
5
  * @public
6
6
  */
7
7
  export class TimeoutError extends Error {
8
- constructor(time: number, what: string = 'no response') {
8
+ constructor(time: number, what = 'no response') {
9
9
  super(`Timeout: ${what} in ${humanDate.deltaTiny(time)}`);
10
10
  }
11
11
  }
package/src/log/logger.ts CHANGED
@@ -2,10 +2,10 @@ import { nameFunction } from '../function/functionName.js';
2
2
  import { isWeb } from '../platform/os.js';
3
3
 
4
4
  export enum ColorKind {
5
- DISABLE,
6
- TERMINAL,
7
- WEB,
8
- DETECT,
5
+ DISABLE = 0,
6
+ TERMINAL = 1,
7
+ WEB = 2,
8
+ DETECT = 3,
9
9
  }
10
10
 
11
11
  export interface WrappedConsoleOptions {
@@ -71,13 +71,12 @@ export abstract class WrappedConsole {
71
71
  protected wrap<T extends keyof Omit<Console & { Console: any }, 'Console'>>(original: T): Function {
72
72
  if (this.bind) {
73
73
  return this.parent[original].bind(this.parent) as any;
74
- } else {
75
- return this.parent[original];
76
74
  }
75
+ return this.parent[original];
77
76
  }
78
77
 
79
78
  private wrapSimple<T extends keyof Omit<Console & { Console: any }, 'Console'>>(original: T): Console[T] {
80
- return nameFunction('console:' + original, this.wrap(original) as any);
79
+ return nameFunction(`console:${original}`, this.wrap(original) as any);
81
80
  }
82
81
 
83
82
  private wrapExtra<T extends keyof Omit<Console & { Console: any }, 'Console'>>(
@@ -85,7 +84,7 @@ export abstract class WrappedConsole {
85
84
  exMessage: string
86
85
  ): Console[T] {
87
86
  const bindedFn = this.wrap(original);
88
- return nameFunction('console:' + original, (...args: any[]) => {
87
+ return nameFunction(`console:${original}`, (...args: any[]) => {
89
88
  this.log(exMessage);
90
89
  bindedFn(...args);
91
90
  });
@@ -115,7 +114,7 @@ export abstract class WrappedConsole {
115
114
 
116
115
  const fn = this.wrap(original);
117
116
 
118
- return nameFunction('console:' + original, (...args: any[]) => {
117
+ return nameFunction(`console:${original}`, (...args: any[]) => {
119
118
  this.convertObjectArg(args, messageLoc);
120
119
  this.processColorLabel(args, messageLoc, original, prefix);
121
120
  fn(...args);
@@ -1,7 +1,5 @@
1
1
  /** Find the index of given item */
2
- export interface Finder<Type> {
3
- (this: Type[], item: Type): number;
4
- }
2
+ export type Finder<Type> = (this: Type[], item: Type) => number;
5
3
 
6
4
  type MyFinder<Type> = (item: Type) => number;
7
5
 
@@ -34,9 +32,8 @@ export class CustomSet<Type = string> {
34
32
  if (index === -1) {
35
33
  this.registry.push(item);
36
34
  return true;
37
- } else {
38
- return false;
39
35
  }
36
+ return false;
40
37
  }
41
38
 
42
39
  /**
@@ -50,10 +47,9 @@ export class CustomSet<Type = string> {
50
47
  const index = this.finder(item);
51
48
  if (index === -1) {
52
49
  return false;
53
- } else {
54
- this.registry.splice(index, 1);
55
- return true;
56
50
  }
51
+ this.registry.splice(index, 1);
52
+ return true;
57
53
  }
58
54
 
59
55
  /**
@@ -18,11 +18,11 @@ export class ExtendMap<K, V> extends Map<K, V> {
18
18
  public override get(id: K, def?: V): V {
19
19
  if (super.has(id)) {
20
20
  return super.get(id)!;
21
- } else if (arguments.length === 2) {
21
+ }
22
+ if (arguments.length === 2) {
22
23
  return def!;
23
- } else {
24
- throw new Error(`Unknown key {${id}} in map.`);
25
24
  }
25
+ throw new Error(`Unknown key {${id}} in map.`);
26
26
  }
27
27
 
28
28
  /**
@@ -31,10 +31,9 @@ export class ExtendMap<K, V> extends Map<K, V> {
31
31
  public entry(id: K, init: (id: K) => V): V {
32
32
  if (super.has(id)) {
33
33
  return super.get(id)!;
34
- } else {
35
- const nv = init(id);
36
- super.set(id, nv);
37
- return nv;
38
34
  }
35
+ const nv = init(id);
36
+ super.set(id, nv);
37
+ return nv;
39
38
  }
40
39
  }
@@ -15,7 +15,6 @@ export function assertNotNull<T>(val: T | null | undefined): T {
15
15
  export function throwNull<T>(val: T) {
16
16
  if (val === undefined || val === null) {
17
17
  return val!;
18
- } else {
19
- throw new Error('Value is ' + typeof val);
20
18
  }
19
+ throw new Error(`Value is ${typeof val}`);
21
20
  }
@@ -42,7 +42,7 @@ export function parseExportsField(exports: IExportsField): IFullExportsField {
42
42
  if (!isPathMap(exports)) {
43
43
  return { '.': exports };
44
44
  }
45
- let ret: IFullExportsField = {};
45
+ const ret: IFullExportsField = {};
46
46
  for (const [path, def] of Object.entries(exports)) {
47
47
  if (typeof def === 'string') {
48
48
  ret[path] = { default: def };
@@ -1,7 +1,5 @@
1
1
  /** @public */
2
- export interface InitFunc<O, T> {
3
- (this: O): T;
4
- }
2
+ export type InitFunc<O, T> = (this: O) => T;
5
3
 
6
4
  /**
7
5
  * Define property on target, call init it when first use, them memorize
@@ -33,8 +33,8 @@ export function isObjectSameRecursive(a: any, b: any) {
33
33
 
34
34
  for (const k of aks) {
35
35
  if (a[k] !== b[k]) {
36
- const av = a[k],
37
- bv = b[k];
36
+ const av = a[k];
37
+ const bv = b[k];
38
38
  if (av && bv && typeof av === 'object' && typeof bv === 'object') {
39
39
  if (Array.isArray(av)) {
40
40
  if (Array.isArray(bv)) {
@@ -10,12 +10,12 @@ const winLetter = /^[a-z]:[\/\\]/i;
10
10
  const doubleSlash = /^[\/\\]{2}[^\/\\]/i;
11
11
 
12
12
  export enum PathKind {
13
- url,
14
- unc,
15
- win,
16
- cifs,
17
- unix,
18
- relative,
13
+ url = 0,
14
+ unc = 1,
15
+ win = 2,
16
+ cifs = 3,
17
+ unix = 4,
18
+ relative = 5,
19
19
  }
20
20
  export interface IPathInfo {
21
21
  kind: PathKind;
@@ -31,18 +31,18 @@ export function analyzePath(p: string) {
31
31
  const u = new URL(p);
32
32
  r = {
33
33
  kind: PathKind.url,
34
- prefix: u.protocol + '//' + u.host,
34
+ prefix: `${u.protocol}//${u.host}`,
35
35
  path: u.pathname.slice(1),
36
36
  url: u,
37
37
  };
38
38
  } else if (unc.test(p)) {
39
39
  p = p.replace(unc, '').replace(/^[\/\\]+/, '');
40
40
  const i = /[\///]/.exec(p)?.index ?? -1;
41
- if (i <= 0) throw new Error('invalid unc path: ' + inp);
41
+ if (i <= 0) throw new Error(`invalid unc path: ${inp}`);
42
42
 
43
43
  r = {
44
44
  kind: PathKind.unc,
45
- prefix: '//?/UNC/' + p.slice(0, i),
45
+ prefix: `//?/UNC/${p.slice(0, i)}`,
46
46
  path: p.slice(i + 1),
47
47
  };
48
48
  } else if (winSp.test(p) || winLetter.test(p)) {
@@ -56,11 +56,11 @@ export function analyzePath(p: string) {
56
56
  } else if (doubleSlash.test(p)) {
57
57
  p = p.replace(/^[\/\\]+/, '');
58
58
  const i = /[\///]/.exec(p)?.index ?? -1;
59
- if (i <= 0) throw new Error('invalid cifs url: ' + inp);
59
+ if (i <= 0) throw new Error(`invalid cifs url: ${inp}`);
60
60
 
61
61
  r = {
62
62
  kind: PathKind.cifs,
63
- prefix: '//' + p.slice(0, i),
63
+ prefix: `//${p.slice(0, i)}`,
64
64
  path: p.slice(i + 1),
65
65
  };
66
66
  } else if (p.startsWith('/')) {
@@ -103,9 +103,8 @@ export function normalizePath(p: string) {
103
103
 
104
104
  if (r.prefix === undefined) {
105
105
  return r.path;
106
- } else {
107
- return `${r.prefix}/${r.path}`;
108
106
  }
107
+ return `${r.prefix}/${r.path}`;
109
108
  }
110
109
 
111
110
  export function relativePath(from: string, to: string) {
@@ -113,9 +112,7 @@ export function relativePath(from: string, to: string) {
113
112
  const r2 = analyzePath(to);
114
113
  if (r1.kind !== r2.kind)
115
114
  throw new Error(
116
- `cannot relative path between different kind: "${PathKind[r1.kind]}::${from}" * "${
117
- PathKind[r2.kind]
118
- }::${to}"`,
115
+ `cannot relative path between different kind: "${PathKind[r1.kind]}::${from}" * "${PathKind[r2.kind]}::${to}"`
119
116
  );
120
117
 
121
118
  if (r1.prefix !== r2.prefix) return to;
@@ -132,7 +129,7 @@ export function relativePath(from: string, to: string) {
132
129
  p2arr.splice(0, i);
133
130
 
134
131
  // add ..
135
- const p1 = p1arr.length > 0 ? p1arr.map(() => '..').join('/') + '/' : '';
132
+ const p1 = p1arr.length > 0 ? `${p1arr.map(() => '..').join('/')}/` : '';
136
133
  const p2 = p2arr.join('/');
137
134
 
138
135
  return p1 + p2;
@@ -10,7 +10,7 @@ const isAbsolute = /^[a-z]:[/\\]/i;
10
10
  abstract class PathArrayAbstract extends Set<string> {
11
11
  constructor(
12
12
  init: string,
13
- private readonly sep: ':' | ';' = isWindows ? ';' : ':',
13
+ private readonly sep: ':' | ';' = isWindows ? ';' : ':'
14
14
  ) {
15
15
  super();
16
16
  if (init) this.add(init);
@@ -44,7 +44,7 @@ abstract class PathArrayAbstract extends Set<string> {
44
44
  * @returns an array with `part` append to every element
45
45
  */
46
46
  join(part: string) {
47
- return [...this.values()].map((p) => normalizePath(p + '/' + part));
47
+ return [...this.values()].map((p) => normalizePath(`${p}/${part}`));
48
48
  }
49
49
  }
50
50
 
@@ -1,7 +1,7 @@
1
1
  import { functionName } from '../function/functionName.js';
2
2
  import { ensureGlobalObject } from './globalObject.js';
3
3
 
4
- const singletonRegistry = ensureGlobalObject(`@@idlebox/global-singleton`, () => {
4
+ const singletonRegistry = ensureGlobalObject('@@idlebox/global-singleton', () => {
5
5
  return new Map<string | symbol, any>();
6
6
  });
7
7
 
@@ -1,6 +1,6 @@
1
1
  import { ensureGlobalObject } from './globalObject.js';
2
2
 
3
- const symbolRegistry = ensureGlobalObject(`@@idlebox/global-symbol`, () => {
3
+ const symbolRegistry = ensureGlobalObject('@@idlebox/global-symbol', () => {
4
4
  return {} as Record<string, Record<string, symbol>>;
5
5
  });
6
6
 
@@ -12,15 +12,14 @@ const symbolRegistry = ensureGlobalObject(`@@idlebox/global-symbol`, () => {
12
12
  */
13
13
  export function createSymbol(category: string, name: string): symbol {
14
14
  if (symbolRegistry[category]?.[name]) {
15
- return symbolRegistry[category]![name]!;
16
- } else {
17
- if (!symbolRegistry[category]) {
18
- symbolRegistry[category] = {};
19
- }
20
- const c = symbolRegistry[category]!;
21
-
22
- return (c[name] = Symbol(name));
15
+ return symbolRegistry[category]?.[name]!;
23
16
  }
17
+ if (!symbolRegistry[category]) {
18
+ symbolRegistry[category] = {};
19
+ }
20
+ const c = symbolRegistry[category]!;
21
+
22
+ return (c[name] = Symbol(name));
24
23
  }
25
24
 
26
25
  /**
@@ -10,7 +10,7 @@ export class PromiseCollection {
10
10
 
11
11
  create(id: string) {
12
12
  if (this.promiseList[id]) {
13
- throw new Error('duplicate promise with id ' + id);
13
+ throw new Error(`duplicate promise with id ${id}`);
14
14
  }
15
15
  const dfd = new DeferredPromise<any>();
16
16
  this.promiseList[id] = dfd;
@@ -22,12 +22,12 @@ export class PromiseCollection {
22
22
  }
23
23
 
24
24
  done(id: string, data: any) {
25
- this.promiseList[id]!.complete(data);
25
+ this.promiseList[id]?.complete(data);
26
26
  delete this.promiseList[id];
27
27
  }
28
28
 
29
29
  error(id: string, e: Error) {
30
- this.promiseList[id]!.error(e);
30
+ this.promiseList[id]?.error(e);
31
31
  delete this.promiseList[id];
32
32
  }
33
33
 
@@ -6,7 +6,7 @@ interface IConstructorOf<T> extends Object {
6
6
 
7
7
  interface IHooks<T, TC> {
8
8
  afterConstruct?: ((obj: T) => void)[];
9
- beforeConstruct?: ((obj: TC) => T | void)[];
9
+ beforeConstruct?: ((obj: TC) => T | undefined)[];
10
10
  }
11
11
 
12
12
  export function hookClass<TC extends IConstructorOf<T>, T>(target: TC): IHooks<T, TC> {
@@ -3,8 +3,8 @@ import { hookClass } from './hookClass.js';
3
3
  export const singletonSymbol = Symbol('@gongt/singleton');
4
4
 
5
5
  export enum SingletonType {
6
- Throw,
7
- Return,
6
+ Throw = 0,
7
+ Return = 1,
8
8
  }
9
9
 
10
10
  export function singleton(type: SingletonType = SingletonType.Return): ClassDecorator {
@@ -21,9 +21,8 @@ export function singleton(type: SingletonType = SingletonType.Return): ClassDeco
21
21
  if (cls[singletonSymbol]) {
22
22
  if (type === SingletonType.Return) {
23
23
  return cls[singletonSymbol];
24
- } else {
25
- throw new Error(`Duplicate new singleton class [${target.name}]`);
26
24
  }
25
+ throw new Error(`Duplicate new singleton class [${target.name}]`);
27
26
  }
28
27
  });
29
28
  hook.afterConstruct.push((obj) => {
@@ -24,7 +24,6 @@ export const bindThis: MethodDecorator = <T>(
24
24
  return fn;
25
25
  },
26
26
  };
27
- } else {
28
- throw new TypeError('@bindThis can only use with method, but not property.');
29
27
  }
28
+ throw new TypeError('@bindThis can only use with method, but not property.');
30
29
  };
@@ -1,11 +1,11 @@
1
- import { InitFunc, initOnRead } from '../../object/initOnRead.js';
1
+ import { type InitFunc, initOnRead } from '../../object/initOnRead.js';
2
2
 
3
3
  /**
4
4
  * Decorater version of `initOnRead`
5
5
  * @see initOnRead
6
6
  */
7
7
  export function init<O, T extends keyof O>(init: InitFunc<O, O[T]>): PropertyDecorator {
8
- return function (target: O, propertyKey: T) {
8
+ return ((target: O, propertyKey: T) => {
9
9
  initOnRead<O, T>(target, propertyKey, init);
10
- } as any;
10
+ }) as any;
11
11
  }
@@ -2,7 +2,7 @@
2
2
  export function camelCase(str: string) {
3
3
  return str
4
4
  .replace(/[-.\/_]+[a-z]/g, (s) => {
5
- return s[1]!.toUpperCase();
5
+ return s[1]?.toUpperCase();
6
6
  })
7
7
  .replace(/[-.\/_]+/g, '');
8
8
  }
@@ -12,7 +12,7 @@ export function camelCase(str: string) {
12
12
  * @public
13
13
  */
14
14
  export function ucfirst<T extends string>(str: T): Capitalize<T> {
15
- return <any>(str[0]!.toUpperCase() + str.slice(1));
15
+ return <any>(str[0]?.toUpperCase() + str.slice(1));
16
16
  }
17
17
 
18
18
  /**
@@ -20,7 +20,7 @@ export function ucfirst<T extends string>(str: T): Capitalize<T> {
20
20
  * @public
21
21
  */
22
22
  export function lcfirst<T extends string>(str: T): Uncapitalize<T> {
23
- return <any>(str[0]!.toLowerCase() + str.slice(1));
23
+ return <any>(str[0]?.toLowerCase() + str.slice(1));
24
24
  }
25
25
 
26
26
  /** @public */
@@ -30,7 +30,7 @@ export function linux_case(str: string) {
30
30
  return s.toLowerCase();
31
31
  })
32
32
  .replace(/[A-Z]+/g, (s) => {
33
- return '_' + s.toLowerCase();
33
+ return `_${s.toLowerCase()}`;
34
34
  })
35
35
  .replace(/[-.\/_]+/g, '_');
36
36
  }
@@ -42,7 +42,7 @@ export function linux_case_hyphen(str: string) {
42
42
  return s.toLowerCase();
43
43
  })
44
44
  .replace(/[A-Z]+/g, (s) => {
45
- return '-' + s.toLowerCase();
45
+ return `-${s.toLowerCase()}`;
46
46
  })
47
47
  .replace(/[-.\/_]+/g, '-');
48
48
  }
@@ -4,8 +4,7 @@
4
4
  */
5
5
  export function pad2(s: number) {
6
6
  if (s < 10) {
7
- return '0' + s;
8
- } else {
9
- return '' + s;
7
+ return `0${s}`;
10
8
  }
9
+ return `${s}`;
11
10
  }