forked from Stellar-split/split-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursorTracker.ts
More file actions
96 lines (79 loc) · 2.69 KB
/
Copy pathcursorTracker.ts
File metadata and controls
96 lines (79 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Cursor tracker for persisting Horizon paging tokens.
*
* Provides an in-memory and pluggable storage-backed cursor store so that
* the horizon paginator can resume from the last-seen position across
* restarts or page walks.
*/
import type { CursorStore } from "./types.js";
export type CursorPersistence = CursorStore;
/**
* In-memory cursor store suitable for session-scoped pagination.
* Cursors are lost when the process exits.
*/
export class InMemoryCursorStore implements CursorStore {
private store = new Map<string, string>();
async save(key: string, cursor: string): Promise<void> {
this.store.set(key, cursor);
}
async load(key: string): Promise<string | null> {
return this.store.get(key) ?? null;
}
async delete(key: string): Promise<void> {
this.store.delete(key);
}
/** Remove all saved cursors. */
clear(): void {
this.store.clear();
}
}
/** Singleton in-memory store shared across the module. */
let defaultStore: CursorStore = new InMemoryCursorStore();
const syncCursorStore = new Map<string, string>();
/**
* Override the default cursor store.
* Useful for plugging in localStorage, IndexedDB, or a remote store.
*/
export function setDefaultCursorStore(store: CursorStore): void {
defaultStore = store;
}
/** Alias retained for public API compatibility. */
export function configureCursorStore(store: CursorStore): void {
setDefaultCursorStore(store);
}
/**
* Get the current default cursor store.
*/
export function getDefaultCursorStore(): CursorStore {
return defaultStore;
}
/** Persist a cursor in the default in-memory stream cursor cache. */
export function setCursor(key: string, cursor: string): void {
syncCursorStore.set(key, cursor);
void defaultStore.save(key, cursor);
}
/** Read a cursor from the default in-memory stream cursor cache. */
export function getCursor(key: string): string | null {
return syncCursorStore.get(key) ?? null;
}
/** Remove a persisted cursor from the default stream cursor cache. */
export function removeCursor(key: string): void {
syncCursorStore.delete(key);
void defaultStore.delete(key);
}
/** Persist a cursor from a snapshot-like object containing a cursor field. */
export function setCursorFromSnapshot(key: string, snapshot: { cursor?: string | number | null }): void {
if (snapshot.cursor !== undefined && snapshot.cursor !== null) {
setCursor(key, String(snapshot.cursor));
}
}
/** Clear in-memory cursors for tests. */
export function _resetCursorTrackerForTesting(): void {
syncCursorStore.clear();
}
/**
* Build a namespaced cursor key from a base name and namespace.
*/
export function buildCursorKey(namespace: string, name: string): string {
return `${namespace}:${name}`;
}