@quantform/core 0.7.0-beta.33 → 0.7.0-beta.35

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 (86) hide show
  1. package/dist/cli/pull.d.ts.map +1 -1
  2. package/dist/cli/pull.js +4 -81
  3. package/dist/cli/replay.d.ts.map +1 -1
  4. package/dist/cli/replay.js +3 -35
  5. package/dist/cli/run.d.ts.map +1 -1
  6. package/dist/cli/run.js +3 -37
  7. package/dist/index.d.ts +1 -4
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +1 -4
  10. package/dist/replay/index.d.ts +6 -4
  11. package/dist/replay/index.d.ts.map +1 -1
  12. package/dist/replay/index.js +6 -4
  13. package/dist/replay/replay-guard.d.ts +10 -0
  14. package/dist/replay/replay-guard.d.ts.map +1 -0
  15. package/dist/replay/replay-guard.js +8 -0
  16. package/dist/replay/replay.d.ts +10 -0
  17. package/dist/replay/replay.d.ts.map +1 -0
  18. package/dist/replay/replay.js +9 -0
  19. package/dist/replay/use-replay-breakpoint.d.ts +3 -0
  20. package/dist/replay/use-replay-breakpoint.d.ts.map +1 -0
  21. package/dist/replay/use-replay-breakpoint.js +22 -0
  22. package/dist/replay/use-replay-manager.d.ts +12 -0
  23. package/dist/replay/use-replay-manager.d.ts.map +1 -0
  24. package/dist/replay/use-replay-manager.js +88 -0
  25. package/dist/replay/use-replay-storage-buffer.d.ts +15 -0
  26. package/dist/replay/use-replay-storage-buffer.d.ts.map +1 -0
  27. package/dist/replay/use-replay-storage-buffer.js +50 -0
  28. package/dist/replay/use-replay-storage.d.ts +11 -8
  29. package/dist/replay/use-replay-storage.d.ts.map +1 -1
  30. package/dist/replay/use-replay-storage.js +34 -5
  31. package/dist/replay/use-replay-storage.spec.d.ts +2 -0
  32. package/dist/replay/use-replay-storage.spec.d.ts.map +1 -0
  33. package/dist/replay/use-replay-storage.spec.js +74 -0
  34. package/dist/replay/use-replay.d.ts +2 -2
  35. package/dist/replay/use-replay.d.ts.map +1 -1
  36. package/dist/replay/use-replay.js +8 -20
  37. package/dist/replay/use-replay.spec.js +125 -101
  38. package/dist/storage/index.d.ts +1 -0
  39. package/dist/storage/index.d.ts.map +1 -1
  40. package/dist/storage/index.js +1 -0
  41. package/dist/use-execution-mode.d.ts +6 -1
  42. package/dist/use-execution-mode.d.ts.map +1 -1
  43. package/dist/use-execution-mode.js +16 -8
  44. package/dist/use-timestamp.js +2 -2
  45. package/package.json +1 -1
  46. package/src/cli/pull.ts +6 -63
  47. package/src/cli/replay.ts +4 -16
  48. package/src/cli/run.ts +4 -18
  49. package/src/index.ts +1 -4
  50. package/src/replay/index.ts +6 -4
  51. package/src/replay/replay-guard.ts +11 -0
  52. package/src/replay/replay.ts +13 -0
  53. package/src/replay/use-replay-breakpoint.ts +29 -0
  54. package/src/replay/use-replay-manager.ts +106 -0
  55. package/src/replay/use-replay-storage-buffer.ts +43 -0
  56. package/src/replay/use-replay-storage.spec.ts +85 -0
  57. package/src/replay/use-replay-storage.ts +26 -5
  58. package/src/replay/use-replay.spec.ts +10 -3
  59. package/src/replay/use-replay.ts +11 -17
  60. package/src/storage/index.ts +1 -0
  61. package/src/use-execution-mode.ts +16 -8
  62. package/src/use-timestamp.ts +2 -2
  63. package/dist/replay/use-replay-coordinator.d.ts +0 -10
  64. package/dist/replay/use-replay-coordinator.d.ts.map +0 -1
  65. package/dist/replay/use-replay-coordinator.js +0 -119
  66. package/dist/replay/use-replay-reader.d.ts +0 -7
  67. package/dist/replay/use-replay-reader.d.ts.map +0 -1
  68. package/dist/replay/use-replay-reader.js +0 -32
  69. package/dist/replay/use-replay-reader.spec.d.ts +0 -2
  70. package/dist/replay/use-replay-reader.spec.d.ts.map +0 -1
  71. package/dist/replay/use-replay-reader.spec.js +0 -58
  72. package/dist/replay/use-replay-writer.d.ts +0 -6
  73. package/dist/replay/use-replay-writer.d.ts.map +0 -1
  74. package/dist/replay/use-replay-writer.js +0 -14
  75. package/dist/replay/use-replay-writer.spec.d.ts +0 -2
  76. package/dist/replay/use-replay-writer.spec.d.ts.map +0 -1
  77. package/dist/replay/use-replay-writer.spec.js +0 -53
  78. package/dist/replay/with-replay.d.ts +0 -4
  79. package/dist/replay/with-replay.d.ts.map +0 -1
  80. package/dist/replay/with-replay.js +0 -8
  81. package/src/replay/use-replay-coordinator.ts +0 -142
  82. package/src/replay/use-replay-reader.spec.ts +0 -64
  83. package/src/replay/use-replay-reader.ts +0 -23
  84. package/src/replay/use-replay-writer.spec.ts +0 -56
  85. package/src/replay/use-replay-writer.ts +0 -17
  86. package/src/replay/with-replay.ts +0 -10
package/src/cli/run.ts CHANGED
@@ -1,29 +1,15 @@
1
- import { join } from 'path';
2
- import { lastValueFrom } from 'rxjs';
3
-
4
1
  import build from '@lib/cli/build';
5
- import { buildDirectory } from '@lib/cli/internal/workspace';
6
- import { core } from '@lib/core';
7
- import { Dependency, Module } from '@lib/module';
8
2
  import { liveExecutionMode } from '@lib/use-execution-mode';
9
3
 
4
+ import { Script } from './internal/script';
5
+
10
6
  export default async function (name: string, options: any) {
11
7
  if (await build()) {
12
8
  return;
13
9
  }
14
10
 
15
- const script = await import(join(buildDirectory(), name));
16
- const dependencies = script.module2 as Dependency[];
17
-
18
- const module = new Module([
19
- ...core(),
20
- ...dependencies,
21
- liveExecutionMode({ recording: true })
22
- ]);
23
-
24
- const { act } = await module.awake();
25
-
26
- const output = await act(() => lastValueFrom(script.default(options)));
11
+ const script = new Script(name, [liveExecutionMode({ recording: true })]);
12
+ const output = await script.run();
27
13
 
28
14
  console.log(output);
29
15
  }
package/src/index.ts CHANGED
@@ -7,13 +7,10 @@ export * from '@lib/use-timestamp';
7
7
  export * from '@lib/simulator';
8
8
  export * from '@lib/make-test-module';
9
9
  export * from '@lib/core';
10
- export * from '@lib/replay/use-replay-coordinator';
11
10
  export * from '@lib/use-execution-mode';
12
- export * from '@lib/storage/use-storage';
13
- export * from '@lib/storage/use-cache';
11
+ export * from '@lib/storage';
14
12
  export * from '@lib/use-logger';
15
13
  export * from '@lib/replay';
16
- export * from '@lib/replay/use-replay-coordinator';
17
14
  export * from '@lib/when-socket';
18
15
  export * from '@lib/with-request';
19
16
  export * from '@lib/with-memo';
@@ -1,6 +1,8 @@
1
1
  export * from './use-replay';
2
- export * from './use-replay-coordinator';
2
+ export * from './use-replay-breakpoint';
3
3
  export * from './use-replay-options';
4
- export * from './use-replay-reader';
5
- export * from './use-replay-writer';
6
- export * from './with-replay';
4
+ export * from './use-replay-storage';
5
+ export * from './use-replay-storage-buffer';
6
+ export * from './use-replay-manager';
7
+ export * from './replay';
8
+ export * from './replay-guard';
@@ -0,0 +1,11 @@
1
+ import { Observable } from 'rxjs';
2
+
3
+ import { dependency } from '@lib/use-hash';
4
+
5
+ import { useReplayBreakpoint } from './use-replay-breakpoint';
6
+
7
+ export function replayGuard<T extends Array<dependency>, K>(
8
+ fn: (...args: T) => Observable<{ timestamp: number; payload: K }>
9
+ ): (...args: T) => Observable<{ timestamp: number; payload: K }> {
10
+ return (...args: T) => useReplayBreakpoint(fn(...args));
11
+ }
@@ -0,0 +1,13 @@
1
+ import { Observable } from 'rxjs';
2
+
3
+ import { dependency } from '@lib/use-hash';
4
+ import { withMemo } from '@lib/with-memo';
5
+
6
+ import { useReplay } from './use-replay';
7
+
8
+ export function replay<T extends Array<dependency>, K>(
9
+ fn: (...args: T) => Observable<{ timestamp: number; payload: K }>,
10
+ dependencies: dependency[]
11
+ ): (...args: T) => Observable<{ timestamp: number; payload: K }> {
12
+ return withMemo((...args: T) => useReplay(fn(...args), [...dependencies, ...args]));
13
+ }
@@ -0,0 +1,29 @@
1
+ import { finalize, Observable } from 'rxjs';
2
+
3
+ import { useExecutionMode } from '@lib/use-execution-mode';
4
+ import { useLogger } from '@lib/use-logger';
5
+
6
+ import { useReplayManager } from './use-replay-manager';
7
+
8
+ export function useReplayBreakpoint<T>(input: Observable<T>): Observable<T> {
9
+ const { isReplay } = useExecutionMode();
10
+
11
+ if (!isReplay) {
12
+ return input;
13
+ }
14
+
15
+ const { info } = useLogger('useReplayBreakpoint');
16
+ const { stop, tryContinue } = useReplayManager();
17
+
18
+ info('locking resource...');
19
+
20
+ stop();
21
+
22
+ return input.pipe(
23
+ finalize(() => {
24
+ info('unlocking resource');
25
+
26
+ tryContinue();
27
+ })
28
+ );
29
+ }
@@ -0,0 +1,106 @@
1
+ import { defer, filter, map, Observable, Subject } from 'rxjs';
2
+
3
+ import { dependency, useHash } from '@lib/use-hash';
4
+ import { useLogger } from '@lib/use-logger';
5
+ import { withMemo } from '@lib/with-memo';
6
+
7
+ import { useReplayOptions } from './use-replay-options';
8
+ import { useReplayStorageBuffer } from './use-replay-storage-buffer';
9
+
10
+ export const useReplayManager = withMemo(() => {
11
+ const { from, to } = useReplayOptions();
12
+ const { info } = useLogger('useReplayManager');
13
+
14
+ let timestamp = from;
15
+ let stopAcquire = 1;
16
+ const subscriptions = Array.of<ReturnType<typeof useReplayStorageBuffer<any>>>();
17
+
18
+ const stream$ = new Subject<
19
+ [ReturnType<typeof useReplayStorageBuffer<any>>, { timestamp: number; payload: any }]
20
+ >();
21
+
22
+ const getNextStorage = async () => {
23
+ let next: ReturnType<typeof useReplayStorageBuffer<any>> | undefined;
24
+
25
+ for (const cursor of subscriptions) {
26
+ if (cursor.size() == 0 && !cursor.completed()) {
27
+ await cursor.fetchNextPage(timestamp, to + 1);
28
+ }
29
+
30
+ if (cursor.peek()) {
31
+ if (!next || next.peek().timestamp > cursor.peek().timestamp) {
32
+ next = cursor;
33
+ }
34
+ }
35
+ }
36
+
37
+ return next;
38
+ };
39
+
40
+ const processNext = async () => {
41
+ const cursor = await getNextStorage();
42
+
43
+ if (!cursor || !cursor.peek()) {
44
+ stream$.complete();
45
+
46
+ return false;
47
+ }
48
+
49
+ const sample = cursor.dequeue();
50
+
51
+ timestamp = sample.timestamp;
52
+
53
+ stream$.next([cursor, sample]);
54
+
55
+ return true;
56
+ };
57
+
58
+ const next = async () => {
59
+ if (await processNext()) {
60
+ if (stopAcquire === 0) {
61
+ setImmediate(next);
62
+ }
63
+ }
64
+ };
65
+
66
+ const tryContinue = () => {
67
+ if (stopAcquire == 0) {
68
+ return;
69
+ }
70
+
71
+ stopAcquire = Math.max(0, stopAcquire - 1);
72
+
73
+ if (stopAcquire != 0) {
74
+ return;
75
+ }
76
+
77
+ next();
78
+ };
79
+
80
+ return {
81
+ timestamp() {
82
+ return timestamp;
83
+ },
84
+ stop() {
85
+ stopAcquire++;
86
+ },
87
+ tryContinue,
88
+ when<T>(dependencies: dependency[]): Observable<{ timestamp: number; payload: T }> {
89
+ const storage = useReplayStorageBuffer<T>(dependencies);
90
+
91
+ if (!subscriptions.includes(storage)) {
92
+ info('subscribing to replay', useHash(dependencies));
93
+ subscriptions.push(storage);
94
+ }
95
+
96
+ return defer(() => {
97
+ tryContinue();
98
+
99
+ return stream$.pipe(
100
+ filter(([cur]) => cur === storage),
101
+ map(([, it]) => ({ timestamp: it.timestamp, payload: it.payload as T }))
102
+ );
103
+ });
104
+ }
105
+ };
106
+ });
@@ -0,0 +1,43 @@
1
+ import { between } from '@lib/storage';
2
+ import { dependency } from '@lib/use-hash';
3
+ import { withMemo } from '@lib/with-memo';
4
+
5
+ import { useReplayStorage } from './use-replay-storage';
6
+
7
+ export const useReplayStorageBuffer = withMemo(<T>(dependencies: dependency[]) => {
8
+ const { query } = useReplayStorage<T>(dependencies);
9
+
10
+ let page = new Array<{ timestamp: number; payload: T }>();
11
+ let index = 0;
12
+ let completed = false;
13
+
14
+ return {
15
+ size() {
16
+ return page.length - index;
17
+ },
18
+ peek() {
19
+ return page[index];
20
+ },
21
+ dequeue() {
22
+ return page[index++];
23
+ },
24
+ completed() {
25
+ return completed;
26
+ },
27
+ async fetchNextPage(from: number, to: number) {
28
+ if (completed) {
29
+ return;
30
+ }
31
+
32
+ index = 0;
33
+
34
+ page = await query({
35
+ where: {
36
+ timestamp: between(from, to)
37
+ },
38
+ limit: 10000
39
+ });
40
+ completed = page.length == 0;
41
+ }
42
+ };
43
+ });
@@ -0,0 +1,85 @@
1
+ import { makeTestModule, mockedFunc } from '@lib/make-test-module';
2
+ import { Query, QueryObject, Storage } from '@lib/storage';
3
+ import { useStorage } from '@lib/storage';
4
+
5
+ import { useReplayStorage } from './use-replay-storage';
6
+
7
+ jest.mock('@lib/storage', () => ({
8
+ ...jest.requireActual('@lib/storage'),
9
+ useStorage: jest.fn()
10
+ }));
11
+
12
+ describe(useReplayStorage.name, () => {
13
+ let fixtures: Awaited<ReturnType<typeof getFixtures>>;
14
+
15
+ beforeEach(async () => {
16
+ fixtures = await getFixtures();
17
+ });
18
+
19
+ describe('query', () => {
20
+ test('happy path', async () => {
21
+ await fixtures.given.stored(fixtures.sample);
22
+ const sample = await fixtures.when.queried({});
23
+
24
+ expect(sample).toEqual(fixtures.sample);
25
+ });
26
+ });
27
+
28
+ describe('save', () => {
29
+ test('happy path', async () => {
30
+ await fixtures.when.saved(fixtures.sample);
31
+ await fixtures.then.stored(fixtures.sample);
32
+ });
33
+ });
34
+ });
35
+
36
+ async function getFixtures() {
37
+ const { act } = await makeTestModule([]);
38
+
39
+ const dependencies = ['binance:btc-usdt', 'candle', 'h1'];
40
+ const save: Storage['save'] = jest.fn();
41
+ const query: jest.MockedFunction<Storage['query']> = jest.fn();
42
+
43
+ mockedFunc(useStorage).mockReturnValue({ save, query } as any);
44
+
45
+ return {
46
+ sample: [
47
+ { timestamp: 1, payload: { o: 1.1, h: 1.1, l: 1.1, c: 1.1 } },
48
+ { timestamp: 2, payload: { o: 1.1, h: 2.2, l: 1.1, c: 2.2 } },
49
+ { timestamp: 3, payload: { o: 1.1, h: 3.3, l: 1.1, c: 3.3 } }
50
+ ],
51
+
52
+ given: {
53
+ stored<T>(sample: { timestamp: number; payload: T }[]) {
54
+ return query.mockReturnValue(
55
+ Promise.resolve(
56
+ sample.map(it => ({
57
+ timestamp: it.timestamp,
58
+ payload: JSON.stringify(it.payload)
59
+ }))
60
+ )
61
+ );
62
+ }
63
+ },
64
+ when: {
65
+ saved<T>(sample: { timestamp: number; payload: T }[]) {
66
+ return act(() => useReplayStorage(dependencies).save(sample));
67
+ },
68
+ queried<T>(query: Query<QueryObject>) {
69
+ return act(() => useReplayStorage<T>(dependencies).query(query));
70
+ }
71
+ },
72
+
73
+ then: {
74
+ stored<T>(sample: { timestamp: number; payload: T }[]) {
75
+ expect(save).toHaveBeenCalledWith(
76
+ expect.anything(),
77
+ sample.map(it => ({
78
+ timestamp: it.timestamp,
79
+ payload: JSON.stringify(it.payload)
80
+ }))
81
+ );
82
+ }
83
+ }
84
+ };
85
+ }
@@ -1,8 +1,29 @@
1
- import { Storage, useStorage } from '@lib/storage';
1
+ import { Query, QueryObject, Storage, useStorage } from '@lib/storage';
2
+ import { dependency, useHash } from '@lib/use-hash';
2
3
 
3
- export const replaySerializableObject = (key: string) =>
4
- Storage.createObject(key, { timestamp: 'number', json: 'string' });
4
+ export function useReplayStorage<T>(dependencies: dependency[]) {
5
+ const storage = useStorage(['replay']);
6
+ const storageObjectKey = useHash(dependencies);
7
+ const storageObject = Storage.createObject(storageObjectKey, {
8
+ timestamp: 'number',
9
+ payload: 'string'
10
+ });
5
11
 
6
- export function useReplayStorage() {
7
- return useStorage(['replay']);
12
+ return {
13
+ async query(query: Query<QueryObject>) {
14
+ return (await storage.query(storageObject, query)).map(it => ({
15
+ timestamp: it.timestamp,
16
+ payload: JSON.parse(it.payload) as T
17
+ }));
18
+ },
19
+ save(objects: { timestamp: number; payload: T }[]) {
20
+ return storage.save(
21
+ storageObject,
22
+ objects.map(it => ({
23
+ timestamp: it.timestamp,
24
+ payload: JSON.stringify(it.payload)
25
+ }))
26
+ );
27
+ }
28
+ };
8
29
  }
@@ -1,15 +1,21 @@
1
+ import exp from 'constants';
1
2
  import { from, lastValueFrom, tap } from 'rxjs';
2
3
 
3
4
  import { makeTestModule } from '@lib/make-test-module';
4
- import { useReplayCoordinator } from '@lib/replay/use-replay-coordinator';
5
5
  import { replayExecutionMode } from '@lib/use-execution-mode';
6
6
  import { dependency } from '@lib/use-hash';
7
7
 
8
8
  import { between } from '..';
9
9
  import { useReplay } from './use-replay';
10
10
  import { replayOptions } from './use-replay-options';
11
- import { useReplayReader } from './use-replay-reader';
12
- import { useReplayWriter } from './use-replay-writer';
11
+
12
+ describe(useReplay.name, () => {
13
+ test('happy path', () => {
14
+ expect(true).toBe(true);
15
+ });
16
+ });
17
+
18
+ /*
13
19
 
14
20
  describe.skip(useReplayCoordinator.name, () => {
15
21
  let fixtures: Awaited<ReturnType<typeof getFixtures>>;
@@ -130,3 +136,4 @@ async function getFixtures() {
130
136
  }
131
137
  };
132
138
  }
139
+ */
@@ -1,34 +1,28 @@
1
- import { concatMap, map, Observable } from 'rxjs';
1
+ import { Observable, tap } from 'rxjs';
2
2
 
3
- import { useReplayCoordinator } from '@lib/replay/use-replay-coordinator';
4
3
  import { useExecutionMode } from '@lib/use-execution-mode';
5
4
  import { dependency } from '@lib/use-hash';
6
5
 
7
- import { useReplayWriter } from './use-replay-writer';
6
+ import { useReplayManager } from './use-replay-manager';
7
+ import { useReplayStorage } from './use-replay-storage';
8
8
 
9
- export const useReplay = <T>(
9
+ export function useReplay<T>(
10
10
  input: Observable<{ timestamp: number; payload: T }>,
11
11
  dependencies: dependency[]
12
- ) => {
12
+ ) {
13
13
  const { isReplay, recording } = useExecutionMode();
14
14
 
15
15
  if (isReplay) {
16
- const { subscribe } = useReplayCoordinator();
16
+ const { when } = useReplayManager();
17
17
 
18
- return subscribe(dependencies).pipe(
19
- map(it => it as unknown as { timestamp: number; payload: T })
20
- );
18
+ return when<T>(dependencies);
21
19
  }
22
20
 
23
21
  if (recording) {
24
- const writer = useReplayWriter(dependencies);
25
- return input.pipe(
26
- concatMap(async it => {
27
- await writer([it]);
28
- return it;
29
- })
30
- );
22
+ const { save } = useReplayStorage<T>(dependencies);
23
+
24
+ return input.pipe(tap(it => save([it])));
31
25
  }
32
26
 
33
27
  return input;
34
- };
28
+ }
@@ -1,4 +1,5 @@
1
1
  export * from './storage';
2
+ export * from './use-cache';
2
3
  export * from './use-storage';
3
4
  export * from './use-storage-factory';
4
5
  export * from './in-memory';
@@ -3,28 +3,35 @@ import { useContext } from '@lib/module';
3
3
  const injectionToken = Symbol('execution-mode');
4
4
 
5
5
  type ExecutionMode = {
6
- mode: 'REPLAY' | 'PAPER' | 'LIVE';
6
+ mode: 'replay' | 'paper' | 'live' | 'idle';
7
7
  recording: boolean;
8
8
  };
9
9
 
10
10
  export function replayExecutionMode() {
11
11
  return {
12
12
  provide: injectionToken,
13
- useValue: { mode: 'REPLAY', recording: false } as ExecutionMode
13
+ useValue: { mode: 'replay', recording: false } as ExecutionMode
14
14
  };
15
15
  }
16
16
 
17
17
  export function paperExecutionMode(options: { recording: boolean }) {
18
18
  return {
19
19
  provide: injectionToken,
20
- useValue: { mode: 'PAPER', ...options } as ExecutionMode
20
+ useValue: { mode: 'paper', ...options } as ExecutionMode
21
21
  };
22
22
  }
23
23
 
24
24
  export function liveExecutionMode(options: { recording: boolean }) {
25
25
  return {
26
26
  provide: injectionToken,
27
- useValue: { mode: 'LIVE', ...options } as ExecutionMode
27
+ useValue: { mode: 'live', ...options } as ExecutionMode
28
+ };
29
+ }
30
+
31
+ export function idleExecutionMode() {
32
+ return {
33
+ provide: injectionToken,
34
+ useValue: { mode: 'idle', recording: false } as ExecutionMode
28
35
  };
29
36
  }
30
37
 
@@ -32,10 +39,11 @@ export const useExecutionMode = () => {
32
39
  const mode = useContext<ExecutionMode>(injectionToken);
33
40
 
34
41
  return {
35
- isReplay: mode.mode === 'REPLAY',
36
- isPaper: mode.mode === 'PAPER',
37
- isLive: mode.mode === 'LIVE',
38
- isSimulation: mode.mode !== 'LIVE',
42
+ isReplay: mode.mode === 'replay',
43
+ isPaper: mode.mode === 'paper',
44
+ isLive: mode.mode === 'live',
45
+ isIdle: mode.mode === 'idle',
46
+ isSimulation: mode.mode !== 'live',
39
47
  recording: mode.recording
40
48
  };
41
49
  };
@@ -1,11 +1,11 @@
1
- import { useReplayCoordinator } from '@lib/replay/use-replay-coordinator';
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 useReplayCoordinator().timestamp();
8
+ return useReplayManager().timestamp();
9
9
  }
10
10
 
11
11
  return Date.now();
@@ -1,10 +0,0 @@
1
- import { dependency } from '../use-hash';
2
- export declare function useReplayCoordinator(): {
3
- timestamp: () => number;
4
- stop: () => number;
5
- tryContinue: () => void;
6
- subscribe: (dependencies: dependency[]) => import("rxjs").Observable<{
7
- timestamp: number;
8
- }>;
9
- };
10
- //# sourceMappingURL=use-replay-coordinator.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-replay-coordinator.d.ts","sourceRoot":"","sources":["../../src/replay/use-replay-coordinator.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAO3C,wBAAgB,oBAAoB;;;;8BAUC,UAAU,EAAE;mBAFW,MAAM;;EAsFjE"}
@@ -1,119 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.useReplayCoordinator = void 0;
13
- const rxjs_1 = require("rxjs");
14
- const use_memo_1 = require("../use-memo");
15
- const __1 = require("..");
16
- const use_replay_options_1 = require("./use-replay-options");
17
- const use_replay_reader_1 = require("./use-replay-reader");
18
- function useReplayCoordinator() {
19
- const options = (0, use_replay_options_1.useReplayOptions)();
20
- return (0, use_memo_1.useMemo)(() => {
21
- let timestamp = options.from;
22
- let stopAcquire = 1;
23
- let sequence = 0;
24
- const subscriptions = Array.of();
25
- const stream$ = new rxjs_1.Subject();
26
- const subscribe = (dependencies) => {
27
- const cursor = useSampleCursor(dependencies);
28
- if (!subscriptions.includes(cursor)) {
29
- subscriptions.push(cursor);
30
- }
31
- return (0, rxjs_1.defer)(() => {
32
- tryContinue();
33
- return stream$.pipe((0, rxjs_1.filter)(([cur]) => cur === cursor), (0, rxjs_1.map)(([, it]) => it));
34
- });
35
- };
36
- const current = () => __awaiter(this, void 0, void 0, function* () {
37
- let next;
38
- for (const cursor of subscriptions) {
39
- if (cursor.size() == 0 && !cursor.completed) {
40
- yield cursor.fetchNextPage(timestamp, options.to + 1);
41
- }
42
- if (cursor.peek()) {
43
- if (!next || next.peek().timestamp > cursor.peek().timestamp) {
44
- next = cursor;
45
- }
46
- }
47
- }
48
- return next;
49
- });
50
- const processNext = () => __awaiter(this, void 0, void 0, function* () {
51
- const cursor = yield current();
52
- if (!cursor || !cursor.peek()) {
53
- stream$.complete();
54
- return false;
55
- }
56
- const sample = cursor.dequeue();
57
- timestamp = sample.timestamp;
58
- sequence++;
59
- stream$.next([cursor, sample]);
60
- return true;
61
- });
62
- const next = () => __awaiter(this, void 0, void 0, function* () {
63
- if (yield processNext()) {
64
- if (stopAcquire === 0) {
65
- setImmediate(next);
66
- }
67
- }
68
- });
69
- const stop = () => stopAcquire++;
70
- const tryContinue = () => {
71
- if (stopAcquire == 0) {
72
- return;
73
- }
74
- stopAcquire = Math.max(0, stopAcquire - 1);
75
- if (stopAcquire != 0) {
76
- return;
77
- }
78
- next();
79
- };
80
- return {
81
- timestamp: () => timestamp,
82
- stop,
83
- tryContinue,
84
- subscribe
85
- };
86
- }, [useReplayCoordinator.name]);
87
- }
88
- exports.useReplayCoordinator = useReplayCoordinator;
89
- function useSampleCursor(dependencies) {
90
- return (0, use_memo_1.useMemo)(() => {
91
- const read = (0, use_replay_reader_1.useReplayReader)(dependencies);
92
- let page = new Array();
93
- let index = 0;
94
- let completed = false;
95
- const size = () => page.length - index;
96
- const peek = () => page[index];
97
- const dequeue = () => page[index++];
98
- const fetchNextPage = (from, to) => __awaiter(this, void 0, void 0, function* () {
99
- if (completed) {
100
- return;
101
- }
102
- index = 0;
103
- page = yield read({
104
- where: {
105
- timestamp: (0, __1.between)(from, to)
106
- },
107
- limit: 10000
108
- });
109
- completed = page.length == 0;
110
- });
111
- return {
112
- size,
113
- peek,
114
- dequeue,
115
- fetchNextPage,
116
- completed
117
- };
118
- }, [useSampleCursor.name, ...dependencies]);
119
- }