@hyper-fetch/db 0.0.0-stage → 10.2.0
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/README.md +180 -2
- package/__tests__/e2e/db.e2e.spec.ts +269 -0
- package/__tests__/features/connection/connection.spec.ts +23 -0
- package/__tests__/features/model/live-query.model.spec.ts +320 -0
- package/__tests__/features/operators/operators.spec.ts +113 -0
- package/__tests__/features/runtime/runtime.spec.ts +185 -0
- package/__tests__/features/serializer/serializer.spec.ts +73 -0
- package/__tests__/features/store/store.spec.ts +147 -0
- package/__tests__/features/types/db.types.spec.ts +337 -0
- package/__tests__/integration/audit.spec.ts +639 -0
- package/__tests__/integration/competitors.spec.ts +393 -0
- package/__tests__/integration/convergence.model.spec.ts +162 -0
- package/__tests__/integration/edge-cases.spec.ts +465 -0
- package/__tests__/integration/errors.spec.ts +178 -0
- package/__tests__/integration/infinite.model.spec.ts +142 -0
- package/__tests__/integration/infinite.spec.ts +290 -0
- package/__tests__/integration/inspect.spec.ts +103 -0
- package/__tests__/integration/joins-many.spec.ts +177 -0
- package/__tests__/integration/joins.spec.ts +179 -0
- package/__tests__/integration/key-loaders.spec.ts +196 -0
- package/__tests__/integration/links.spec.ts +105 -0
- package/__tests__/integration/mutations.spec.ts +337 -0
- package/__tests__/integration/normalize.spec.ts +80 -0
- package/__tests__/integration/offline.spec.ts +479 -0
- package/__tests__/integration/queries.spec.ts +513 -0
- package/__tests__/integration/races.spec.ts +127 -0
- package/__tests__/integration/server-operators.spec.ts +123 -0
- package/__tests__/integration/ssr.spec.ts +111 -0
- package/__tests__/integration/syncs.spec.ts +564 -0
- package/__tests__/integration/transactions.spec.ts +344 -0
- package/__tests__/integration/windows.spec.ts +192 -0
- package/__tests__/tsconfig.json +11 -0
- package/__tests__/type-budget/budget.fixture.ts +2476 -0
- package/__tests__/type-budget/tsconfig.json +4 -0
- package/__tests__/utils/fixtures.ts +225 -0
- package/__tests__/utils/random.ts +19 -0
- package/__tests__/vitest.setup.ts +11 -0
- package/dist/collection/collection.d.ts +111 -0
- package/dist/collection/collection.d.ts.map +1 -0
- package/dist/collection/collection.types.d.ts +316 -0
- package/dist/collection/collection.types.d.ts.map +1 -0
- package/dist/collection/resource.types.d.ts +77 -0
- package/dist/collection/resource.types.d.ts.map +1 -0
- package/dist/db/collection.runtime.d.ts +98 -0
- package/dist/db/collection.runtime.d.ts.map +1 -0
- package/dist/db/db.d.ts +154 -0
- package/dist/db/db.d.ts.map +1 -0
- package/dist/db/db.runtime.d.ts +114 -0
- package/dist/db/db.runtime.d.ts.map +1 -0
- package/dist/db/db.types.d.ts +162 -0
- package/dist/db/db.types.d.ts.map +1 -0
- package/dist/graphql/connection.d.ts +25 -0
- package/dist/graphql/connection.d.ts.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +4220 -0
- package/dist/index.mjs.map +1 -0
- package/dist/mutation/mutation.d.ts +136 -0
- package/dist/mutation/mutation.d.ts.map +1 -0
- package/dist/mutation/mutation.errors.d.ts +6 -0
- package/dist/mutation/mutation.errors.d.ts.map +1 -0
- package/dist/mutation/mutation.utils.d.ts +35 -0
- package/dist/mutation/mutation.utils.d.ts.map +1 -0
- package/dist/operators/operators.d.ts +33 -0
- package/dist/operators/operators.d.ts.map +1 -0
- package/dist/operators/operators.types.d.ts +68 -0
- package/dist/operators/operators.types.d.ts.map +1 -0
- package/dist/operators/operators.utils.d.ts +19 -0
- package/dist/operators/operators.utils.d.ts.map +1 -0
- package/dist/persistence/persistence.d.ts +42 -0
- package/dist/persistence/persistence.d.ts.map +1 -0
- package/dist/persistence/serializer.d.ts +23 -0
- package/dist/persistence/serializer.d.ts.map +1 -0
- package/dist/persistence/snapshot.d.ts +29 -0
- package/dist/persistence/snapshot.d.ts.map +1 -0
- package/dist/persistence/tabs.d.ts +53 -0
- package/dist/persistence/tabs.d.ts.map +1 -0
- package/dist/query/infinite-query.d.ts +59 -0
- package/dist/query/infinite-query.d.ts.map +1 -0
- package/dist/query/live-query.d.ts +85 -0
- package/dist/query/live-query.d.ts.map +1 -0
- package/dist/query/planner.d.ts +57 -0
- package/dist/query/planner.d.ts.map +1 -0
- package/dist/query/query.d.ts +155 -0
- package/dist/query/query.d.ts.map +1 -0
- package/dist/query/query.types.d.ts +71 -0
- package/dist/query/query.types.d.ts.map +1 -0
- package/dist/schema/schema.types.d.ts +33 -0
- package/dist/schema/schema.types.d.ts.map +1 -0
- package/dist/store/store.d.ts +76 -0
- package/dist/store/store.d.ts.map +1 -0
- package/dist/store/store.types.d.ts +27 -0
- package/dist/store/store.types.d.ts.map +1 -0
- package/dist/subset/base-subset.d.ts +66 -0
- package/dist/subset/base-subset.d.ts.map +1 -0
- package/dist/subset/key-subset.d.ts +23 -0
- package/dist/subset/key-subset.d.ts.map +1 -0
- package/dist/subset/list-subset.d.ts +63 -0
- package/dist/subset/list-subset.d.ts.map +1 -0
- package/dist/sync/sync.d.ts +42 -0
- package/dist/sync/sync.d.ts.map +1 -0
- package/dist/utils/request.utils.d.ts +11 -0
- package/dist/utils/request.utils.d.ts.map +1 -0
- package/package.json +71 -4
- package/project.json +4 -0
- package/scripts/docs-typescript.mjs +24 -0
- package/scripts/type-budget.mjs +37 -0
- package/src/collection/collection.ts +330 -0
- package/src/collection/collection.types.ts +383 -0
- package/src/collection/resource.types.ts +114 -0
- package/src/db/collection.runtime.ts +572 -0
- package/src/db/db.runtime.ts +537 -0
- package/src/db/db.ts +451 -0
- package/src/db/db.types.ts +157 -0
- package/src/graphql/connection.ts +23 -0
- package/src/index.ts +16 -0
- package/src/mutation/mutation.errors.ts +7 -0
- package/src/mutation/mutation.ts +776 -0
- package/src/mutation/mutation.utils.ts +58 -0
- package/src/operators/operators.ts +90 -0
- package/src/operators/operators.types.ts +67 -0
- package/src/operators/operators.utils.ts +169 -0
- package/src/persistence/persistence.ts +168 -0
- package/src/persistence/serializer.ts +123 -0
- package/src/persistence/snapshot.ts +76 -0
- package/src/persistence/tabs.ts +86 -0
- package/src/query/infinite-query.ts +206 -0
- package/src/query/live-query.ts +552 -0
- package/src/query/planner.ts +199 -0
- package/src/query/query.ts +319 -0
- package/src/query/query.types.ts +90 -0
- package/src/schema/schema.types.ts +29 -0
- package/src/store/store.ts +300 -0
- package/src/store/store.types.ts +17 -0
- package/src/subset/base-subset.ts +169 -0
- package/src/subset/key-subset.ts +44 -0
- package/src/subset/list-subset.ts +328 -0
- package/src/sync/sync.ts +99 -0
- package/src/utils/request.utils.ts +67 -0
- package/tsconfig.json +12 -0
- package/vite.config.ts +71 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { RowKey } from "../collection/collection.types";
|
|
2
|
+
import type { CollectionRuntime } from "../db/collection.runtime";
|
|
3
|
+
import type { ListPlan } from "../query/planner";
|
|
4
|
+
import type { QueryDefinition } from "../query/query.types";
|
|
5
|
+
import type { Row } from "../store/store.types";
|
|
6
|
+
|
|
7
|
+
/** A loaded subset as persistence and server rendering keep it */
|
|
8
|
+
export type SubsetSnapshot = {
|
|
9
|
+
plan: ListPlan;
|
|
10
|
+
definition: Pick<QueryDefinition, "orderBy" | "limit">;
|
|
11
|
+
owned: RowKey[];
|
|
12
|
+
cursor: unknown;
|
|
13
|
+
hasMore: boolean;
|
|
14
|
+
/** How many pages a paginated subset had loaded, so a refetch reloads all of them */
|
|
15
|
+
pages: number;
|
|
16
|
+
/** When the subset loaded, or 0 when that is unknown */
|
|
17
|
+
loadedAt: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** A collection's server rows and loaded subsets. Optimistic layers are never part of it. */
|
|
21
|
+
export type CollectionSnapshot = {
|
|
22
|
+
rows: [RowKey, Row][];
|
|
23
|
+
subsets: SubsetSnapshot[];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const snapshotCollection = (collection: CollectionRuntime): CollectionSnapshot => {
|
|
27
|
+
const { store } = collection;
|
|
28
|
+
return {
|
|
29
|
+
rows: store.getServerKeys().map((key) => [key, store.getServerRow(key)!]),
|
|
30
|
+
subsets: [...collection.listSubsets.values()]
|
|
31
|
+
.filter((subset) => subset.hasLoaded)
|
|
32
|
+
.map((subset) => ({
|
|
33
|
+
plan: subset.plan,
|
|
34
|
+
definition: subset.definition,
|
|
35
|
+
// In the order the last load returned, so a restored list without `orderBy` keeps its order
|
|
36
|
+
owned: [...subset.owned].toSorted(
|
|
37
|
+
(left, right) => subset.orderIndex.get(left)! - subset.orderIndex.get(right)!,
|
|
38
|
+
),
|
|
39
|
+
cursor: subset.cursor,
|
|
40
|
+
hasMore: subset.hasMore,
|
|
41
|
+
pages: subset.pages,
|
|
42
|
+
loadedAt: subset.loadedAt,
|
|
43
|
+
})),
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Writes a snapshot's rows and subsets into a collection. The subsets answer queries at once but count as stale,
|
|
49
|
+
* because events sent since the snapshot was taken were missed, so they refetch when a query mounts.
|
|
50
|
+
*/
|
|
51
|
+
export const restoreSnapshot = (collection: CollectionRuntime, snapshot: CollectionSnapshot) => {
|
|
52
|
+
collection.db.batch(() => {
|
|
53
|
+
snapshot.rows.forEach(([rowKey, row]) => {
|
|
54
|
+
collection.store.write(rowKey, row, { complete: true, stamp: { kind: "sent", seq: 0, wall: 0 } });
|
|
55
|
+
});
|
|
56
|
+
snapshot.subsets.forEach((stored) => {
|
|
57
|
+
const subset = collection.getListSubset(stored.plan, {
|
|
58
|
+
collection: collection.name,
|
|
59
|
+
orderBy: stored.definition.orderBy,
|
|
60
|
+
limit: stored.definition.limit,
|
|
61
|
+
local: false,
|
|
62
|
+
});
|
|
63
|
+
stored.owned.forEach((ownedKey, index) => {
|
|
64
|
+
subset.owned.add(ownedKey);
|
|
65
|
+
subset.orderIndex.set(ownedKey, index);
|
|
66
|
+
});
|
|
67
|
+
subset.cursor = stored.cursor;
|
|
68
|
+
subset.hasMore = stored.hasMore;
|
|
69
|
+
subset.pages = stored.pages;
|
|
70
|
+
subset.hasLoaded = true;
|
|
71
|
+
subset.loadedAt = stored.loadedAt;
|
|
72
|
+
subset.invalidated = true;
|
|
73
|
+
subset.notify();
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { DbChannel, DbLockManager, TransactionRecord } from "../db/db.types";
|
|
2
|
+
|
|
3
|
+
export type TabsMessage =
|
|
4
|
+
/** A follower's writes, which only the leader persists and replays */
|
|
5
|
+
| { type: "enqueue"; records: TransactionRecord[] }
|
|
6
|
+
/** A tab became the leader: followers send it the writes they still wait for */
|
|
7
|
+
| { type: "leader" }
|
|
8
|
+
/** A tab cleared the db, for example on sign-out */
|
|
9
|
+
| { type: "clear" }
|
|
10
|
+
/** A replayed write settled. `key` is the key the server gave an insert that left it to the server. */
|
|
11
|
+
| { type: "outcome"; id: string; success: boolean; error?: unknown; key?: string | number }
|
|
12
|
+
| { type: "rows"; collection: string; rows: [string | number, Record<string, unknown>][] };
|
|
13
|
+
|
|
14
|
+
const getDefaultLocks = (): DbLockManager | undefined => {
|
|
15
|
+
const navigatorLike = (globalThis as { navigator?: { locks?: DbLockManager } }).navigator;
|
|
16
|
+
return navigatorLike?.locks;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const getDefaultChannel = (name: string): DbChannel | undefined => {
|
|
20
|
+
if (typeof BroadcastChannel === "undefined") {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
return new BroadcastChannel(name) as unknown as DbChannel;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Elects the one tab that replays the outbox and writes persisted rows, with Web Locks. Without Web Locks every tab
|
|
28
|
+
* treats itself as the leader. Tabs exchange outbox changes, outcomes and rows over a `BroadcastChannel`.
|
|
29
|
+
*/
|
|
30
|
+
export class Tabs {
|
|
31
|
+
isLeader = false;
|
|
32
|
+
private closed = false;
|
|
33
|
+
private readonly locks: DbLockManager | undefined;
|
|
34
|
+
private readonly channel: DbChannel | undefined;
|
|
35
|
+
private releaseLock: (() => void) | undefined;
|
|
36
|
+
private readonly listener = (event: { data: unknown }) => this.handlers.onMessage(event.data as TabsMessage);
|
|
37
|
+
|
|
38
|
+
constructor(
|
|
39
|
+
private readonly name: string,
|
|
40
|
+
options: { locks?: DbLockManager; channel?: DbChannel } | undefined,
|
|
41
|
+
private readonly handlers: { onLeader: () => void; onMessage: (message: TabsMessage) => void },
|
|
42
|
+
) {
|
|
43
|
+
this.locks = options ? options.locks : getDefaultLocks();
|
|
44
|
+
this.channel = options ? options.channel : getDefaultChannel(name);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
start = () => {
|
|
48
|
+
this.channel?.addEventListener("message", this.listener);
|
|
49
|
+
if (!this.locks) {
|
|
50
|
+
this.becomeLeader();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
this.locks.request(`${this.name}:leader`, () => {
|
|
54
|
+
// A tab destroyed while it waited releases the lock at once instead of leading
|
|
55
|
+
if (this.closed) {
|
|
56
|
+
return Promise.resolve();
|
|
57
|
+
}
|
|
58
|
+
this.becomeLeader();
|
|
59
|
+
return new Promise<void>((resolve) => {
|
|
60
|
+
this.releaseLock = resolve;
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
post = (message: TabsMessage) => {
|
|
66
|
+
// A write can finish after the db was destroyed, and a closed channel throws on post
|
|
67
|
+
if (this.closed) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.channel?.postMessage(message);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
close = () => {
|
|
74
|
+
this.closed = true;
|
|
75
|
+
this.releaseLock?.();
|
|
76
|
+
this.releaseLock = undefined;
|
|
77
|
+
this.isLeader = false;
|
|
78
|
+
this.channel?.removeEventListener("message", this.listener);
|
|
79
|
+
this.channel?.close();
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
private becomeLeader = () => {
|
|
83
|
+
this.isLeader = true;
|
|
84
|
+
this.handlers.onLeader();
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import type { DbRuntime } from "../db/db.runtime";
|
|
2
|
+
import { stableStringify } from "../operators/operators.utils";
|
|
3
|
+
import type { LiveQuery } from "./live-query";
|
|
4
|
+
import type { QueryDefinition, QueryState } from "./query.types";
|
|
5
|
+
|
|
6
|
+
/** The state of an infinite query: the rows of its window, split into pages */
|
|
7
|
+
export type InfiniteQueryState<Item> = QueryState<Item[]> & {
|
|
8
|
+
/** `data` split into pages of `pageSize` */
|
|
9
|
+
pages: Item[][];
|
|
10
|
+
/** Whether more rows are loading to fill the window after `fetchNextPage` */
|
|
11
|
+
isFetchingNextPage: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type InfiniteQueryOptions = {
|
|
15
|
+
/** How many rows each page holds. Pages are independent of the backend's page size. */
|
|
16
|
+
pageSize: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const toPages = <Item>(items: Item[], pageSize: number): Item[][] => {
|
|
20
|
+
return Array.from({ length: Math.ceil(items.length / pageSize) }, (_, index) =>
|
|
21
|
+
items.slice(index * pageSize, (index + 1) * pageSize),
|
|
22
|
+
);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A window over an ordered live query: the first `pageSize` times the number of pages rows of its result. Widening it
|
|
27
|
+
* shows rows the db holds at once and loads more from the query's loader only when the window runs past them.
|
|
28
|
+
*
|
|
29
|
+
* The window belongs to this instance, so two components showing the same query share its live query and its subset,
|
|
30
|
+
* and each keeps its own number of pages.
|
|
31
|
+
*/
|
|
32
|
+
export class InfiniteQuery<Item> {
|
|
33
|
+
/** Identifies the infinite query by its query's structure and page size */
|
|
34
|
+
readonly key: string;
|
|
35
|
+
private pageCount = 1;
|
|
36
|
+
private live: LiveQuery;
|
|
37
|
+
/** The live query of the previous window, shown while a wider `limit` loads */
|
|
38
|
+
private previous: { live: LiveQuery; unsubscribe: () => void; pageCount: number } | undefined;
|
|
39
|
+
private unsubscribeLive: (() => void) | undefined;
|
|
40
|
+
private listeners = new Set<() => void>();
|
|
41
|
+
private filling: Promise<void> | undefined;
|
|
42
|
+
private widening: Promise<void> | undefined;
|
|
43
|
+
private readonly paging: "cursor" | "limit" | "none";
|
|
44
|
+
private snapshot: { source: QueryState<unknown>; pageCount: number; fetching: boolean; value: unknown } | undefined;
|
|
45
|
+
|
|
46
|
+
constructor(
|
|
47
|
+
private readonly runtime: DbRuntime,
|
|
48
|
+
private readonly definition: QueryDefinition,
|
|
49
|
+
private readonly options: InfiniteQueryOptions,
|
|
50
|
+
) {
|
|
51
|
+
if (!Number.isInteger(options.pageSize) || options.pageSize < 1) {
|
|
52
|
+
throw new Error(`An infinite query needs a whole pageSize of at least 1, received ${options.pageSize}`);
|
|
53
|
+
}
|
|
54
|
+
this.key = stableStringify({ definition, pageSize: options.pageSize });
|
|
55
|
+
this.paging = runtime.getLiveQuery(definition).getPaging();
|
|
56
|
+
this.live = runtime.getLiveQuery(this.getDefinition());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
subscribe = (listener: () => void) => {
|
|
60
|
+
this.listeners.add(listener);
|
|
61
|
+
if (this.listeners.size === 1) {
|
|
62
|
+
// The db forgets live queries nobody uses, so the window attaches to the one the db serves now
|
|
63
|
+
this.live = this.runtime.getLiveQuery(this.getDefinition());
|
|
64
|
+
this.unsubscribeLive = this.live.subscribe(this.onLiveChange);
|
|
65
|
+
this.fill();
|
|
66
|
+
}
|
|
67
|
+
return () => {
|
|
68
|
+
this.listeners.delete(listener);
|
|
69
|
+
if (this.listeners.size === 0) {
|
|
70
|
+
this.unsubscribeLive!();
|
|
71
|
+
this.unsubscribeLive = undefined;
|
|
72
|
+
this.dropPrevious();
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
getSnapshot = (): InfiniteQueryState<Item> => {
|
|
78
|
+
const { source, pageCount } = this.getSource();
|
|
79
|
+
const fetching = this.isFetchingNextPage();
|
|
80
|
+
const cached = this.snapshot;
|
|
81
|
+
if (cached && cached.source === source && cached.pageCount === pageCount && cached.fetching === fetching) {
|
|
82
|
+
return cached.value as InfiniteQueryState<Item>;
|
|
83
|
+
}
|
|
84
|
+
const size = pageCount * this.options.pageSize;
|
|
85
|
+
const rows = source.data as Item[] | undefined;
|
|
86
|
+
const data = rows?.slice(0, size);
|
|
87
|
+
const value: InfiniteQueryState<Item> = {
|
|
88
|
+
...(source as QueryState<Item[]>),
|
|
89
|
+
data,
|
|
90
|
+
pages: data ? toPages(data, this.options.pageSize) : [],
|
|
91
|
+
hasNextPage: (rows?.length ?? 0) > size || (this.paging === "cursor" && source.hasNextPage),
|
|
92
|
+
isFetchingNextPage: fetching,
|
|
93
|
+
};
|
|
94
|
+
this.snapshot = { source, pageCount, fetching, value };
|
|
95
|
+
return value;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Widens the window by one page. Rows the db holds show at once, and the loader loads more when the window runs past
|
|
100
|
+
* them. Resolves once the window is filled or the backend has no more rows.
|
|
101
|
+
*/
|
|
102
|
+
fetchNextPage = (): Promise<void> => {
|
|
103
|
+
// A page that is still loading answers a second call, so a double scroll event loads one page
|
|
104
|
+
if (this.filling || this.previous) {
|
|
105
|
+
return this.filling ?? this.widening!;
|
|
106
|
+
}
|
|
107
|
+
// Pages load for subscribers, as queries do
|
|
108
|
+
if (!this.unsubscribeLive || !this.getSnapshot().hasNextPage) {
|
|
109
|
+
return Promise.resolve();
|
|
110
|
+
}
|
|
111
|
+
this.pageCount += 1;
|
|
112
|
+
if (this.paging === "limit") {
|
|
113
|
+
return this.widenLimit();
|
|
114
|
+
}
|
|
115
|
+
this.notify();
|
|
116
|
+
return this.fill();
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/** Loads the query again from its first page */
|
|
120
|
+
refetch = (): Promise<void> => this.live.refetch();
|
|
121
|
+
|
|
122
|
+
private getDefinition = (): QueryDefinition => {
|
|
123
|
+
if (this.paging !== "limit") {
|
|
124
|
+
return this.definition;
|
|
125
|
+
}
|
|
126
|
+
return { ...this.definition, limit: this.pageCount * this.options.pageSize + 1 };
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/** The state and window the snapshot shows: the previous window while a wider one loads its first response */
|
|
130
|
+
private getSource = () => {
|
|
131
|
+
const state = this.live.getState();
|
|
132
|
+
if (this.previous && state.status === "loading") {
|
|
133
|
+
return { source: this.previous.live.getState(), pageCount: this.previous.pageCount };
|
|
134
|
+
}
|
|
135
|
+
return { source: state, pageCount: this.pageCount };
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
private isFetchingNextPage = () => Boolean(this.filling) || Boolean(this.previous);
|
|
139
|
+
|
|
140
|
+
private onLiveChange = () => {
|
|
141
|
+
if (this.previous && this.live.getState().status !== "loading") {
|
|
142
|
+
this.dropPrevious();
|
|
143
|
+
}
|
|
144
|
+
this.fill();
|
|
145
|
+
this.notify();
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/** Loads backend pages until the db holds one row past the window, or the backend has none */
|
|
149
|
+
private fill = (): Promise<void> => {
|
|
150
|
+
if (this.filling) {
|
|
151
|
+
return this.filling;
|
|
152
|
+
}
|
|
153
|
+
const state = this.live.getState();
|
|
154
|
+
const size = this.pageCount * this.options.pageSize;
|
|
155
|
+
const held = (state.data as unknown[] | undefined)?.length ?? 0;
|
|
156
|
+
// Only subscribers load pages, so an unsubscribed query never loops on a subset it does not attach to
|
|
157
|
+
const blocked = !this.unsubscribeLive || this.paging !== "cursor" || state.status === "loading";
|
|
158
|
+
if (blocked || !state.hasNextPage || held > size) {
|
|
159
|
+
return Promise.resolve();
|
|
160
|
+
}
|
|
161
|
+
this.filling = this.live.fetchNextPage().then(() => {
|
|
162
|
+
this.filling = undefined;
|
|
163
|
+
const next = this.live.getState();
|
|
164
|
+
// A failed page stops filling, so a broken backend is not asked again in a loop, and so does a page that
|
|
165
|
+
// brought no row, such as an empty page a server filtered
|
|
166
|
+
const failed = next.error !== null && next.error !== undefined;
|
|
167
|
+
this.notify();
|
|
168
|
+
// A call that brought no row stops filling, so filling never spins; the next `fetchNextPage` goes on
|
|
169
|
+
return failed || (next.data as unknown[]).length <= held ? undefined : this.fill();
|
|
170
|
+
});
|
|
171
|
+
this.notify();
|
|
172
|
+
return this.filling;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/** A loader with `limit` and no cursor is sent the window size plus one, as a new live query */
|
|
176
|
+
private widenLimit = (): Promise<void> => {
|
|
177
|
+
const next = this.runtime.getLiveQuery(this.getDefinition());
|
|
178
|
+
this.previous = { live: this.live, unsubscribe: this.unsubscribeLive!, pageCount: this.pageCount - 1 };
|
|
179
|
+
this.live = next;
|
|
180
|
+
this.unsubscribeLive = next.subscribe(this.onLiveChange);
|
|
181
|
+
this.notify();
|
|
182
|
+
this.widening = new Promise((resolve) => {
|
|
183
|
+
let settled = false;
|
|
184
|
+
const check = () => {
|
|
185
|
+
if (!settled && next.getState().status !== "loading") {
|
|
186
|
+
settled = true;
|
|
187
|
+
resolve();
|
|
188
|
+
// The first state can arrive while subscribing, before `stop` exists
|
|
189
|
+
queueMicrotask(() => stop());
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
const stop = next.subscribe(check);
|
|
193
|
+
check();
|
|
194
|
+
});
|
|
195
|
+
return this.widening;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
private dropPrevious = () => {
|
|
199
|
+
this.previous?.unsubscribe();
|
|
200
|
+
this.previous = undefined;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
private notify = () => {
|
|
204
|
+
this.listeners.forEach((listener) => listener());
|
|
205
|
+
};
|
|
206
|
+
}
|