@quantform/core 0.7.0-beta.8 → 0.7.9
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.
- package/jest.config.ts +1 -1
- package/{dist → lib}/asset/asset.d.ts +3 -0
- package/lib/asset/asset.d.ts.map +1 -0
- package/{dist → lib}/asset/asset.js +8 -2
- package/{dist → lib}/cli/index.js +9 -6
- package/lib/cli/internal/script.d.ts +8 -0
- package/lib/cli/internal/script.d.ts.map +1 -0
- package/lib/cli/internal/script.js +75 -0
- package/{dist → lib}/cli/internal/workspace.js +1 -2
- package/lib/cli/live.d.ts +5 -0
- package/lib/cli/live.d.ts.map +1 -0
- package/lib/cli/live.js +33 -0
- package/lib/cli/paper.d.ts +5 -0
- package/lib/cli/paper.d.ts.map +1 -0
- package/lib/cli/paper.js +33 -0
- package/lib/cli/pull.d.ts.map +1 -0
- package/lib/cli/pull.js +28 -0
- package/lib/cli/replay.d.ts +6 -0
- package/lib/cli/replay.d.ts.map +1 -0
- package/lib/cli/replay.js +37 -0
- package/{dist → lib}/component/distinct-until-timesamp-changed.d.ts.map +1 -1
- package/{dist → lib}/component/distinct-until-timesamp-changed.js +1 -2
- package/lib/component/ohlc-operator.d.ts.map +1 -0
- package/{dist → lib}/component/ohlc-operator.js +3 -4
- package/{dist → lib}/component/ohlc.d.ts.map +1 -1
- package/{dist → lib}/component/timeframe.js +3 -3
- package/{dist → lib}/core.js +4 -5
- package/lib/index.d.ts +21 -0
- package/lib/index.d.ts.map +1 -0
- package/{dist → lib}/index.js +6 -13
- package/{dist → lib}/instrument/commission/commission.js +3 -3
- package/{dist → lib}/instrument/instrument.d.ts +3 -0
- package/lib/instrument/instrument.d.ts.map +1 -0
- package/{dist → lib}/instrument/instrument.js +8 -2
- package/lib/make-test-module.d.ts +13 -0
- package/lib/make-test-module.d.ts.map +1 -0
- package/{dist → lib}/make-test-module.js +13 -4
- package/lib/module.d.ts.map +1 -0
- package/lib/operators.d.ts +5 -0
- package/lib/operators.d.ts.map +1 -0
- package/lib/operators.js +15 -0
- package/lib/replay/index.d.ts +10 -0
- package/lib/replay/index.d.ts.map +1 -0
- package/{dist → lib}/replay/index.js +7 -4
- package/lib/replay/replay-guard.d.ts +10 -0
- package/lib/replay/replay-guard.d.ts.map +1 -0
- package/lib/replay/replay-guard.js +7 -0
- package/lib/replay/replay.d.ts +10 -0
- package/lib/replay/replay.d.ts.map +1 -0
- package/lib/replay/replay.js +8 -0
- package/lib/replay/storage/use-replay-storage-buffer.d.ts +15 -0
- package/lib/replay/storage/use-replay-storage-buffer.d.ts.map +1 -0
- package/lib/replay/storage/use-replay-storage-buffer.js +57 -0
- package/lib/replay/storage/use-replay-storage-cursor.d.ts +30 -0
- package/lib/replay/storage/use-replay-storage-cursor.d.ts.map +1 -0
- package/lib/replay/storage/use-replay-storage-cursor.js +49 -0
- package/lib/replay/storage/use-replay-storage.d.ts +13 -0
- package/lib/replay/storage/use-replay-storage.d.ts.map +1 -0
- package/lib/replay/storage/use-replay-storage.js +38 -0
- package/lib/replay/storage/use-replay-storage.spec.d.ts +2 -0
- package/lib/replay/storage/use-replay-storage.spec.d.ts.map +1 -0
- package/lib/replay/storage/use-replay-storage.spec.js +74 -0
- package/lib/replay/use-replay-lock.d.ts +3 -0
- package/lib/replay/use-replay-lock.d.ts.map +1 -0
- package/lib/replay/use-replay-lock.js +21 -0
- package/lib/replay/use-replay-manager.d.ts +28 -0
- package/lib/replay/use-replay-manager.d.ts.map +1 -0
- package/lib/replay/use-replay-manager.js +68 -0
- package/{dist → lib}/replay/use-replay-options.js +2 -2
- package/{dist → lib}/replay/use-replay.d.ts +2 -2
- package/lib/replay/use-replay.d.ts.map +1 -0
- package/lib/replay/use-replay.js +19 -0
- package/lib/replay/use-replay.spec.js +130 -0
- package/lib/replay/when-replay-finished.d.ts +2 -0
- package/lib/replay/when-replay-finished.d.ts.map +1 -0
- package/lib/replay/when-replay-finished.js +14 -0
- package/lib/session/use-session-object.d.ts +9 -0
- package/lib/session/use-session-object.d.ts.map +1 -0
- package/{dist → lib}/session/use-session-storage.js +2 -2
- package/lib/session/use-session.d.ts +13 -0
- package/lib/session/use-session.d.ts.map +1 -0
- package/lib/session/use-session.js +14 -0
- package/{dist → lib}/shared/datetime.js +1 -2
- package/lib/shared/decimals.d.ts +14 -0
- package/lib/shared/decimals.d.ts.map +1 -0
- package/lib/shared/decimals.js +18 -0
- package/{dist → lib}/shared/environment.js +2 -3
- package/{dist → lib}/simulator/use-simulator.js +1 -2
- package/{dist → lib}/simulator/use-simulator.spec.js +12 -18
- package/{dist → lib}/storage/in-memory/in-memory-storage.factory.js +2 -2
- package/{dist → lib}/storage/index.d.ts +1 -0
- package/{dist → lib}/storage/index.d.ts.map +1 -1
- package/{dist → lib}/storage/index.js +1 -0
- package/lib/storage/storage.d.ts.map +1 -0
- package/{dist → lib}/storage/use-cache.d.ts.map +1 -1
- package/lib/storage/use-storage-factory.d.ts +10 -0
- package/lib/storage/use-storage-factory.d.ts.map +1 -0
- package/lib/storage/use-storage-factory.js +12 -0
- package/lib/storage/use-storage.d.ts.map +1 -0
- package/{dist → lib}/storage/use-storage.js +2 -2
- package/lib/strategy.d.ts +15 -0
- package/lib/strategy.d.ts.map +1 -0
- package/lib/strategy.js +26 -0
- package/lib/strategy.spec.d.ts +2 -0
- package/lib/strategy.spec.d.ts.map +1 -0
- package/lib/strategy.spec.js +34 -0
- package/lib/use-execution-mode.d.ts +20 -0
- package/lib/use-execution-mode.d.ts.map +1 -0
- package/lib/use-execution-mode.js +32 -0
- package/{dist → lib}/use-hash.js +1 -2
- package/{dist → lib}/use-logger.js +5 -5
- package/lib/use-memo.d.ts +8 -0
- package/lib/use-memo.d.ts.map +1 -0
- package/lib/use-memo.js +19 -0
- package/lib/use-socket.d.ts +20 -0
- package/lib/use-socket.d.ts.map +1 -0
- package/lib/use-socket.js +82 -0
- package/{dist → lib}/use-timestamp.js +3 -4
- package/lib/when-socket.d.ts +8 -0
- package/lib/when-socket.d.ts.map +1 -0
- package/{dist/use-socket.js → lib/when-socket.js} +5 -5
- package/lib/with-memo.d.ts +5 -0
- package/lib/with-memo.d.ts.map +1 -0
- package/{dist/use.js → lib/with-memo.js} +4 -5
- package/lib/with-memo.spec.d.ts +2 -0
- package/lib/with-memo.spec.d.ts.map +1 -0
- package/{dist/use.spec.js → lib/with-memo.spec.js} +3 -3
- package/{dist/use-request.d.ts → lib/with-request.d.ts} +6 -6
- package/lib/with-request.d.ts.map +1 -0
- package/lib/with-request.js +68 -0
- package/package.json +5 -5
- package/src/asset/asset.ts +6 -0
- package/src/cli/index.ts +11 -8
- package/src/cli/internal/script.ts +70 -0
- package/src/cli/live.ts +22 -0
- package/src/cli/paper.ts +22 -0
- package/src/cli/pull.ts +6 -66
- package/src/cli/replay.ts +15 -19
- package/src/core.ts +6 -6
- package/src/index.ts +6 -13
- package/src/instrument/instrument.ts +6 -0
- package/src/make-test-module.ts +20 -4
- package/src/operators.ts +18 -0
- package/src/replay/index.ts +7 -4
- package/src/replay/replay-guard.ts +11 -0
- package/src/replay/replay.ts +13 -0
- package/src/replay/storage/use-replay-storage-buffer.ts +52 -0
- package/src/replay/storage/use-replay-storage-cursor.ts +44 -0
- package/src/replay/storage/use-replay-storage.spec.ts +84 -0
- package/src/replay/storage/use-replay-storage.ts +29 -0
- package/src/replay/use-replay-lock.ts +29 -0
- package/src/replay/use-replay-manager.ts +83 -0
- package/src/replay/use-replay.spec.ts +10 -4
- package/src/replay/use-replay.ts +11 -17
- package/src/replay/when-replay-finished.ts +20 -0
- package/src/session/use-session-storage.ts +2 -2
- package/src/session/use-session.ts +16 -4
- package/src/shared/decimals.ts +0 -45
- package/src/simulator/use-simulator.spec.ts +10 -26
- package/src/storage/index.ts +1 -0
- package/src/storage/use-storage-factory.ts +7 -7
- package/src/storage/use-storage.ts +2 -2
- package/src/strategy.spec.ts +42 -0
- package/src/strategy.ts +36 -0
- package/src/use-execution-mode.ts +27 -27
- package/src/use-logger.ts +7 -7
- package/src/use-memo.ts +6 -21
- package/src/use-socket.ts +82 -44
- package/src/use-timestamp.ts +2 -2
- package/src/when-socket.ts +61 -0
- package/src/{use.spec.ts → with-memo.spec.ts} +3 -3
- package/src/{use.ts → with-memo.ts} +4 -4
- package/src/with-request.ts +83 -0
- package/tsconfig.json +2 -2
- package/dist/as-readonly.d.ts +0 -3
- package/dist/as-readonly.d.ts.map +0 -1
- package/dist/as-readonly.js +0 -8
- package/dist/asset/asset.d.ts.map +0 -1
- package/dist/cli/dev.d.ts +0 -2
- package/dist/cli/dev.d.ts.map +0 -1
- package/dist/cli/dev.js +0 -61
- package/dist/cli/pull.d.ts.map +0 -1
- package/dist/cli/pull.js +0 -107
- package/dist/cli/replay.d.ts +0 -2
- package/dist/cli/replay.d.ts.map +0 -1
- package/dist/cli/replay.js +0 -64
- package/dist/cli/run.d.ts +0 -2
- package/dist/cli/run.d.ts.map +0 -1
- package/dist/cli/run.js +0 -62
- package/dist/component/ohlc-operator.d.ts.map +0 -1
- package/dist/defined.d.ts +0 -3
- package/dist/defined.d.ts.map +0 -1
- package/dist/defined.js +0 -8
- package/dist/exclude.d.ts +0 -3
- package/dist/exclude.d.ts.map +0 -1
- package/dist/exclude.js +0 -8
- package/dist/index.d.ts +0 -28
- package/dist/index.d.ts.map +0 -1
- package/dist/instrument/instrument.d.ts.map +0 -1
- package/dist/make-test-module.d.ts +0 -13
- package/dist/make-test-module.d.ts.map +0 -1
- package/dist/missed.d.ts +0 -2
- package/dist/missed.d.ts.map +0 -1
- package/dist/missed.js +0 -4
- package/dist/module.d.ts.map +0 -1
- package/dist/replay/index.d.ts +0 -7
- package/dist/replay/index.d.ts.map +0 -1
- package/dist/replay/use-replay-coordinator.d.ts +0 -10
- package/dist/replay/use-replay-coordinator.d.ts.map +0 -1
- package/dist/replay/use-replay-coordinator.js +0 -119
- package/dist/replay/use-replay-reader.d.ts +0 -7
- package/dist/replay/use-replay-reader.d.ts.map +0 -1
- package/dist/replay/use-replay-reader.js +0 -32
- package/dist/replay/use-replay-reader.spec.d.ts +0 -2
- package/dist/replay/use-replay-reader.spec.d.ts.map +0 -1
- package/dist/replay/use-replay-reader.spec.js +0 -58
- package/dist/replay/use-replay-storage.d.ts +0 -10
- package/dist/replay/use-replay-storage.d.ts.map +0 -1
- package/dist/replay/use-replay-storage.js +0 -10
- package/dist/replay/use-replay-writer.d.ts +0 -6
- package/dist/replay/use-replay-writer.d.ts.map +0 -1
- package/dist/replay/use-replay-writer.js +0 -14
- package/dist/replay/use-replay-writer.spec.d.ts +0 -2
- package/dist/replay/use-replay-writer.spec.d.ts.map +0 -1
- package/dist/replay/use-replay-writer.spec.js +0 -53
- package/dist/replay/use-replay.d.ts.map +0 -1
- package/dist/replay/use-replay.js +0 -32
- package/dist/replay/use-replay.spec.js +0 -106
- package/dist/replay/with-replay.d.ts +0 -4
- package/dist/replay/with-replay.d.ts.map +0 -1
- package/dist/replay/with-replay.js +0 -8
- package/dist/session/use-session-object.d.ts +0 -18
- package/dist/session/use-session-object.d.ts.map +0 -1
- package/dist/session/use-session.d.ts +0 -4
- package/dist/session/use-session.d.ts.map +0 -1
- package/dist/session/use-session.js +0 -7
- package/dist/shared/decimals.d.ts +0 -30
- package/dist/shared/decimals.d.ts.map +0 -1
- package/dist/shared/decimals.js +0 -61
- package/dist/storage/storage.d.ts.map +0 -1
- package/dist/storage/use-storage-factory.d.ts +0 -10
- package/dist/storage/use-storage-factory.d.ts.map +0 -1
- package/dist/storage/use-storage-factory.js +0 -14
- package/dist/storage/use-storage.d.ts.map +0 -1
- package/dist/strat.d.ts +0 -7
- package/dist/strat.d.ts.map +0 -1
- package/dist/strat.js +0 -7
- package/dist/use-execution-mode.d.ts +0 -29
- package/dist/use-execution-mode.d.ts.map +0 -1
- package/dist/use-execution-mode.js +0 -37
- package/dist/use-lock.d.ts +0 -9
- package/dist/use-lock.d.ts.map +0 -1
- package/dist/use-lock.js +0 -40
- package/dist/use-memo.d.ts +0 -21
- package/dist/use-memo.d.ts.map +0 -1
- package/dist/use-memo.js +0 -35
- package/dist/use-request.d.ts.map +0 -1
- package/dist/use-request.js +0 -27
- package/dist/use-socket.d.ts +0 -6
- package/dist/use-socket.d.ts.map +0 -1
- package/dist/use-state.d.ts +0 -4
- package/dist/use-state.d.ts.map +0 -1
- package/dist/use-state.js +0 -24
- package/dist/use-state.spec.d.ts +0 -2
- package/dist/use-state.spec.d.ts.map +0 -1
- package/dist/use-state.spec.js +0 -36
- package/dist/use.d.ts +0 -5
- package/dist/use.d.ts.map +0 -1
- package/dist/use.spec.d.ts +0 -2
- package/dist/use.spec.d.ts.map +0 -1
- package/src/as-readonly.ts +0 -5
- package/src/cli/dev.ts +0 -31
- package/src/cli/run.ts +0 -29
- package/src/defined.ts +0 -6
- package/src/exclude.ts +0 -9
- package/src/missed.ts +0 -1
- package/src/replay/use-replay-coordinator.ts +0 -142
- package/src/replay/use-replay-reader.spec.ts +0 -64
- package/src/replay/use-replay-reader.ts +0 -23
- package/src/replay/use-replay-storage.ts +0 -8
- package/src/replay/use-replay-writer.spec.ts +0 -56
- package/src/replay/use-replay-writer.ts +0 -17
- package/src/replay/with-replay.ts +0 -10
- package/src/strat.ts +0 -7
- package/src/use-lock.ts +0 -52
- package/src/use-request.ts +0 -47
- package/src/use-state.spec.ts +0 -31
- package/src/use-state.ts +0 -30
- package/{dist → lib}/asset/asset.spec.d.ts +0 -0
- package/{dist → lib}/asset/asset.spec.d.ts.map +0 -0
- package/{dist → lib}/asset/asset.spec.js +0 -0
- package/{dist → lib}/asset/index.d.ts +0 -0
- package/{dist → lib}/asset/index.d.ts.map +0 -0
- package/{dist → lib}/asset/index.js +0 -0
- package/{dist → lib}/cli/build.d.ts +0 -0
- package/{dist → lib}/cli/build.d.ts.map +0 -0
- package/{dist → lib}/cli/build.js +1 -1
- /package/{dist → lib}/cli/index.d.ts +0 -0
- /package/{dist → lib}/cli/index.d.ts.map +0 -0
- /package/{dist → lib}/cli/internal/workspace.d.ts +0 -0
- /package/{dist → lib}/cli/internal/workspace.d.ts.map +0 -0
- /package/{dist → lib}/cli/pull.d.ts +0 -0
- /package/{dist → lib}/component/distinct-until-timesamp-changed.d.ts +0 -0
- /package/{dist → lib}/component/error.d.ts +0 -0
- /package/{dist → lib}/component/error.d.ts.map +0 -0
- /package/{dist → lib}/component/error.js +0 -0
- /package/{dist → lib}/component/index.d.ts +0 -0
- /package/{dist → lib}/component/index.d.ts.map +0 -0
- /package/{dist → lib}/component/index.js +0 -0
- /package/{dist → lib}/component/ohlc-operator.d.ts +0 -0
- /package/{dist → lib}/component/ohlc-operator.spec.d.ts +0 -0
- /package/{dist → lib}/component/ohlc-operator.spec.d.ts.map +0 -0
- /package/{dist → lib}/component/ohlc-operator.spec.js +0 -0
- /package/{dist → lib}/component/ohlc.d.ts +0 -0
- /package/{dist → lib}/component/ohlc.js +0 -0
- /package/{dist → lib}/component/ohlc.spec.d.ts +0 -0
- /package/{dist → lib}/component/ohlc.spec.d.ts.map +0 -0
- /package/{dist → lib}/component/ohlc.spec.js +0 -0
- /package/{dist → lib}/component/timeframe.d.ts +0 -0
- /package/{dist → lib}/component/timeframe.d.ts.map +0 -0
- /package/{dist → lib}/core.d.ts +0 -0
- /package/{dist → lib}/core.d.ts.map +0 -0
- /package/{dist → lib}/instrument/commission/commission.d.ts +0 -0
- /package/{dist → lib}/instrument/commission/commission.d.ts.map +0 -0
- /package/{dist → lib}/instrument/commission/commission.spec.d.ts +0 -0
- /package/{dist → lib}/instrument/commission/commission.spec.d.ts.map +0 -0
- /package/{dist → lib}/instrument/commission/commission.spec.js +0 -0
- /package/{dist → lib}/instrument/index.d.ts +0 -0
- /package/{dist → lib}/instrument/index.d.ts.map +0 -0
- /package/{dist → lib}/instrument/index.js +0 -0
- /package/{dist → lib}/instrument/instrument.spec.d.ts +0 -0
- /package/{dist → lib}/instrument/instrument.spec.d.ts.map +0 -0
- /package/{dist → lib}/instrument/instrument.spec.js +0 -0
- /package/{dist → lib}/module.d.ts +0 -0
- /package/{dist → lib}/module.js +0 -0
- /package/{dist → lib}/module.spec.d.ts +0 -0
- /package/{dist → lib}/module.spec.d.ts.map +0 -0
- /package/{dist → lib}/module.spec.js +0 -0
- /package/{dist → lib}/replay/use-replay-options.d.ts +0 -0
- /package/{dist → lib}/replay/use-replay-options.d.ts.map +0 -0
- /package/{dist → lib}/replay/use-replay.spec.d.ts +0 -0
- /package/{dist → lib}/replay/use-replay.spec.d.ts.map +0 -0
- /package/{dist → lib}/session/index.d.ts +0 -0
- /package/{dist → lib}/session/index.d.ts.map +0 -0
- /package/{dist → lib}/session/index.js +0 -0
- /package/{dist → lib}/session/use-session-object.js +0 -0
- /package/{dist → lib}/session/use-session-storage.d.ts +0 -0
- /package/{dist → lib}/session/use-session-storage.d.ts.map +0 -0
- /package/{dist → lib}/shared/datetime.d.ts +0 -0
- /package/{dist → lib}/shared/datetime.d.ts.map +0 -0
- /package/{dist → lib}/shared/decimals.spec.d.ts +0 -0
- /package/{dist → lib}/shared/decimals.spec.d.ts.map +0 -0
- /package/{dist → lib}/shared/decimals.spec.js +0 -0
- /package/{dist → lib}/shared/environment.d.ts +0 -0
- /package/{dist → lib}/shared/environment.d.ts.map +0 -0
- /package/{dist → lib}/shared/index.d.ts +0 -0
- /package/{dist → lib}/shared/index.d.ts.map +0 -0
- /package/{dist → lib}/shared/index.js +0 -0
- /package/{dist → lib}/simulator/index.d.ts +0 -0
- /package/{dist → lib}/simulator/index.d.ts.map +0 -0
- /package/{dist → lib}/simulator/index.js +0 -0
- /package/{dist → lib}/simulator/use-simulator.d.ts +0 -0
- /package/{dist → lib}/simulator/use-simulator.d.ts.map +0 -0
- /package/{dist → lib}/simulator/use-simulator.spec.d.ts +0 -0
- /package/{dist → lib}/simulator/use-simulator.spec.d.ts.map +0 -0
- /package/{dist → lib}/storage/in-memory/in-memory-storage.d.ts +0 -0
- /package/{dist → lib}/storage/in-memory/in-memory-storage.d.ts.map +0 -0
- /package/{dist → lib}/storage/in-memory/in-memory-storage.factory.d.ts +0 -0
- /package/{dist → lib}/storage/in-memory/in-memory-storage.factory.d.ts.map +0 -0
- /package/{dist → lib}/storage/in-memory/in-memory-storage.js +0 -0
- /package/{dist → lib}/storage/in-memory/in-memory-storage.spec.d.ts +0 -0
- /package/{dist → lib}/storage/in-memory/in-memory-storage.spec.d.ts.map +0 -0
- /package/{dist → lib}/storage/in-memory/in-memory-storage.spec.js +0 -0
- /package/{dist → lib}/storage/in-memory/index.d.ts +0 -0
- /package/{dist → lib}/storage/in-memory/index.d.ts.map +0 -0
- /package/{dist → lib}/storage/in-memory/index.js +0 -0
- /package/{dist → lib}/storage/storage.d.ts +0 -0
- /package/{dist → lib}/storage/storage.js +0 -0
- /package/{dist → lib}/storage/use-cache.d.ts +0 -0
- /package/{dist → lib}/storage/use-cache.js +0 -0
- /package/{dist → lib}/storage/use-cache.spec.d.ts +0 -0
- /package/{dist → lib}/storage/use-cache.spec.d.ts.map +0 -0
- /package/{dist → lib}/storage/use-cache.spec.js +0 -0
- /package/{dist → lib}/storage/use-storage.d.ts +0 -0
- /package/{dist → lib}/use-hash.d.ts +0 -0
- /package/{dist → lib}/use-hash.d.ts.map +0 -0
- /package/{dist → lib}/use-hash.spec.d.ts +0 -0
- /package/{dist → lib}/use-hash.spec.d.ts.map +0 -0
- /package/{dist → lib}/use-hash.spec.js +0 -0
- /package/{dist → lib}/use-logger.d.ts +0 -0
- /package/{dist → lib}/use-logger.d.ts.map +0 -0
- /package/{dist → lib}/use-memo.spec.d.ts +0 -0
- /package/{dist → lib}/use-memo.spec.d.ts.map +0 -0
- /package/{dist → lib}/use-memo.spec.js +0 -0
- /package/{dist → lib}/use-timestamp.d.ts +0 -0
- /package/{dist → lib}/use-timestamp.d.ts.map +0 -0
package/src/shared/decimals.ts
CHANGED
|
@@ -16,54 +16,9 @@ Decimal.prototype.toCeil = function (decimalPlaces: number) {
|
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
export class decimal extends Decimal {}
|
|
19
|
-
/*
|
|
20
|
-
decimal.prototype.toJSON = function () {
|
|
21
|
-
return '444';
|
|
22
|
-
};
|
|
23
|
-
*/
|
|
24
19
|
|
|
25
20
|
export function d(value: Decimal.Value) {
|
|
26
21
|
return new decimal(value);
|
|
27
22
|
}
|
|
28
23
|
|
|
29
24
|
d.Zero = new decimal(0);
|
|
30
|
-
|
|
31
|
-
export function weightedMean(values: decimal[], weights: decimal[]): decimal {
|
|
32
|
-
const result = values
|
|
33
|
-
.map((value, i) => {
|
|
34
|
-
const weight = weights[i];
|
|
35
|
-
const sum = value.mul(weight);
|
|
36
|
-
return [sum, weight];
|
|
37
|
-
})
|
|
38
|
-
.reduce((p, c) => [p[0].add(c[0]), p[1].add(c[1])], [d.Zero, d.Zero]);
|
|
39
|
-
|
|
40
|
-
if (!result[1]) {
|
|
41
|
-
return d.Zero;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return result[0].div(result[1]);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function pnl(entryRate: decimal, exitRate: decimal, amount: decimal) {
|
|
48
|
-
return exitRate.div(entryRate).minus(1).mul(amount);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
*
|
|
53
|
-
* @param timestamp
|
|
54
|
-
* @param timeframe
|
|
55
|
-
* @returns nearest timestamp to the given timeframe
|
|
56
|
-
*/
|
|
57
|
-
export function candledown(timestamp: number, timeframe: number): number {
|
|
58
|
-
return timestamp - (timestamp % timeframe);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
*
|
|
63
|
-
* @param timestamp
|
|
64
|
-
* @param timeframe
|
|
65
|
-
* @returns nearest timestamp to the given timeframe
|
|
66
|
-
*/
|
|
67
|
-
export function candleup(timestamp: number, timeframe: number): number {
|
|
68
|
-
return candledown(timestamp, timeframe) + timeframe;
|
|
69
|
-
}
|
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
import { makeTestModule
|
|
1
|
+
import { makeTestModule } from '@lib/make-test-module';
|
|
2
2
|
import { useExecutionMode } from '@lib/use-execution-mode';
|
|
3
3
|
|
|
4
4
|
import { useSimulator } from './use-simulator';
|
|
5
5
|
|
|
6
|
-
jest.mock('@lib/use-execution-mode', () => ({
|
|
7
|
-
...jest.requireActual('@lib/use-execution-mode'),
|
|
8
|
-
useExecutionMode: jest.fn()
|
|
9
|
-
}));
|
|
10
|
-
|
|
11
6
|
describe(useSimulator.name, () => {
|
|
12
7
|
let fixtures: Awaited<ReturnType<typeof getFixtures>>;
|
|
13
8
|
|
|
@@ -15,22 +10,16 @@ describe(useSimulator.name, () => {
|
|
|
15
10
|
fixtures = await getFixtures();
|
|
16
11
|
});
|
|
17
12
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
test('get real value when simulator mode disabled', () => {
|
|
21
|
-
const { act } = fixtures;
|
|
22
|
-
|
|
23
|
-
fixtures.givenSimulationEnabled(false);
|
|
13
|
+
test('get real value when simulator mode disabled', async () => {
|
|
14
|
+
const { act } = await fixtures.givenModule(false);
|
|
24
15
|
|
|
25
16
|
const value = act(() => useSimulator('simulator', 'real'));
|
|
26
17
|
|
|
27
18
|
expect(value).toBe('real');
|
|
28
19
|
});
|
|
29
20
|
|
|
30
|
-
test('get simulation value when simulator mode enabled', () => {
|
|
31
|
-
const { act } = fixtures;
|
|
32
|
-
|
|
33
|
-
fixtures.givenSimulationEnabled(true);
|
|
21
|
+
test('get simulation value when simulator mode enabled', async () => {
|
|
22
|
+
const { act } = await fixtures.givenModule(true);
|
|
34
23
|
|
|
35
24
|
const value = act(() => useSimulator('simulator', 'real'));
|
|
36
25
|
|
|
@@ -39,16 +28,11 @@ describe(useSimulator.name, () => {
|
|
|
39
28
|
});
|
|
40
29
|
|
|
41
30
|
async function getFixtures() {
|
|
42
|
-
const { act } = await makeTestModule([]);
|
|
43
|
-
|
|
44
31
|
return {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
} as any);
|
|
51
|
-
},
|
|
52
|
-
clear: jest.clearAllMocks
|
|
32
|
+
async givenModule(isSimulation: boolean) {
|
|
33
|
+
return isSimulation
|
|
34
|
+
? await makeTestModule([useExecutionMode.paperOptions({ recording: false })])
|
|
35
|
+
: await makeTestModule([useExecutionMode.liveOptions({ recording: false })]);
|
|
36
|
+
}
|
|
53
37
|
};
|
|
54
38
|
}
|
package/src/storage/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useContext } from '@lib/module';
|
|
1
|
+
import { Dependency, useContext } from '@lib/module';
|
|
2
2
|
import { Storage } from '@lib/storage';
|
|
3
3
|
|
|
4
4
|
const token = Symbol('storage-factory-token');
|
|
@@ -7,11 +7,11 @@ export interface StorageFactory {
|
|
|
7
7
|
for(key: string): Storage;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export function
|
|
11
|
-
return
|
|
12
|
-
provide: token,
|
|
13
|
-
useValue: factory
|
|
14
|
-
};
|
|
10
|
+
export function useStorageFactory() {
|
|
11
|
+
return useContext<StorageFactory>(token);
|
|
15
12
|
}
|
|
16
13
|
|
|
17
|
-
|
|
14
|
+
useStorageFactory.options = (factory: StorageFactory): Dependency => ({
|
|
15
|
+
provide: token,
|
|
16
|
+
useValue: factory
|
|
17
|
+
});
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { use } from '@lib/use';
|
|
2
1
|
import { dependency, useHash } from '@lib/use-hash';
|
|
2
|
+
import { withMemo } from '@lib/with-memo';
|
|
3
3
|
|
|
4
4
|
import { useStorageFactory } from './use-storage-factory';
|
|
5
5
|
|
|
6
|
-
export const useStorage =
|
|
6
|
+
export const useStorage = withMemo((dependencies: dependency[]) => {
|
|
7
7
|
const key = useHash(dependencies);
|
|
8
8
|
const factory = useStorageFactory();
|
|
9
9
|
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { of } from 'rxjs';
|
|
2
|
+
|
|
3
|
+
import { Dependency } from './module';
|
|
4
|
+
import { after, before, behavior, strategy } from './strategy';
|
|
5
|
+
|
|
6
|
+
describe(strategy.name, () => {
|
|
7
|
+
let fixtures: ReturnType<typeof getFixtures>;
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
fixtures = getFixtures();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('happy path', () => {
|
|
14
|
+
const sut = strategy(() => {
|
|
15
|
+
before(() => of('before'));
|
|
16
|
+
behavior(() => of('behavior 1'));
|
|
17
|
+
behavior(() => of('behavior 2'));
|
|
18
|
+
after(() => of('after'));
|
|
19
|
+
|
|
20
|
+
return fixtures.given.dependencies();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
expect(sut.dependencies).toEqual(
|
|
24
|
+
expect.arrayContaining(fixtures.given.dependencies())
|
|
25
|
+
);
|
|
26
|
+
expect(sut.description).toEqual({
|
|
27
|
+
before: [expect.any(Function)],
|
|
28
|
+
behavior: [expect.any(Function), expect.any(Function)],
|
|
29
|
+
after: [expect.any(Function)]
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
function getFixtures() {
|
|
35
|
+
return {
|
|
36
|
+
given: {
|
|
37
|
+
dependencies(): Dependency[] {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
package/src/strategy.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Observable, of } from 'rxjs';
|
|
2
|
+
|
|
3
|
+
import { Dependency } from './module';
|
|
4
|
+
|
|
5
|
+
export type Descriptor = () => Observable<unknown>;
|
|
6
|
+
|
|
7
|
+
export let before: (descriptor: Descriptor) => void;
|
|
8
|
+
export let behavior: (descriptor: Descriptor) => void;
|
|
9
|
+
export let after: (descriptor: Descriptor) => void;
|
|
10
|
+
|
|
11
|
+
export function strategy(descriptor: () => Dependency[]) {
|
|
12
|
+
const description = {
|
|
13
|
+
before: Array.of<Descriptor>(),
|
|
14
|
+
behavior: Array.of<Descriptor>(),
|
|
15
|
+
after: Array.of<Descriptor>()
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
before = descriptor => description.before.push(descriptor);
|
|
19
|
+
behavior = descriptor => description.behavior.push(descriptor);
|
|
20
|
+
after = descriptor => description.after.push(descriptor);
|
|
21
|
+
|
|
22
|
+
const dependencies = descriptor();
|
|
23
|
+
|
|
24
|
+
if (!description.before.length) {
|
|
25
|
+
description.before.push(() => of(true));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!description.after.length) {
|
|
29
|
+
description.after.push(() => of(true));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
dependencies,
|
|
34
|
+
description
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
import { useContext } from '@lib/module';
|
|
1
|
+
import { Dependency, useContext } from '@lib/module';
|
|
2
2
|
|
|
3
3
|
const injectionToken = Symbol('execution-mode');
|
|
4
4
|
|
|
5
5
|
type ExecutionMode = {
|
|
6
|
-
mode: '
|
|
6
|
+
mode: 'replay' | 'paper' | 'live' | 'idle';
|
|
7
7
|
recording: boolean;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
-
export function
|
|
11
|
-
|
|
12
|
-
provide: injectionToken,
|
|
13
|
-
useValue: { mode: 'REPLAY', recording: false } as ExecutionMode
|
|
14
|
-
};
|
|
15
|
-
}
|
|
10
|
+
export function useExecutionMode() {
|
|
11
|
+
const { mode, recording } = useContext<ExecutionMode>(injectionToken);
|
|
16
12
|
|
|
17
|
-
export function paperExecutionMode(options: { recording: boolean }) {
|
|
18
13
|
return {
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
isReplay: mode === 'replay',
|
|
15
|
+
isPaper: mode === 'paper',
|
|
16
|
+
isLive: mode === 'live',
|
|
17
|
+
isIdle: mode === 'idle',
|
|
18
|
+
isSimulation: mode !== 'live',
|
|
19
|
+
recording
|
|
21
20
|
};
|
|
22
21
|
}
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
};
|
|
29
|
-
}
|
|
23
|
+
useExecutionMode.replayOptions = (): Dependency => ({
|
|
24
|
+
provide: injectionToken,
|
|
25
|
+
useValue: { mode: 'replay', recording: false } as ExecutionMode
|
|
26
|
+
});
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
useExecutionMode.paperOptions = (options: { recording: boolean }): Dependency => ({
|
|
29
|
+
provide: injectionToken,
|
|
30
|
+
useValue: { mode: 'paper', ...options } as ExecutionMode
|
|
31
|
+
});
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
33
|
+
useExecutionMode.liveOptions = (options: { recording: boolean }): Dependency => ({
|
|
34
|
+
provide: injectionToken,
|
|
35
|
+
useValue: { mode: 'live', ...options } as ExecutionMode
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
useExecutionMode.idleOptions = (): Dependency => ({
|
|
39
|
+
provide: injectionToken,
|
|
40
|
+
useValue: { mode: 'idle', recording: false } as ExecutionMode
|
|
41
|
+
});
|
package/src/use-logger.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
2
|
|
|
3
|
-
import { use } from './use';
|
|
4
3
|
import { useTimestamp } from './use-timestamp';
|
|
4
|
+
import { withMemo } from './with-memo';
|
|
5
5
|
|
|
6
6
|
const colorize = (content: string) => {
|
|
7
7
|
let hash = 0x811c9dc5;
|
|
@@ -17,11 +17,11 @@ const colorize = (content: string) => {
|
|
|
17
17
|
/**
|
|
18
18
|
*
|
|
19
19
|
*/
|
|
20
|
-
export const useLogger =
|
|
20
|
+
export const useLogger = withMemo((context: string, tint?: string) => {
|
|
21
21
|
const prefix = () =>
|
|
22
|
-
`${chalk.
|
|
23
|
-
|
|
24
|
-
)(context)}`;
|
|
22
|
+
`${chalk.hex(tint ?? colorize(context))(
|
|
23
|
+
new Date(useTimestamp()).toISOString()
|
|
24
|
+
)} ${chalk.hex(tint ?? colorize(context))(context)}`;
|
|
25
25
|
|
|
26
26
|
return {
|
|
27
27
|
info: (message: any, ...params: unknown[]) =>
|
|
@@ -41,7 +41,7 @@ export const useLogger = use((context: string, tint?: string) => {
|
|
|
41
41
|
|
|
42
42
|
error: (message: any, ...params: unknown[]) =>
|
|
43
43
|
params?.length
|
|
44
|
-
? console.error(`${prefix()}: ${message}`, params)
|
|
45
|
-
: console.error(`${prefix()}: ${message}`)
|
|
44
|
+
? console.error(`${prefix()}: ${chalk.red(message)}`, params)
|
|
45
|
+
: console.error(`${prefix()}: ${chalk.red(message)}`)
|
|
46
46
|
};
|
|
47
47
|
});
|
package/src/use-memo.ts
CHANGED
|
@@ -1,28 +1,8 @@
|
|
|
1
|
-
import { useContext } from '@lib/module';
|
|
1
|
+
import { Dependency, useContext } from '@lib/module';
|
|
2
2
|
import { dependency, useHash } from '@lib/use-hash';
|
|
3
3
|
|
|
4
4
|
export const token = Symbol('memo-token');
|
|
5
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
6
|
export function useMemo<T>(calculateValue: () => T, dependencies: dependency[]) {
|
|
27
7
|
const memory = useContext<Record<string, any>>(token);
|
|
28
8
|
const hash = useHash(dependencies);
|
|
@@ -33,3 +13,8 @@ export function useMemo<T>(calculateValue: () => T, dependencies: dependency[])
|
|
|
33
13
|
|
|
34
14
|
return (memory[hash] = calculateValue()) as T;
|
|
35
15
|
}
|
|
16
|
+
|
|
17
|
+
useMemo.options = (): Dependency => ({
|
|
18
|
+
provide: token,
|
|
19
|
+
useValue: {}
|
|
20
|
+
});
|
package/src/use-socket.ts
CHANGED
|
@@ -1,59 +1,97 @@
|
|
|
1
|
-
import { Observable } from 'rxjs';
|
|
1
|
+
import { defer, Observable, of, ReplaySubject } from 'rxjs';
|
|
2
2
|
import { WebSocket } from 'ws';
|
|
3
3
|
|
|
4
4
|
import { useLogger } from './use-logger';
|
|
5
5
|
import { useTimestamp } from './use-timestamp';
|
|
6
6
|
|
|
7
7
|
export function useSocket(
|
|
8
|
-
url: string
|
|
9
|
-
|
|
8
|
+
url: string,
|
|
9
|
+
options: { pingInterval?: number } = { pingInterval: 5000 }
|
|
10
|
+
) {
|
|
10
11
|
const { debug } = useLogger('useSocket');
|
|
12
|
+
const socket = new WebSocket(url);
|
|
13
|
+
const monitor = new ReplaySubject<'opened' | 'closed' | 'errored'>();
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
15
|
+
socket.on('error', e => {
|
|
16
|
+
debug('errored', url, e);
|
|
17
|
+
monitor.next('errored');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
socket.on('close', () => {
|
|
21
|
+
debug('closed', url);
|
|
22
|
+
monitor.next('closed');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
socket.on('open', () => {
|
|
26
|
+
debug('opened', url);
|
|
27
|
+
monitor.next('opened');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
/**
|
|
32
|
+
* Observes socket events and handles connection health monitoring via ping/pong
|
|
33
|
+
* @returns observable emitting message events with timestamps and parsed payloads
|
|
34
|
+
*/
|
|
35
|
+
watch(): Observable<{ timestamp: number; payload: unknown }> {
|
|
36
|
+
let isAlive = false;
|
|
37
|
+
let interval: NodeJS.Timeout | undefined;
|
|
38
|
+
|
|
39
|
+
return new Observable(stream => {
|
|
40
|
+
socket.onmessage = it =>
|
|
41
|
+
stream.next({
|
|
42
|
+
timestamp: useTimestamp(),
|
|
43
|
+
payload: JSON.parse(it.data as string)
|
|
44
|
+
});
|
|
45
|
+
socket.onerror = it => {
|
|
46
|
+
clearInterval(interval);
|
|
47
|
+
stream.error(it);
|
|
48
|
+
};
|
|
49
|
+
socket.onclose = () => {
|
|
38
50
|
clearInterval(interval);
|
|
39
|
-
|
|
40
|
-
|
|
51
|
+
stream.error();
|
|
52
|
+
};
|
|
53
|
+
socket.onopen = () => {
|
|
54
|
+
isAlive = true;
|
|
55
|
+
interval = setInterval(() => {
|
|
56
|
+
if (isAlive) {
|
|
57
|
+
isAlive = false;
|
|
41
58
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
59
|
+
socket.ping();
|
|
60
|
+
} else {
|
|
61
|
+
socket.terminate();
|
|
62
|
+
clearInterval(interval);
|
|
63
|
+
}
|
|
64
|
+
}, options.pingInterval);
|
|
65
|
+
|
|
66
|
+
socket.on('pong', () => {
|
|
67
|
+
isAlive = true;
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
socket.on('ping', () => {
|
|
71
|
+
isAlive = true;
|
|
72
|
+
socket.pong();
|
|
73
|
+
});
|
|
74
|
+
};
|
|
45
75
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
76
|
+
return () => {
|
|
77
|
+
clearInterval(interval);
|
|
78
|
+
socket.terminate();
|
|
79
|
+
};
|
|
49
80
|
});
|
|
50
|
-
}
|
|
81
|
+
},
|
|
51
82
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
83
|
+
send(message: { payload: unknown }): Observable<{ timestamp: number }> {
|
|
84
|
+
return defer(() => {
|
|
85
|
+
debug('sent', url, message.payload);
|
|
86
|
+
|
|
87
|
+
socket.send(JSON.stringify(message.payload));
|
|
88
|
+
|
|
89
|
+
return of({ timestamp: useTimestamp() });
|
|
90
|
+
});
|
|
91
|
+
},
|
|
57
92
|
|
|
58
|
-
|
|
93
|
+
monitor() {
|
|
94
|
+
return monitor.asObservable();
|
|
95
|
+
}
|
|
96
|
+
};
|
|
59
97
|
}
|
package/src/use-timestamp.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useReplayManager } from '@lib/replay';
|
|
2
2
|
import { useExecutionMode } from '@lib/use-execution-mode';
|
|
3
3
|
|
|
4
4
|
export function useTimestamp() {
|
|
5
5
|
const { isReplay } = useExecutionMode();
|
|
6
6
|
|
|
7
7
|
if (isReplay) {
|
|
8
|
-
return
|
|
8
|
+
return useReplayManager().timestamp();
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
return Date.now();
|
|
@@ -0,0 +1,61 @@
|
|
|
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 whenSocket(
|
|
8
|
+
url: string,
|
|
9
|
+
options: { pingInterval?: number } = { pingInterval: 5000 }
|
|
10
|
+
): [Observable<{ timestamp: number; payload: unknown }>, (message: unknown) => void] {
|
|
11
|
+
const { debug } = useLogger('whenSocket');
|
|
12
|
+
|
|
13
|
+
const message = new Observable<{ timestamp: number; payload: unknown }>(stream => {
|
|
14
|
+
const socket = new WebSocket(url);
|
|
15
|
+
let isAlive = false;
|
|
16
|
+
let interval: NodeJS.Timeout | undefined;
|
|
17
|
+
|
|
18
|
+
socket.onmessage = it =>
|
|
19
|
+
stream.next({ timestamp: useTimestamp(), payload: JSON.parse(it.data as string) });
|
|
20
|
+
socket.onerror = it => {
|
|
21
|
+
clearInterval(interval);
|
|
22
|
+
debug('errored', url);
|
|
23
|
+
stream.error(it);
|
|
24
|
+
};
|
|
25
|
+
socket.onclose = () => {
|
|
26
|
+
debug('closed', url);
|
|
27
|
+
clearInterval(interval);
|
|
28
|
+
stream.error();
|
|
29
|
+
};
|
|
30
|
+
socket.onopen = () => {
|
|
31
|
+
debug('opened', url);
|
|
32
|
+
isAlive = true;
|
|
33
|
+
interval = setInterval(() => {
|
|
34
|
+
if (isAlive) {
|
|
35
|
+
isAlive = false;
|
|
36
|
+
|
|
37
|
+
socket.ping();
|
|
38
|
+
} else {
|
|
39
|
+
socket.terminate();
|
|
40
|
+
clearInterval(interval);
|
|
41
|
+
}
|
|
42
|
+
}, options.pingInterval);
|
|
43
|
+
|
|
44
|
+
socket.on('pong', () => {
|
|
45
|
+
isAlive = true;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
socket.on('ping', () => {
|
|
49
|
+
isAlive = true;
|
|
50
|
+
socket.pong();
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return () => {
|
|
55
|
+
socket.terminate();
|
|
56
|
+
clearInterval(interval);
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return [message, (message: unknown) => JSON.stringify(message)];
|
|
61
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { makeTestModule } from '@lib/make-test-module';
|
|
2
2
|
|
|
3
3
|
import { instrumentOf, InstrumentSelector } from './instrument';
|
|
4
|
-
import {
|
|
4
|
+
import { withMemo } from './with-memo';
|
|
5
5
|
|
|
6
|
-
describe(
|
|
6
|
+
describe(withMemo.name, () => {
|
|
7
7
|
let fixtures: Awaited<ReturnType<typeof getFixtures>>;
|
|
8
8
|
|
|
9
9
|
beforeEach(async () => {
|
|
@@ -37,7 +37,7 @@ describe(use.name, () => {
|
|
|
37
37
|
async function getFixtures() {
|
|
38
38
|
const { act } = await makeTestModule([]);
|
|
39
39
|
|
|
40
|
-
const getValue =
|
|
40
|
+
const getValue = withMemo((instrument: InstrumentSelector) => instrument.id);
|
|
41
41
|
|
|
42
42
|
return {
|
|
43
43
|
act,
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { isObservable, Observable, shareReplay } from 'rxjs';
|
|
2
2
|
import { v4 } from 'uuid';
|
|
3
3
|
|
|
4
|
-
import { asReadonly } from './as-readonly';
|
|
5
4
|
import { throwWithContext } from './module';
|
|
5
|
+
import { asReadonly } from './operators';
|
|
6
6
|
import { dependency } from './use-hash';
|
|
7
7
|
import { useMemo } from './use-memo';
|
|
8
8
|
|
|
9
|
-
export function
|
|
9
|
+
export function withMemo<T extends Array<dependency>, U>(
|
|
10
10
|
fn: (...args: T) => U
|
|
11
11
|
): (...args: T) => U;
|
|
12
|
-
export function
|
|
12
|
+
export function withMemo<T extends Array<dependency>, U>(
|
|
13
13
|
fn: (...args: T) => Observable<U>
|
|
14
14
|
): (...args: T) => Observable<U>;
|
|
15
15
|
|
|
16
|
-
export function
|
|
16
|
+
export function withMemo<T extends Array<dependency>, U>(
|
|
17
17
|
fn: (...args: T) => U | Observable<U>
|
|
18
18
|
): (...args: T) => U | Observable<U> {
|
|
19
19
|
throwWithContext();
|