@celljs/core 3.8.0 → 4.0.0

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