@quantform/core 0.6.7 → 0.7.0-beta.5

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 (576) hide show
  1. package/dist/as-readonly.d.ts +3 -0
  2. package/dist/as-readonly.d.ts.map +1 -0
  3. package/dist/as-readonly.js +8 -0
  4. package/dist/{domain → asset}/asset.d.ts +1 -0
  5. package/dist/asset/asset.d.ts.map +1 -0
  6. package/dist/{domain → asset}/asset.js +8 -5
  7. package/dist/{domain → asset}/asset.spec.d.ts.map +1 -1
  8. package/dist/{domain → asset}/asset.spec.js +12 -12
  9. package/dist/asset/index.d.ts +2 -0
  10. package/dist/asset/index.d.ts.map +1 -0
  11. package/dist/{adapter/paper → asset}/index.js +1 -2
  12. package/dist/cli/dev.d.ts.map +1 -1
  13. package/dist/cli/dev.js +13 -9
  14. package/dist/cli/index.js +4 -4
  15. package/dist/cli/pull.d.ts.map +1 -1
  16. package/dist/cli/pull.js +48 -18
  17. package/dist/cli/{test.d.ts → replay.d.ts} +1 -1
  18. package/dist/cli/replay.d.ts.map +1 -0
  19. package/dist/cli/{test.js → replay.js} +16 -19
  20. package/dist/cli/run.d.ts.map +1 -1
  21. package/dist/cli/run.js +14 -9
  22. package/dist/component/distinct-until-timesamp-changed.d.ts +5 -0
  23. package/dist/component/distinct-until-timesamp-changed.d.ts.map +1 -0
  24. package/dist/component/distinct-until-timesamp-changed.js +9 -0
  25. package/dist/{domain → component}/error.d.ts +1 -1
  26. package/dist/component/error.d.ts.map +1 -0
  27. package/dist/component/index.d.ts +8 -0
  28. package/dist/component/index.d.ts.map +1 -0
  29. package/dist/{store → component}/index.js +7 -10
  30. package/dist/{domain → component}/ohlc-operator.d.ts +1 -1
  31. package/dist/component/ohlc-operator.d.ts.map +1 -0
  32. package/dist/{domain → component}/ohlc-operator.js +4 -4
  33. package/dist/component/ohlc-operator.spec.d.ts.map +1 -0
  34. package/dist/component/ohlc-operator.spec.js +110 -0
  35. package/dist/component/ohlc.d.ts.map +1 -0
  36. package/dist/{domain → component}/ohlc.spec.d.ts.map +1 -1
  37. package/dist/{domain → component}/ohlc.spec.js +3 -3
  38. package/dist/component/timeframe.d.ts.map +1 -0
  39. package/dist/core.d.ts +3 -0
  40. package/dist/core.d.ts.map +1 -0
  41. package/dist/core.js +15 -0
  42. package/dist/defined.d.ts +3 -0
  43. package/dist/defined.d.ts.map +1 -0
  44. package/dist/defined.js +8 -0
  45. package/dist/exclude.d.ts +3 -0
  46. package/dist/exclude.d.ts.map +1 -0
  47. package/dist/exclude.js +8 -0
  48. package/dist/index.d.ts +25 -27
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +25 -67
  51. package/dist/{domain → instrument/commission}/commission.d.ts +1 -1
  52. package/dist/instrument/commission/commission.d.ts.map +1 -0
  53. package/dist/{domain → instrument/commission}/commission.js +2 -2
  54. package/dist/instrument/commission/commission.spec.d.ts.map +1 -0
  55. package/dist/{domain → instrument/commission}/commission.spec.js +6 -6
  56. package/dist/instrument/index.d.ts +3 -0
  57. package/dist/instrument/index.d.ts.map +1 -0
  58. package/dist/{adapter → instrument}/index.js +2 -5
  59. package/dist/{domain → instrument}/instrument.d.ts +4 -4
  60. package/dist/instrument/instrument.d.ts.map +1 -0
  61. package/dist/{domain → instrument}/instrument.js +11 -10
  62. package/dist/instrument/instrument.spec.d.ts.map +1 -0
  63. package/dist/{domain → instrument}/instrument.spec.js +10 -10
  64. package/dist/make-test-module.d.ts +13 -0
  65. package/dist/make-test-module.d.ts.map +1 -0
  66. package/dist/make-test-module.js +46 -0
  67. package/dist/module.d.ts +57 -0
  68. package/dist/module.d.ts.map +1 -0
  69. package/dist/module.js +136 -0
  70. package/dist/module.spec.d.ts +2 -0
  71. package/dist/module.spec.d.ts.map +1 -0
  72. package/dist/module.spec.js +86 -0
  73. package/dist/not-found.d.ts +2 -0
  74. package/dist/not-found.d.ts.map +1 -0
  75. package/dist/{domain/component.js → not-found.js} +2 -0
  76. package/dist/replay/index.d.ts +7 -0
  77. package/dist/replay/index.d.ts.map +1 -0
  78. package/dist/replay/index.js +22 -0
  79. package/dist/replay/use-replay-coordinator.d.ts +10 -0
  80. package/dist/replay/use-replay-coordinator.d.ts.map +1 -0
  81. package/dist/replay/use-replay-coordinator.js +119 -0
  82. package/dist/replay/use-replay-options.d.ts +15 -0
  83. package/dist/replay/use-replay-options.d.ts.map +1 -0
  84. package/dist/replay/use-replay-options.js +20 -0
  85. package/dist/replay/use-replay-reader.d.ts +7 -0
  86. package/dist/replay/use-replay-reader.d.ts.map +1 -0
  87. package/dist/replay/use-replay-reader.js +32 -0
  88. package/dist/replay/use-replay-reader.spec.d.ts +2 -0
  89. package/dist/replay/use-replay-reader.spec.d.ts.map +1 -0
  90. package/dist/replay/use-replay-reader.spec.js +58 -0
  91. package/dist/replay/use-replay-storage.d.ts +10 -0
  92. package/dist/replay/use-replay-storage.d.ts.map +1 -0
  93. package/dist/replay/use-replay-storage.js +10 -0
  94. package/dist/replay/use-replay-writer.d.ts +6 -0
  95. package/dist/replay/use-replay-writer.d.ts.map +1 -0
  96. package/dist/replay/use-replay-writer.js +14 -0
  97. package/dist/replay/use-replay-writer.spec.d.ts +2 -0
  98. package/dist/replay/use-replay-writer.spec.d.ts.map +1 -0
  99. package/dist/replay/use-replay-writer.spec.js +53 -0
  100. package/dist/replay/use-replay.d.ts +10 -0
  101. package/dist/replay/use-replay.d.ts.map +1 -0
  102. package/dist/replay/use-replay.js +32 -0
  103. package/dist/replay/use-replay.spec.d.ts +2 -0
  104. package/dist/replay/use-replay.spec.d.ts.map +1 -0
  105. package/dist/replay/use-replay.spec.js +106 -0
  106. package/dist/replay/with-replay.d.ts +4 -0
  107. package/dist/replay/with-replay.d.ts.map +1 -0
  108. package/dist/replay/with-replay.js +8 -0
  109. package/dist/session/index.d.ts +4 -0
  110. package/dist/session/index.d.ts.map +1 -0
  111. package/dist/{adapter/backtester → session}/index.js +3 -4
  112. package/dist/session/use-session-object.d.ts +18 -0
  113. package/dist/session/use-session-object.d.ts.map +1 -0
  114. package/dist/session/use-session-object.js +13 -0
  115. package/dist/session/use-session-storage.d.ts +2 -0
  116. package/dist/session/use-session-storage.d.ts.map +1 -0
  117. package/dist/session/use-session-storage.js +10 -0
  118. package/dist/session/use-session.d.ts +4 -0
  119. package/dist/session/use-session.d.ts.map +1 -0
  120. package/dist/session/use-session.js +7 -0
  121. package/dist/shared/datetime.d.ts +1 -1
  122. package/dist/shared/datetime.d.ts.map +1 -1
  123. package/dist/shared/decimals.d.ts.map +1 -1
  124. package/dist/shared/decimals.js +5 -0
  125. package/dist/shared/index.d.ts +0 -4
  126. package/dist/shared/index.d.ts.map +1 -1
  127. package/dist/shared/index.js +0 -4
  128. package/dist/simulator/index.d.ts +2 -0
  129. package/dist/simulator/index.d.ts.map +1 -0
  130. package/dist/simulator/index.js +17 -0
  131. package/dist/simulator/use-simulator.d.ts +2 -0
  132. package/dist/simulator/use-simulator.d.ts.map +1 -0
  133. package/dist/simulator/use-simulator.js +9 -0
  134. package/dist/simulator/use-simulator.spec.d.ts +2 -0
  135. package/dist/simulator/use-simulator.spec.d.ts.map +1 -0
  136. package/dist/simulator/use-simulator.spec.js +49 -0
  137. package/dist/storage/in-memory/in-memory-storage.d.ts +9 -0
  138. package/dist/storage/in-memory/in-memory-storage.d.ts.map +1 -0
  139. package/dist/storage/in-memory/in-memory-storage.factory.d.ts +6 -0
  140. package/dist/storage/in-memory/in-memory-storage.factory.d.ts.map +1 -0
  141. package/dist/storage/in-memory/in-memory-storage.factory.js +23 -0
  142. package/dist/storage/in-memory/in-memory-storage.js +72 -0
  143. package/dist/storage/in-memory/in-memory-storage.spec.d.ts +2 -0
  144. package/dist/storage/in-memory/in-memory-storage.spec.d.ts.map +1 -0
  145. package/dist/storage/in-memory/in-memory-storage.spec.js +167 -0
  146. package/dist/storage/in-memory/index.d.ts +3 -0
  147. package/dist/storage/in-memory/index.d.ts.map +1 -0
  148. package/dist/storage/in-memory/index.js +18 -0
  149. package/dist/storage/index.d.ts +4 -4
  150. package/dist/storage/index.d.ts.map +1 -1
  151. package/dist/storage/index.js +4 -4
  152. package/dist/storage/storage.d.ts +51 -61
  153. package/dist/storage/storage.d.ts.map +1 -1
  154. package/dist/storage/storage.js +20 -85
  155. package/dist/storage/use-cache.d.ts +4 -0
  156. package/dist/storage/use-cache.d.ts.map +1 -0
  157. package/dist/storage/use-cache.js +34 -0
  158. package/dist/storage/use-cache.spec.d.ts +2 -0
  159. package/dist/storage/use-cache.spec.d.ts.map +1 -0
  160. package/dist/storage/use-cache.spec.js +36 -0
  161. package/dist/storage/use-storage-factory.d.ts +10 -0
  162. package/dist/storage/use-storage-factory.d.ts.map +1 -0
  163. package/dist/storage/use-storage-factory.js +14 -0
  164. package/dist/storage/use-storage.d.ts +3 -0
  165. package/dist/storage/use-storage.d.ts.map +1 -0
  166. package/dist/storage/use-storage.js +11 -0
  167. package/dist/strat.d.ts +7 -0
  168. package/dist/strat.d.ts.map +1 -0
  169. package/dist/strat.js +7 -0
  170. package/dist/use-execution-mode.d.ts +29 -0
  171. package/dist/use-execution-mode.d.ts.map +1 -0
  172. package/dist/use-execution-mode.js +37 -0
  173. package/dist/use-hash.d.ts +5 -0
  174. package/dist/use-hash.d.ts.map +1 -0
  175. package/dist/use-hash.js +7 -0
  176. package/dist/use-hash.spec.d.ts +2 -0
  177. package/dist/use-hash.spec.d.ts.map +1 -0
  178. package/dist/use-hash.spec.js +16 -0
  179. package/dist/use-lock.d.ts +9 -0
  180. package/dist/use-lock.d.ts.map +1 -0
  181. package/dist/use-lock.js +40 -0
  182. package/dist/{shared/logger.d.ts → use-logger.d.ts} +6 -7
  183. package/dist/use-logger.d.ts.map +1 -0
  184. package/dist/use-logger.js +37 -0
  185. package/dist/use-memo.d.ts +21 -0
  186. package/dist/use-memo.d.ts.map +1 -0
  187. package/dist/use-memo.js +35 -0
  188. package/dist/use-memo.spec.d.ts +2 -0
  189. package/dist/use-memo.spec.d.ts.map +1 -0
  190. package/dist/use-memo.spec.js +35 -0
  191. package/dist/use-request.d.ts +16 -0
  192. package/dist/use-request.d.ts.map +1 -0
  193. package/dist/use-request.js +27 -0
  194. package/dist/use-socket.d.ts +6 -0
  195. package/dist/use-socket.d.ts.map +1 -0
  196. package/dist/use-socket.js +52 -0
  197. package/dist/use-state.d.ts +4 -0
  198. package/dist/use-state.d.ts.map +1 -0
  199. package/dist/use-state.js +24 -0
  200. package/dist/use-state.spec.d.ts +2 -0
  201. package/dist/use-state.spec.d.ts.map +1 -0
  202. package/dist/use-state.spec.js +36 -0
  203. package/dist/use-timestamp.d.ts +2 -0
  204. package/dist/use-timestamp.d.ts.map +1 -0
  205. package/dist/use-timestamp.js +13 -0
  206. package/dist/use.d.ts +5 -0
  207. package/dist/use.d.ts.map +1 -0
  208. package/dist/use.js +20 -0
  209. package/dist/use.spec.d.ts +2 -0
  210. package/dist/use.spec.d.ts.map +1 -0
  211. package/dist/use.spec.js +47 -0
  212. package/package.json +13 -10
  213. package/src/as-readonly.ts +5 -0
  214. package/src/{domain → asset}/asset.spec.ts +1 -1
  215. package/src/{domain → asset}/asset.ts +5 -1
  216. package/src/asset/index.ts +1 -0
  217. package/src/cli/dev.ts +16 -11
  218. package/src/cli/index.ts +3 -3
  219. package/src/cli/pull.ts +20 -8
  220. package/src/cli/replay.ts +31 -0
  221. package/src/cli/run.ts +15 -11
  222. package/src/component/distinct-until-timesamp-changed.ts +11 -0
  223. package/src/{domain → component}/error.ts +1 -1
  224. package/src/component/index.ts +7 -0
  225. package/src/{domain → component}/ohlc-operator.spec.ts +1 -1
  226. package/src/{domain → component}/ohlc-operator.ts +1 -1
  227. package/src/{domain → component}/ohlc.spec.ts +1 -1
  228. package/src/core.ts +14 -0
  229. package/src/defined.ts +6 -0
  230. package/src/exclude.ts +9 -0
  231. package/src/index.ts +25 -79
  232. package/src/{domain → instrument/commission}/commission.spec.ts +2 -1
  233. package/src/instrument/index.ts +2 -0
  234. package/src/{domain → instrument}/instrument.spec.ts +1 -1
  235. package/src/{domain → instrument}/instrument.ts +9 -14
  236. package/src/make-test-module.ts +46 -0
  237. package/src/module.spec.ts +74 -0
  238. package/src/module.ts +156 -0
  239. package/src/not-found.ts +1 -0
  240. package/src/replay/index.ts +6 -0
  241. package/src/replay/use-replay-coordinator.ts +142 -0
  242. package/src/replay/use-replay-options.ts +23 -0
  243. package/src/replay/use-replay-reader.spec.ts +64 -0
  244. package/src/replay/use-replay-reader.ts +23 -0
  245. package/src/replay/use-replay-storage.ts +8 -0
  246. package/src/replay/use-replay-writer.spec.ts +56 -0
  247. package/src/replay/use-replay-writer.ts +17 -0
  248. package/src/replay/use-replay.spec.ts +132 -0
  249. package/src/replay/use-replay.ts +34 -0
  250. package/src/replay/with-replay.ts +10 -0
  251. package/src/session/index.ts +3 -0
  252. package/src/session/use-session-object.ts +26 -0
  253. package/src/session/use-session-storage.ts +9 -0
  254. package/src/session/use-session.ts +5 -0
  255. package/src/shared/decimals.ts +5 -0
  256. package/src/shared/index.ts +0 -4
  257. package/src/simulator/index.ts +1 -0
  258. package/src/simulator/use-simulator.spec.ts +54 -0
  259. package/src/simulator/use-simulator.ts +7 -0
  260. package/src/storage/in-memory/in-memory-storage.factory.ts +16 -0
  261. package/src/storage/in-memory/in-memory-storage.spec.ts +193 -0
  262. package/src/storage/in-memory/in-memory-storage.ts +80 -0
  263. package/src/storage/in-memory/index.ts +2 -0
  264. package/src/storage/index.ts +4 -4
  265. package/src/storage/storage.ts +63 -119
  266. package/src/storage/use-cache.spec.ts +31 -0
  267. package/src/storage/use-cache.ts +50 -0
  268. package/src/storage/use-storage-factory.ts +17 -0
  269. package/src/storage/use-storage.ts +11 -0
  270. package/src/strat.ts +7 -0
  271. package/src/use-execution-mode.ts +41 -0
  272. package/src/use-hash.spec.ts +16 -0
  273. package/src/use-hash.ts +5 -0
  274. package/src/use-lock.ts +52 -0
  275. package/src/use-logger.ts +47 -0
  276. package/src/use-memo.spec.ts +30 -0
  277. package/src/use-memo.ts +35 -0
  278. package/src/use-request.ts +47 -0
  279. package/src/use-socket.ts +59 -0
  280. package/src/use-state.spec.ts +31 -0
  281. package/src/use-state.ts +30 -0
  282. package/src/use-timestamp.ts +12 -0
  283. package/src/use.spec.ts +46 -0
  284. package/src/use.ts +33 -0
  285. package/dist/adapter/adapter-aggregate.d.ts +0 -57
  286. package/dist/adapter/adapter-aggregate.d.ts.map +0 -1
  287. package/dist/adapter/adapter-aggregate.js +0 -158
  288. package/dist/adapter/adapter.d.ts +0 -53
  289. package/dist/adapter/adapter.d.ts.map +0 -1
  290. package/dist/adapter/adapter.js +0 -20
  291. package/dist/adapter/backtester/backtester-adapter.d.ts +0 -26
  292. package/dist/adapter/backtester/backtester-adapter.d.ts.map +0 -1
  293. package/dist/adapter/backtester/backtester-adapter.js +0 -67
  294. package/dist/adapter/backtester/backtester-cursor.d.ts +0 -16
  295. package/dist/adapter/backtester/backtester-cursor.d.ts.map +0 -1
  296. package/dist/adapter/backtester/backtester-cursor.js +0 -52
  297. package/dist/adapter/backtester/backtester-cursor.spec.d.ts +0 -2
  298. package/dist/adapter/backtester/backtester-cursor.spec.d.ts.map +0 -1
  299. package/dist/adapter/backtester/backtester-cursor.spec.js +0 -47
  300. package/dist/adapter/backtester/backtester-streamer.d.ts +0 -50
  301. package/dist/adapter/backtester/backtester-streamer.d.ts.map +0 -1
  302. package/dist/adapter/backtester/backtester-streamer.js +0 -118
  303. package/dist/adapter/backtester/backtester-streamer.spec.d.ts +0 -2
  304. package/dist/adapter/backtester/backtester-streamer.spec.d.ts.map +0 -1
  305. package/dist/adapter/backtester/backtester-streamer.spec.js +0 -45
  306. package/dist/adapter/backtester/error.d.ts +0 -7
  307. package/dist/adapter/backtester/error.d.ts.map +0 -1
  308. package/dist/adapter/backtester/error.js +0 -15
  309. package/dist/adapter/backtester/index.d.ts +0 -5
  310. package/dist/adapter/backtester/index.d.ts.map +0 -1
  311. package/dist/adapter/error.d.ts +0 -10
  312. package/dist/adapter/error.d.ts.map +0 -1
  313. package/dist/adapter/error.js +0 -21
  314. package/dist/adapter/index.d.ts +0 -6
  315. package/dist/adapter/index.d.ts.map +0 -1
  316. package/dist/adapter/paper/engine/paper-engine.d.ts +0 -12
  317. package/dist/adapter/paper/engine/paper-engine.d.ts.map +0 -1
  318. package/dist/adapter/paper/engine/paper-engine.js +0 -108
  319. package/dist/adapter/paper/engine/paper-engine.spec.d.ts +0 -2
  320. package/dist/adapter/paper/engine/paper-engine.spec.d.ts.map +0 -1
  321. package/dist/adapter/paper/engine/paper-engine.spec.js +0 -63
  322. package/dist/adapter/paper/index.d.ts +0 -3
  323. package/dist/adapter/paper/index.d.ts.map +0 -1
  324. package/dist/adapter/paper/paper-adapter.d.ts +0 -28
  325. package/dist/adapter/paper/paper-adapter.d.ts.map +0 -1
  326. package/dist/adapter/paper/paper-adapter.js +0 -88
  327. package/dist/cli/test.d.ts.map +0 -1
  328. package/dist/domain/asset.d.ts.map +0 -1
  329. package/dist/domain/balance-operator.d.ts +0 -5
  330. package/dist/domain/balance-operator.d.ts.map +0 -1
  331. package/dist/domain/balance-operator.js +0 -9
  332. package/dist/domain/balance-operator.spec.d.ts +0 -2
  333. package/dist/domain/balance-operator.spec.d.ts.map +0 -1
  334. package/dist/domain/balance-operator.spec.js +0 -21
  335. package/dist/domain/balance.d.ts +0 -46
  336. package/dist/domain/balance.d.ts.map +0 -1
  337. package/dist/domain/balance.js +0 -79
  338. package/dist/domain/balance.spec.d.ts +0 -2
  339. package/dist/domain/balance.spec.d.ts.map +0 -1
  340. package/dist/domain/balance.spec.js +0 -32
  341. package/dist/domain/commission.d.ts.map +0 -1
  342. package/dist/domain/commission.spec.d.ts.map +0 -1
  343. package/dist/domain/component.d.ts +0 -6
  344. package/dist/domain/component.d.ts.map +0 -1
  345. package/dist/domain/error.d.ts.map +0 -1
  346. package/dist/domain/index.d.ts +0 -22
  347. package/dist/domain/index.d.ts.map +0 -1
  348. package/dist/domain/index.js +0 -37
  349. package/dist/domain/instrument-operator.d.ts +0 -6
  350. package/dist/domain/instrument-operator.d.ts.map +0 -1
  351. package/dist/domain/instrument-operator.js +0 -13
  352. package/dist/domain/instrument-operator.spec.d.ts +0 -2
  353. package/dist/domain/instrument-operator.spec.d.ts.map +0 -1
  354. package/dist/domain/instrument-operator.spec.js +0 -21
  355. package/dist/domain/instrument.d.ts.map +0 -1
  356. package/dist/domain/instrument.spec.d.ts.map +0 -1
  357. package/dist/domain/ohlc-operator.d.ts.map +0 -1
  358. package/dist/domain/ohlc-operator.spec.d.ts.map +0 -1
  359. package/dist/domain/ohlc-operator.spec.js +0 -110
  360. package/dist/domain/ohlc.d.ts.map +0 -1
  361. package/dist/domain/order-operator.d.ts +0 -6
  362. package/dist/domain/order-operator.d.ts.map +0 -1
  363. package/dist/domain/order-operator.js +0 -16
  364. package/dist/domain/order-operator.spec.d.ts +0 -2
  365. package/dist/domain/order-operator.spec.d.ts.map +0 -1
  366. package/dist/domain/order-operator.spec.js +0 -61
  367. package/dist/domain/order.d.ts +0 -21
  368. package/dist/domain/order.d.ts.map +0 -1
  369. package/dist/domain/order.js +0 -41
  370. package/dist/domain/order.spec.d.ts +0 -2
  371. package/dist/domain/order.spec.d.ts.map +0 -1
  372. package/dist/domain/order.spec.js +0 -27
  373. package/dist/domain/orderbook-operator.d.ts +0 -5
  374. package/dist/domain/orderbook-operator.d.ts.map +0 -1
  375. package/dist/domain/orderbook-operator.js +0 -9
  376. package/dist/domain/orderbook-operator.spec.d.ts +0 -2
  377. package/dist/domain/orderbook-operator.spec.d.ts.map +0 -1
  378. package/dist/domain/orderbook-operator.spec.js +0 -19
  379. package/dist/domain/orderbook.d.ts +0 -31
  380. package/dist/domain/orderbook.d.ts.map +0 -1
  381. package/dist/domain/orderbook.js +0 -23
  382. package/dist/domain/orderbook.spec.d.ts +0 -2
  383. package/dist/domain/orderbook.spec.d.ts.map +0 -1
  384. package/dist/domain/orderbook.spec.js +0 -11
  385. package/dist/domain/position-operator.d.ts +0 -11
  386. package/dist/domain/position-operator.d.ts.map +0 -1
  387. package/dist/domain/position-operator.js +0 -44
  388. package/dist/domain/position-operator.spec.d.ts +0 -2
  389. package/dist/domain/position-operator.spec.d.ts.map +0 -1
  390. package/dist/domain/position-operator.spec.js +0 -44
  391. package/dist/domain/position.d.ts +0 -19
  392. package/dist/domain/position.d.ts.map +0 -1
  393. package/dist/domain/position.js +0 -25
  394. package/dist/domain/position.spec.d.ts +0 -2
  395. package/dist/domain/position.spec.d.ts.map +0 -1
  396. package/dist/domain/position.spec.js +0 -28
  397. package/dist/domain/session-builder.d.ts +0 -58
  398. package/dist/domain/session-builder.d.ts.map +0 -1
  399. package/dist/domain/session-builder.js +0 -101
  400. package/dist/domain/session.d.ts +0 -77
  401. package/dist/domain/session.d.ts.map +0 -1
  402. package/dist/domain/session.js +0 -156
  403. package/dist/domain/session.spec.d.ts +0 -2
  404. package/dist/domain/session.spec.d.ts.map +0 -1
  405. package/dist/domain/session.spec.js +0 -17
  406. package/dist/domain/timeframe.d.ts.map +0 -1
  407. package/dist/domain/trade-operator.d.ts +0 -5
  408. package/dist/domain/trade-operator.d.ts.map +0 -1
  409. package/dist/domain/trade-operator.js +0 -9
  410. package/dist/domain/trade-operator.spec.d.ts +0 -2
  411. package/dist/domain/trade-operator.spec.d.ts.map +0 -1
  412. package/dist/domain/trade-operator.spec.js +0 -21
  413. package/dist/domain/trade.d.ts +0 -17
  414. package/dist/domain/trade.d.ts.map +0 -1
  415. package/dist/domain/trade.js +0 -20
  416. package/dist/domain/trade.spec.d.ts +0 -2
  417. package/dist/domain/trade.spec.d.ts.map +0 -1
  418. package/dist/domain/trade.spec.js +0 -11
  419. package/dist/shared/collections.d.ts +0 -29
  420. package/dist/shared/collections.d.ts.map +0 -1
  421. package/dist/shared/collections.js +0 -123
  422. package/dist/shared/collections.spec.d.ts +0 -2
  423. package/dist/shared/collections.spec.d.ts.map +0 -1
  424. package/dist/shared/collections.spec.js +0 -29
  425. package/dist/shared/hash.d.ts +0 -2
  426. package/dist/shared/hash.d.ts.map +0 -1
  427. package/dist/shared/hash.js +0 -12
  428. package/dist/shared/logger.d.ts.map +0 -1
  429. package/dist/shared/logger.js +0 -40
  430. package/dist/shared/policy.d.ts +0 -2
  431. package/dist/shared/policy.d.ts.map +0 -1
  432. package/dist/shared/policy.js +0 -8
  433. package/dist/storage/cache.d.ts +0 -10
  434. package/dist/storage/cache.d.ts.map +0 -1
  435. package/dist/storage/cache.js +0 -43
  436. package/dist/storage/cache.spec.d.ts +0 -2
  437. package/dist/storage/cache.spec.d.ts.map +0 -1
  438. package/dist/storage/cache.spec.js +0 -25
  439. package/dist/storage/feed.d.ts +0 -38
  440. package/dist/storage/feed.d.ts.map +0 -1
  441. package/dist/storage/feed.js +0 -108
  442. package/dist/storage/measurement.d.ts +0 -34
  443. package/dist/storage/measurement.d.ts.map +0 -1
  444. package/dist/storage/measurement.js +0 -59
  445. package/dist/store/error.d.ts +0 -23
  446. package/dist/store/error.d.ts.map +0 -1
  447. package/dist/store/error.js +0 -45
  448. package/dist/store/index.d.ts +0 -11
  449. package/dist/store/index.d.ts.map +0 -1
  450. package/dist/store/store-balance-event.d.ts +0 -26
  451. package/dist/store/store-balance-event.d.ts.map +0 -1
  452. package/dist/store/store-balance-event.js +0 -59
  453. package/dist/store/store-balance-event.spec.d.ts +0 -2
  454. package/dist/store/store-balance-event.spec.d.ts.map +0 -1
  455. package/dist/store/store-balance-event.spec.js +0 -24
  456. package/dist/store/store-event.d.ts +0 -7
  457. package/dist/store/store-event.d.ts.map +0 -1
  458. package/dist/store/store-event.js +0 -2
  459. package/dist/store/store-instrument-event.d.ts +0 -21
  460. package/dist/store/store-instrument-event.d.ts.map +0 -1
  461. package/dist/store/store-instrument-event.js +0 -59
  462. package/dist/store/store-instrument-event.spec.d.ts +0 -2
  463. package/dist/store/store-instrument-event.spec.d.ts.map +0 -1
  464. package/dist/store/store-instrument-event.spec.js +0 -22
  465. package/dist/store/store-order-event.d.ts +0 -63
  466. package/dist/store/store-order-event.d.ts.map +0 -1
  467. package/dist/store/store-order-event.js +0 -223
  468. package/dist/store/store-order-event.spec.d.ts +0 -2
  469. package/dist/store/store-order-event.spec.d.ts.map +0 -1
  470. package/dist/store/store-order-event.spec.js +0 -23
  471. package/dist/store/store-orderbook-event.d.ts +0 -12
  472. package/dist/store/store-orderbook-event.d.ts.map +0 -1
  473. package/dist/store/store-orderbook-event.js +0 -43
  474. package/dist/store/store-orderbook-event.spec.d.ts +0 -2
  475. package/dist/store/store-orderbook-event.spec.d.ts.map +0 -1
  476. package/dist/store/store-orderbook-event.spec.js +0 -27
  477. package/dist/store/store-position-event.d.ts +0 -21
  478. package/dist/store/store-position-event.d.ts.map +0 -1
  479. package/dist/store/store-position-event.js +0 -96
  480. package/dist/store/store-state.d.ts +0 -28
  481. package/dist/store/store-state.d.ts.map +0 -1
  482. package/dist/store/store-state.js +0 -29
  483. package/dist/store/store-trade-event.d.ts +0 -16
  484. package/dist/store/store-trade-event.d.ts.map +0 -1
  485. package/dist/store/store-trade-event.js +0 -34
  486. package/dist/store/store-trade-event.spec.d.ts +0 -2
  487. package/dist/store/store-trade-event.spec.d.ts.map +0 -1
  488. package/dist/store/store-trade-event.spec.js +0 -44
  489. package/dist/store/store.d.ts +0 -14
  490. package/dist/store/store.d.ts.map +0 -1
  491. package/dist/store/store.js +0 -35
  492. package/dist/store/store.spec.d.ts +0 -2
  493. package/dist/store/store.spec.d.ts.map +0 -1
  494. package/dist/store/store.spec.js +0 -122
  495. package/src/adapter/adapter-aggregate.ts +0 -185
  496. package/src/adapter/adapter.ts +0 -82
  497. package/src/adapter/backtester/backtester-adapter.ts +0 -100
  498. package/src/adapter/backtester/backtester-cursor.spec.ts +0 -44
  499. package/src/adapter/backtester/backtester-cursor.ts +0 -47
  500. package/src/adapter/backtester/backtester-streamer.spec.ts +0 -60
  501. package/src/adapter/backtester/backtester-streamer.ts +0 -158
  502. package/src/adapter/backtester/error.ts +0 -11
  503. package/src/adapter/backtester/index.ts +0 -4
  504. package/src/adapter/error.ts +0 -19
  505. package/src/adapter/index.ts +0 -5
  506. package/src/adapter/paper/engine/paper-engine.spec.ts +0 -110
  507. package/src/adapter/paper/engine/paper-engine.ts +0 -136
  508. package/src/adapter/paper/index.ts +0 -2
  509. package/src/adapter/paper/paper-adapter.ts +0 -118
  510. package/src/cli/test.ts +0 -41
  511. package/src/domain/balance-operator.spec.ts +0 -23
  512. package/src/domain/balance-operator.ts +0 -14
  513. package/src/domain/balance.spec.ts +0 -46
  514. package/src/domain/balance.ts +0 -110
  515. package/src/domain/component.ts +0 -6
  516. package/src/domain/index.ts +0 -21
  517. package/src/domain/instrument-operator.spec.ts +0 -38
  518. package/src/domain/instrument-operator.ts +0 -25
  519. package/src/domain/order-operator.spec.ts +0 -89
  520. package/src/domain/order-operator.ts +0 -24
  521. package/src/domain/order.spec.ts +0 -40
  522. package/src/domain/order.ts +0 -65
  523. package/src/domain/orderbook-operator.spec.ts +0 -42
  524. package/src/domain/orderbook-operator.ts +0 -14
  525. package/src/domain/orderbook.spec.ts +0 -23
  526. package/src/domain/orderbook.ts +0 -32
  527. package/src/domain/position-operator.spec.ts +0 -67
  528. package/src/domain/position-operator.ts +0 -74
  529. package/src/domain/position.spec.ts +0 -40
  530. package/src/domain/position.ts +0 -33
  531. package/src/domain/session-builder.ts +0 -158
  532. package/src/domain/session.spec.ts +0 -33
  533. package/src/domain/session.ts +0 -261
  534. package/src/domain/trade-operator.spec.ts +0 -30
  535. package/src/domain/trade-operator.ts +0 -14
  536. package/src/domain/trade.spec.ts +0 -18
  537. package/src/domain/trade.ts +0 -22
  538. package/src/shared/collections.spec.ts +0 -34
  539. package/src/shared/collections.ts +0 -154
  540. package/src/shared/hash.ts +0 -10
  541. package/src/shared/logger.ts +0 -46
  542. package/src/shared/policy.ts +0 -5
  543. package/src/storage/cache.spec.ts +0 -17
  544. package/src/storage/cache.ts +0 -37
  545. package/src/storage/feed.ts +0 -124
  546. package/src/storage/measurement.ts +0 -56
  547. package/src/store/error.ts +0 -47
  548. package/src/store/index.ts +0 -10
  549. package/src/store/store-balance-event.spec.ts +0 -28
  550. package/src/store/store-balance-event.ts +0 -72
  551. package/src/store/store-event.ts +0 -8
  552. package/src/store/store-instrument-event.spec.ts +0 -25
  553. package/src/store/store-instrument-event.ts +0 -87
  554. package/src/store/store-order-event.spec.ts +0 -33
  555. package/src/store/store-order-event.ts +0 -288
  556. package/src/store/store-orderbook-event.spec.ts +0 -38
  557. package/src/store/store-orderbook-event.ts +0 -60
  558. package/src/store/store-position-event.ts +0 -125
  559. package/src/store/store-state.ts +0 -48
  560. package/src/store/store-trade-event.spec.ts +0 -61
  561. package/src/store/store-trade-event.ts +0 -46
  562. package/src/store/store.spec.ts +0 -196
  563. package/src/store/store.ts +0 -40
  564. package/dist/{domain → asset}/asset.spec.d.ts +0 -0
  565. package/dist/{domain → component}/error.js +0 -0
  566. package/dist/{domain → component}/ohlc-operator.spec.d.ts +0 -0
  567. package/dist/{domain → component}/ohlc.d.ts +0 -0
  568. package/dist/{domain → component}/ohlc.js +0 -0
  569. package/dist/{domain → component}/ohlc.spec.d.ts +0 -0
  570. package/dist/{domain → component}/timeframe.d.ts +0 -0
  571. package/dist/{domain → component}/timeframe.js +1 -1
  572. /package/dist/{domain → instrument/commission}/commission.spec.d.ts +0 -0
  573. /package/dist/{domain → instrument}/instrument.spec.d.ts +0 -0
  574. /package/src/{domain → component}/ohlc.ts +0 -0
  575. /package/src/{domain → component}/timeframe.ts +0 -0
  576. /package/src/{domain → instrument/commission}/commission.ts +0 -0
@@ -1,126 +1,70 @@
1
- export type StorageDocument = {
2
- timestamp: number;
3
- kind: string;
4
- json: string;
1
+ import { decimal } from '@lib/shared';
2
+
3
+ type Types = string | number | decimal;
4
+
5
+ export const eq = <T extends Types>(value: T) => ({ type: 'eq' as const, value });
6
+ export const gt = <T extends number>(value: T) => ({ type: 'gt' as const, value });
7
+ export const lt = <T extends number>(value: T) => ({ type: 'lt' as const, value });
8
+ export const between = <T extends number>(min: T, max: T) => ({
9
+ type: 'between' as const,
10
+ min,
11
+ max
12
+ });
13
+
14
+ export type QueryObject = Record<string, Types> & { timestamp: number };
15
+ export type QueryObjectType<T extends QueryObject> = {
16
+ discriminator: string;
17
+ type: {
18
+ [key in keyof T]: QueryMappingType;
19
+ };
5
20
  };
6
21
 
7
- export type StorageQueryOptions = {
8
- from?: number;
9
- to?: number;
10
- kind?: string;
11
- count: number;
22
+ export type QueryWhere =
23
+ | ReturnType<typeof eq>
24
+ | ReturnType<typeof gt>
25
+ | ReturnType<typeof lt>
26
+ | ReturnType<typeof between>;
27
+
28
+ export type Query<T extends QueryObject> = {
29
+ where?: Partial<{
30
+ [key in keyof T]: QueryWhere;
31
+ }>;
32
+ orderBy?: 'ASC' | 'DESC';
33
+ limit?: number;
34
+ offset?: number;
12
35
  };
13
36
 
14
- /**
15
- *
16
- */
17
- export type StorageFactory = (type: string) => Storage;
18
-
19
- /**
20
- *
21
- */
22
- export interface Storage {
23
- /**
24
- *
25
- */
26
- index(): Promise<Array<string>>;
27
-
28
- /**
29
- *
30
- * @param library
31
- * @param documents
32
- */
33
- save(library: string, documents: StorageDocument[]): Promise<void>;
34
-
35
- /**
36
- *
37
- * @param library
38
- * @param options
39
- */
40
- query(library: string, options: StorageQueryOptions): Promise<StorageDocument[]>;
41
- }
42
-
43
- export function inMemoryStorageFactory(): StorageFactory {
44
- const storage: Record<string, Storage> = {};
45
-
46
- return (type: string) => storage[type] ?? (storage[type] = new InMemoryStorage());
47
- }
48
-
49
- /**
50
- *
51
- */
52
- export class InMemoryStorage implements Storage {
53
- private tables: Record<string, StorageDocument[]> = {};
54
-
55
- /**
56
- *
57
- * @returns
58
- */
59
- async index(): Promise<Array<string>> {
60
- return Object.keys(this.tables);
37
+ export type QueryMappingType = 'number' | 'string' | 'decimal';
38
+ export type InferQueryObject<T> = T extends QueryObjectType<infer U>
39
+ ? {
40
+ [key in keyof T['type']]: T['type'][key] extends 'number'
41
+ ? number
42
+ : T['type'][key] extends 'string'
43
+ ? string
44
+ : T['type'][key] extends 'decimal'
45
+ ? decimal
46
+ : never;
47
+ } & { timestamp: number }
48
+ : never;
49
+
50
+ export abstract class Storage {
51
+ static createObject<
52
+ K extends QueryObject,
53
+ T extends { [key in keyof K]: QueryMappingType }
54
+ >(discriminator: string, type: T) {
55
+ return {
56
+ discriminator,
57
+ type
58
+ };
61
59
  }
62
60
 
63
- /**
64
- *
65
- * @param library
66
- * @param options
67
- * @returns
68
- */
69
- async query(library: string, options: StorageQueryOptions): Promise<StorageDocument[]> {
70
- if (!this.tables[library]) {
71
- return [];
72
- }
73
-
74
- let query = this.tables[library];
75
-
76
- const { from, to, kind, count } = options;
77
-
78
- if (from) {
79
- query = query.filter(it => it.timestamp > from);
80
- }
81
-
82
- if (to) {
83
- query = query.filter(it => it.timestamp < to);
84
- }
85
-
86
- if (kind) {
87
- query = query.filter(it => it.kind == kind);
88
- }
89
-
90
- if (from == undefined && to) {
91
- query = query.reverse();
92
- }
93
-
94
- if (count) {
95
- query = query.slice(0, options.count);
96
- }
97
-
98
- return query;
99
- }
100
-
101
- /**
102
- *
103
- * @param library
104
- * @param documents
105
- */
106
- async save(library: string, documents: StorageDocument[]): Promise<void> {
107
- if (!this.tables[library]) {
108
- this.tables[library] = [];
109
- }
110
-
111
- const buffer = this.tables[library];
112
-
113
- for (const document of documents) {
114
- buffer.push(document);
115
- }
116
-
117
- buffer.sort((lhs, rhs) => lhs.timestamp - rhs.timestamp);
118
- }
119
-
120
- /**
121
- *
122
- */
123
- clear() {
124
- this.tables = {};
125
- }
61
+ abstract index(): Promise<Array<string>>;
62
+ abstract save<T extends QueryObjectType<K>, K extends QueryObject>(
63
+ type: T,
64
+ objects: InferQueryObject<T>[]
65
+ ): Promise<void>;
66
+ abstract query<T extends QueryObjectType<K>, K extends QueryObject>(
67
+ type: T,
68
+ query: Query<InferQueryObject<T>>
69
+ ): Promise<InferQueryObject<T>[]>;
126
70
  }
@@ -0,0 +1,31 @@
1
+ import { firstValueFrom, Observable, of } from 'rxjs';
2
+
3
+ import { makeTestModule } from '@lib/make-test-module';
4
+ import { useCache } from '@lib/storage/use-cache';
5
+ import { dependency } from '@lib/use-hash';
6
+
7
+ describe(useCache.name, () => {
8
+ let fixtures: Awaited<ReturnType<typeof getFixtures>>;
9
+
10
+ beforeEach(async () => {
11
+ fixtures = await getFixtures();
12
+ });
13
+
14
+ test('cache value for dependencies', async () => {
15
+ const value1 = await firstValueFrom(fixtures.givenCacheValue(of(1), ['val']));
16
+ const value2 = await firstValueFrom(fixtures.givenCacheValue(of(2), ['val']));
17
+
18
+ expect(value1).toEqual(1);
19
+ expect(value2).toEqual(1);
20
+ });
21
+ });
22
+
23
+ async function getFixtures() {
24
+ const { act } = await makeTestModule([]);
25
+
26
+ return {
27
+ givenCacheValue<T>(value: Observable<T>, dependencies: dependency[]) {
28
+ return act(() => useCache(value, dependencies));
29
+ }
30
+ };
31
+ }
@@ -0,0 +1,50 @@
1
+ import { from, map, Observable, of, switchMap } from 'rxjs';
2
+
3
+ import { now } from '@lib/shared';
4
+ import { useStorage } from '@lib/storage/use-storage';
5
+ import { dependency, useHash } from '@lib/use-hash';
6
+
7
+ import { eq, gt, Storage } from './storage';
8
+
9
+ const object = Storage.createObject('keyValue', {
10
+ timestamp: 'number',
11
+ forKey: 'string',
12
+ rawJson: 'string'
13
+ });
14
+
15
+ export const useCache = <T>(
16
+ calculateValue: Observable<T>,
17
+ dependencies: dependency[],
18
+ ttl: number = 60 * 60 * 24 * 1000
19
+ ): Observable<T> => {
20
+ const storage = useStorage(['cache']);
21
+ const key = useHash(dependencies);
22
+ const timestamp = now();
23
+
24
+ return from(
25
+ storage.query(object, {
26
+ where: {
27
+ timestamp: gt(timestamp - ttl),
28
+ forKey: eq(key)
29
+ },
30
+ limit: 1,
31
+ orderBy: 'DESC'
32
+ })
33
+ ).pipe(
34
+ switchMap(([value]) => {
35
+ if (value) {
36
+ return of(JSON.parse(value.rawJson));
37
+ }
38
+
39
+ return calculateValue.pipe(
40
+ switchMap(newValue =>
41
+ from(
42
+ storage.save(object, [
43
+ { timestamp, forKey: key, rawJson: JSON.stringify(newValue) }
44
+ ])
45
+ ).pipe(map(() => newValue))
46
+ )
47
+ );
48
+ })
49
+ );
50
+ };
@@ -0,0 +1,17 @@
1
+ import { useContext } from '@lib/module';
2
+ import { Storage } from '@lib/storage';
3
+
4
+ const token = Symbol('storage-factory-token');
5
+
6
+ export interface StorageFactory {
7
+ for(key: string): Storage;
8
+ }
9
+
10
+ export function storage(factory: StorageFactory) {
11
+ return {
12
+ provide: token,
13
+ useValue: factory
14
+ };
15
+ }
16
+
17
+ export const useStorageFactory = () => useContext<StorageFactory>(token);
@@ -0,0 +1,11 @@
1
+ import { use } from '@lib/use';
2
+ import { dependency, useHash } from '@lib/use-hash';
3
+
4
+ import { useStorageFactory } from './use-storage-factory';
5
+
6
+ export const useStorage = use((dependencies: dependency[]) => {
7
+ const key = useHash(dependencies);
8
+ const factory = useStorageFactory();
9
+
10
+ return factory.for(key);
11
+ });
package/src/strat.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { Observable } from 'rxjs';
2
+
3
+ import { Dependency } from './module';
4
+
5
+ export function strat(fn: () => Observable<unknown>, dependencies: Dependency[]) {
6
+ return { fn, dependencies };
7
+ }
@@ -0,0 +1,41 @@
1
+ import { useContext } from '@lib/module';
2
+
3
+ const injectionToken = Symbol('execution-mode');
4
+
5
+ type ExecutionMode = {
6
+ mode: 'REPLAY' | 'PAPER' | 'LIVE';
7
+ recording: boolean;
8
+ };
9
+
10
+ export function replayExecutionMode() {
11
+ return {
12
+ provide: injectionToken,
13
+ useValue: { mode: 'REPLAY', recording: false } as ExecutionMode
14
+ };
15
+ }
16
+
17
+ export function paperExecutionMode(options: { recording: boolean }) {
18
+ return {
19
+ provide: injectionToken,
20
+ useValue: { mode: 'PAPER', ...options } as ExecutionMode
21
+ };
22
+ }
23
+
24
+ export function liveExecutionMode(options: { recording: boolean }) {
25
+ return {
26
+ provide: injectionToken,
27
+ useValue: { mode: 'LIVE', ...options } as ExecutionMode
28
+ };
29
+ }
30
+
31
+ export const useExecutionMode = () => {
32
+ const mode = useContext<ExecutionMode>(injectionToken);
33
+
34
+ return {
35
+ isReplay: mode.mode === 'REPLAY',
36
+ isPaper: mode.mode === 'PAPER',
37
+ isLive: mode.mode === 'LIVE',
38
+ isSimulation: mode.mode !== 'LIVE',
39
+ recording: mode.recording
40
+ };
41
+ };
@@ -0,0 +1,16 @@
1
+ import { assetOf } from './asset';
2
+ import { instrumentOf } from './instrument';
3
+ import { dependency, useHash } from './use-hash';
4
+
5
+ describe(useHash.name, () => {
6
+ it.each<[dependency[], string]>([
7
+ [[assetOf('binance:btc')], 'binance:btc'],
8
+ [[assetOf('binance:btc'), 'test'], 'binance:btc/test'],
9
+ [[instrumentOf('binance:btc-usdt')], 'binance:btc-usdt'],
10
+ [[instrumentOf('binance:btc-usdt'), 123], 'binance:btc-usdt/123']
11
+ ])('hash list of dependencies from %p to %p', (dependencies, hashed) => {
12
+ const hash = useHash(dependencies);
13
+
14
+ expect(hash).toEqual(hashed);
15
+ });
16
+ });
@@ -0,0 +1,5 @@
1
+ export type dependency = { toString(): string } | undefined;
2
+
3
+ export function useHash(dependencies: dependency[]) {
4
+ return dependencies.join('/');
5
+ }
@@ -0,0 +1,52 @@
1
+ import { filter, finalize, Observable } from 'rxjs';
2
+
3
+ import { use } from '@lib/use';
4
+ import { dependency, useHash } from '@lib/use-hash';
5
+
6
+ export const useExclusiveLock = use(() => {
7
+ const locking = {} as Record<string, boolean>;
8
+ const acquire = (dependencies: dependency[]) => {
9
+ const hash = useHash(dependencies);
10
+
11
+ if (locking[hash]) {
12
+ throw Error('nested locks not allowed');
13
+ }
14
+
15
+ locking[hash] = true;
16
+ };
17
+
18
+ const release = (dependencies: dependency[]) => {
19
+ const hash = useHash(dependencies);
20
+
21
+ if (!locking[hash]) {
22
+ throw Error('nested locks not allowed');
23
+ }
24
+
25
+ locking[hash] = false;
26
+ };
27
+
28
+ const alreadyAcquired = (dependencies: dependency[]) => {
29
+ const hash = useHash(dependencies);
30
+
31
+ return locking[hash] == true;
32
+ };
33
+
34
+ return {
35
+ acquire,
36
+ release,
37
+ alreadyAcquired
38
+ };
39
+ });
40
+
41
+ export function exclusive<T>(dependencies: dependency[]) {
42
+ return (input: Observable<T>) => {
43
+ const { acquire, release, alreadyAcquired } = useExclusiveLock();
44
+
45
+ acquire(dependencies);
46
+
47
+ return input.pipe(
48
+ filter(() => alreadyAcquired(dependencies)),
49
+ finalize(() => release(dependencies))
50
+ );
51
+ };
52
+ }
@@ -0,0 +1,47 @@
1
+ import chalk from 'chalk';
2
+
3
+ import { use } from './use';
4
+ import { useTimestamp } from './use-timestamp';
5
+
6
+ const colorize = (content: string) => {
7
+ let hash = 0x811c9dc5;
8
+
9
+ for (let i = 0; i < content.length; i++) {
10
+ hash ^= content.charCodeAt(i);
11
+ hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
12
+ }
13
+
14
+ return chalk.hex('#' + (hash * 0xfffff * 1000000).toString(16).slice(0, 6))(content);
15
+ };
16
+
17
+ /**
18
+ *
19
+ */
20
+ export const useLogger = use((context: string, tint?: string) => {
21
+ const prefix = () =>
22
+ `${chalk.gray(new Date(useTimestamp()).toISOString())} ${chalk.hex(
23
+ tint ?? colorize(context)
24
+ )(context)}`;
25
+
26
+ return {
27
+ info: (message: any, ...params: unknown[]) =>
28
+ params?.length
29
+ ? console.info(`${prefix()}: ${message}`, params)
30
+ : console.info(`${prefix()}: ${message}`),
31
+
32
+ debug: (message: any, ...params: unknown[]) =>
33
+ params?.length
34
+ ? console.debug(`${prefix()}: ${message}`, params)
35
+ : console.debug(`${prefix()}: ${message}`),
36
+
37
+ warn: (message: any, ...params: unknown[]) =>
38
+ params?.length
39
+ ? console.warn(`${prefix()}: ${message}`, params)
40
+ : console.warn(`${prefix()}: ${message}`),
41
+
42
+ error: (message: any, ...params: unknown[]) =>
43
+ params?.length
44
+ ? console.error(`${prefix()}: ${message}`, params)
45
+ : console.error(`${prefix()}: ${message}`)
46
+ };
47
+ });
@@ -0,0 +1,30 @@
1
+ import { makeTestModule } from '@lib/make-test-module';
2
+ import { useMemo } from '@lib/use-memo';
3
+
4
+ import { dependency } from './use-hash';
5
+
6
+ describe(useMemo.name, () => {
7
+ let fixtures: Awaited<ReturnType<typeof getFixtures>>;
8
+
9
+ beforeEach(async () => {
10
+ fixtures = await getFixtures();
11
+ });
12
+
13
+ test('memorize value for dependencies', async () => {
14
+ const value1 = await fixtures.givenMemoValue(() => 1, [useMemo.name]);
15
+ const value2 = await fixtures.givenMemoValue(() => 2, [useMemo.name]);
16
+
17
+ expect(value1).toEqual(1);
18
+ expect(value2).toEqual(1);
19
+ });
20
+ });
21
+
22
+ async function getFixtures() {
23
+ const { act } = await makeTestModule([]);
24
+
25
+ return {
26
+ givenMemoValue<T>(value: () => T, dependencies: dependency[]) {
27
+ return act(() => useMemo(value, dependencies));
28
+ }
29
+ };
30
+ }
@@ -0,0 +1,35 @@
1
+ import { useContext } from '@lib/module';
2
+ import { dependency, useHash } from '@lib/use-hash';
3
+
4
+ export const token = Symbol('memo-token');
5
+
6
+ /**
7
+ *
8
+ */
9
+ export function memo() {
10
+ return {
11
+ provide: token,
12
+ useValue: {}
13
+ };
14
+ }
15
+
16
+ /**
17
+ * @name useMemo
18
+ * @description
19
+ * A hook that caches the result of an expensive calculation, based on a set of dependencies.
20
+ *
21
+ * @template T The type of the value that is memoized.
22
+ * @param {() => T} calculateValue A function that returns the value to memoize.
23
+ * @param {dependency[]} dependencies An array of dependencies that determine when the value should be recalculated.
24
+ * @returns {T} The memoized value.
25
+ */
26
+ export function useMemo<T>(calculateValue: () => T, dependencies: dependency[]) {
27
+ const memory = useContext<Record<string, any>>(token);
28
+ const hash = useHash(dependencies);
29
+
30
+ if (memory[hash]) {
31
+ return memory[hash] as T;
32
+ }
33
+
34
+ return (memory[hash] = calculateValue()) as T;
35
+ }
@@ -0,0 +1,47 @@
1
+ import { from, map, switchMap, throwError } from 'rxjs';
2
+ import { request } from 'undici';
3
+
4
+ import { useTimestamp } from './use-timestamp';
5
+
6
+ export type RequestMethod =
7
+ | 'GET'
8
+ | 'HEAD'
9
+ | 'POST'
10
+ | 'PUT'
11
+ | 'DELETE'
12
+ | 'CONNECT'
13
+ | 'OPTIONS'
14
+ | 'TRACE'
15
+ | 'PATCH';
16
+
17
+ export class RequestNetworkError extends Error {
18
+ constructor(readonly statusCode: number, readonly json: () => Promise<string>) {
19
+ super(`Request network error, received status code: ${statusCode}`);
20
+ }
21
+ }
22
+
23
+ export function useRequest(args: {
24
+ method: RequestMethod;
25
+ url: string;
26
+ headers?: Record<string, any>;
27
+ body?: string;
28
+ }) {
29
+ return from(
30
+ request(args.url, {
31
+ method: args.method,
32
+ headers: args.headers,
33
+ body: args.body
34
+ })
35
+ ).pipe(
36
+ switchMap(it => {
37
+ if (it.statusCode !== 200) {
38
+ return throwError(
39
+ () => new RequestNetworkError(it.statusCode, () => it.body.json())
40
+ );
41
+ }
42
+
43
+ return from(it.body.json());
44
+ }),
45
+ map(payload => ({ timestamp: useTimestamp(), payload }))
46
+ );
47
+ }
@@ -0,0 +1,59 @@
1
+ import { Observable } from 'rxjs';
2
+ import { WebSocket } from 'ws';
3
+
4
+ import { useLogger } from './use-logger';
5
+ import { useTimestamp } from './use-timestamp';
6
+
7
+ export function useSocket(
8
+ url: string
9
+ ): [Observable<{ timestamp: number; payload: unknown }>, (message: unknown) => void] {
10
+ const { debug } = useLogger('useSocket');
11
+
12
+ const message = new Observable<{ timestamp: number; payload: unknown }>(stream => {
13
+ const socket = new WebSocket(url);
14
+ let isAlive = false;
15
+ let interval: NodeJS.Timer | undefined;
16
+
17
+ socket.onmessage = it =>
18
+ stream.next({ timestamp: useTimestamp(), payload: JSON.parse(it.data as string) });
19
+ socket.onerror = it => {
20
+ clearInterval(interval);
21
+ stream.error(it);
22
+ };
23
+ socket.onclose = () => {
24
+ debug('closed', url);
25
+ clearInterval(interval);
26
+ stream.error();
27
+ };
28
+ socket.onopen = () => {
29
+ debug('opened', url);
30
+ isAlive = true;
31
+ interval = setInterval(() => {
32
+ if (isAlive) {
33
+ isAlive = false;
34
+
35
+ socket.ping();
36
+ } else {
37
+ socket.terminate();
38
+ clearInterval(interval);
39
+ }
40
+ }, 5000);
41
+
42
+ socket.on('pong', () => {
43
+ isAlive = true;
44
+ });
45
+
46
+ socket.on('ping', () => {
47
+ isAlive = true;
48
+ socket.pong();
49
+ });
50
+ };
51
+
52
+ return () => {
53
+ socket.terminate();
54
+ clearInterval(interval);
55
+ };
56
+ });
57
+
58
+ return [message, (message: unknown) => JSON.stringify(message)];
59
+ }
@@ -0,0 +1,31 @@
1
+ import { makeTestModule } from '@lib/make-test-module';
2
+ import { useState } from '@lib/use-state';
3
+
4
+ import { dependency } from './use-hash';
5
+
6
+ describe(useState.name, () => {
7
+ let fixtures: Awaited<ReturnType<typeof getFixtures>>;
8
+
9
+ beforeEach(async () => {
10
+ fixtures = await getFixtures();
11
+ });
12
+
13
+ test('keep same state between multiple requests for the same dependency', async () => {
14
+ const state1 = await fixtures.givenState({ text: 'Hello my state' }, ['my-state']);
15
+ const state2 = await fixtures.givenState({ text: 'Hello my override state' }, [
16
+ 'my-state'
17
+ ]);
18
+
19
+ expect(Object.is(state1, state2)).toBeTruthy();
20
+ });
21
+ });
22
+
23
+ async function getFixtures() {
24
+ const { act } = await makeTestModule([]);
25
+
26
+ return {
27
+ givenState<T>(value: T, dependencies: dependency[]) {
28
+ return act(() => useState(value, dependencies));
29
+ }
30
+ };
31
+ }