@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,261 +0,0 @@
1
- import {
2
- concat,
3
- defer,
4
- filter,
5
- from,
6
- map,
7
- mergeMap,
8
- Observable,
9
- of,
10
- ReplaySubject,
11
- share,
12
- shareReplay,
13
- startWith,
14
- switchMap,
15
- take
16
- } from 'rxjs';
17
- import { v4 } from 'uuid';
18
-
19
- import { AdapterAggregate } from '@lib/adapter';
20
- import {
21
- AssetSelector,
22
- Balance,
23
- balance,
24
- Instrument,
25
- instrument,
26
- instruments,
27
- InstrumentSelector,
28
- InvalidInstrumentSelectorError,
29
- Ohlc,
30
- Order,
31
- order,
32
- Orderbook,
33
- orderbook,
34
- orders,
35
- Position,
36
- position,
37
- positions,
38
- Trade,
39
- trade
40
- } from '@lib/domain';
41
- import { decimal } from '@lib/shared';
42
- import { Measurement } from '@lib/storage';
43
- import { Store } from '@lib/store';
44
-
45
- type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
46
-
47
- export class Session {
48
- private initialized = false;
49
-
50
- get timestamp(): number {
51
- return this.store.snapshot.timestamp;
52
- }
53
-
54
- constructor(
55
- readonly id: number,
56
- readonly store: Store,
57
- readonly aggregate: AdapterAggregate,
58
- readonly measurement: Measurement | undefined
59
- ) {}
60
-
61
- async awake(): Promise<void> {
62
- if (this.initialized) {
63
- return;
64
- }
65
-
66
- this.initialized = true;
67
-
68
- // awake all adapters and synchronize trading accounts with store.
69
- await this.aggregate.awake();
70
- }
71
-
72
- async dispose(): Promise<void> {
73
- if (!this.initialized) {
74
- return;
75
- }
76
-
77
- this.store.dispose();
78
-
79
- await this.aggregate.dispose();
80
-
81
- this.initialized = false;
82
- }
83
-
84
- /**
85
- * Subscribes to specific instrument. Usually forces adapter to subscribe
86
- * for orderbook and ticker streams.
87
- */
88
- subscribe(instrument: Array<InstrumentSelector>): Promise<void> {
89
- return this.aggregate.subscribe(instrument);
90
- }
91
-
92
- /**
93
- * Opens a new order.
94
- */
95
- open(order: {
96
- instrument: InstrumentSelector;
97
- quantity: decimal;
98
- rate?: decimal;
99
- }): Observable<Readonly<Order>> {
100
- const instrument = this.store.snapshot.universe.instrument.get(order.instrument.id);
101
- if (!instrument) {
102
- throw new InvalidInstrumentSelectorError(order.instrument.id);
103
- }
104
-
105
- const newOrder = new Order(
106
- this.timestamp,
107
- v4(),
108
- instrument,
109
- order.quantity,
110
- this.timestamp,
111
- order.rate
112
- );
113
-
114
- return of(newOrder).pipe(
115
- switchMap(() => this.aggregate.open(newOrder)),
116
- switchMap(() =>
117
- this.order(order.instrument).pipe(filter(it => it.id == newOrder.id))
118
- )
119
- );
120
- }
121
-
122
- /**
123
- * Cancels specific order.
124
- */
125
- cancel(order: Order): Observable<Readonly<Order>> {
126
- return defer(() => from(this.aggregate.cancel(order))).pipe(
127
- switchMap(() => this.order(order.instrument).pipe(filter(it => it.id == order.id)))
128
- );
129
- }
130
-
131
- /**
132
- * Subscribes to specific instrument changes.
133
- * When adapter awake then it will fetch collection of all available instruments.
134
- */
135
- instrument(selector: InstrumentSelector): Observable<Readonly<Instrument>> {
136
- this.subscribe([selector]);
137
-
138
- return this.store.changes$.pipe(instrument(selector, this.store.snapshot));
139
- }
140
-
141
- /**
142
- * Subscribes to instruments changes.
143
- * When adapter awake then it will fetch collection of all available instruments.
144
- */
145
- instruments(): Observable<Readonly<Instrument[]>> {
146
- return this.store.changes$.pipe(instruments(this.store.snapshot));
147
- }
148
-
149
- /**
150
- * Subscribes to balance changes.
151
- */
152
- balance(selector: AssetSelector): Observable<Readonly<Balance>> {
153
- return this.store.changes$.pipe(balance(selector, this.store.snapshot));
154
- }
155
-
156
- /**
157
- * Subscribes to trade/ticker changes.
158
- */
159
- trade(selector: InstrumentSelector): Observable<Readonly<Trade>> {
160
- this.subscribe([selector]);
161
-
162
- return this.store.changes$.pipe(trade(selector, this.store.snapshot));
163
- }
164
-
165
- /**
166
- * Subscribes to orderbook changes.
167
- * Right now you can access only best bid and best ask.
168
- */
169
- orderbook(selector: InstrumentSelector): Observable<Readonly<Orderbook>> {
170
- this.subscribe([selector]);
171
-
172
- return this.store.changes$.pipe(orderbook(selector, this.store.snapshot));
173
- }
174
-
175
- /**
176
- * Subscribes to position on leveraged market.
177
- */
178
- position(selector: InstrumentSelector): Observable<Readonly<Position>> {
179
- this.subscribe([selector]);
180
-
181
- return this.store.changes$.pipe(position(selector));
182
- }
183
-
184
- /**
185
- * Subscribes to positions on leveraged markets.
186
- */
187
- positions(selector: InstrumentSelector): Observable<Readonly<Position[]>> {
188
- this.subscribe([selector]);
189
-
190
- return this.store.changes$.pipe(positions(selector, this.store.snapshot));
191
- }
192
-
193
- order(selector: InstrumentSelector): Observable<Readonly<Order>> {
194
- this.subscribe([selector]);
195
-
196
- return this.store.changes$.pipe(order(selector));
197
- }
198
-
199
- orders(selector: InstrumentSelector): Observable<Readonly<Order[]>> {
200
- this.subscribe([selector]);
201
-
202
- return this.store.changes$.pipe(orders(selector, this.store.snapshot));
203
- }
204
-
205
- history(
206
- selector: InstrumentSelector,
207
- timeframe: number,
208
- length: number
209
- ): Observable<Readonly<Ohlc>> {
210
- return this.store.changes$.pipe(
211
- startWith(this.store.snapshot.universe.instrument.get(selector.id)),
212
- filter(it => it !== undefined && it.type === Instrument.type),
213
- map(it => it as Instrument),
214
- filter(it => it.id === selector.id),
215
- switchMap(() => from(this.aggregate.history(selector, timeframe, length))),
216
- take(1),
217
- shareReplay(),
218
- mergeMap(it => it)
219
- );
220
- }
221
-
222
- measure<T extends { timestamp: number }>(
223
- spec: {
224
- kind: string;
225
- timestamp?: number;
226
- },
227
- defaultValue?: Optional<T, 'timestamp'>
228
- ): [Observable<T>, (value: T) => void] {
229
- if (!this.measurement) {
230
- throw new Error();
231
- }
232
-
233
- const changes$ = new ReplaySubject<Optional<T, 'timestamp'>>();
234
- const persisted$ = from(
235
- this.measurement.query(this.id, {
236
- to: spec.timestamp ?? this.timestamp,
237
- kind: spec.kind,
238
- count: 1
239
- })
240
- ).pipe(
241
- map(it =>
242
- it.length ? { timestamp: it[0].timestamp, ...it[0].payload } : defaultValue
243
- ),
244
- share()
245
- );
246
-
247
- const setter = (value: Optional<T, 'timestamp'>) => {
248
- const timestamp = value.timestamp ?? this.timestamp;
249
- const measure = { timestamp, kind: spec.kind, payload: value };
250
-
251
- this.measurement?.save(this.id, [measure]);
252
-
253
- changes$.next({ ...value, timestamp });
254
- };
255
-
256
- return [
257
- concat(persisted$, changes$.asObservable()).pipe(filter(it => it !== undefined)),
258
- setter
259
- ];
260
- }
261
- }
@@ -1,30 +0,0 @@
1
- import { Subject } from 'rxjs';
2
-
3
- import { Asset, Commission, Component, Instrument, Trade, trade } from '@lib/domain';
4
- import { d } from '@lib/shared';
5
- import { State } from '@lib/store';
6
-
7
- describe(trade.name, () => {
8
- const instrument = new Instrument(
9
- 0,
10
- new Asset('abc', 'xyz', 4),
11
- new Asset('def', 'xyz', 4),
12
- 'abc-def',
13
- Commission.Zero
14
- );
15
-
16
- const state = new State();
17
-
18
- beforeEach(() => {
19
- state.trade.upsert(new Trade(0, instrument, d.Zero, d.Zero));
20
- });
21
-
22
- test('should pipe an orderbook on subscription', done => {
23
- new Subject<Component>().pipe(trade(instrument, state)).subscribe({
24
- next: it => {
25
- expect(it.id).toEqual(instrument.id);
26
- done();
27
- }
28
- });
29
- });
30
- });
@@ -1,14 +0,0 @@
1
- import { filter, map, Observable, startWith } from 'rxjs';
2
-
3
- import { Component, InstrumentSelector, Trade } from '@lib/domain';
4
- import { State } from '@lib/store';
5
-
6
- export function trade(selector: InstrumentSelector, state: State) {
7
- return (source$: Observable<Component>) =>
8
- source$.pipe(
9
- startWith(state.trade.get(selector.id)),
10
- filter(it => it !== undefined && it.type === Trade.type),
11
- map(it => it as Trade),
12
- filter(it => it.instrument.id === selector.id)
13
- );
14
- }
@@ -1,18 +0,0 @@
1
- import { Asset, Commission, Instrument, Trade } from '@lib/domain';
2
- import { d } from '@lib/shared';
3
-
4
- describe(Trade.name, () => {
5
- const instrument = new Instrument(
6
- 0,
7
- new Asset('abc', 'xyz', 4),
8
- new Asset('def', 'xyz', 4),
9
- 'abc-def',
10
- Commission.Zero
11
- );
12
-
13
- test('should construct an empty trade', () => {
14
- const sut = new Trade(0, instrument, d.Zero, d.Zero);
15
-
16
- expect(sut.id).toEqual(instrument.id);
17
- });
18
- });
@@ -1,22 +0,0 @@
1
- import { Component, Instrument } from '@lib/domain';
2
- import { decimal, hash } from '@lib/shared';
3
-
4
- /**
5
- * Simple trade or ticker executed on the market, it's a match of buyer
6
- * and seller of the same asset.
7
- */
8
- export class Trade implements Component {
9
- static type = hash(Trade.name);
10
- readonly type = Trade.type;
11
-
12
- readonly id: string;
13
-
14
- constructor(
15
- public timestamp: number,
16
- public readonly instrument: Instrument,
17
- public rate: decimal,
18
- public quantity: decimal
19
- ) {
20
- this.id = instrument.id;
21
- }
22
- }
@@ -1,34 +0,0 @@
1
- import { Liquidity, LiquidityAskComparer } from '@lib/domain';
2
- import { d, PriorityList } from '@lib/shared';
3
-
4
- describe('PriorityList', () => {
5
- test('should patch a best bid and ask', () => {
6
- const input = [
7
- { rate: d(2), quantity: d(2) },
8
- { rate: d(2), quantity: d(3) },
9
- { rate: d(5), quantity: d(1) },
10
- { rate: d(5), quantity: d(2) },
11
- { rate: d(3), quantity: d(1) },
12
- { rate: d(1), quantity: d(8) },
13
- { rate: d(1), quantity: d(0) },
14
- { rate: d(3), quantity: d(0) },
15
- { rate: d(5), quantity: d(0) }
16
- ];
17
-
18
- const sut = new PriorityList<Liquidity>(LiquidityAskComparer, it =>
19
- it.rate.toString()
20
- );
21
-
22
- input.forEach(it => (it.quantity.greaterThan(0) ? sut.enqueue(it) : sut.dequeue(it)));
23
-
24
- const volume = sut.reduce((it, agg) => agg.add(it.quantity.mul(it.rate)), d.Zero);
25
-
26
- const head = sut.head ?? fail();
27
-
28
- expect(head.rate).toEqual(d(2));
29
- expect(head.quantity).toEqual(d(3));
30
- expect(volume).toEqual(d(6));
31
- expect(sut.getByKey(d(2).toString()).quantity).toEqual(d(3));
32
- expect(sut.getByKey(d(3).toString())).toEqual(undefined);
33
- });
34
- });
@@ -1,154 +0,0 @@
1
- export class Set<T extends { id: string }> {
2
- private readonly array: Array<T> = [];
3
-
4
- constructor(values?: ReadonlyArray<T>) {
5
- if (values) {
6
- values.forEach(it => this.upsert(it));
7
- }
8
- }
9
-
10
- get(id: string) {
11
- return this.array.find(it => it.id == id);
12
- }
13
-
14
- tryGetOrSet(id: string, setter: () => T): T {
15
- return this.get(id) ?? this.upsert(setter());
16
- }
17
-
18
- upsert(value: T) {
19
- const index = this.array.findIndex(it => it.id == value.id);
20
-
21
- if (index >= 0) {
22
- this.array[index] = value;
23
-
24
- return value;
25
- }
26
-
27
- this.array.push(value);
28
-
29
- return value;
30
- }
31
-
32
- remove(value: T) {
33
- const index = this.array.indexOf(value);
34
- if (index > -1) {
35
- this.array.splice(index, 1);
36
- }
37
- }
38
-
39
- asReadonlyArray(): ReadonlyArray<T> {
40
- return this.array;
41
- }
42
-
43
- clear() {
44
- this.array.splice(0, this.array.length);
45
- }
46
- }
47
-
48
- export class PriorityList<T extends { next: T | undefined }> {
49
- private valueByKey: Record<string, T> = {};
50
-
51
- head: T | undefined;
52
-
53
- constructor(
54
- private readonly comparer: (lhs: Omit<T, 'next'>, rhs: Omit<T, 'next'>) => number,
55
- private readonly getKeyFn: (key: Omit<T, 'next'>) => string
56
- ) {}
57
-
58
- getByKey(key: string): T {
59
- return this.valueByKey[key];
60
- }
61
-
62
- private make(value: Omit<T, 'next'>, next: T | undefined = undefined): T {
63
- const node = { ...value, next } as T;
64
-
65
- this.valueByKey[this.getKeyFn(node)] = node;
66
-
67
- return node;
68
- }
69
-
70
- enqueue(value: Omit<T, 'next'>) {
71
- if (!this.head) {
72
- this.head = this.make(value);
73
- }
74
-
75
- if (this.comparer(this.head, value) > 0) {
76
- this.head = this.make(value, this.head);
77
- } else if (this.comparer(this.head, value) == 0) {
78
- this.head = this.make(value, this.head.next);
79
- } else {
80
- this.visit(it => {
81
- if (it.next) {
82
- if (this.comparer(it.next, value) == 0) {
83
- it.next = this.make(value, it.next.next);
84
-
85
- return false;
86
- }
87
-
88
- if (this.comparer(it.next, value) > 0) {
89
- it.next = this.make(value, it.next);
90
-
91
- return false;
92
- }
93
-
94
- return true;
95
- } else {
96
- it.next = this.make(value);
97
-
98
- return false;
99
- }
100
- });
101
- }
102
- }
103
-
104
- dequeue(value: Omit<T, 'next'>) {
105
- if (!this.head) {
106
- return;
107
- }
108
-
109
- if (this.comparer(this.head, value) == 0) {
110
- this.head = this.head.next;
111
-
112
- delete this.valueByKey[this.getKeyFn(value)];
113
- }
114
-
115
- this.visit(it => {
116
- if (it.next && this.comparer(it.next, value) == 0) {
117
- it.next = it.next.next;
118
-
119
- delete this.valueByKey[this.getKeyFn(value)];
120
-
121
- return true;
122
- }
123
-
124
- return false;
125
- });
126
- }
127
-
128
- clear() {
129
- this.head = undefined;
130
- this.valueByKey = {};
131
- }
132
-
133
- visit(fn: (value: T) => boolean) {
134
- let top = this.head;
135
-
136
- while (top) {
137
- if (!fn(top)) {
138
- break;
139
- }
140
-
141
- top = top.next;
142
- }
143
- }
144
-
145
- reduce<K>(fn: (value: T, aggregate: K) => K, initValue: K) {
146
- this.visit(it => {
147
- initValue = fn(it, initValue);
148
-
149
- return true;
150
- });
151
-
152
- return initValue;
153
- }
154
- }
@@ -1,10 +0,0 @@
1
- export function hash(str: string): number {
2
- let hash = 7373;
3
- let i = str.length;
4
-
5
- while (i) {
6
- hash = (hash * 77) ^ str.charCodeAt(--i);
7
- }
8
-
9
- return hash >>> 0;
10
- }
@@ -1,46 +0,0 @@
1
- import chalk from 'chalk';
2
-
3
- import { now } from '@lib/shared';
4
-
5
- const colorize = (content: string) => {
6
- let hash = 0x811c9dc5;
7
-
8
- for (let i = 0; i < content.length; i++) {
9
- hash ^= content.charCodeAt(i);
10
- hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
11
- }
12
-
13
- return chalk.hex('#' + (hash * 0xfffff * 1000000).toString(16).slice(0, 6))(content);
14
- };
15
-
16
- const time = () => chalk.gray(new Date(now()).toISOString());
17
-
18
- export class Logger {
19
- constructor(private readonly context: string) {}
20
-
21
- public info = (message: any, ...params: unknown[]) =>
22
- params?.length
23
- ? console.info(`${this.prefix()}: ${message}`, params)
24
- : console.info(`${this.prefix()}: ${message}`);
25
-
26
- public debug = (message: any, ...params: unknown[]) =>
27
- params?.length
28
- ? console.debug(`${this.prefix()}: ${message}`, params)
29
- : console.debug(`${this.prefix()}: ${message}`);
30
-
31
- public warn = (message: any, ...params: unknown[]) =>
32
- params?.length
33
- ? console.warn(`${this.prefix()}: ${message}`, params)
34
- : console.warn(`${this.prefix()}: ${message}`);
35
-
36
- public error = (message: any, ...params: unknown[]) =>
37
- params?.length
38
- ? console.error(`${this.prefix()}: ${message}`, params)
39
- : console.error(`${this.prefix()}: ${message}`);
40
-
41
- public prefix = () => `${time()} ${colorize(this.context)}`;
42
- }
43
-
44
- export function log(context: string): Logger {
45
- return new Logger(context);
46
- }
@@ -1,5 +0,0 @@
1
- import { retryAsync } from 'ts-retry';
2
-
3
- export function retry<T>(fn: () => Promise<T>): Promise<T> {
4
- return retryAsync(fn, { delay: 1000, maxTry: 3 });
5
- }
@@ -1,17 +0,0 @@
1
- import { Cache, InMemoryStorage } from '@lib/storage';
2
-
3
- describe(Cache.name, () => {
4
- test('should return cached value', async () => {
5
- const cache = new Cache(new InMemoryStorage());
6
-
7
- const value1 = await cache.tryGet(() => Promise.resolve('quantform-1'), {
8
- key: 'test'
9
- });
10
- const value2 = await cache.tryGet(() => Promise.resolve('quantform-2'), {
11
- key: 'test'
12
- });
13
-
14
- expect(value1).toEqual('quantform-1');
15
- expect(value2).toEqual('quantform-1');
16
- });
17
- });
@@ -1,37 +0,0 @@
1
- import { now } from '@lib/shared';
2
- import { Storage } from '@lib/storage';
3
-
4
- export class Cache {
5
- constructor(private readonly storage: Storage) {}
6
-
7
- async tryGet<T>(
8
- getter: () => Promise<T>,
9
- options: { key: string; ttl?: number }
10
- ): Promise<T> {
11
- const time = now();
12
-
13
- const payload = await this.storage.query(options.key, {
14
- kind: 'cache',
15
- count: 1,
16
- to: time + 1
17
- });
18
-
19
- const ttl = options.ttl ?? 60 * 60 * 24;
20
-
21
- if (!payload.length || payload[0].timestamp < time - ttl) {
22
- const value = await getter();
23
-
24
- await this.storage.save(options.key, [
25
- {
26
- timestamp: time,
27
- kind: 'cache',
28
- json: JSON.stringify(value)
29
- }
30
- ]);
31
-
32
- return value;
33
- }
34
-
35
- return JSON.parse(payload[0].json);
36
- }
37
- }