@celljs/core 3.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.
Files changed (550) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/cell-remote.yml +3 -0
  4. package/cell.yml +15 -0
  5. package/lib/browser/application/frontend-application-state.d.ts +5 -0
  6. package/lib/browser/application/frontend-application-state.d.ts.map +1 -0
  7. package/lib/browser/application/frontend-application-state.js +17 -0
  8. package/lib/browser/application/frontend-application-state.js.map +1 -0
  9. package/lib/browser/application/frontend-application.d.ts +31 -0
  10. package/lib/browser/application/frontend-application.d.ts.map +1 -0
  11. package/lib/browser/application/frontend-application.js +145 -0
  12. package/lib/browser/application/frontend-application.js.map +1 -0
  13. package/lib/browser/application/index.d.ts +3 -0
  14. package/lib/browser/application/index.d.ts.map +1 -0
  15. package/lib/browser/application/index.js +19 -0
  16. package/lib/browser/application/index.js.map +1 -0
  17. package/lib/browser/browser.d.ts +23 -0
  18. package/lib/browser/browser.d.ts.map +1 -0
  19. package/lib/browser/browser.js +53 -0
  20. package/lib/browser/browser.js.map +1 -0
  21. package/lib/browser/index.d.ts +4 -0
  22. package/lib/browser/index.d.ts.map +1 -0
  23. package/lib/browser/index.js +20 -0
  24. package/lib/browser/index.js.map +1 -0
  25. package/lib/browser/shell/application-shell.d.ts +5 -0
  26. package/lib/browser/shell/application-shell.d.ts.map +1 -0
  27. package/lib/browser/shell/application-shell.js +21 -0
  28. package/lib/browser/shell/application-shell.js.map +1 -0
  29. package/lib/browser/shell/index.d.ts +3 -0
  30. package/lib/browser/shell/index.d.ts.map +1 -0
  31. package/lib/browser/shell/index.js +19 -0
  32. package/lib/browser/shell/index.js.map +1 -0
  33. package/lib/browser/shell/shell-protocol.d.ts +5 -0
  34. package/lib/browser/shell/shell-protocol.d.ts.map +1 -0
  35. package/lib/browser/shell/shell-protocol.js +5 -0
  36. package/lib/browser/shell/shell-protocol.js.map +1 -0
  37. package/lib/browser/static-module.d.ts +4 -0
  38. package/lib/browser/static-module.d.ts.map +1 -0
  39. package/lib/browser/static-module.js +6 -0
  40. package/lib/browser/static-module.js.map +1 -0
  41. package/lib/common/annotation/aspect.d.ts +8 -0
  42. package/lib/common/annotation/aspect.d.ts.map +1 -0
  43. package/lib/common/annotation/aspect.js +14 -0
  44. package/lib/common/annotation/aspect.js.map +1 -0
  45. package/lib/common/annotation/autowired-provider.d.ts +8 -0
  46. package/lib/common/annotation/autowired-provider.d.ts.map +1 -0
  47. package/lib/common/annotation/autowired-provider.js +31 -0
  48. package/lib/common/annotation/autowired-provider.js.map +1 -0
  49. package/lib/common/annotation/autowired.d.ts +23 -0
  50. package/lib/common/annotation/autowired.d.ts.map +1 -0
  51. package/lib/common/annotation/autowired.js +55 -0
  52. package/lib/common/annotation/autowired.js.map +1 -0
  53. package/lib/common/annotation/component.d.ts +58 -0
  54. package/lib/common/annotation/component.d.ts.map +1 -0
  55. package/lib/common/annotation/component.js +71 -0
  56. package/lib/common/annotation/component.js.map +1 -0
  57. package/lib/common/annotation/constant.d.ts +9 -0
  58. package/lib/common/annotation/constant.d.ts.map +1 -0
  59. package/lib/common/annotation/constant.js +17 -0
  60. package/lib/common/annotation/constant.js.map +1 -0
  61. package/lib/common/annotation/decorate.d.ts +2 -0
  62. package/lib/common/annotation/decorate.d.ts.map +1 -0
  63. package/lib/common/annotation/decorate.js +6 -0
  64. package/lib/common/annotation/decorate.js.map +1 -0
  65. package/lib/common/annotation/detached/autowired-provider.d.ts +3 -0
  66. package/lib/common/annotation/detached/autowired-provider.d.ts.map +1 -0
  67. package/lib/common/annotation/detached/autowired-provider.js +16 -0
  68. package/lib/common/annotation/detached/autowired-provider.js.map +1 -0
  69. package/lib/common/annotation/detached/autowired.d.ts +3 -0
  70. package/lib/common/annotation/detached/autowired.d.ts.map +1 -0
  71. package/lib/common/annotation/detached/autowired.js +13 -0
  72. package/lib/common/annotation/detached/autowired.js.map +1 -0
  73. package/lib/common/annotation/detached/index.d.ts +4 -0
  74. package/lib/common/annotation/detached/index.d.ts.map +1 -0
  75. package/lib/common/annotation/detached/index.js +20 -0
  76. package/lib/common/annotation/detached/index.js.map +1 -0
  77. package/lib/common/annotation/detached/value.d.ts +3 -0
  78. package/lib/common/annotation/detached/value.d.ts.map +1 -0
  79. package/lib/common/annotation/detached/value.js +13 -0
  80. package/lib/common/annotation/detached/value.js.map +1 -0
  81. package/lib/common/annotation/index.d.ts +18 -0
  82. package/lib/common/annotation/index.d.ts.map +1 -0
  83. package/lib/common/annotation/index.js +34 -0
  84. package/lib/common/annotation/index.js.map +1 -0
  85. package/lib/common/annotation/inject.d.ts +2 -0
  86. package/lib/common/annotation/inject.d.ts.map +1 -0
  87. package/lib/common/annotation/inject.js +6 -0
  88. package/lib/common/annotation/inject.js.map +1 -0
  89. package/lib/common/annotation/injectable.d.ts +2 -0
  90. package/lib/common/annotation/injectable.d.ts.map +1 -0
  91. package/lib/common/annotation/injectable.js +6 -0
  92. package/lib/common/annotation/injectable.js.map +1 -0
  93. package/lib/common/annotation/named.d.ts +2 -0
  94. package/lib/common/annotation/named.d.ts.map +1 -0
  95. package/lib/common/annotation/named.js +6 -0
  96. package/lib/common/annotation/named.js.map +1 -0
  97. package/lib/common/annotation/optional.d.ts +2 -0
  98. package/lib/common/annotation/optional.d.ts.map +1 -0
  99. package/lib/common/annotation/optional.js +6 -0
  100. package/lib/common/annotation/optional.js.map +1 -0
  101. package/lib/common/annotation/post-construct.d.ts +2 -0
  102. package/lib/common/annotation/post-construct.d.ts.map +1 -0
  103. package/lib/common/annotation/post-construct.js +6 -0
  104. package/lib/common/annotation/post-construct.js.map +1 -0
  105. package/lib/common/annotation/pre-destroy.d.ts +2 -0
  106. package/lib/common/annotation/pre-destroy.d.ts.map +1 -0
  107. package/lib/common/annotation/pre-destroy.js +6 -0
  108. package/lib/common/annotation/pre-destroy.js.map +1 -0
  109. package/lib/common/annotation/service.d.ts +5 -0
  110. package/lib/common/annotation/service.d.ts.map +1 -0
  111. package/lib/common/annotation/service.js +18 -0
  112. package/lib/common/annotation/service.js.map +1 -0
  113. package/lib/common/annotation/tagged.d.ts +2 -0
  114. package/lib/common/annotation/tagged.d.ts.map +1 -0
  115. package/lib/common/annotation/tagged.js +6 -0
  116. package/lib/common/annotation/tagged.js.map +1 -0
  117. package/lib/common/annotation/target-name.d.ts +2 -0
  118. package/lib/common/annotation/target-name.d.ts.map +1 -0
  119. package/lib/common/annotation/target-name.js +6 -0
  120. package/lib/common/annotation/target-name.js.map +1 -0
  121. package/lib/common/annotation/unmanaged.d.ts +2 -0
  122. package/lib/common/annotation/unmanaged.d.ts.map +1 -0
  123. package/lib/common/annotation/unmanaged.js +6 -0
  124. package/lib/common/annotation/unmanaged.js.map +1 -0
  125. package/lib/common/annotation/value.d.ts +16 -0
  126. package/lib/common/annotation/value.d.ts.map +1 -0
  127. package/lib/common/annotation/value.js +58 -0
  128. package/lib/common/annotation/value.js.map +1 -0
  129. package/lib/common/aop/aop-protocol.d.ts +41 -0
  130. package/lib/common/aop/aop-protocol.d.ts.map +1 -0
  131. package/lib/common/aop/aop-protocol.js +11 -0
  132. package/lib/common/aop/aop-protocol.js.map +1 -0
  133. package/lib/common/aop/aop-proxy-factory.d.ts +7 -0
  134. package/lib/common/aop/aop-proxy-factory.d.ts.map +1 -0
  135. package/lib/common/aop/aop-proxy-factory.js +70 -0
  136. package/lib/common/aop/aop-proxy-factory.js.map +1 -0
  137. package/lib/common/aop/class-filter.d.ts +7 -0
  138. package/lib/common/aop/class-filter.d.ts.map +1 -0
  139. package/lib/common/aop/class-filter.js +31 -0
  140. package/lib/common/aop/class-filter.js.map +1 -0
  141. package/lib/common/aop/index.d.ts +4 -0
  142. package/lib/common/aop/index.d.ts.map +1 -0
  143. package/lib/common/aop/index.js +20 -0
  144. package/lib/common/aop/index.js.map +1 -0
  145. package/lib/common/application/application-entry.d.ts +2 -0
  146. package/lib/common/application/application-entry.d.ts.map +1 -0
  147. package/lib/common/application/application-entry.js +16 -0
  148. package/lib/common/application/application-entry.js.map +1 -0
  149. package/lib/common/application/application-error.d.ts +21 -0
  150. package/lib/common/application/application-error.d.ts.map +1 -0
  151. package/lib/common/application/application-error.js +47 -0
  152. package/lib/common/application/application-error.js.map +1 -0
  153. package/lib/common/application/application-factory.d.ts +8 -0
  154. package/lib/common/application/application-factory.d.ts.map +1 -0
  155. package/lib/common/application/application-factory.js +23 -0
  156. package/lib/common/application/application-factory.js.map +1 -0
  157. package/lib/common/application/application-protocol.d.ts +54 -0
  158. package/lib/common/application/application-protocol.d.ts.map +1 -0
  159. package/lib/common/application/application-protocol.js +108 -0
  160. package/lib/common/application/application-protocol.js.map +1 -0
  161. package/lib/common/application/application-state.d.ts +4 -0
  162. package/lib/common/application/application-state.d.ts.map +1 -0
  163. package/lib/common/application/application-state.js +18 -0
  164. package/lib/common/application/application-state.js.map +1 -0
  165. package/lib/common/application/application.d.ts +7 -0
  166. package/lib/common/application/application.d.ts.map +1 -0
  167. package/lib/common/application/application.js +34 -0
  168. package/lib/common/application/application.js.map +1 -0
  169. package/lib/common/application/index.d.ts +3 -0
  170. package/lib/common/application/index.d.ts.map +1 -0
  171. package/lib/common/application/index.js +19 -0
  172. package/lib/common/application/index.js.map +1 -0
  173. package/lib/common/config/config-protocol.d.ts +5 -0
  174. package/lib/common/config/config-protocol.d.ts.map +1 -0
  175. package/lib/common/config/config-protocol.js +5 -0
  176. package/lib/common/config/config-protocol.js.map +1 -0
  177. package/lib/common/config/config-provider.d.ts +7 -0
  178. package/lib/common/config/config-provider.d.ts.map +1 -0
  179. package/lib/common/config/config-provider.js +29 -0
  180. package/lib/common/config/config-provider.js.map +1 -0
  181. package/lib/common/config/config-util.d.ts +5 -0
  182. package/lib/common/config/config-util.d.ts.map +1 -0
  183. package/lib/common/config/config-util.js +19 -0
  184. package/lib/common/config/config-util.js.map +1 -0
  185. package/lib/common/config/dynamic-config.d.ts +4 -0
  186. package/lib/common/config/dynamic-config.d.ts.map +1 -0
  187. package/lib/common/config/dynamic-config.js +6 -0
  188. package/lib/common/config/dynamic-config.js.map +1 -0
  189. package/lib/common/config/index.d.ts +4 -0
  190. package/lib/common/config/index.d.ts.map +1 -0
  191. package/lib/common/config/index.js +20 -0
  192. package/lib/common/config/index.js.map +1 -0
  193. package/lib/common/constants.d.ts +6 -0
  194. package/lib/common/constants.d.ts.map +1 -0
  195. package/lib/common/constants.js +9 -0
  196. package/lib/common/constants.js.map +1 -0
  197. package/lib/common/container/auto-bind.d.ts +4 -0
  198. package/lib/common/container/auto-bind.d.ts.map +1 -0
  199. package/lib/common/container/auto-bind.js +103 -0
  200. package/lib/common/container/auto-bind.js.map +1 -0
  201. package/lib/common/container/container-factory.d.ts +5 -0
  202. package/lib/common/container/container-factory.d.ts.map +1 -0
  203. package/lib/common/container/container-factory.js +13 -0
  204. package/lib/common/container/container-factory.js.map +1 -0
  205. package/lib/common/container/container-provider.d.ts +7 -0
  206. package/lib/common/container/container-provider.d.ts.map +1 -0
  207. package/lib/common/container/container-provider.js +26 -0
  208. package/lib/common/container/container-provider.js.map +1 -0
  209. package/lib/common/container/container-util.d.ts +10 -0
  210. package/lib/common/container/container-util.d.ts.map +1 -0
  211. package/lib/common/container/container-util.js +32 -0
  212. package/lib/common/container/container-util.js.map +1 -0
  213. package/lib/common/container/dynamic-container.d.ts +3 -0
  214. package/lib/common/container/dynamic-container.d.ts.map +1 -0
  215. package/lib/common/container/dynamic-container.js +7 -0
  216. package/lib/common/container/dynamic-container.js.map +1 -0
  217. package/lib/common/container/index.d.ts +9 -0
  218. package/lib/common/container/index.d.ts.map +1 -0
  219. package/lib/common/container/index.js +22 -0
  220. package/lib/common/container/index.js.map +1 -0
  221. package/lib/common/container/scope.d.ts +6 -0
  222. package/lib/common/container/scope.d.ts.map +1 -0
  223. package/lib/common/container/scope.js +10 -0
  224. package/lib/common/container/scope.js.map +1 -0
  225. package/lib/common/el/core-context-initializer.d.ts +7 -0
  226. package/lib/common/el/core-context-initializer.d.ts.map +1 -0
  227. package/lib/common/el/core-context-initializer.js +39 -0
  228. package/lib/common/el/core-context-initializer.js.map +1 -0
  229. package/lib/common/el/expression-compiler.d.ts +18 -0
  230. package/lib/common/el/expression-compiler.d.ts.map +1 -0
  231. package/lib/common/el/expression-compiler.js +171 -0
  232. package/lib/common/el/expression-compiler.js.map +1 -0
  233. package/lib/common/el/expression-context-provider.d.ts +10 -0
  234. package/lib/common/el/expression-context-provider.d.ts.map +1 -0
  235. package/lib/common/el/expression-context-provider.js +46 -0
  236. package/lib/common/el/expression-context-provider.js.map +1 -0
  237. package/lib/common/el/expression-handler.d.ts +12 -0
  238. package/lib/common/el/expression-handler.d.ts.map +1 -0
  239. package/lib/common/el/expression-handler.js +97 -0
  240. package/lib/common/el/expression-handler.js.map +1 -0
  241. package/lib/common/el/expression-protocol.d.ts +25 -0
  242. package/lib/common/el/expression-protocol.d.ts.map +1 -0
  243. package/lib/common/el/expression-protocol.js +9 -0
  244. package/lib/common/el/expression-protocol.js.map +1 -0
  245. package/lib/common/el/index.d.ts +7 -0
  246. package/lib/common/el/index.d.ts.map +1 -0
  247. package/lib/common/el/index.js +23 -0
  248. package/lib/common/el/index.js.map +1 -0
  249. package/lib/common/el/jexl-engine-provider.d.ts +6 -0
  250. package/lib/common/el/jexl-engine-provider.d.ts.map +1 -0
  251. package/lib/common/el/jexl-engine-provider.js +25 -0
  252. package/lib/common/el/jexl-engine-provider.js.map +1 -0
  253. package/lib/common/error/cutom-error.d.ts +4 -0
  254. package/lib/common/error/cutom-error.d.ts.map +1 -0
  255. package/lib/common/error/cutom-error.js +8 -0
  256. package/lib/common/error/cutom-error.js.map +1 -0
  257. package/lib/common/error/error-protocol.d.ts +4 -0
  258. package/lib/common/error/error-protocol.d.ts.map +1 -0
  259. package/lib/common/error/error-protocol.js +3 -0
  260. package/lib/common/error/error-protocol.js.map +1 -0
  261. package/lib/common/error/illegal-argument-error.d.ts +4 -0
  262. package/lib/common/error/illegal-argument-error.d.ts.map +1 -0
  263. package/lib/common/error/illegal-argument-error.js +8 -0
  264. package/lib/common/error/illegal-argument-error.js.map +1 -0
  265. package/lib/common/error/index.d.ts +4 -0
  266. package/lib/common/error/index.d.ts.map +1 -0
  267. package/lib/common/error/index.js +20 -0
  268. package/lib/common/error/index.js.map +1 -0
  269. package/lib/common/index.d.ts +13 -0
  270. package/lib/common/index.d.ts.map +1 -0
  271. package/lib/common/index.js +29 -0
  272. package/lib/common/index.js.map +1 -0
  273. package/lib/common/logger/abstract-logger.d.ts +14 -0
  274. package/lib/common/logger/abstract-logger.d.ts.map +1 -0
  275. package/lib/common/logger/abstract-logger.js +39 -0
  276. package/lib/common/logger/abstract-logger.js.map +1 -0
  277. package/lib/common/logger/index.d.ts +4 -0
  278. package/lib/common/logger/index.d.ts.map +1 -0
  279. package/lib/common/logger/index.js +20 -0
  280. package/lib/common/logger/index.js.map +1 -0
  281. package/lib/common/logger/logger-protocol.d.ts +16 -0
  282. package/lib/common/logger/logger-protocol.d.ts.map +1 -0
  283. package/lib/common/logger/logger-protocol.js +8 -0
  284. package/lib/common/logger/logger-protocol.js.map +1 -0
  285. package/lib/common/logger/logger.d.ts +8 -0
  286. package/lib/common/logger/logger.d.ts.map +1 -0
  287. package/lib/common/logger/logger.js +38 -0
  288. package/lib/common/logger/logger.js.map +1 -0
  289. package/lib/common/provider/component-filter-registry.d.ts +12 -0
  290. package/lib/common/provider/component-filter-registry.d.ts.map +1 -0
  291. package/lib/common/provider/component-filter-registry.js +69 -0
  292. package/lib/common/provider/component-filter-registry.js.map +1 -0
  293. package/lib/common/provider/index.d.ts +6 -0
  294. package/lib/common/provider/index.d.ts.map +1 -0
  295. package/lib/common/provider/index.js +22 -0
  296. package/lib/common/provider/index.js.map +1 -0
  297. package/lib/common/provider/provider-creator.d.ts +7 -0
  298. package/lib/common/provider/provider-creator.d.ts.map +1 -0
  299. package/lib/common/provider/provider-creator.js +22 -0
  300. package/lib/common/provider/provider-creator.js.map +1 -0
  301. package/lib/common/provider/provider-protocol.d.ts +49 -0
  302. package/lib/common/provider/provider-protocol.d.ts.map +1 -0
  303. package/lib/common/provider/provider-protocol.js +8 -0
  304. package/lib/common/provider/provider-protocol.js.map +1 -0
  305. package/lib/common/provider/provider-util.d.ts +6 -0
  306. package/lib/common/provider/provider-util.d.ts.map +1 -0
  307. package/lib/common/provider/provider-util.js +13 -0
  308. package/lib/common/provider/provider-util.js.map +1 -0
  309. package/lib/common/provider/provider.d.ts +14 -0
  310. package/lib/common/provider/provider.d.ts.map +1 -0
  311. package/lib/common/provider/provider.js +58 -0
  312. package/lib/common/provider/provider.js.map +1 -0
  313. package/lib/common/static-module.d.ts +4 -0
  314. package/lib/common/static-module.d.ts.map +1 -0
  315. package/lib/common/static-module.js +11 -0
  316. package/lib/common/static-module.js.map +1 -0
  317. package/lib/common/test/index.d.ts +2 -0
  318. package/lib/common/test/index.d.ts.map +1 -0
  319. package/lib/common/test/index.js +18 -0
  320. package/lib/common/test/index.js.map +1 -0
  321. package/lib/common/test/mock-logger.d.ts +9 -0
  322. package/lib/common/test/mock-logger.d.ts.map +1 -0
  323. package/lib/common/test/mock-logger.js +32 -0
  324. package/lib/common/test/mock-logger.js.map +1 -0
  325. package/lib/common/test/test-module.d.ts +3 -0
  326. package/lib/common/test/test-module.d.ts.map +1 -0
  327. package/lib/common/test/test-module.js +15 -0
  328. package/lib/common/test/test-module.js.map +1 -0
  329. package/lib/common/utils/annotation-util.d.ts +5 -0
  330. package/lib/common/utils/annotation-util.d.ts.map +1 -0
  331. package/lib/common/utils/annotation-util.js +27 -0
  332. package/lib/common/utils/annotation-util.js.map +1 -0
  333. package/lib/common/utils/async.d.ts +368 -0
  334. package/lib/common/utils/async.d.ts.map +1 -0
  335. package/lib/common/utils/async.js +964 -0
  336. package/lib/common/utils/async.js.map +1 -0
  337. package/lib/common/utils/cancellation.d.ts +22 -0
  338. package/lib/common/utils/cancellation.d.ts.map +1 -0
  339. package/lib/common/utils/cancellation.js +95 -0
  340. package/lib/common/utils/cancellation.js.map +1 -0
  341. package/lib/common/utils/class-util.d.ts +3 -0
  342. package/lib/common/utils/class-util.d.ts.map +1 -0
  343. package/lib/common/utils/class-util.js +24 -0
  344. package/lib/common/utils/class-util.js.map +1 -0
  345. package/lib/common/utils/disposable.d.ts +30 -0
  346. package/lib/common/utils/disposable.d.ts.map +1 -0
  347. package/lib/common/utils/disposable.js +82 -0
  348. package/lib/common/utils/disposable.js.map +1 -0
  349. package/lib/common/utils/emitter.d.ts +64 -0
  350. package/lib/common/utils/emitter.d.ts.map +1 -0
  351. package/lib/common/utils/emitter.js +226 -0
  352. package/lib/common/utils/emitter.js.map +1 -0
  353. package/lib/common/utils/event.d.ts +41 -0
  354. package/lib/common/utils/event.d.ts.map +1 -0
  355. package/lib/common/utils/event.js +90 -0
  356. package/lib/common/utils/event.js.map +1 -0
  357. package/lib/common/utils/global-util.d.ts +2 -0
  358. package/lib/common/utils/global-util.d.ts.map +1 -0
  359. package/lib/common/utils/global-util.js +19 -0
  360. package/lib/common/utils/global-util.js.map +1 -0
  361. package/lib/common/utils/index.d.ts +17 -0
  362. package/lib/common/utils/index.d.ts.map +1 -0
  363. package/lib/common/utils/index.js +33 -0
  364. package/lib/common/utils/index.js.map +1 -0
  365. package/lib/common/utils/metadata-util.d.ts +2 -0
  366. package/lib/common/utils/metadata-util.d.ts.map +1 -0
  367. package/lib/common/utils/metadata-util.js +29 -0
  368. package/lib/common/utils/metadata-util.js.map +1 -0
  369. package/lib/common/utils/os.d.ts +20 -0
  370. package/lib/common/utils/os.d.ts.map +1 -0
  371. package/lib/common/utils/os.js +50 -0
  372. package/lib/common/utils/os.js.map +1 -0
  373. package/lib/common/utils/prioritizeable.d.ts +18 -0
  374. package/lib/common/utils/prioritizeable.d.ts.map +1 -0
  375. package/lib/common/utils/prioritizeable.js +41 -0
  376. package/lib/common/utils/prioritizeable.js.map +1 -0
  377. package/lib/common/utils/promise-util.d.ts +6 -0
  378. package/lib/common/utils/promise-util.d.ts.map +1 -0
  379. package/lib/common/utils/promise-util.js +13 -0
  380. package/lib/common/utils/promise-util.js.map +1 -0
  381. package/lib/common/utils/proxy-util.d.ts +5 -0
  382. package/lib/common/utils/proxy-util.d.ts.map +1 -0
  383. package/lib/common/utils/proxy-util.js +41 -0
  384. package/lib/common/utils/proxy-util.js.map +1 -0
  385. package/lib/common/utils/types.d.ts +13 -0
  386. package/lib/common/utils/types.d.ts.map +1 -0
  387. package/lib/common/utils/types.js +3 -0
  388. package/lib/common/utils/types.js.map +1 -0
  389. package/lib/common/utils/url-util.d.ts +4 -0
  390. package/lib/common/utils/url-util.d.ts.map +1 -0
  391. package/lib/common/utils/url-util.js +58 -0
  392. package/lib/common/utils/url-util.js.map +1 -0
  393. package/lib/common/utils/uuid.d.ts +3 -0
  394. package/lib/common/utils/uuid.d.ts.map +1 -0
  395. package/lib/common/utils/uuid.js +65 -0
  396. package/lib/common/utils/uuid.js.map +1 -0
  397. package/lib/node/application/backend-application-state.d.ts +5 -0
  398. package/lib/node/application/backend-application-state.d.ts.map +1 -0
  399. package/lib/node/application/backend-application-state.js +17 -0
  400. package/lib/node/application/backend-application-state.js.map +1 -0
  401. package/lib/node/application/backend-application.d.ts +12 -0
  402. package/lib/node/application/backend-application.d.ts.map +1 -0
  403. package/lib/node/application/backend-application.js +55 -0
  404. package/lib/node/application/backend-application.js.map +1 -0
  405. package/lib/node/application/index.d.ts +3 -0
  406. package/lib/node/application/index.d.ts.map +1 -0
  407. package/lib/node/application/index.js +19 -0
  408. package/lib/node/application/index.js.map +1 -0
  409. package/lib/node/constants.d.ts +16 -0
  410. package/lib/node/constants.d.ts.map +1 -0
  411. package/lib/node/constants.js +19 -0
  412. package/lib/node/constants.js.map +1 -0
  413. package/lib/node/context/context.d.ts +16 -0
  414. package/lib/node/context/context.d.ts.map +1 -0
  415. package/lib/node/context/context.js +60 -0
  416. package/lib/node/context/context.js.map +1 -0
  417. package/lib/node/context/index.d.ts +2 -0
  418. package/lib/node/context/index.d.ts.map +1 -0
  419. package/lib/node/context/index.js +18 -0
  420. package/lib/node/context/index.js.map +1 -0
  421. package/lib/node/index.d.ts +4 -0
  422. package/lib/node/index.d.ts.map +1 -0
  423. package/lib/node/index.js +20 -0
  424. package/lib/node/index.js.map +1 -0
  425. package/lib/node/static-module.d.ts +4 -0
  426. package/lib/node/static-module.d.ts.map +1 -0
  427. package/lib/node/static-module.js +6 -0
  428. package/lib/node/static-module.js.map +1 -0
  429. package/lib/node/utils/crc.d.ts +2 -0
  430. package/lib/node/utils/crc.d.ts.map +1 -0
  431. package/lib/node/utils/crc.js +11 -0
  432. package/lib/node/utils/crc.js.map +1 -0
  433. package/lib/node/utils/index.d.ts +2 -0
  434. package/lib/node/utils/index.d.ts.map +1 -0
  435. package/lib/node/utils/index.js +18 -0
  436. package/lib/node/utils/index.js.map +1 -0
  437. package/lib/package.spec.d.ts +1 -0
  438. package/lib/package.spec.d.ts.map +1 -0
  439. package/lib/package.spec.js +4 -0
  440. package/lib/package.spec.js.map +1 -0
  441. package/package.json +55 -0
  442. package/src/browser/application/frontend-application-state.ts +11 -0
  443. package/src/browser/application/frontend-application.ts +142 -0
  444. package/src/browser/application/index.ts +2 -0
  445. package/src/browser/browser.ts +59 -0
  446. package/src/browser/index.ts +3 -0
  447. package/src/browser/shell/application-shell.ts +10 -0
  448. package/src/browser/shell/index.ts +2 -0
  449. package/src/browser/shell/shell-protocol.ts +5 -0
  450. package/src/browser/static-module.ts +4 -0
  451. package/src/common/annotation/aspect.ts +18 -0
  452. package/src/common/annotation/autowired-provider.ts +31 -0
  453. package/src/common/annotation/autowired.ts +76 -0
  454. package/src/common/annotation/component.ts +120 -0
  455. package/src/common/annotation/constant.ts +20 -0
  456. package/src/common/annotation/decorate.ts +1 -0
  457. package/src/common/annotation/detached/autowired-provider.ts +13 -0
  458. package/src/common/annotation/detached/autowired.ts +9 -0
  459. package/src/common/annotation/detached/index.ts +3 -0
  460. package/src/common/annotation/detached/value.ts +9 -0
  461. package/src/common/annotation/index.ts +17 -0
  462. package/src/common/annotation/inject.ts +1 -0
  463. package/src/common/annotation/injectable.ts +1 -0
  464. package/src/common/annotation/named.ts +1 -0
  465. package/src/common/annotation/optional.ts +1 -0
  466. package/src/common/annotation/post-construct.ts +1 -0
  467. package/src/common/annotation/pre-destroy.ts +1 -0
  468. package/src/common/annotation/service.ts +15 -0
  469. package/src/common/annotation/tagged.ts +1 -0
  470. package/src/common/annotation/target-name.ts +1 -0
  471. package/src/common/annotation/unmanaged.ts +1 -0
  472. package/src/common/annotation/value.ts +64 -0
  473. package/src/common/aop/aop-protocol.ts +57 -0
  474. package/src/common/aop/aop-proxy-factory.ts +61 -0
  475. package/src/common/aop/class-filter.ts +22 -0
  476. package/src/common/aop/index.ts +3 -0
  477. package/src/common/application/application-entry.ts +14 -0
  478. package/src/common/application/application-error.ts +57 -0
  479. package/src/common/application/application-factory.ts +20 -0
  480. package/src/common/application/application-protocol.ts +143 -0
  481. package/src/common/application/application-state.ts +7 -0
  482. package/src/common/application/application.ts +20 -0
  483. package/src/common/application/index.ts +2 -0
  484. package/src/common/config/config-protocol.ts +5 -0
  485. package/src/common/config/config-provider.ts +15 -0
  486. package/src/common/config/config-util.ts +14 -0
  487. package/src/common/config/dynamic-config.ts +2 -0
  488. package/src/common/config/index.ts +3 -0
  489. package/src/common/constants.ts +6 -0
  490. package/src/common/container/auto-bind.ts +101 -0
  491. package/src/common/container/container-factory.ts +9 -0
  492. package/src/common/container/container-provider.ts +23 -0
  493. package/src/common/container/container-util.ts +28 -0
  494. package/src/common/container/dynamic-container.ts +4 -0
  495. package/src/common/container/index.ts +9 -0
  496. package/src/common/container/scope.ts +3 -0
  497. package/src/common/el/core-context-initializer.ts +25 -0
  498. package/src/common/el/expression-compiler.ts +157 -0
  499. package/src/common/el/expression-context-provider.ts +34 -0
  500. package/src/common/el/expression-handler.ts +84 -0
  501. package/src/common/el/expression-protocol.ts +36 -0
  502. package/src/common/el/index.ts +6 -0
  503. package/src/common/el/jexl-engine-provider.ts +18 -0
  504. package/src/common/error/cutom-error.ts +5 -0
  505. package/src/common/error/error-protocol.ts +4 -0
  506. package/src/common/error/illegal-argument-error.ts +5 -0
  507. package/src/common/error/index.ts +3 -0
  508. package/src/common/index.ts +12 -0
  509. package/src/common/logger/abstract-logger.ts +32 -0
  510. package/src/common/logger/index.ts +3 -0
  511. package/src/common/logger/logger-protocol.ts +23 -0
  512. package/src/common/logger/logger.ts +31 -0
  513. package/src/common/provider/component-filter-registry.ts +56 -0
  514. package/src/common/provider/index.ts +6 -0
  515. package/src/common/provider/provider-creator.ts +12 -0
  516. package/src/common/provider/provider-protocol.ts +59 -0
  517. package/src/common/provider/provider-util.ts +10 -0
  518. package/src/common/provider/provider.ts +63 -0
  519. package/src/common/static-module.ts +9 -0
  520. package/src/common/test/index.ts +1 -0
  521. package/src/common/test/mock-logger.ts +21 -0
  522. package/src/common/test/test-module.ts +12 -0
  523. package/src/common/utils/annotation-util.ts +21 -0
  524. package/src/common/utils/async.ts +1222 -0
  525. package/src/common/utils/cancellation.ts +112 -0
  526. package/src/common/utils/class-util.ts +20 -0
  527. package/src/common/utils/disposable.ts +94 -0
  528. package/src/common/utils/emitter.ts +268 -0
  529. package/src/common/utils/event.ts +124 -0
  530. package/src/common/utils/global-util.ts +16 -0
  531. package/src/common/utils/index.ts +16 -0
  532. package/src/common/utils/metadata-util.ts +24 -0
  533. package/src/common/utils/os.ts +49 -0
  534. package/src/common/utils/prioritizeable.ts +44 -0
  535. package/src/common/utils/promise-util.ts +9 -0
  536. package/src/common/utils/proxy-util.ts +36 -0
  537. package/src/common/utils/types.ts +13 -0
  538. package/src/common/utils/url-util.ts +64 -0
  539. package/src/common/utils/uuid.ts +75 -0
  540. package/src/node/application/backend-application-state.ts +9 -0
  541. package/src/node/application/backend-application.ts +43 -0
  542. package/src/node/application/index.ts +2 -0
  543. package/src/node/constants.ts +16 -0
  544. package/src/node/context/context.ts +58 -0
  545. package/src/node/context/index.ts +1 -0
  546. package/src/node/index.ts +3 -0
  547. package/src/node/static-module.ts +4 -0
  548. package/src/node/utils/crc.ts +7 -0
  549. package/src/node/utils/index.ts +1 -0
  550. package/src/package.spec.ts +4 -0
@@ -0,0 +1,1222 @@
1
+ /* eslint-disable @typescript-eslint/no-shadow */
2
+ import { CancellationError, CancellationToken, CancellationTokenSource } from './cancellation';
3
+ import { Disposable } from './disposable';
4
+ import { Emitter } from './emitter';
5
+ import { Event } from './event';
6
+
7
+ interface Thenable<T> {
8
+ then<TResult>(onfulfilled?: (value: T) => TResult | Thenable<TResult>, onrejected?: (reason: any) => TResult | Thenable<TResult>): Thenable<TResult>;
9
+ then<TResult>(onfulfilled?: (value: T) => TResult | Thenable<TResult>, onrejected?: (reason: any) => void): Thenable<TResult>;
10
+ }
11
+
12
+ export function isThenable<T>(obj: unknown): obj is Promise<T> {
13
+ return !!obj && typeof (obj as unknown as Promise<T>).then === 'function';
14
+ }
15
+
16
+ export interface CancelablePromise<T> extends Promise<T> {
17
+ cancel(): void;
18
+ }
19
+
20
+ /**
21
+ * A function to allow a promise resolution to be delayed by a number of milliseconds. Usage is as follows:
22
+ *
23
+ * `const stringValue = await myPromise.then(delay(600)).then(value => value.toString());`
24
+ *
25
+ * @param ms the number of millisecond to delay
26
+ * @returns a function that returns a promise that returns the given value, but delayed
27
+ */
28
+ export function delay<T>(ms: number): (value: T) => Promise<T> {
29
+ return value => new Promise((resolve, reject) => { setTimeout(() => resolve(value), ms); });
30
+ }
31
+
32
+ /**
33
+ * Constructs a promise that will resolve after a given delay.
34
+ * @param ms the number of milliseconds to wait
35
+ */
36
+ export async function wait(ms: number): Promise<void> {
37
+ await delay(ms)(undefined);
38
+ }
39
+
40
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
+ export function waitForEvent<T>(event: Event<T>, ms: number, thisArg?: any, disposables?: Disposable[]): Promise<T> {
42
+ return new Promise<T>((resolve, reject) => {
43
+ const registration = setTimeout(() => {
44
+ listener.dispose();
45
+ reject(new CancellationError());
46
+ }, ms);
47
+
48
+ const listener = event((evt: T) => {
49
+ clearTimeout(registration);
50
+ listener.dispose();
51
+ resolve(evt);
52
+ }, thisArg, disposables);
53
+
54
+ });
55
+ }
56
+
57
+ export function createCancelablePromise<T>(callback: (token: CancellationToken) => Promise<T>): CancelablePromise<T> {
58
+ const source = new CancellationTokenSource();
59
+
60
+ const thenable = callback(source.token);
61
+ const promise = new Promise<T>((resolve, reject) => {
62
+ const subscription = source.token.onCancellationRequested(() => {
63
+ subscription.dispose();
64
+ source.dispose();
65
+ reject(new CancellationError());
66
+ });
67
+ Promise.resolve(thenable).then(value => {
68
+ subscription.dispose();
69
+ source.dispose();
70
+ resolve(value);
71
+ }, err => {
72
+ subscription.dispose();
73
+ source.dispose();
74
+ reject(err);
75
+ });
76
+ });
77
+
78
+ return <CancelablePromise<T>>new class {
79
+ cancel() {
80
+ source.cancel();
81
+ }
82
+ then<TResult1 = T, TResult2 = never>(resolve?: ((value: T) => TResult1 | Promise<TResult1>) | undefined | undefined,
83
+ reject?: ((reason: any) => TResult2 | Promise<TResult2>) | undefined | undefined): Promise<TResult1 | TResult2> {
84
+ return promise.then(resolve, reject);
85
+ }
86
+ catch<TResult = never>(reject?: ((reason: any) => TResult | Promise<TResult>) | undefined | undefined): Promise<T | TResult> {
87
+ return this.then(undefined, reject);
88
+ }
89
+ finally(onfinally?: (() => void) | undefined | undefined): Promise<T> {
90
+ return promise.finally(onfinally);
91
+ }
92
+ };
93
+ }
94
+
95
+ export function raceCancellation<T>(promise: Promise<T>, token: CancellationToken): Promise<T | undefined>;
96
+
97
+ export function raceCancellation<T>(promise: Promise<T>, token: CancellationToken, defaultValue: T): Promise<T>;
98
+
99
+ export function raceCancellation<T>(promise: Promise<T>, token: CancellationToken, defaultValue?: T): Promise<T | undefined> {
100
+ return new Promise((resolve, reject) => {
101
+ const ref = token.onCancellationRequested(() => {
102
+ ref.dispose();
103
+ resolve(defaultValue);
104
+ });
105
+ promise.then(resolve, reject).finally(() => ref.dispose());
106
+ });
107
+ }
108
+
109
+ export function raceCancellationError<T>(promise: Promise<T>, token: CancellationToken): Promise<T> {
110
+ return new Promise((resolve, reject) => {
111
+ const ref = token.onCancellationRequested(() => {
112
+ ref.dispose();
113
+ reject(new CancellationError());
114
+ });
115
+ promise.then(resolve, reject).finally(() => ref.dispose());
116
+ });
117
+ }
118
+
119
+ export async function raceCancellablePromises<T>(cancellablePromises: CancelablePromise<T>[]): Promise<T> {
120
+ let resolvedPromiseIndex = -1;
121
+ const promises = cancellablePromises.map((promise, index) => promise.then(result => { resolvedPromiseIndex = index; return result; }));
122
+ const result = await Promise.race(promises);
123
+ cancellablePromises.forEach((cancellablePromise, index) => {
124
+ if (index !== resolvedPromiseIndex) {
125
+ cancellablePromise.cancel();
126
+ }
127
+ });
128
+ return result;
129
+ }
130
+
131
+ export function raceTimeout<T>(promise: Promise<T>, timeout: number, onTimeout?: () => void): Promise<T | undefined> {
132
+ let promiseResolve: ((value: T | undefined) => void) | undefined = undefined;
133
+
134
+ const timer = setTimeout(() => {
135
+ promiseResolve?.(undefined);
136
+ onTimeout?.();
137
+ }, timeout);
138
+
139
+ return Promise.race([
140
+ promise.finally(() => clearTimeout(timer)),
141
+ new Promise<T | undefined>(resolve => promiseResolve = resolve)
142
+ ]);
143
+ }
144
+
145
+ export function asPromise<T>(callback: () => T | Thenable<T>): Promise<T> {
146
+ return new Promise<T>((resolve, reject) => {
147
+ const item = callback();
148
+ if (isThenable<T>(item)) {
149
+ item.then(resolve, reject);
150
+ } else {
151
+ resolve(item);
152
+ }
153
+ });
154
+ }
155
+
156
+ export interface ITask<T> {
157
+ (): T;
158
+ }
159
+
160
+ /**
161
+ * A helper to prevent accumulation of sequential async tasks.
162
+ *
163
+ * Imagine a mail man with the sole task of delivering letters. As soon as
164
+ * a letter submitted for delivery, he drives to the destination, delivers it
165
+ * and returns to his base. Imagine that during the trip, N more letters were submitted.
166
+ * When the mail man returns, he picks those N letters and delivers them all in a
167
+ * single trip. Even though N+1 submissions occurred, only 2 deliveries were made.
168
+ *
169
+ * The throttler implements this via the queue() method, by providing it a task
170
+ * factory. Following the example:
171
+ *
172
+ * const throttler = new Throttler();
173
+ * const letters = [];
174
+ *
175
+ * function deliver() {
176
+ * const lettersToDeliver = letters;
177
+ * letters = [];
178
+ * return makeTheTrip(lettersToDeliver);
179
+ * }
180
+ *
181
+ * function onLetterReceived(l) {
182
+ * letters.push(l);
183
+ * throttler.queue(deliver);
184
+ * }
185
+ */
186
+ export class Throttler {
187
+
188
+ private activePromise: Promise<any> | undefined;
189
+ private queuedPromise: Promise<any> | undefined;
190
+ private queuedPromiseFactory: ITask<Promise<any>> | undefined;
191
+
192
+ constructor() {
193
+ this.activePromise = undefined;
194
+ this.queuedPromise = undefined;
195
+ this.queuedPromiseFactory = undefined;
196
+ }
197
+
198
+ queue<T>(promiseFactory: ITask<Promise<T>>): Promise<T> {
199
+ if (this.activePromise) {
200
+ this.queuedPromiseFactory = promiseFactory;
201
+
202
+ if (!this.queuedPromise) {
203
+ const onComplete = () => {
204
+ this.queuedPromise = undefined;
205
+
206
+ const result = this.queue(this.queuedPromiseFactory!);
207
+ this.queuedPromiseFactory = undefined;
208
+
209
+ return result;
210
+ };
211
+
212
+ this.queuedPromise = new Promise(resolve => {
213
+ this.activePromise!.then(onComplete, onComplete).then(resolve);
214
+ });
215
+ }
216
+
217
+ return new Promise((resolve, reject) => {
218
+ this.queuedPromise!.then(resolve, reject);
219
+ });
220
+ }
221
+
222
+ this.activePromise = promiseFactory();
223
+
224
+ return new Promise((resolve, reject) => {
225
+ this.activePromise!.then((result: T) => {
226
+ this.activePromise = undefined;
227
+ resolve(result);
228
+ }, (err: unknown) => {
229
+ this.activePromise = undefined;
230
+ reject(err);
231
+ });
232
+ });
233
+ }
234
+ }
235
+
236
+ export class Sequencer {
237
+
238
+ private current: Promise<unknown> = Promise.resolve(undefined);
239
+
240
+ queue<T>(promiseTask: ITask<Promise<T>>): Promise<T> {
241
+ return this.current = this.current.then(() => promiseTask(), () => promiseTask());
242
+ }
243
+ }
244
+
245
+ export class SequencerByKey<TKey> {
246
+
247
+ private promiseMap = new Map<TKey, Promise<unknown>>();
248
+
249
+ queue<T>(key: TKey, promiseTask: ITask<Promise<T>>): Promise<T> {
250
+ const runningPromise = this.promiseMap.get(key) ?? Promise.resolve();
251
+ const newPromise = runningPromise
252
+ .catch(() => { })
253
+ .then(promiseTask)
254
+ .finally(() => {
255
+ if (this.promiseMap.get(key) === newPromise) {
256
+ this.promiseMap.delete(key);
257
+ }
258
+ });
259
+ this.promiseMap.set(key, newPromise);
260
+ return newPromise;
261
+ }
262
+ }
263
+
264
+ interface IScheduledLater extends Disposable {
265
+ isTriggered(): boolean;
266
+ }
267
+
268
+ const timeoutDeferred = (timeout: number, fn: () => void): IScheduledLater => {
269
+ let scheduled = true;
270
+ const handle = setTimeout(() => {
271
+ scheduled = false;
272
+ fn();
273
+ }, timeout);
274
+ return {
275
+ isTriggered: () => scheduled,
276
+ dispose: () => {
277
+ clearTimeout(handle);
278
+ scheduled = false;
279
+ },
280
+ };
281
+ };
282
+
283
+ const microtaskDeferred = (fn: () => void): IScheduledLater => {
284
+ let scheduled = true;
285
+ queueMicrotask(() => {
286
+ if (scheduled) {
287
+ scheduled = false;
288
+ fn();
289
+ }
290
+ });
291
+
292
+ return {
293
+ isTriggered: () => scheduled,
294
+ dispose: () => { scheduled = false; },
295
+ };
296
+ };
297
+
298
+ /** Can be passed into the Delayed to defer using a microtask */
299
+ export const MicrotaskDelay = Symbol('MicrotaskDelay');
300
+
301
+ /**
302
+ * A helper to delay (debounce) execution of a task that is being requested often.
303
+ *
304
+ * Following the throttler, now imagine the mail man wants to optimize the number of
305
+ * trips proactively. The trip itself can be long, so he decides not to make the trip
306
+ * as soon as a letter is submitted. Instead he waits a while, in case more
307
+ * letters are submitted. After said waiting period, if no letters were submitted, he
308
+ * decides to make the trip. Imagine that N more letters were submitted after the first
309
+ * one, all within a short period of time between each other. Even though N+1
310
+ * submissions occurred, only 1 delivery was made.
311
+ *
312
+ * The delayer offers this behavior via the trigger() method, into which both the task
313
+ * to be executed and the waiting period (delay) must be passed in as arguments. Following
314
+ * the example:
315
+ *
316
+ * const delayer = new Delayer(WAITING_PERIOD);
317
+ * const letters = [];
318
+ *
319
+ * function letterReceived(l) {
320
+ * letters.push(l);
321
+ * delayer.trigger(() => { return makeTheTrip(); });
322
+ * }
323
+ */
324
+ export class Delayer<T> implements Disposable {
325
+
326
+ private deferred: IScheduledLater | undefined;
327
+ private completionPromise: Promise<any> | undefined;
328
+ private doResolve: ((value?: any | Promise<any>) => void) | undefined;
329
+ private doReject: ((err: any) => void) | undefined;
330
+ private task: ITask<T | Promise<T>> | undefined;
331
+
332
+ constructor(public defaultDelay: number | typeof MicrotaskDelay) {
333
+ this.deferred = undefined;
334
+ this.completionPromise = undefined;
335
+ this.doResolve = undefined;
336
+ this.doReject = undefined;
337
+ this.task = undefined;
338
+ }
339
+
340
+ trigger(task: ITask<T | Promise<T>>, delay = this.defaultDelay): Promise<T> {
341
+ this.task = task;
342
+ this.cancelTimeout();
343
+
344
+ if (!this.completionPromise) {
345
+ this.completionPromise = new Promise((resolve, reject) => {
346
+ this.doResolve = resolve;
347
+ this.doReject = reject;
348
+ }).then(() => {
349
+ this.completionPromise = undefined;
350
+ this.doResolve = undefined;
351
+ if (this.task) {
352
+ const task = this.task;
353
+ this.task = undefined;
354
+ return task();
355
+ }
356
+ return undefined;
357
+ });
358
+ }
359
+
360
+ const fn = () => {
361
+ this.deferred = undefined;
362
+ this.doResolve?.(undefined);
363
+ };
364
+
365
+ this.deferred = delay === MicrotaskDelay ? microtaskDeferred(fn) : timeoutDeferred(delay, fn);
366
+
367
+ return this.completionPromise;
368
+ }
369
+
370
+ isTriggered(): boolean {
371
+ return !!this.deferred?.isTriggered();
372
+ }
373
+
374
+ cancel(): void {
375
+ this.cancelTimeout();
376
+
377
+ if (this.completionPromise) {
378
+ if (this.doReject) {
379
+ this.doReject(new CancellationError());
380
+ }
381
+ this.completionPromise = undefined;
382
+ }
383
+ }
384
+
385
+ private cancelTimeout(): void {
386
+ this.deferred?.dispose();
387
+ this.deferred = undefined;
388
+ }
389
+
390
+ dispose(): void {
391
+ this.cancel();
392
+ }
393
+ }
394
+
395
+ /**
396
+ * A helper to delay execution of a task that is being requested often, while
397
+ * preventing accumulation of consecutive executions, while the task runs.
398
+ *
399
+ * The mail man is clever and waits for a certain amount of time, before going
400
+ * out to deliver letters. While the mail man is going out, more letters arrive
401
+ * and can only be delivered once he is back. Once he is back the mail man will
402
+ * do one more trip to deliver the letters that have accumulated while he was out.
403
+ */
404
+ export class ThrottledDelayer<T> {
405
+
406
+ private delayer: Delayer<Promise<T>>;
407
+ private throttler: Throttler;
408
+
409
+ constructor(defaultDelay: number) {
410
+ this.delayer = new Delayer(defaultDelay);
411
+ this.throttler = new Throttler();
412
+ }
413
+
414
+ trigger(promiseFactory: ITask<Promise<T>>, delay?: number): Promise<T> {
415
+ return this.delayer.trigger(() => this.throttler.queue(promiseFactory), delay) as unknown as Promise<T>;
416
+ }
417
+
418
+ isTriggered(): boolean {
419
+ return this.delayer.isTriggered();
420
+ }
421
+
422
+ cancel(): void {
423
+ this.delayer.cancel();
424
+ }
425
+
426
+ dispose(): void {
427
+ this.delayer.dispose();
428
+ }
429
+ }
430
+
431
+ /**
432
+ * A barrier that is initially closed and then becomes opened permanently.
433
+ */
434
+ export class Barrier {
435
+
436
+ private _isOpen: boolean;
437
+ private _promise: Promise<boolean>;
438
+ private _completePromise!: (v: boolean) => void;
439
+
440
+ constructor() {
441
+ this._isOpen = false;
442
+ this._promise = new Promise<boolean>((c, e) => {
443
+ this._completePromise = c;
444
+ });
445
+ }
446
+
447
+ isOpen(): boolean {
448
+ return this._isOpen;
449
+ }
450
+
451
+ open(): void {
452
+ this._isOpen = true;
453
+ this._completePromise(true);
454
+ }
455
+
456
+ wait(): Promise<boolean> {
457
+ return this._promise;
458
+ }
459
+ }
460
+
461
+ /**
462
+ * A barrier that is initially closed and then becomes opened permanently after a certain period of
463
+ * time or when open is called explicitly
464
+ */
465
+ export class AutoOpenBarrier extends Barrier {
466
+
467
+ private readonly _timeout: any;
468
+
469
+ constructor(autoOpenTimeMs: number) {
470
+ super();
471
+ this._timeout = setTimeout(() => this.open(), autoOpenTimeMs);
472
+ }
473
+
474
+ override open(): void {
475
+ clearTimeout(this._timeout);
476
+ super.open();
477
+ }
478
+ }
479
+
480
+ export function timeout(millis: number): CancelablePromise<void>;
481
+ export function timeout(millis: number, token: CancellationToken): Promise<void>;
482
+ export function timeout(millis: number, token?: CancellationToken): CancelablePromise<void> | Promise<void> {
483
+ if (!token) {
484
+ return createCancelablePromise(token => timeout(millis, token));
485
+ }
486
+
487
+ return new Promise((resolve, reject) => {
488
+ const handle = setTimeout(() => {
489
+ disposable.dispose();
490
+ resolve();
491
+ }, millis);
492
+ const disposable = token.onCancellationRequested(() => {
493
+ clearTimeout(handle);
494
+ disposable.dispose();
495
+ reject(new CancellationError());
496
+ });
497
+ });
498
+ }
499
+
500
+ export function disposableTimeout(handler: () => void, timeout = 0): Disposable {
501
+ const timer = setTimeout(handler, timeout);
502
+ return Disposable.create(() => clearTimeout(timer));
503
+ }
504
+
505
+ /**
506
+ * Runs the provided list of promise factories in sequential order. The returned
507
+ * promise will complete to an array of results from each promise.
508
+ */
509
+
510
+ export function sequence<T>(promiseFactories: ITask<Promise<T>>[]): Promise<T[]> {
511
+ const results: T[] = [];
512
+ let index = 0;
513
+ const len = promiseFactories.length;
514
+
515
+ function next(): Promise<T> | undefined {
516
+ return index < len ? promiseFactories[index++]() : undefined;
517
+ }
518
+
519
+ function thenHandler(result: any): Promise<any> {
520
+ if (result !== undefined && result !== undefined) {
521
+ results.push(result);
522
+ }
523
+
524
+ const n = next();
525
+ if (n) {
526
+ return n.then(thenHandler);
527
+ }
528
+
529
+ return Promise.resolve(results);
530
+ }
531
+
532
+ return Promise.resolve(undefined).then(thenHandler);
533
+ }
534
+
535
+ export function first<T>(promiseFactories: ITask<Promise<T>>[], shouldStop: (t: T) => boolean = t => !!t, defaultValue: T | undefined = undefined): Promise<T | undefined> {
536
+ let index = 0;
537
+ const len = promiseFactories.length;
538
+
539
+ const loop: () => Promise<T | undefined> = () => {
540
+ if (index >= len) {
541
+ return Promise.resolve(defaultValue);
542
+ }
543
+
544
+ const factory = promiseFactories[index++];
545
+ const promise = Promise.resolve(factory());
546
+
547
+ return promise.then(result => {
548
+ if (shouldStop(result)) {
549
+ return Promise.resolve(result);
550
+ }
551
+
552
+ return loop();
553
+ });
554
+ };
555
+
556
+ return loop();
557
+ }
558
+
559
+ /**
560
+ * Returns the result of the first promise that matches the "shouldStop",
561
+ * running all promises in parallel. Supports cancelable promises.
562
+ */
563
+ export function firstParallel<T>(promiseList: Promise<T>[], shouldStop?: (t: T) => boolean, defaultValue?: T | undefined): Promise<T | undefined>;
564
+ export function firstParallel<T, R extends T>(promiseList: Promise<T>[], shouldStop: (t: T) => t is R, defaultValue?: R | undefined): Promise<R | undefined>;
565
+ export function firstParallel<T>(promiseList: Promise<T>[], shouldStop: (t: T) => boolean = t => !!t, defaultValue: T | undefined = undefined) {
566
+ if (promiseList.length === 0) {
567
+ return Promise.resolve(defaultValue);
568
+ }
569
+
570
+ let todo = promiseList.length;
571
+ const finish = () => {
572
+ todo = -1;
573
+ for (const promise of promiseList) {
574
+ (promise as Partial<CancelablePromise<T>>).cancel?.();
575
+ }
576
+ };
577
+
578
+ return new Promise<T | undefined>((resolve, reject) => {
579
+ for (const promise of promiseList) {
580
+ promise.then(result => {
581
+ if (--todo >= 0 && shouldStop(result)) {
582
+ finish();
583
+ resolve(result);
584
+ } else if (todo === 0) {
585
+ resolve(defaultValue);
586
+ }
587
+ })
588
+ .catch(err => {
589
+ if (--todo >= 0) {
590
+ finish();
591
+ reject(err);
592
+ }
593
+ });
594
+ }
595
+ });
596
+ }
597
+
598
+ interface ILimitedTaskFactory<T> {
599
+ factory: ITask<Promise<T>>;
600
+ c: (value: T | Promise<T>) => void;
601
+ e: (error?: unknown) => void;
602
+ }
603
+
604
+ export interface ILimiter<T> {
605
+
606
+ readonly size: number;
607
+
608
+ queue(factory: ITask<Promise<T>>): Promise<T>;
609
+ }
610
+
611
+ /**
612
+ * A helper to queue N promises and run them all with a max degree of parallelism. The helper
613
+ * ensures that at any time no more than M promises are running at the same time.
614
+ */
615
+ export class Limiter<T> implements ILimiter<T> {
616
+
617
+ private _size = 0;
618
+ private runningPromises: number;
619
+ private readonly maxDegreeOfParalellism: number;
620
+ private readonly outstandingPromises: ILimitedTaskFactory<T>[];
621
+ private readonly _onDrained: Emitter<void>;
622
+
623
+ constructor(maxDegreeOfParalellism: number) {
624
+ this.maxDegreeOfParalellism = maxDegreeOfParalellism;
625
+ this.outstandingPromises = [];
626
+ this.runningPromises = 0;
627
+ this._onDrained = new Emitter<void>();
628
+ }
629
+
630
+ /**
631
+ * An event that fires when every promise in the queue
632
+ * has started to execute. In other words: no work is
633
+ * pending to be scheduled.
634
+ *
635
+ * This is NOT an event that signals when all promises
636
+ * have finished though.
637
+ */
638
+ get onDrained(): Event<void> {
639
+ return this._onDrained.event;
640
+ }
641
+
642
+ get size(): number {
643
+ return this._size;
644
+ }
645
+
646
+ queue(factory: ITask<Promise<T>>): Promise<T> {
647
+ this._size++;
648
+
649
+ return new Promise<T>((c, e) => {
650
+ this.outstandingPromises.push({ factory, c, e });
651
+ this.consume();
652
+ });
653
+ }
654
+
655
+ private consume(): void {
656
+ while (this.outstandingPromises.length && this.runningPromises < this.maxDegreeOfParalellism) {
657
+ const iLimitedTask = this.outstandingPromises.shift()!;
658
+ this.runningPromises++;
659
+
660
+ const promise = iLimitedTask.factory();
661
+ promise.then(iLimitedTask.c, iLimitedTask.e);
662
+ promise.then(() => this.consumed(), () => this.consumed());
663
+ }
664
+ }
665
+
666
+ private consumed(): void {
667
+ this._size--;
668
+ this.runningPromises--;
669
+
670
+ if (this.outstandingPromises.length > 0) {
671
+ this.consume();
672
+ } else {
673
+ this._onDrained.fire();
674
+ }
675
+ }
676
+
677
+ dispose(): void {
678
+ this._onDrained.dispose();
679
+ }
680
+ }
681
+
682
+ /**
683
+ * A queue is handles one promise at a time and guarantees that at any time only one promise is executing.
684
+ */
685
+ export class Queue<T> extends Limiter<T> {
686
+
687
+ constructor() {
688
+ super(1);
689
+ }
690
+ }
691
+
692
+ export class TimeoutTimer implements Disposable {
693
+ private _token: any;
694
+
695
+ constructor();
696
+ constructor(runner: () => void, timeout: number);
697
+ constructor(runner?: () => void, timeout?: number) {
698
+ this._token = -1;
699
+
700
+ if (typeof runner === 'function' && typeof timeout === 'number') {
701
+ this.setIfNotSet(runner, timeout);
702
+ }
703
+ }
704
+
705
+ dispose(): void {
706
+ this.cancel();
707
+ }
708
+
709
+ cancel(): void {
710
+ if (this._token !== -1) {
711
+ clearTimeout(this._token);
712
+ this._token = -1;
713
+ }
714
+ }
715
+
716
+ cancelAndSet(runner: () => void, timeout: number): void {
717
+ this.cancel();
718
+ this._token = setTimeout(() => {
719
+ this._token = -1;
720
+ runner();
721
+ }, timeout);
722
+ }
723
+
724
+ setIfNotSet(runner: () => void, timeout: number): void {
725
+ if (this._token !== -1) {
726
+ // timer is already set
727
+ return;
728
+ }
729
+ this._token = setTimeout(() => {
730
+ this._token = -1;
731
+ runner();
732
+ }, timeout);
733
+ }
734
+ }
735
+
736
+ export class IntervalTimer implements Disposable {
737
+
738
+ private _token: any;
739
+
740
+ constructor() {
741
+ this._token = -1;
742
+ }
743
+
744
+ dispose(): void {
745
+ this.cancel();
746
+ }
747
+
748
+ cancel(): void {
749
+ if (this._token !== -1) {
750
+ clearInterval(this._token);
751
+ this._token = -1;
752
+ }
753
+ }
754
+
755
+ cancelAndSet(runner: () => void, interval: number): void {
756
+ this.cancel();
757
+ this._token = setInterval(() => {
758
+ runner();
759
+ }, interval);
760
+ }
761
+ }
762
+
763
+ export class RunOnceScheduler {
764
+
765
+ protected runner: ((...args: unknown[]) => void) | undefined;
766
+
767
+ private timeoutToken: any;
768
+ private timeout: number;
769
+ private timeoutHandler: () => void;
770
+
771
+ constructor(runner: (...args: any[]) => void, delay: number) {
772
+ this.timeoutToken = -1;
773
+ this.runner = runner;
774
+ this.timeout = delay;
775
+ this.timeoutHandler = this.onTimeout.bind(this);
776
+ }
777
+
778
+ /**
779
+ * Dispose RunOnceScheduler
780
+ */
781
+ dispose(): void {
782
+ this.cancel();
783
+ this.runner = undefined;
784
+ }
785
+
786
+ /**
787
+ * Cancel current scheduled runner (if any).
788
+ */
789
+ cancel(): void {
790
+ if (this.isScheduled()) {
791
+ clearTimeout(this.timeoutToken);
792
+ this.timeoutToken = -1;
793
+ }
794
+ }
795
+
796
+ /**
797
+ * Cancel previous runner (if any) & schedule a new runner.
798
+ */
799
+ schedule(delay = this.timeout): void {
800
+ this.cancel();
801
+ this.timeoutToken = setTimeout(this.timeoutHandler, delay);
802
+ }
803
+
804
+ get delay(): number {
805
+ return this.timeout;
806
+ }
807
+
808
+ set delay(value: number) {
809
+ this.timeout = value;
810
+ }
811
+
812
+ /**
813
+ * Returns true if scheduled.
814
+ */
815
+ isScheduled(): boolean {
816
+ return this.timeoutToken !== -1;
817
+ }
818
+
819
+ private onTimeout() {
820
+ this.timeoutToken = -1;
821
+ if (this.runner) {
822
+ this.doRun();
823
+ }
824
+ }
825
+
826
+ protected doRun(): void {
827
+ if (this.runner) {
828
+ this.runner();
829
+ }
830
+ }
831
+ }
832
+
833
+ /**
834
+ * Same as `RunOnceScheduler`, but doesn't count the time spent in sleep mode.
835
+ * > **NOTE**: Only offers 1s resolution.
836
+ *
837
+ * When calling `setTimeout` with 3hrs, and putting the computer immediately to sleep
838
+ * for 8hrs, `setTimeout` will fire **as soon as the computer wakes from sleep**. But
839
+ * this scheduler will execute 3hrs **after waking the computer from sleep**.
840
+ */
841
+ export class ProcessTimeRunOnceScheduler {
842
+
843
+ private runner: (() => void) | undefined;
844
+ private timeout: number;
845
+
846
+ private counter: number;
847
+ private intervalToken: any;
848
+ private intervalHandler: () => void;
849
+
850
+ constructor(runner: () => void, delay: number) {
851
+ if (delay % 1000 !== 0) {
852
+ console.warn(`ProcessTimeRunOnceScheduler resolution is 1s, ${delay}ms is not a multiple of 1000ms.`);
853
+ }
854
+ this.runner = runner;
855
+ this.timeout = delay;
856
+ this.counter = 0;
857
+ this.intervalToken = -1;
858
+ this.intervalHandler = this.onInterval.bind(this);
859
+ }
860
+
861
+ dispose(): void {
862
+ this.cancel();
863
+ this.runner = undefined;
864
+ }
865
+
866
+ cancel(): void {
867
+ if (this.isScheduled()) {
868
+ clearInterval(this.intervalToken);
869
+ this.intervalToken = -1;
870
+ }
871
+ }
872
+
873
+ /**
874
+ * Cancel previous runner (if any) & schedule a new runner.
875
+ */
876
+ schedule(delay = this.timeout): void {
877
+ if (delay % 1000 !== 0) {
878
+ console.warn(`ProcessTimeRunOnceScheduler resolution is 1s, ${delay}ms is not a multiple of 1000ms.`);
879
+ }
880
+ this.cancel();
881
+ this.counter = Math.ceil(delay / 1000);
882
+ this.intervalToken = setInterval(this.intervalHandler, 1000);
883
+ }
884
+
885
+ /**
886
+ * Returns true if scheduled.
887
+ */
888
+ isScheduled(): boolean {
889
+ return this.intervalToken !== -1;
890
+ }
891
+
892
+ private onInterval() {
893
+ this.counter--;
894
+ if (this.counter > 0) {
895
+ // still need to wait
896
+ return;
897
+ }
898
+
899
+ // time elapsed
900
+ clearInterval(this.intervalToken);
901
+ this.intervalToken = -1;
902
+ if (this.runner) {
903
+ this.runner();
904
+ }
905
+ }
906
+ }
907
+
908
+ export class RunOnceWorker<T> extends RunOnceScheduler {
909
+ private units: T[] = [];
910
+
911
+ constructor(runner: (units: T[]) => void, timeout: number) {
912
+ super(runner, timeout);
913
+ }
914
+
915
+ work(unit: T): void {
916
+ this.units.push(unit);
917
+
918
+ if (!this.isScheduled()) {
919
+ this.schedule();
920
+ }
921
+ }
922
+
923
+ protected override doRun(): void {
924
+ const units = this.units;
925
+ this.units = [];
926
+
927
+ if (this.runner) {
928
+ this.runner(units);
929
+ }
930
+ }
931
+
932
+ override dispose(): void {
933
+ this.units = [];
934
+
935
+ super.dispose();
936
+ }
937
+ }
938
+
939
+ export interface IThrottledWorkerOptions {
940
+
941
+ /**
942
+ * maximum of units the worker will pass onto handler at once
943
+ */
944
+ maxWorkChunkSize: number;
945
+
946
+ /**
947
+ * maximum of units the worker will keep in memory for processing
948
+ */
949
+ maxBufferedWork: number | undefined;
950
+
951
+ /**
952
+ * delay before processing the next round of chunks when chunk size exceeds limits
953
+ */
954
+ throttleDelay: number;
955
+ }
956
+
957
+ export async function retry<T>(task: ITask<Promise<T>>, delay: number, retries: number): Promise<T> {
958
+ let lastError: Error | undefined;
959
+
960
+ for (let i = 0; i < retries; i++) {
961
+ try {
962
+ return await task();
963
+ } catch (error) {
964
+ lastError = error;
965
+
966
+ await timeout(delay);
967
+ }
968
+ }
969
+
970
+ throw lastError;
971
+ }
972
+
973
+ // #region Task Sequentializer
974
+
975
+ interface IPendingTask {
976
+ taskId: number;
977
+ cancel: () => void;
978
+ promise: Promise<void>;
979
+ }
980
+
981
+ interface ISequentialTask {
982
+ promise: Promise<void>;
983
+ promiseResolve: () => void;
984
+ promiseReject: (error: Error) => void;
985
+ run: () => Promise<void>;
986
+ }
987
+
988
+ export interface ITaskSequentializerWithPendingTask {
989
+ readonly pending: Promise<void>;
990
+ }
991
+
992
+ export class TaskSequentializer {
993
+ private _pending?: IPendingTask;
994
+ private _next?: ISequentialTask;
995
+
996
+ hasPending(taskId?: number): this is ITaskSequentializerWithPendingTask {
997
+ if (!this._pending) {
998
+ return false;
999
+ }
1000
+
1001
+ if (typeof taskId === 'number') {
1002
+ return this._pending.taskId === taskId;
1003
+ }
1004
+
1005
+ return !!this._pending;
1006
+ }
1007
+
1008
+ get pending(): Promise<void> | undefined {
1009
+ return this._pending ? this._pending.promise : undefined;
1010
+ }
1011
+
1012
+ cancelPending(): void {
1013
+ this._pending?.cancel();
1014
+ }
1015
+
1016
+ setPending(taskId: number, promise: Promise<void>, onCancel?: () => void, ): Promise<void> {
1017
+ this._pending = { taskId, cancel: () => onCancel?.(), promise };
1018
+
1019
+ promise.then(() => this.donePending(taskId), () => this.donePending(taskId));
1020
+
1021
+ return promise;
1022
+ }
1023
+
1024
+ private donePending(taskId: number): void {
1025
+ if (this._pending && taskId === this._pending.taskId) {
1026
+
1027
+ // only set pending to done if the promise finished that is associated with that taskId
1028
+ this._pending = undefined;
1029
+
1030
+ // schedule the next task now that we are free if we have any
1031
+ this.triggerNext();
1032
+ }
1033
+ }
1034
+
1035
+ private triggerNext(): void {
1036
+ if (this._next) {
1037
+ const next = this._next;
1038
+ this._next = undefined;
1039
+
1040
+ // Run next task and complete on the associated promise
1041
+ next.run().then(next.promiseResolve, next.promiseReject);
1042
+ }
1043
+ }
1044
+
1045
+ setNext(run: () => Promise<void>): Promise<void> {
1046
+
1047
+ // this is our first next task, so we create associated promise with it
1048
+ // so that we can return a promise that completes when the task has
1049
+ // completed.
1050
+ if (!this._next) {
1051
+ let promiseResolve: () => void;
1052
+ let promiseReject: (error: Error) => void;
1053
+ const promise = new Promise<void>((resolve, reject) => {
1054
+ promiseResolve = resolve;
1055
+ promiseReject = reject;
1056
+ });
1057
+
1058
+ this._next = {
1059
+ run,
1060
+ promise,
1061
+ promiseResolve: promiseResolve!,
1062
+ promiseReject: promiseReject!
1063
+ };
1064
+ } else {
1065
+ this._next.run = run;
1066
+ }
1067
+
1068
+ return this._next.promise;
1069
+ }
1070
+ }
1071
+
1072
+ // #endregion
1073
+
1074
+ // #region
1075
+
1076
+ /**
1077
+ * The `IntervalCounter` allows to count the number
1078
+ * of calls to `increment()` over a duration of
1079
+ * `interval`. This utility can be used to conditionally
1080
+ * throttle a frequent task when a certain threshold
1081
+ * is reached.
1082
+ */
1083
+ export class IntervalCounter {
1084
+
1085
+ private lastIncrementTime = 0;
1086
+
1087
+ private value = 0;
1088
+
1089
+ constructor(private readonly interval: number, private readonly nowFn = () => Date.now()) { }
1090
+
1091
+ increment(): number {
1092
+ const now = this.nowFn();
1093
+
1094
+ // We are outside of the range of `interval` and as such
1095
+ // start counting from 0 and remember the time
1096
+ if (now - this.lastIncrementTime > this.interval) {
1097
+ this.lastIncrementTime = now;
1098
+ this.value = 0;
1099
+ }
1100
+
1101
+ this.value++;
1102
+
1103
+ return this.value;
1104
+ }
1105
+ }
1106
+
1107
+ // #endregion
1108
+
1109
+ // #region
1110
+
1111
+ export type ValueCallback<T = unknown> = (value: T | Promise<T>) => void;
1112
+
1113
+ /**
1114
+ * Creates a promise whose resolution or rejection can be controlled imperatively.
1115
+ */
1116
+ export class DeferredPromise<T> {
1117
+
1118
+ private completeCallback!: ValueCallback<T>;
1119
+ private errorCallback!: (err: unknown) => void;
1120
+ private rejected = false;
1121
+ private resolved = false;
1122
+
1123
+ public get isRejected() {
1124
+ return this.rejected;
1125
+ }
1126
+
1127
+ public get isResolved() {
1128
+ return this.resolved;
1129
+ }
1130
+
1131
+ public get isSettled() {
1132
+ return this.rejected || this.resolved;
1133
+ }
1134
+
1135
+ public p: Promise<T>;
1136
+
1137
+ constructor() {
1138
+ this.p = new Promise<T>((c, e) => {
1139
+ this.completeCallback = c;
1140
+ this.errorCallback = e;
1141
+ });
1142
+ }
1143
+
1144
+ public complete(value: T) {
1145
+ return new Promise<void>(resolve => {
1146
+ this.completeCallback(value);
1147
+ this.resolved = true;
1148
+ resolve();
1149
+ });
1150
+ }
1151
+
1152
+ public error(err: unknown) {
1153
+ return new Promise<void>(resolve => {
1154
+ this.errorCallback(err);
1155
+ this.rejected = true;
1156
+ resolve();
1157
+ });
1158
+ }
1159
+
1160
+ public cancel() {
1161
+ new Promise<void>(resolve => {
1162
+ this.errorCallback(new CancellationError());
1163
+ this.rejected = true;
1164
+ resolve();
1165
+ });
1166
+ }
1167
+ }
1168
+
1169
+ // #endregion
1170
+
1171
+ // #region Promises
1172
+
1173
+ export namespace Promises {
1174
+
1175
+ /**
1176
+ * A drop-in replacement for `Promise.all` with the only difference
1177
+ * that the method awaits every promise to either fulfill or reject.
1178
+ *
1179
+ * Similar to `Promise.all`, only the first error will be returned
1180
+ * if any.
1181
+ */
1182
+ export async function settled<T>(promises: Promise<T>[]): Promise<T[]> {
1183
+ let firstError: Error | undefined = undefined;
1184
+
1185
+ const result = await Promise.all(promises.map(promise => promise.then(value => value, error => {
1186
+ if (!firstError) {
1187
+ firstError = error;
1188
+ }
1189
+
1190
+ return undefined; // do not rethrow so that other promises can settle
1191
+ })));
1192
+
1193
+ if (typeof firstError !== 'undefined') {
1194
+ throw firstError;
1195
+ }
1196
+
1197
+ return result as unknown as T[]; // cast is needed and protected by the `throw` above
1198
+ }
1199
+
1200
+ /**
1201
+ * A helper to create a new `Promise<T>` with a body that is a promise
1202
+ * itself. By default, an error that raises from the async body will
1203
+ * end up as a unhandled rejection, so this utility properly awaits the
1204
+ * body and rejects the promise as a normal promise does without async
1205
+ * body.
1206
+ *
1207
+ * This method should only be used in rare cases where otherwise `async`
1208
+ * cannot be used (e.g. when callbacks are involved that require this).
1209
+ */
1210
+ export function withAsyncBody<T, E = Error>(bodyFn: (resolve: (value: T) => unknown, reject: (error: E) => unknown) => Promise<unknown>): Promise<T> {
1211
+ // eslint-disable-next-line no-async-promise-executor
1212
+ return new Promise<T>(async (resolve, reject) => {
1213
+ try {
1214
+ await bodyFn(resolve, reject);
1215
+ } catch (error) {
1216
+ reject(error);
1217
+ }
1218
+ });
1219
+ }
1220
+ }
1221
+
1222
+ // #endregion