@codual/react-native-ui 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +18 -2
- package/dist/announce.d.ts +2 -0
- package/dist/announce.js +8 -0
- package/dist/charts/data.d.ts +32 -0
- package/dist/charts/data.js +13 -0
- package/dist/charts/labels.d.ts +54 -0
- package/dist/charts/labels.js +91 -0
- package/dist/charts/machine.d.ts +62 -0
- package/dist/charts/machine.js +144 -0
- package/dist/charts/points.d.ts +23 -0
- package/dist/charts/points.js +46 -0
- package/dist/charts/summary.d.ts +29 -0
- package/dist/charts/summary.js +45 -0
- package/dist/charts/use-chart-interaction.d.ts +62 -0
- package/dist/charts/use-chart-interaction.js +79 -0
- package/dist/forms/async-checks.d.ts +34 -0
- package/dist/forms/async-checks.js +77 -0
- package/dist/forms/field-errors.d.ts +8 -0
- package/dist/forms/field-errors.js +16 -0
- package/dist/forms/form-types.d.ts +43 -0
- package/dist/forms/form-types.js +2 -0
- package/dist/forms/machine.d.ts +63 -0
- package/dist/forms/machine.js +251 -0
- package/dist/forms/numbers.d.ts +5 -0
- package/dist/forms/numbers.js +83 -0
- package/dist/forms/rules.d.ts +47 -0
- package/dist/forms/rules.js +72 -0
- package/dist/forms/server-errors.d.ts +17 -0
- package/dist/forms/server-errors.js +22 -0
- package/dist/forms/use-form.d.ts +49 -0
- package/dist/forms/use-form.js +61 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +94 -0
- package/dist/lists/PagedList.d.ts +47 -0
- package/dist/lists/PagedList.js +81 -0
- package/dist/lists/controller.d.ts +23 -0
- package/dist/lists/controller.js +20 -0
- package/dist/lists/inverted-list.d.ts +23 -0
- package/dist/lists/inverted-list.js +60 -0
- package/dist/lists/machine.d.ts +81 -0
- package/dist/lists/machine.js +252 -0
- package/dist/lists/newest-end.d.ts +30 -0
- package/dist/lists/newest-end.js +58 -0
- package/dist/lists/paged-content.d.ts +46 -0
- package/dist/lists/paged-content.js +65 -0
- package/dist/lists/paged-list-types.d.ts +56 -0
- package/dist/lists/paged-list-types.js +2 -0
- package/dist/lists/system-footer.d.ts +28 -0
- package/dist/lists/system-footer.js +50 -0
- package/dist/lists/use-paged-list.d.ts +25 -0
- package/dist/lists/use-paged-list.js +34 -0
- package/dist/permissions/access.d.ts +30 -0
- package/dist/permissions/access.js +2 -0
- package/dist/permissions/android-permission.d.ts +52 -0
- package/dist/permissions/android-permission.js +143 -0
- package/dist/permissions/permission.d.ts +69 -0
- package/dist/permissions/permission.js +201 -0
- package/dist/permissions/setup.d.ts +25 -0
- package/dist/permissions/setup.js +41 -0
- package/dist/permissions/status.d.ts +29 -0
- package/dist/permissions/status.js +18 -0
- package/dist/permissions/use-permission.d.ts +23 -0
- package/dist/permissions/use-permission.js +29 -0
- package/dist/purchases/Paywall.d.ts +27 -0
- package/dist/purchases/Paywall.js +28 -0
- package/dist/purchases/paywall-content.d.ts +20 -0
- package/dist/purchases/paywall-content.js +34 -0
- package/dist/purchases/purchases.d.ts +92 -0
- package/dist/purchases/purchases.js +236 -0
- package/dist/purchases/status.d.ts +62 -0
- package/dist/purchases/status.js +25 -0
- package/dist/purchases/store.d.ts +71 -0
- package/dist/purchases/store.js +2 -0
- package/dist/purchases/system-views.d.ts +49 -0
- package/dist/purchases/system-views.js +91 -0
- package/dist/purchases/use-purchases.d.ts +3 -0
- package/dist/purchases/use-purchases.js +8 -0
- package/dist/react-native-iap/iap-purchases.d.ts +24 -0
- package/dist/react-native-iap/iap-purchases.js +90 -0
- package/dist/react-native-iap/iap-store.d.ts +56 -0
- package/dist/react-native-iap/iap-store.js +249 -0
- package/dist/react-native-iap/iap-types.d.ts +12 -0
- package/dist/react-native-iap/iap-types.js +11 -0
- package/dist/react-native-iap/index.d.ts +31 -0
- package/dist/react-native-iap/index.js +24 -0
- package/dist/react-native-permissions/index.d.ts +24 -0
- package/dist/react-native-permissions/index.js +88 -0
- package/dist/screen-states/ScreenStates.d.ts +38 -0
- package/dist/screen-states/ScreenStates.js +69 -0
- package/dist/screen-states/errors.d.ts +9 -0
- package/dist/screen-states/errors.js +24 -0
- package/dist/screen-states/machine.d.ts +97 -0
- package/dist/screen-states/machine.js +172 -0
- package/dist/screen-states/system-views.d.ts +40 -0
- package/dist/screen-states/system-views.js +66 -0
- package/dist/screen-states/use-screen-states.d.ts +23 -0
- package/dist/screen-states/use-screen-states.js +41 -0
- package/dist/search/SearchResults.d.ts +22 -0
- package/dist/search/SearchResults.js +25 -0
- package/dist/search/announcements.d.ts +17 -0
- package/dist/search/announcements.js +31 -0
- package/dist/search/labels.d.ts +15 -0
- package/dist/search/labels.js +14 -0
- package/dist/search/machine.d.ts +65 -0
- package/dist/search/machine.js +241 -0
- package/dist/search/recent-search-sync.d.ts +32 -0
- package/dist/search/recent-search-sync.js +87 -0
- package/dist/search/recent-searches.d.ts +22 -0
- package/dist/search/recent-searches.js +40 -0
- package/dist/search/system-views.d.ts +11 -0
- package/dist/search/system-views.js +16 -0
- package/dist/search/types.d.ts +51 -0
- package/dist/search/types.js +2 -0
- package/dist/search/use-search.d.ts +73 -0
- package/dist/search/use-search.js +123 -0
- package/dist/selection/config.d.ts +34 -0
- package/dist/selection/config.js +13 -0
- package/dist/selection/labels.d.ts +19 -0
- package/dist/selection/labels.js +23 -0
- package/dist/selection/machine.d.ts +80 -0
- package/dist/selection/machine.js +274 -0
- package/dist/selection/snapshot.d.ts +54 -0
- package/dist/selection/snapshot.js +45 -0
- package/dist/selection/state.d.ts +25 -0
- package/dist/selection/state.js +24 -0
- package/dist/selection/system-views.d.ts +11 -0
- package/dist/selection/system-views.js +22 -0
- package/dist/selection/use-selection.d.ts +73 -0
- package/dist/selection/use-selection.js +100 -0
- package/dist/sheets/machine.d.ts +101 -0
- package/dist/sheets/machine.js +213 -0
- package/dist/sheets/use-sheet.d.ts +78 -0
- package/dist/sheets/use-sheet.js +103 -0
- package/dist/victory-native/index.d.ts +37 -0
- package/dist/victory-native/index.js +41 -0
- package/package.json +94 -4
- package/react-native-iap/package.json +4 -0
- package/react-native-permissions/package.json +4 -0
- package/victory-native/package.json +4 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PagedListMachine = void 0;
|
|
4
|
+
const machine_1 = require("../screen-states/machine");
|
|
5
|
+
const newest_end_1 = require("./newest-end");
|
|
6
|
+
const paged_content_1 = require("./paged-content");
|
|
7
|
+
/** A page load that adds no new items is followed at once by the next, at most this many times in a row. */
|
|
8
|
+
const MAX_PAGES_WITH_NOTHING_NEW = 3;
|
|
9
|
+
/**
|
|
10
|
+
* The state machine behind `usePagedList`. The first page is a screen state; later pages load one at a time, and a
|
|
11
|
+
* refresh or `disappear()` aborts the one loading and drops its result. `source` is read when a load starts and
|
|
12
|
+
* ends, so it can change between renders.
|
|
13
|
+
*/
|
|
14
|
+
class PagedListMachine {
|
|
15
|
+
source;
|
|
16
|
+
inverted;
|
|
17
|
+
screen;
|
|
18
|
+
pageLoad;
|
|
19
|
+
generation = 0;
|
|
20
|
+
listeners = new Set();
|
|
21
|
+
newest = new newest_end_1.NewestEnd(() => {
|
|
22
|
+
for (const listener of this.listeners)
|
|
23
|
+
listener();
|
|
24
|
+
});
|
|
25
|
+
/** The newer items that arrived while the first page loads, for the page it brings, and whether each was shown. */
|
|
26
|
+
arrivedDuringFirstPage;
|
|
27
|
+
derived;
|
|
28
|
+
/**
|
|
29
|
+
* @param initialPage A first page the app already has, shown at once with no request. One with no items counts as
|
|
30
|
+
* none, and the first page loads.
|
|
31
|
+
* @param inverted A chat: the first page is the newest, at the bottom, with its items newest first.
|
|
32
|
+
*/
|
|
33
|
+
constructor(source, initialPage, inverted = false) {
|
|
34
|
+
this.source = source;
|
|
35
|
+
this.inverted = inverted;
|
|
36
|
+
this.screen = new machine_1.ScreenStatesMachine(() => ({
|
|
37
|
+
load: (signal) => this.loadFirstPage(signal),
|
|
38
|
+
isEmpty: (content) => content.items.length === 0,
|
|
39
|
+
isOffline: this.source().isOffline,
|
|
40
|
+
}));
|
|
41
|
+
if (initialPage !== undefined && initialPage.items.length > 0) {
|
|
42
|
+
this.screen.showLoaded((0, paged_content_1.firstPageContent)(initialPage, this.source().keyExtractor));
|
|
43
|
+
}
|
|
44
|
+
const from = this.screen.getSnapshot();
|
|
45
|
+
const newest = this.newest.snapshot;
|
|
46
|
+
this.derived = { from, newest, to: this.derive(from, newest) };
|
|
47
|
+
}
|
|
48
|
+
subscribe = (listener) => {
|
|
49
|
+
const unsubscribe = this.screen.subscribe(listener);
|
|
50
|
+
this.listeners.add(listener);
|
|
51
|
+
return () => {
|
|
52
|
+
unsubscribe();
|
|
53
|
+
this.listeners.delete(listener);
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
getSnapshot = () => {
|
|
57
|
+
const from = this.screen.getSnapshot();
|
|
58
|
+
const newest = this.newest.snapshot;
|
|
59
|
+
if (from !== this.derived.from || newest !== this.derived.newest) {
|
|
60
|
+
this.derived = { from, newest, to: this.derive(from, newest) };
|
|
61
|
+
}
|
|
62
|
+
return this.derived.to;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Shows a first page at once with no request, even one with no items, which shows the empty view: for a behavior
|
|
66
|
+
* built on this one, such as search filtering items in memory. Does nothing once a load has started.
|
|
67
|
+
*/
|
|
68
|
+
showLoaded(page) {
|
|
69
|
+
this.screen.showLoaded((0, paged_content_1.firstPageContent)(page, this.source().keyExtractor));
|
|
70
|
+
}
|
|
71
|
+
/** Replaces the whole list shown with this first page, keeping the list itself (and its scroll position). */
|
|
72
|
+
replaceFirstPage(page) {
|
|
73
|
+
this.screen.replaceLoaded((0, paged_content_1.firstPageContent)(page, this.source().keyExtractor));
|
|
74
|
+
}
|
|
75
|
+
/** Loads the first page. Safe to call on every mount: once it has started or finished, it does nothing. */
|
|
76
|
+
appear = () => this.screen.appear();
|
|
77
|
+
/** Aborts the running load, of the first page or a later one, and drops its result. */
|
|
78
|
+
disappear = () => {
|
|
79
|
+
this.cancelPageLoad();
|
|
80
|
+
this.screen.disappear();
|
|
81
|
+
};
|
|
82
|
+
/** Loads the first page again from the failed or offline view. */
|
|
83
|
+
retry = () => this.screen.retry();
|
|
84
|
+
/**
|
|
85
|
+
* Loads the first page again while the list stays on screen, and replaces the whole list with it if it succeeds.
|
|
86
|
+
* A page that is loading is aborted first. Resolves when the load finishes.
|
|
87
|
+
*/
|
|
88
|
+
refresh = () => {
|
|
89
|
+
this.cancelPageLoad();
|
|
90
|
+
return this.screen.refresh();
|
|
91
|
+
};
|
|
92
|
+
dismissRefreshError = () => this.screen.dismissRefreshError();
|
|
93
|
+
/** Loads the next page, if there is one and nothing is loading, failed or refreshing: the Load More action. */
|
|
94
|
+
loadMore = () => {
|
|
95
|
+
const content = this.shownContent();
|
|
96
|
+
if (content?.next === undefined)
|
|
97
|
+
return;
|
|
98
|
+
if (content.paging.status === "ready" || content.paging.status === "stalled")
|
|
99
|
+
this.startPageLoad(content.next, 0);
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Loads the next page as the list nears its end (`FlatList`'s `onEndReached`), as `loadMore` does, except after
|
|
103
|
+
* pages that added nothing: then only `loadMore` goes on, so a server that keeps returning tokens can't loop.
|
|
104
|
+
*/
|
|
105
|
+
endReached = () => {
|
|
106
|
+
if (this.shownContent()?.paging.status === "ready")
|
|
107
|
+
this.loadMore();
|
|
108
|
+
};
|
|
109
|
+
/** Loads the page that failed again. */
|
|
110
|
+
retryPage = () => {
|
|
111
|
+
const content = this.shownContent();
|
|
112
|
+
if (content?.next !== undefined && content.paging.status === "failed")
|
|
113
|
+
this.startPageLoad(content.next, 0);
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Adds items that arrived after the newest one (a message just received), oldest first, below the newest row.
|
|
117
|
+
* Items already in the list are dropped. When the newest row is on screen they scroll into view; otherwise the
|
|
118
|
+
* rows on screen stay still and `newItemCount` goes up. Only for an inverted list.
|
|
119
|
+
*/
|
|
120
|
+
appendNewer = (items) => {
|
|
121
|
+
if (!this.inverted)
|
|
122
|
+
throw new Error("appendNewer is for an inverted paged list: pass inverted: true.");
|
|
123
|
+
const { state } = this.screen.getSnapshot();
|
|
124
|
+
const { keyExtractor } = this.source();
|
|
125
|
+
const content = state.status === "content"
|
|
126
|
+
? state.value
|
|
127
|
+
: state.status === "empty"
|
|
128
|
+
? (0, paged_content_1.firstPageContent)({ items: [] }, keyExtractor)
|
|
129
|
+
: undefined;
|
|
130
|
+
this.arrivedDuringFirstPage?.push(...items.map((item) => ({ item, shown: content !== undefined })));
|
|
131
|
+
if (content === undefined)
|
|
132
|
+
return;
|
|
133
|
+
const { content: next, added } = (0, paged_content_1.addingNewer)(content, items, keyExtractor);
|
|
134
|
+
if (added === 0)
|
|
135
|
+
return;
|
|
136
|
+
this.screen.replaceLoaded(next);
|
|
137
|
+
this.source().announce({ kind: "newItems", count: added });
|
|
138
|
+
this.newest.arrived(added);
|
|
139
|
+
};
|
|
140
|
+
/** Scrolls to the newest row and sets `newItemCount` back to 0: the action of the app's "new items" indicator. */
|
|
141
|
+
scrollToBottom = () => this.newest.scrollToBottom();
|
|
142
|
+
/** Call as the newest row comes on screen and goes off it; `PagedList` does. */
|
|
143
|
+
bottomChanged = (isAtBottom) => this.newest.bottomChanged(isAtBottom);
|
|
144
|
+
/** Call when the user starts dragging the list (`onScrollBeginDrag`); `PagedList` does. */
|
|
145
|
+
userScrolled = () => this.newest.userScrolled();
|
|
146
|
+
derive(from, newest) {
|
|
147
|
+
const { state, isRefreshing, refreshError } = from;
|
|
148
|
+
if (state.status !== "content")
|
|
149
|
+
return { state, isRefreshing, refreshError, ...newest, footer: undefined };
|
|
150
|
+
const items = { status: "content", value: state.value.items };
|
|
151
|
+
return { state: items, isRefreshing, refreshError, ...newest, footer: this.footer(state.value) };
|
|
152
|
+
}
|
|
153
|
+
footer(content) {
|
|
154
|
+
switch (content.paging.status) {
|
|
155
|
+
case "loading":
|
|
156
|
+
return { status: "loading" };
|
|
157
|
+
case "failed":
|
|
158
|
+
return { status: "failed", error: content.paging.error, retry: this.retryPage };
|
|
159
|
+
case "ready":
|
|
160
|
+
case "stalled":
|
|
161
|
+
return content.next === undefined ? { status: "end" } : { status: "more", loadMore: this.loadMore };
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/** The list on screen, when a page may load: never during a refresh. */
|
|
165
|
+
shownContent() {
|
|
166
|
+
const { state, isRefreshing } = this.screen.getSnapshot();
|
|
167
|
+
return state.status === "content" && !isRefreshing ? state.value : undefined;
|
|
168
|
+
}
|
|
169
|
+
currentContent() {
|
|
170
|
+
const { state } = this.screen.getSnapshot();
|
|
171
|
+
return state.status === "content" ? state.value : undefined;
|
|
172
|
+
}
|
|
173
|
+
async loadFirstPage(signal) {
|
|
174
|
+
const arrived = [];
|
|
175
|
+
this.arrivedDuringFirstPage = arrived;
|
|
176
|
+
try {
|
|
177
|
+
const { keyExtractor, announce } = this.source();
|
|
178
|
+
const page = await (0, paged_content_1.loadFirstPages)(this.source, signal, MAX_PAGES_WITH_NOTHING_NEW);
|
|
179
|
+
const content = (0, paged_content_1.addingNewer)(page, arrived.map(({ item }) => item), keyExtractor).content;
|
|
180
|
+
// A load the list canceled is dropped: it mustn't move the list or announce anything.
|
|
181
|
+
if (this.inverted && !signal.aborted) {
|
|
182
|
+
this.newest.scrollToBottom();
|
|
183
|
+
const unannounced = arrived.filter(({ item, shown }) => !shown && !page.keys.has(keyExtractor(item)));
|
|
184
|
+
const count = new Set(unannounced.map(({ item }) => keyExtractor(item))).size;
|
|
185
|
+
if (count > 0)
|
|
186
|
+
announce({ kind: "newItems", count });
|
|
187
|
+
}
|
|
188
|
+
return content;
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
if (this.arrivedDuringFirstPage === arrived)
|
|
192
|
+
this.arrivedDuringFirstPage = undefined;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
startPageLoad(token, pagesWithNothingNew) {
|
|
196
|
+
this.setPaging({ status: "loading" });
|
|
197
|
+
this.generation += 1;
|
|
198
|
+
const id = this.generation;
|
|
199
|
+
const controller = new AbortController();
|
|
200
|
+
this.pageLoad = controller;
|
|
201
|
+
const { loadPage } = this.source();
|
|
202
|
+
// The executor runs `loadPage` now and turns a synchronous throw into a failed page.
|
|
203
|
+
new Promise((resolve) => resolve(loadPage(token, controller.signal))).then((page) => this.pageLoaded(page, id, pagesWithNothingNew), (error) => this.pageFailed(error, id));
|
|
204
|
+
}
|
|
205
|
+
pageLoaded(page, id, pagesWithNothingNew) {
|
|
206
|
+
const content = this.finishPageLoad(id);
|
|
207
|
+
if (content === undefined)
|
|
208
|
+
return;
|
|
209
|
+
const { content: next, added } = (0, paged_content_1.addingPage)(content, page, this.source().keyExtractor);
|
|
210
|
+
const isStalled = added === 0 && next.next !== undefined;
|
|
211
|
+
if (isStalled && next.next !== undefined && pagesWithNothingNew + 1 < MAX_PAGES_WITH_NOTHING_NEW) {
|
|
212
|
+
this.screen.replaceContent(next);
|
|
213
|
+
this.startPageLoad(next.next, pagesWithNothingNew + 1);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
this.screen.replaceContent({ ...next, paging: { status: isStalled ? "stalled" : "ready" } });
|
|
217
|
+
const isEnd = next.next === undefined;
|
|
218
|
+
if (added > 0)
|
|
219
|
+
this.source().announce({ kind: "loaded", count: added, isEnd });
|
|
220
|
+
else if (isEnd)
|
|
221
|
+
this.source().announce({ kind: "end" });
|
|
222
|
+
}
|
|
223
|
+
pageFailed(error, id) {
|
|
224
|
+
const content = this.finishPageLoad(id);
|
|
225
|
+
if (content === undefined)
|
|
226
|
+
return;
|
|
227
|
+
// Only a refresh or disappear() aborts a page, and both drop its result before this, so every failure is shown,
|
|
228
|
+
// an abort the app's own load makes included.
|
|
229
|
+
this.screen.replaceContent({ ...content, paging: { status: "failed", error } });
|
|
230
|
+
this.source().announce({ kind: "failed" });
|
|
231
|
+
}
|
|
232
|
+
/** Ends the page load `id` and returns the list, unless a refresh or the screen going away replaced it. */
|
|
233
|
+
finishPageLoad(id) {
|
|
234
|
+
if (id !== this.generation)
|
|
235
|
+
return undefined;
|
|
236
|
+
this.pageLoad = undefined;
|
|
237
|
+
return this.currentContent();
|
|
238
|
+
}
|
|
239
|
+
cancelPageLoad() {
|
|
240
|
+
this.pageLoad?.abort();
|
|
241
|
+
this.pageLoad = undefined;
|
|
242
|
+
this.generation += 1;
|
|
243
|
+
if (this.currentContent()?.paging.status === "loading")
|
|
244
|
+
this.setPaging({ status: "ready" });
|
|
245
|
+
}
|
|
246
|
+
setPaging(paging) {
|
|
247
|
+
const content = this.currentContent();
|
|
248
|
+
if (content !== undefined)
|
|
249
|
+
this.screen.replaceContent({ ...content, paging });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
exports.PagedListMachine = PagedListMachine;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** The bottom of an inverted list: whether its newest row is on screen, and what arrived while it wasn't. */
|
|
2
|
+
export interface NewestEndSnapshot {
|
|
3
|
+
/** Whether the newest row is on screen, so newer items scroll into view. */
|
|
4
|
+
readonly isAtBottom: boolean;
|
|
5
|
+
/** How many newer items arrived while the newest row was off screen. */
|
|
6
|
+
readonly newItemCount: number;
|
|
7
|
+
/** Goes up by one each time the view should scroll to the newest row: `scrollToOffset({ offset: 0 })`. */
|
|
8
|
+
readonly scrollToBottomRequest: number;
|
|
9
|
+
}
|
|
10
|
+
/** Keeps a `NewestEndSnapshot`, a new object for each change, and tells its listener. */
|
|
11
|
+
export declare class NewestEnd {
|
|
12
|
+
private readonly changed;
|
|
13
|
+
private current;
|
|
14
|
+
/**
|
|
15
|
+
* A scroll to the newest row is on its way: until it lands, or the user takes over, the newest row going off screen
|
|
16
|
+
* (as a burst of rows arrives below it) doesn't count as scrolling away.
|
|
17
|
+
*/
|
|
18
|
+
private following;
|
|
19
|
+
/** What the view last reported, kept while following so a drag can go back to it. */
|
|
20
|
+
private reportedAtBottom;
|
|
21
|
+
constructor(changed: () => void);
|
|
22
|
+
get snapshot(): NewestEndSnapshot;
|
|
23
|
+
/** `count` newer items were added: they scroll into view at the bottom, and are counted anywhere else. */
|
|
24
|
+
arrived(count: number): void;
|
|
25
|
+
bottomChanged(isAtBottom: boolean): void;
|
|
26
|
+
/** The user started scrolling: a scroll to the newest row still on its way no longer holds the bottom. */
|
|
27
|
+
userScrolled(): void;
|
|
28
|
+
scrollToBottom(): void;
|
|
29
|
+
private update;
|
|
30
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NewestEnd = void 0;
|
|
4
|
+
/** Keeps a `NewestEndSnapshot`, a new object for each change, and tells its listener. */
|
|
5
|
+
class NewestEnd {
|
|
6
|
+
changed;
|
|
7
|
+
current = { isAtBottom: true, newItemCount: 0, scrollToBottomRequest: 0 };
|
|
8
|
+
/**
|
|
9
|
+
* A scroll to the newest row is on its way: until it lands, or the user takes over, the newest row going off screen
|
|
10
|
+
* (as a burst of rows arrives below it) doesn't count as scrolling away.
|
|
11
|
+
*/
|
|
12
|
+
following = false;
|
|
13
|
+
/** What the view last reported, kept while following so a drag can go back to it. */
|
|
14
|
+
reportedAtBottom = true;
|
|
15
|
+
constructor(changed) {
|
|
16
|
+
this.changed = changed;
|
|
17
|
+
}
|
|
18
|
+
get snapshot() {
|
|
19
|
+
return this.current;
|
|
20
|
+
}
|
|
21
|
+
/** `count` newer items were added: they scroll into view at the bottom, and are counted anywhere else. */
|
|
22
|
+
arrived(count) {
|
|
23
|
+
const { isAtBottom, newItemCount, scrollToBottomRequest } = this.current;
|
|
24
|
+
if (isAtBottom)
|
|
25
|
+
this.following = true;
|
|
26
|
+
this.update(isAtBottom
|
|
27
|
+
? { scrollToBottomRequest: scrollToBottomRequest + 1 }
|
|
28
|
+
: { newItemCount: newItemCount + count });
|
|
29
|
+
}
|
|
30
|
+
bottomChanged(isAtBottom) {
|
|
31
|
+
this.reportedAtBottom = isAtBottom;
|
|
32
|
+
if (isAtBottom)
|
|
33
|
+
this.following = false;
|
|
34
|
+
if (!isAtBottom && this.following)
|
|
35
|
+
return;
|
|
36
|
+
if (isAtBottom === this.current.isAtBottom && (!isAtBottom || this.current.newItemCount === 0))
|
|
37
|
+
return;
|
|
38
|
+
this.update(isAtBottom ? { isAtBottom, newItemCount: 0 } : { isAtBottom });
|
|
39
|
+
}
|
|
40
|
+
/** The user started scrolling: a scroll to the newest row still on its way no longer holds the bottom. */
|
|
41
|
+
userScrolled() {
|
|
42
|
+
this.following = false;
|
|
43
|
+
this.bottomChanged(this.reportedAtBottom);
|
|
44
|
+
}
|
|
45
|
+
scrollToBottom() {
|
|
46
|
+
this.following = true;
|
|
47
|
+
this.update({
|
|
48
|
+
isAtBottom: true,
|
|
49
|
+
newItemCount: 0,
|
|
50
|
+
scrollToBottomRequest: this.current.scrollToBottomRequest + 1,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
update(change) {
|
|
54
|
+
this.current = { ...this.current, ...change };
|
|
55
|
+
this.changed();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.NewestEnd = NewestEnd;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** One page of a paged list: its items, and the token of the next page, or none (`null` or left out) at the end. */
|
|
2
|
+
export interface Page<Item, Token> {
|
|
3
|
+
readonly items: readonly Item[];
|
|
4
|
+
readonly next?: Token | null | undefined;
|
|
5
|
+
}
|
|
6
|
+
export type Paging = {
|
|
7
|
+
readonly status: "ready";
|
|
8
|
+
}
|
|
9
|
+
/** Ready, after pages that added nothing: only an explicit request for more loads the next. */
|
|
10
|
+
| {
|
|
11
|
+
readonly status: "stalled";
|
|
12
|
+
} | {
|
|
13
|
+
readonly status: "loading";
|
|
14
|
+
} | {
|
|
15
|
+
readonly status: "failed";
|
|
16
|
+
readonly error: unknown;
|
|
17
|
+
};
|
|
18
|
+
/** The items a paged list has loaded, in order with no two of the same key, and how its paging stands. */
|
|
19
|
+
export interface PagedContent<Item, Token> {
|
|
20
|
+
readonly items: readonly Item[];
|
|
21
|
+
readonly keys: ReadonlySet<string>;
|
|
22
|
+
readonly next: Token | undefined;
|
|
23
|
+
readonly paging: Paging;
|
|
24
|
+
}
|
|
25
|
+
export declare function firstPageContent<Item, Token>(page: Page<Item, Token>, keyExtractor: (item: Item) => string): PagedContent<Item, Token>;
|
|
26
|
+
/**
|
|
27
|
+
* The content with the items whose key isn't in it yet before its first, the newest first; `arrived` runs oldest
|
|
28
|
+
* first, as the items arrived.
|
|
29
|
+
*/
|
|
30
|
+
export declare function addingNewer<Item, Token>(content: PagedContent<Item, Token>, arrived: readonly Item[], keyExtractor: (item: Item) => string): {
|
|
31
|
+
readonly content: PagedContent<Item, Token>;
|
|
32
|
+
readonly added: number;
|
|
33
|
+
};
|
|
34
|
+
/** The content with the page's items whose key isn't in it yet, and the page's next token. */
|
|
35
|
+
export declare function addingPage<Item, Token>(content: PagedContent<Item, Token>, page: Page<Item, Token>, keyExtractor: (item: Item) => string): {
|
|
36
|
+
readonly content: PagedContent<Item, Token>;
|
|
37
|
+
readonly added: number;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Loads the first page and, while it brings no items but a next token, the pages after it, up to `maxPages` in all.
|
|
41
|
+
* `source` is read for each page, so it can change between them.
|
|
42
|
+
*/
|
|
43
|
+
export declare function loadFirstPages<Item, Token>(source: () => {
|
|
44
|
+
readonly loadPage: (token: Token | undefined, signal: AbortSignal) => Promise<Page<Item, Token>>;
|
|
45
|
+
readonly keyExtractor: (item: Item) => string;
|
|
46
|
+
}, signal: AbortSignal, maxPages: number): Promise<PagedContent<Item, Token>>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.firstPageContent = firstPageContent;
|
|
4
|
+
exports.addingNewer = addingNewer;
|
|
5
|
+
exports.addingPage = addingPage;
|
|
6
|
+
exports.loadFirstPages = loadFirstPages;
|
|
7
|
+
const READY = { status: "ready" };
|
|
8
|
+
function firstPageContent(page, keyExtractor) {
|
|
9
|
+
return addingPage({ items: [], keys: new Set(), next: undefined, paging: READY }, page, keyExtractor)
|
|
10
|
+
.content;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The content with the items whose key isn't in it yet before its first, the newest first; `arrived` runs oldest
|
|
14
|
+
* first, as the items arrived.
|
|
15
|
+
*/
|
|
16
|
+
function addingNewer(content, arrived, keyExtractor) {
|
|
17
|
+
const keys = new Set(content.keys);
|
|
18
|
+
const newer = [];
|
|
19
|
+
for (const item of arrived) {
|
|
20
|
+
const key = keyExtractor(item);
|
|
21
|
+
if (keys.has(key))
|
|
22
|
+
continue;
|
|
23
|
+
keys.add(key);
|
|
24
|
+
newer.push(item);
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
content: { ...content, items: [...newer.reverse(), ...content.items], keys },
|
|
28
|
+
added: newer.length,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** The content with the page's items whose key isn't in it yet, and the page's next token. */
|
|
32
|
+
function addingPage(content, page, keyExtractor) {
|
|
33
|
+
const keys = new Set(content.keys);
|
|
34
|
+
const newItems = [];
|
|
35
|
+
for (const item of page.items) {
|
|
36
|
+
const key = keyExtractor(item);
|
|
37
|
+
if (keys.has(key))
|
|
38
|
+
continue;
|
|
39
|
+
keys.add(key);
|
|
40
|
+
newItems.push(item);
|
|
41
|
+
}
|
|
42
|
+
const next = page.next ?? undefined;
|
|
43
|
+
return {
|
|
44
|
+
content: { ...content, items: [...content.items, ...newItems], keys, next },
|
|
45
|
+
added: newItems.length,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Loads the first page and, while it brings no items but a next token, the pages after it, up to `maxPages` in all.
|
|
50
|
+
* `source` is read for each page, so it can change between them.
|
|
51
|
+
*/
|
|
52
|
+
async function loadFirstPages(source, signal, maxPages) {
|
|
53
|
+
let content = firstPageContent(await source().loadPage(undefined, signal), source().keyExtractor);
|
|
54
|
+
for (let pages = 1; content.items.length === 0 && pages < maxPages; pages += 1) {
|
|
55
|
+
if (content.next === undefined)
|
|
56
|
+
break;
|
|
57
|
+
// A load that ignored the abort must not start another; the list has gone or moved on.
|
|
58
|
+
// `throwIfAborted()` is missing from older React Native's AbortSignal.
|
|
59
|
+
if (signal.aborted)
|
|
60
|
+
throw new Error("The list stopped loading its first page.");
|
|
61
|
+
const page = await source().loadPage(content.next, signal);
|
|
62
|
+
content = addingPage(content, page, source().keyExtractor).content;
|
|
63
|
+
}
|
|
64
|
+
return content;
|
|
65
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { ScreenStatesSnapshot } from "../screen-states/machine";
|
|
2
|
+
import type { NewestEndSnapshot } from "./newest-end";
|
|
3
|
+
import type { Page } from "./paged-content";
|
|
4
|
+
/**
|
|
5
|
+
* Loads the page with this token, or the first page for `undefined`. Abort the work when `signal` aborts: the
|
|
6
|
+
* list went away or is refreshing, and the result is dropped.
|
|
7
|
+
*/
|
|
8
|
+
export type LoadPage<Item, Token> = (token: Token | undefined, signal: AbortSignal) => Promise<Page<Item, Token>>;
|
|
9
|
+
/** What the end of a paged list shows, after its last row. */
|
|
10
|
+
export type PagedListFooter =
|
|
11
|
+
/** There is a next page and none is loading; `loadMore` loads it. */
|
|
12
|
+
{
|
|
13
|
+
readonly status: "more";
|
|
14
|
+
readonly loadMore: () => void;
|
|
15
|
+
} | {
|
|
16
|
+
readonly status: "loading";
|
|
17
|
+
}
|
|
18
|
+
/** The last page failed; every item already loaded stays. `retry` loads that page again. */
|
|
19
|
+
| {
|
|
20
|
+
readonly status: "failed";
|
|
21
|
+
readonly error: unknown;
|
|
22
|
+
readonly retry: () => void;
|
|
23
|
+
} | {
|
|
24
|
+
readonly status: "end";
|
|
25
|
+
};
|
|
26
|
+
/** What a screen reader hears when a page after the first finishes. */
|
|
27
|
+
export type PagedListAnnouncement = {
|
|
28
|
+
readonly kind: "loaded";
|
|
29
|
+
readonly count: number;
|
|
30
|
+
readonly isEnd: boolean;
|
|
31
|
+
} | {
|
|
32
|
+
readonly kind: "end";
|
|
33
|
+
} | {
|
|
34
|
+
readonly kind: "failed";
|
|
35
|
+
}
|
|
36
|
+
/** Newer items arrived in an inverted list. */
|
|
37
|
+
| {
|
|
38
|
+
readonly kind: "newItems";
|
|
39
|
+
readonly count: number;
|
|
40
|
+
};
|
|
41
|
+
/** The first page's state, with every item loaded so far, first page first, as its content. */
|
|
42
|
+
export interface PagedListSnapshot<Item> extends ScreenStatesSnapshot<readonly Item[]>, NewestEndSnapshot {
|
|
43
|
+
/** What to show after the last row, or `undefined` when the list isn't on screen. */
|
|
44
|
+
readonly footer: PagedListFooter | undefined;
|
|
45
|
+
}
|
|
46
|
+
export interface PagedListSource<Item, Token> {
|
|
47
|
+
readonly loadPage: LoadPage<Item, Token>;
|
|
48
|
+
readonly keyExtractor: (item: Item) => string;
|
|
49
|
+
readonly isOffline: (error: unknown) => boolean;
|
|
50
|
+
readonly announce: (announcement: PagedListAnnouncement) => void;
|
|
51
|
+
}
|
|
52
|
+
/** A newer item that arrived while the first page loaded, and whether it was shown (and announced) then. */
|
|
53
|
+
export interface Arrival<Item> {
|
|
54
|
+
readonly item: Item;
|
|
55
|
+
readonly shown: boolean;
|
|
56
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { PagedListAnnouncement, PagedListFooter } from "./machine";
|
|
2
|
+
/** The text of the footer `PagedList` shows when the app leaves it out, and of its announcements. */
|
|
3
|
+
export interface PagedListLabels {
|
|
4
|
+
readonly loadingMore: string;
|
|
5
|
+
readonly loadMoreFailed: string;
|
|
6
|
+
readonly loadMore: string;
|
|
7
|
+
readonly noMoreItems: string;
|
|
8
|
+
readonly tryAgain: string;
|
|
9
|
+
/** "Loaded 20 more items.", with the plural your language needs. */
|
|
10
|
+
readonly loadedMore: (count: number) => string;
|
|
11
|
+
readonly endOfList: string;
|
|
12
|
+
readonly loadMoreFailedAnnouncement: string;
|
|
13
|
+
/** The end footer of an inverted list, a chat's start. */
|
|
14
|
+
readonly noEarlierItems: string;
|
|
15
|
+
/** The end of an inverted list, announced. */
|
|
16
|
+
readonly startOfList: string;
|
|
17
|
+
/** "1 new item.", "3 new items.", for newer items arriving in an inverted list. */
|
|
18
|
+
readonly newItems: (count: number) => string;
|
|
19
|
+
}
|
|
20
|
+
export declare const defaultPagedListLabels: PagedListLabels;
|
|
21
|
+
/** What a screen reader hears when a page after the first finishes. The end of an inverted list is its start. */
|
|
22
|
+
export declare function announcementText(announcement: PagedListAnnouncement, labels: PagedListLabels, inverted?: boolean): string;
|
|
23
|
+
export declare function SystemPagedListFooter({ footer, labels, inverted, }: {
|
|
24
|
+
readonly footer: PagedListFooter;
|
|
25
|
+
readonly labels: PagedListLabels;
|
|
26
|
+
/** An inverted list's end reads as the start of a chat. */
|
|
27
|
+
readonly inverted?: boolean;
|
|
28
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultPagedListLabels = void 0;
|
|
4
|
+
exports.announcementText = announcementText;
|
|
5
|
+
exports.SystemPagedListFooter = SystemPagedListFooter;
|
|
6
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
7
|
+
const react_native_1 = require("react-native");
|
|
8
|
+
const system_views_1 = require("../screen-states/system-views");
|
|
9
|
+
exports.defaultPagedListLabels = {
|
|
10
|
+
loadingMore: "Loading More",
|
|
11
|
+
loadMoreFailed: "Couldn't Load More",
|
|
12
|
+
loadMore: "Load More",
|
|
13
|
+
noMoreItems: "No More Items",
|
|
14
|
+
tryAgain: system_views_1.defaultScreenStatesLabels.tryAgain,
|
|
15
|
+
loadedMore: (count) => (count === 1 ? "Loaded 1 more item." : `Loaded ${count} more items.`),
|
|
16
|
+
endOfList: "End of list.",
|
|
17
|
+
loadMoreFailedAnnouncement: "Couldn't load more items.",
|
|
18
|
+
noEarlierItems: "No Earlier Items",
|
|
19
|
+
startOfList: "Start of list.",
|
|
20
|
+
newItems: (count) => (count === 1 ? "1 new item." : `${count} new items.`),
|
|
21
|
+
};
|
|
22
|
+
/** What a screen reader hears when a page after the first finishes. The end of an inverted list is its start. */
|
|
23
|
+
function announcementText(announcement, labels, inverted = false) {
|
|
24
|
+
const end = inverted ? labels.startOfList : labels.endOfList;
|
|
25
|
+
switch (announcement.kind) {
|
|
26
|
+
case "loaded": {
|
|
27
|
+
const loaded = labels.loadedMore(announcement.count);
|
|
28
|
+
return announcement.isEnd ? `${loaded} ${end}` : loaded;
|
|
29
|
+
}
|
|
30
|
+
case "end":
|
|
31
|
+
return end;
|
|
32
|
+
case "failed":
|
|
33
|
+
return labels.loadMoreFailedAnnouncement;
|
|
34
|
+
case "newItems":
|
|
35
|
+
return labels.newItems(announcement.count);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// System components only, with no styles of their own.
|
|
39
|
+
function SystemPagedListFooter({ footer, labels, inverted = false, }) {
|
|
40
|
+
switch (footer.status) {
|
|
41
|
+
case "more":
|
|
42
|
+
return (0, jsx_runtime_1.jsx)(react_native_1.Button, { title: labels.loadMore, onPress: footer.loadMore });
|
|
43
|
+
case "loading":
|
|
44
|
+
return ((0, jsx_runtime_1.jsx)(react_native_1.ActivityIndicator, { accessible: true, accessibilityRole: "progressbar", accessibilityLabel: labels.loadingMore }));
|
|
45
|
+
case "failed":
|
|
46
|
+
return ((0, jsx_runtime_1.jsxs)(react_native_1.View, { children: [(0, jsx_runtime_1.jsx)(react_native_1.Text, { children: labels.loadMoreFailed }), (0, jsx_runtime_1.jsx)(react_native_1.Button, { title: labels.tryAgain, onPress: footer.retry })] }));
|
|
47
|
+
case "end":
|
|
48
|
+
return (0, jsx_runtime_1.jsx)(react_native_1.Text, { children: inverted ? labels.noEarlierItems : labels.noMoreItems });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type PagedListController } from "./controller";
|
|
2
|
+
import { type LoadPage } from "./machine";
|
|
3
|
+
import type { Page } from "./paged-content";
|
|
4
|
+
import { type PagedListLabels } from "./system-footer";
|
|
5
|
+
export interface PagedListOptions<Item, Token> {
|
|
6
|
+
/** The identity of an item: an item whose key is already in the list is dropped. `FlatList` uses it too. */
|
|
7
|
+
readonly keyExtractor: (item: Item) => string;
|
|
8
|
+
/** Whether a failed first page shows the offline view. By default, `fetch`'s "Network request failed". */
|
|
9
|
+
readonly isOffline?: (error: unknown) => boolean;
|
|
10
|
+
/** The text of the announcements, and of the footer `PagedList` draws. */
|
|
11
|
+
readonly labels?: Partial<PagedListLabels>;
|
|
12
|
+
/** A first page the screen already has, shown at once with no request. One with no items counts as none. */
|
|
13
|
+
readonly initialPage?: Page<Item, Token>;
|
|
14
|
+
/**
|
|
15
|
+
* A chat: the first page is the newest, drawn at the bottom of an inverted `FlatList`, with its items newest
|
|
16
|
+
* first, and newer items arrive with `appendNewer`. Read once, when the list mounts.
|
|
17
|
+
*/
|
|
18
|
+
readonly inverted?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The state of a list that loads in pages, for apps that draw it themselves. Loads the first page on mount, aborts
|
|
22
|
+
* any load on unmount, and always calls the latest `loadPage` and options. Each page that changes the list is
|
|
23
|
+
* announced to screen readers. Give the component a new `key` to load again for a new input.
|
|
24
|
+
*/
|
|
25
|
+
export declare function usePagedList<Item, Token>(loadPage: LoadPage<Item, Token>, options: PagedListOptions<Item, Token>): PagedListController<Item>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.usePagedList = usePagedList;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const announce_1 = require("../announce");
|
|
6
|
+
const errors_1 = require("../screen-states/errors");
|
|
7
|
+
const controller_1 = require("./controller");
|
|
8
|
+
const machine_1 = require("./machine");
|
|
9
|
+
const system_footer_1 = require("./system-footer");
|
|
10
|
+
/**
|
|
11
|
+
* The state of a list that loads in pages, for apps that draw it themselves. Loads the first page on mount, aborts
|
|
12
|
+
* any load on unmount, and always calls the latest `loadPage` and options. Each page that changes the list is
|
|
13
|
+
* announced to screen readers. Give the component a new `key` to load again for a new input.
|
|
14
|
+
*/
|
|
15
|
+
function usePagedList(loadPage, options) {
|
|
16
|
+
const labels = { ...system_footer_1.defaultPagedListLabels, ...options.labels };
|
|
17
|
+
const source = {
|
|
18
|
+
loadPage,
|
|
19
|
+
keyExtractor: options.keyExtractor,
|
|
20
|
+
isOffline: options.isOffline ?? errors_1.isOfflineError,
|
|
21
|
+
announce: (announcement) => (0, announce_1.announce)((0, system_footer_1.announcementText)(announcement, labels, options.inverted)),
|
|
22
|
+
};
|
|
23
|
+
const latest = (0, react_1.useRef)(source);
|
|
24
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
25
|
+
latest.current = source;
|
|
26
|
+
});
|
|
27
|
+
const [machine] = (0, react_1.useState)(() => new machine_1.PagedListMachine(() => latest.current, options.initialPage, options.inverted));
|
|
28
|
+
const snapshot = (0, react_1.useSyncExternalStore)(machine.subscribe, machine.getSnapshot);
|
|
29
|
+
(0, react_1.useEffect)(() => {
|
|
30
|
+
machine.appear();
|
|
31
|
+
return machine.disappear;
|
|
32
|
+
}, [machine]);
|
|
33
|
+
return (0, react_1.useMemo)(() => (0, controller_1.pagedListController)(machine, snapshot), [snapshot, machine]);
|
|
34
|
+
}
|