@teleporthq/teleport-plugin-next-data-source 0.44.0 → 0.44.4
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/__tests__/_helpers/first-page-runtime.ts +184 -0
- package/__tests__/_helpers/pagination-widget-fixture.ts +67 -1
- package/__tests__/browser-row-policy.test.ts +202 -2
- package/__tests__/data-source-request-locale.test.ts +4 -3
- package/__tests__/fetchers.test.ts +6 -2
- package/__tests__/infinite-scroll.test.ts +210 -5
- package/__tests__/page-data-visitor-read.test.ts +196 -0
- package/__tests__/plain-list-sort.test.ts +1 -1
- package/__tests__/plain-list-starting-sort.test.ts +323 -0
- package/__tests__/refetch-loading-state.test.ts +158 -2
- package/__tests__/starting-sort-first-page.test.ts +285 -0
- package/dist/cjs/cache/ast.d.ts +2 -0
- package/dist/cjs/cache/ast.d.ts.map +1 -1
- package/dist/cjs/cache/ast.js +5 -2
- package/dist/cjs/cache/ast.js.map +1 -1
- package/dist/cjs/data-source-fetchers.d.ts.map +1 -1
- package/dist/cjs/data-source-fetchers.js +10 -2
- package/dist/cjs/data-source-fetchers.js.map +1 -1
- package/dist/cjs/fetchers/teleport.d.ts.map +1 -1
- package/dist/cjs/fetchers/teleport.js +1 -1
- package/dist/cjs/fetchers/teleport.js.map +1 -1
- package/dist/cjs/fetchers/utils/browser-row-policy.d.ts.map +1 -1
- package/dist/cjs/fetchers/utils/browser-row-policy.js +5 -1
- package/dist/cjs/fetchers/utils/browser-row-policy.js.map +1 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +14 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/infinite-scroll.d.ts +64 -2
- package/dist/cjs/infinite-scroll.d.ts.map +1 -1
- package/dist/cjs/infinite-scroll.js +102 -3
- package/dist/cjs/infinite-scroll.js.map +1 -1
- package/dist/cjs/loading-state.d.ts +23 -2
- package/dist/cjs/loading-state.d.ts.map +1 -1
- package/dist/cjs/loading-state.js +50 -15
- package/dist/cjs/loading-state.js.map +1 -1
- package/dist/cjs/pagination-plugin.d.ts.map +1 -1
- package/dist/cjs/pagination-plugin.js +200 -64
- package/dist/cjs/pagination-plugin.js.map +1 -1
- package/dist/cjs/sort-utils.d.ts +53 -1
- package/dist/cjs/sort-utils.d.ts.map +1 -1
- package/dist/cjs/sort-utils.js +105 -13
- package/dist/cjs/sort-utils.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/cjs/utils.d.ts +3 -2
- package/dist/cjs/utils.d.ts.map +1 -1
- package/dist/cjs/utils.js +48 -18
- package/dist/cjs/utils.js.map +1 -1
- package/dist/esm/cache/ast.d.ts +2 -0
- package/dist/esm/cache/ast.d.ts.map +1 -1
- package/dist/esm/cache/ast.js +3 -1
- package/dist/esm/cache/ast.js.map +1 -1
- package/dist/esm/data-source-fetchers.d.ts.map +1 -1
- package/dist/esm/data-source-fetchers.js +10 -2
- package/dist/esm/data-source-fetchers.js.map +1 -1
- package/dist/esm/fetchers/teleport.d.ts.map +1 -1
- package/dist/esm/fetchers/teleport.js +1 -1
- package/dist/esm/fetchers/teleport.js.map +1 -1
- package/dist/esm/fetchers/utils/browser-row-policy.d.ts.map +1 -1
- package/dist/esm/fetchers/utils/browser-row-policy.js +6 -2
- package/dist/esm/fetchers/utils/browser-row-policy.js.map +1 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +14 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/infinite-scroll.d.ts +64 -2
- package/dist/esm/infinite-scroll.d.ts.map +1 -1
- package/dist/esm/infinite-scroll.js +97 -2
- package/dist/esm/infinite-scroll.js.map +1 -1
- package/dist/esm/loading-state.d.ts +23 -2
- package/dist/esm/loading-state.d.ts.map +1 -1
- package/dist/esm/loading-state.js +50 -15
- package/dist/esm/loading-state.js.map +1 -1
- package/dist/esm/pagination-plugin.d.ts.map +1 -1
- package/dist/esm/pagination-plugin.js +204 -68
- package/dist/esm/pagination-plugin.js.map +1 -1
- package/dist/esm/sort-utils.d.ts +53 -1
- package/dist/esm/sort-utils.d.ts.map +1 -1
- package/dist/esm/sort-utils.js +100 -12
- package/dist/esm/sort-utils.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/utils.d.ts +3 -2
- package/dist/esm/utils.d.ts.map +1 -1
- package/dist/esm/utils.js +49 -19
- package/dist/esm/utils.js.map +1 -1
- package/package.json +4 -4
- package/src/cache/ast.ts +4 -1
- package/src/data-source-fetchers.ts +33 -14
- package/src/fetchers/teleport.ts +8 -2
- package/src/fetchers/utils/browser-row-policy.ts +79 -5
- package/src/index.ts +8 -2
- package/src/infinite-scroll.ts +182 -4
- package/src/loading-state.ts +85 -13
- package/src/pagination-plugin.ts +369 -83
- package/src/sort-utils.ts +164 -21
- package/src/utils.ts +55 -18
package/src/index.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
UIDLDynamicReference,
|
|
9
9
|
} from '@teleporthq/teleport-types'
|
|
10
10
|
import { UIDLUtils, StringUtils } from '@teleporthq/teleport-shared'
|
|
11
|
+
import { RouteUtils } from '@teleporthq/teleport-plugin-common'
|
|
11
12
|
import {
|
|
12
13
|
extractDataSourceIntoNextAPIFolder,
|
|
13
14
|
extractDataSourceIntoGetStaticProps,
|
|
@@ -652,7 +653,9 @@ export const createNextPagesDataSourcePlugin: ComponentPluginFactory<{}> = () =>
|
|
|
652
653
|
dependencies,
|
|
653
654
|
dynamicRouteAttr,
|
|
654
655
|
uidl.outputOptions?.folderPath,
|
|
655
|
-
transformOptions
|
|
656
|
+
transformOptions,
|
|
657
|
+
() => RouteUtils.pageDataReadArguments(uidl, options.auth),
|
|
658
|
+
uidl.stateDefinitions
|
|
656
659
|
)
|
|
657
660
|
|
|
658
661
|
if (result.success && result.chunk) {
|
|
@@ -794,7 +797,10 @@ export const createNextPagesDataSourcePlugin: ComponentPluginFactory<{}> = () =>
|
|
|
794
797
|
types.identifier(fetcherImportName),
|
|
795
798
|
types.identifier('fetchData')
|
|
796
799
|
),
|
|
797
|
-
[
|
|
800
|
+
[
|
|
801
|
+
types.objectExpression([]),
|
|
802
|
+
...RouteUtils.pageDataReadArguments(uidl, options.auth),
|
|
803
|
+
]
|
|
798
804
|
)
|
|
799
805
|
|
|
800
806
|
const safeFetchExpression = types.callExpression(
|
package/src/infinite-scroll.ts
CHANGED
|
@@ -6,9 +6,10 @@ import * as types from '@babel/types'
|
|
|
6
6
|
* The accumulation lives inside the generated `fetchData` closure rather than
|
|
7
7
|
* around the provider. `DataProvider` renders whatever `fetchData` resolves
|
|
8
8
|
* with, so returning "every page fetched so far, in order" is enough to make it
|
|
9
|
-
* append — no change to the runtime component library, and
|
|
9
|
+
* append — no change to the runtime component library, and
|
|
10
10
|
* `persistDataDuringLoading` keeps the rows already on screen while the next
|
|
11
|
-
* page is in flight
|
|
11
|
+
* page is in flight (`buildAppendingRequestCheck`: the loading slot replaces
|
|
12
|
+
* them only for a list loading afresh).
|
|
12
13
|
*
|
|
13
14
|
* A `useRef` holds the pages rather than a state, because writing accumulated
|
|
14
15
|
* rows into state from inside `fetchData` would re-render the provider mid-fetch
|
|
@@ -24,6 +25,11 @@ import * as types from '@babel/types'
|
|
|
24
25
|
* more" fetched a page already on show. `DataProvider` also renders whichever
|
|
25
26
|
* answer arrives LAST, so a late answer to an earlier query must not replace
|
|
26
27
|
* the list either.
|
|
28
|
+
*
|
|
29
|
+
* On a page, the list starts from the rows `getStaticProps` already fetched
|
|
30
|
+
* (`buildFirstPageDeclaration`, `buildFirstPageSeedEffect`): the server HTML
|
|
31
|
+
* carries them instead of the loading state, the accumulator holds them as
|
|
32
|
+
* page 1, and the browser's first request is page 2.
|
|
27
33
|
*/
|
|
28
34
|
|
|
29
35
|
// tslint:disable:no-any
|
|
@@ -34,6 +40,7 @@ export interface InfiniteScrollVars {
|
|
|
34
40
|
setHasMoreFlagVar: string
|
|
35
41
|
hasMoreVar: string
|
|
36
42
|
sentinelRefVar: string
|
|
43
|
+
firstPageVar: string
|
|
37
44
|
}
|
|
38
45
|
|
|
39
46
|
export const getInfiniteScrollVars = (index: number): InfiniteScrollVars => ({
|
|
@@ -42,6 +49,7 @@ export const getInfiniteScrollVars = (index: number): InfiniteScrollVars => ({
|
|
|
42
49
|
setHasMoreFlagVar: `setDs_${index}_hasMoreFlag`,
|
|
43
50
|
hasMoreVar: `ds_${index}_hasMore`,
|
|
44
51
|
sentinelRefVar: `ds_${index}_sentinelRef`,
|
|
52
|
+
firstPageVar: `ds_${index}_firstPage`,
|
|
45
53
|
})
|
|
46
54
|
|
|
47
55
|
/**
|
|
@@ -172,12 +180,12 @@ const writeAtPath = (
|
|
|
172
180
|
}
|
|
173
181
|
|
|
174
182
|
/** `JSON.stringify({ ...params, page: 0 })`: everything that identifies the query but the page. */
|
|
175
|
-
const buildQuerySignature = (): types.CallExpression =>
|
|
183
|
+
const buildQuerySignature = (paramsVar: string = 'params'): types.CallExpression =>
|
|
176
184
|
types.callExpression(
|
|
177
185
|
types.memberExpression(types.identifier('JSON'), types.identifier('stringify')),
|
|
178
186
|
[
|
|
179
187
|
types.objectExpression([
|
|
180
|
-
types.spreadElement(types.identifier(
|
|
188
|
+
types.spreadElement(types.identifier(paramsVar)),
|
|
181
189
|
types.objectProperty(types.identifier('page'), types.numericLiteral(0)),
|
|
182
190
|
]),
|
|
183
191
|
]
|
|
@@ -214,6 +222,176 @@ export const buildAccumulatingRequestMark = (vars: InfiniteScrollVars): types.St
|
|
|
214
222
|
)
|
|
215
223
|
)
|
|
216
224
|
|
|
225
|
+
/**
|
|
226
|
+
* Whether the request `fetchData` is about to make only adds a page to the rows
|
|
227
|
+
* on show: a page past the first, of the query the accumulator holds. Read
|
|
228
|
+
* after `buildAccumulatingRequestMark` has recorded that request.
|
|
229
|
+
*
|
|
230
|
+
* ds_0_accumRef.current.latest.page > 1 && ds_0_accumRef.current.latest.sig === ds_0_accumRef.current.sig
|
|
231
|
+
*
|
|
232
|
+
* A filter, sort or search change is no append even past page 1 — the page it
|
|
233
|
+
* fires before the reset belongs to another query — and neither is a list's
|
|
234
|
+
* first page: both load the list afresh, behind its loading slot.
|
|
235
|
+
*/
|
|
236
|
+
export const buildAppendingRequestCheck = (vars: InfiniteScrollVars): types.Expression => {
|
|
237
|
+
const latestOf = (key: string): types.MemberExpression =>
|
|
238
|
+
types.memberExpression(
|
|
239
|
+
types.memberExpression(accumCurrentOf(vars), types.identifier('latest')),
|
|
240
|
+
types.identifier(key)
|
|
241
|
+
)
|
|
242
|
+
return types.logicalExpression(
|
|
243
|
+
'&&',
|
|
244
|
+
types.binaryExpression('>', latestOf('page'), types.numericLiteral(1)),
|
|
245
|
+
types.binaryExpression(
|
|
246
|
+
'===',
|
|
247
|
+
latestOf('sig'),
|
|
248
|
+
types.memberExpression(accumCurrentOf(vars), types.identifier('sig'))
|
|
249
|
+
)
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The rows inside a server payload: the payload itself, or what sits at
|
|
255
|
+
* `itemsPath` in one that wraps them. `<rows>?.length > 0` is how the caller
|
|
256
|
+
* asks whether the server found any.
|
|
257
|
+
*/
|
|
258
|
+
export const buildServerRowsExpression = (
|
|
259
|
+
payload: types.Expression,
|
|
260
|
+
itemsPath?: string[]
|
|
261
|
+
): types.Expression => (itemsPath?.length ? readAtPath(payload, itemsPath) : payload)
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The first page the list starts from, when its page already fetched it:
|
|
265
|
+
*
|
|
266
|
+
* const ds_0_firstPage = useMemo(() => <starts> ? props?.items_ds_0 : undefined, [<instance deps>])
|
|
267
|
+
*
|
|
268
|
+
* Handed to `DataProvider` as `initialData`, so the server HTML holds the rows
|
|
269
|
+
* rather than the loading state, and the provider skips its own fetch of page 1.
|
|
270
|
+
*
|
|
271
|
+
* ⛔ Frozen for the provider's lifetime. `DataProvider` copies `initialData`
|
|
272
|
+
* into what it shows every time the value changes, so a value that moved with
|
|
273
|
+
* the page — the paged lists' `page === 1 ? … : undefined`, fine for them since
|
|
274
|
+
* they remount per page — would wipe the appended rows on the first scroll.
|
|
275
|
+
* `instanceDeps` are what remounts the provider (its `key`): nothing for a list
|
|
276
|
+
* without search, the applied query for one with search.
|
|
277
|
+
*/
|
|
278
|
+
export const buildFirstPageDeclaration = (
|
|
279
|
+
vars: InfiniteScrollVars,
|
|
280
|
+
startsFromServerRows: types.Expression,
|
|
281
|
+
serverPayload: types.Expression,
|
|
282
|
+
instanceDeps: types.Expression[]
|
|
283
|
+
): types.Statement =>
|
|
284
|
+
types.variableDeclaration('const', [
|
|
285
|
+
types.variableDeclarator(
|
|
286
|
+
types.identifier(vars.firstPageVar),
|
|
287
|
+
types.callExpression(types.identifier('useMemo'), [
|
|
288
|
+
types.arrowFunctionExpression(
|
|
289
|
+
[],
|
|
290
|
+
types.conditionalExpression(
|
|
291
|
+
startsFromServerRows,
|
|
292
|
+
serverPayload,
|
|
293
|
+
types.identifier('undefined')
|
|
294
|
+
)
|
|
295
|
+
),
|
|
296
|
+
types.arrayExpression(instanceDeps),
|
|
297
|
+
])
|
|
298
|
+
),
|
|
299
|
+
])
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Makes the server rows page 1 of the accumulator whenever the provider is
|
|
303
|
+
* handed them instead of fetching:
|
|
304
|
+
*
|
|
305
|
+
* useEffect(() => {
|
|
306
|
+
* if (ds_0_firstPage === undefined) return
|
|
307
|
+
* const rows = ds_0_firstPage || []
|
|
308
|
+
* const sig = JSON.stringify({ ...ds_0_params, page: 0 })
|
|
309
|
+
* ds_0_accumRef.current = { sig, pages: { 1: rows }, latest: { sig, page: 1 } }
|
|
310
|
+
* setDs_0_hasMoreFlag(rows.length >= 20)
|
|
311
|
+
* }, [ds_0_firstPage])
|
|
312
|
+
*
|
|
313
|
+
* Without it the accumulator never sees page 1 (the provider skipped that
|
|
314
|
+
* fetch), and the answer to page 2 would REPLACE the rows on screen with page 2
|
|
315
|
+
* alone. The signature is the one `fetchData` computes for the same params, so
|
|
316
|
+
* the next page appends to these rows while a filter, sort or search change
|
|
317
|
+
* still starts a fresh list; `latest` keeps an answer still in flight from an
|
|
318
|
+
* earlier query from changing anything. A short first page is the last one, as
|
|
319
|
+
* it would be had it been fetched.
|
|
320
|
+
*/
|
|
321
|
+
export const buildFirstPageSeedEffect = (
|
|
322
|
+
vars: InfiniteScrollVars,
|
|
323
|
+
paramsVar: string,
|
|
324
|
+
perPage: number,
|
|
325
|
+
itemsPath?: string[]
|
|
326
|
+
): types.Statement =>
|
|
327
|
+
types.expressionStatement(
|
|
328
|
+
types.callExpression(types.identifier('useEffect'), [
|
|
329
|
+
types.arrowFunctionExpression(
|
|
330
|
+
[],
|
|
331
|
+
types.blockStatement([
|
|
332
|
+
types.ifStatement(
|
|
333
|
+
types.binaryExpression(
|
|
334
|
+
'===',
|
|
335
|
+
types.identifier(vars.firstPageVar),
|
|
336
|
+
types.identifier('undefined')
|
|
337
|
+
),
|
|
338
|
+
types.returnStatement()
|
|
339
|
+
),
|
|
340
|
+
types.variableDeclaration('const', [
|
|
341
|
+
types.variableDeclarator(
|
|
342
|
+
types.identifier('rows'),
|
|
343
|
+
types.logicalExpression(
|
|
344
|
+
'||',
|
|
345
|
+
buildServerRowsExpression(types.identifier(vars.firstPageVar), itemsPath),
|
|
346
|
+
types.arrayExpression([])
|
|
347
|
+
)
|
|
348
|
+
),
|
|
349
|
+
]),
|
|
350
|
+
types.variableDeclaration('const', [
|
|
351
|
+
types.variableDeclarator(types.identifier('sig'), buildQuerySignature(paramsVar)),
|
|
352
|
+
]),
|
|
353
|
+
types.expressionStatement(
|
|
354
|
+
types.assignmentExpression(
|
|
355
|
+
'=',
|
|
356
|
+
accumCurrentOf(vars),
|
|
357
|
+
types.objectExpression([
|
|
358
|
+
types.objectProperty(types.identifier('sig'), types.identifier('sig'), false, true),
|
|
359
|
+
types.objectProperty(
|
|
360
|
+
types.identifier('pages'),
|
|
361
|
+
types.objectExpression([
|
|
362
|
+
types.objectProperty(types.numericLiteral(1), types.identifier('rows')),
|
|
363
|
+
])
|
|
364
|
+
),
|
|
365
|
+
types.objectProperty(
|
|
366
|
+
types.identifier('latest'),
|
|
367
|
+
types.objectExpression([
|
|
368
|
+
types.objectProperty(
|
|
369
|
+
types.identifier('sig'),
|
|
370
|
+
types.identifier('sig'),
|
|
371
|
+
false,
|
|
372
|
+
true
|
|
373
|
+
),
|
|
374
|
+
types.objectProperty(types.identifier('page'), types.numericLiteral(1)),
|
|
375
|
+
])
|
|
376
|
+
),
|
|
377
|
+
])
|
|
378
|
+
)
|
|
379
|
+
),
|
|
380
|
+
types.expressionStatement(
|
|
381
|
+
types.callExpression(types.identifier(vars.setHasMoreFlagVar), [
|
|
382
|
+
types.binaryExpression(
|
|
383
|
+
'>=',
|
|
384
|
+
types.memberExpression(types.identifier('rows'), types.identifier('length')),
|
|
385
|
+
types.numericLiteral(perPage)
|
|
386
|
+
),
|
|
387
|
+
])
|
|
388
|
+
),
|
|
389
|
+
])
|
|
390
|
+
),
|
|
391
|
+
types.arrayExpression([types.identifier(vars.firstPageVar)]),
|
|
392
|
+
])
|
|
393
|
+
)
|
|
394
|
+
|
|
217
395
|
/**
|
|
218
396
|
* The `.then(...)` tail that accumulates a page and reports whether more exist.
|
|
219
397
|
*
|
package/src/loading-state.ts
CHANGED
|
@@ -52,6 +52,14 @@ import { findCachedFetchNetworkChain } from './cache/ast'
|
|
|
52
52
|
* Nothing changes for a provider that is idle or mounting: `isFetching` starts
|
|
53
53
|
* at `false`, so the server render and the first client render are identical
|
|
54
54
|
* (no hydration mismatch) and the `initialData` fast path is untouched.
|
|
55
|
+
*
|
|
56
|
+
* An appending list (infinite scroll, Load more) is the exception to "show the
|
|
57
|
+
* loading slot while a request is in flight": its next page belongs BELOW the
|
|
58
|
+
* rows on show. Swapping those rows for the loading slot until the page arrived
|
|
59
|
+
* collapsed the list on every append — the page jumped up under the visitor's
|
|
60
|
+
* scroll. Such a list also tracks whether the request in flight only appends
|
|
61
|
+
* (`isAppendingVar`) and keeps its rows through it; the loading slot replaces
|
|
62
|
+
* them only when the list loads afresh (a filter, sort or search change).
|
|
55
63
|
*/
|
|
56
64
|
|
|
57
65
|
export interface LoadingStateVars {
|
|
@@ -80,6 +88,13 @@ export interface LoadingStateVars {
|
|
|
80
88
|
fetchSeqRefVar: string
|
|
81
89
|
/** Ref holding the rows of the newest request that settled — what an overtaken one resolves with. */
|
|
82
90
|
latestDataRefVar: string
|
|
91
|
+
/**
|
|
92
|
+
* Boolean state of an appending list: `true` while the request it made last
|
|
93
|
+
* adds a page to the rows on show, so they stay there through it.
|
|
94
|
+
*/
|
|
95
|
+
isAppendingVar: string
|
|
96
|
+
/** Setter for `isAppendingVar`. */
|
|
97
|
+
setIsAppendingVar: string
|
|
83
98
|
}
|
|
84
99
|
|
|
85
100
|
export function getLoadingStateVars(index: number): LoadingStateVars {
|
|
@@ -89,6 +104,8 @@ export function getLoadingStateVars(index: number): LoadingStateVars {
|
|
|
89
104
|
inFlightRefVar: `ds_${index}_fetchesInFlight`,
|
|
90
105
|
fetchSeqRefVar: `ds_${index}_fetchSeq`,
|
|
91
106
|
latestDataRefVar: `ds_${index}_latestData`,
|
|
107
|
+
isAppendingVar: `ds_${index}_isAppending`,
|
|
108
|
+
setIsAppendingVar: `setDs_${index}_isAppending`,
|
|
92
109
|
}
|
|
93
110
|
}
|
|
94
111
|
|
|
@@ -97,14 +114,18 @@ export function getLoadingStateVars(index: number): LoadingStateVars {
|
|
|
97
114
|
* `const ds_N_fetchSeq = useRef(0)`
|
|
98
115
|
* `const ds_N_latestData = useRef(undefined)`
|
|
99
116
|
* `const [ds_N_isFetching, setDs_N_isFetching] = useState(false)`
|
|
117
|
+
* `const [ds_N_isAppending, setDs_N_isAppending] = useState(false)` (an appending list)
|
|
100
118
|
*
|
|
101
119
|
* All are stable across renders (ref objects and a `useState` setter), which
|
|
102
120
|
* is what lets the wrapped `fetchData` keep its empty `useCallback` dependency
|
|
103
121
|
* array — a changing `fetchData` identity would retrigger the provider's fetch
|
|
104
122
|
* effect on every render.
|
|
105
123
|
*/
|
|
106
|
-
export function buildLoadingStateDeclarations(
|
|
107
|
-
|
|
124
|
+
export function buildLoadingStateDeclarations(
|
|
125
|
+
vars: LoadingStateVars,
|
|
126
|
+
appends: boolean = false
|
|
127
|
+
): types.Statement[] {
|
|
128
|
+
const declarations: types.Statement[] = [
|
|
108
129
|
types.variableDeclaration('const', [
|
|
109
130
|
types.variableDeclarator(
|
|
110
131
|
types.identifier(vars.inFlightRefVar),
|
|
@@ -133,6 +154,20 @@ export function buildLoadingStateDeclarations(vars: LoadingStateVars): types.Sta
|
|
|
133
154
|
),
|
|
134
155
|
]),
|
|
135
156
|
]
|
|
157
|
+
if (appends) {
|
|
158
|
+
declarations.push(
|
|
159
|
+
types.variableDeclaration('const', [
|
|
160
|
+
types.variableDeclarator(
|
|
161
|
+
types.arrayPattern([
|
|
162
|
+
types.identifier(vars.isAppendingVar),
|
|
163
|
+
types.identifier(vars.setIsAppendingVar),
|
|
164
|
+
]),
|
|
165
|
+
types.callExpression(types.identifier('useState'), [types.booleanLiteral(false)])
|
|
166
|
+
),
|
|
167
|
+
])
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
return declarations
|
|
136
171
|
}
|
|
137
172
|
|
|
138
173
|
/**
|
|
@@ -146,11 +181,17 @@ export function buildLoadingStateDeclarations(vars: LoadingStateVars): types.Sta
|
|
|
146
181
|
* - `fetchData` missing, not memoized, or not an expression-bodied promise
|
|
147
182
|
* chain — see `findMemoizedFetchDataArrow`.
|
|
148
183
|
* - already wired — keeps a second pass over the same AST idempotent.
|
|
184
|
+
*
|
|
185
|
+
* `appendingRequest` makes it an appending list's: the expression telling,
|
|
186
|
+
* inside `fetchData`, whether the request about to be made only adds a page to
|
|
187
|
+
* the rows on show (see `isAppendingVar`). The caller declares the state with
|
|
188
|
+
* `buildLoadingStateDeclarations(vars, true)`.
|
|
149
189
|
*/
|
|
150
190
|
export function applyLoadingStateToDataProvider(
|
|
151
191
|
// tslint:disable-next-line:no-any
|
|
152
192
|
dataProvider: any,
|
|
153
|
-
vars: LoadingStateVars
|
|
193
|
+
vars: LoadingStateVars,
|
|
194
|
+
appendingRequest?: types.Expression
|
|
154
195
|
): boolean {
|
|
155
196
|
const attributes = dataProvider?.openingElement?.attributes
|
|
156
197
|
if (!Array.isArray(attributes)) {
|
|
@@ -172,16 +213,16 @@ export function applyLoadingStateToDataProvider(
|
|
|
172
213
|
}
|
|
173
214
|
|
|
174
215
|
if (fetchArrow.body.type === 'CallExpression') {
|
|
175
|
-
fetchArrow.body = buildTrackedFetchBody(fetchArrow.body, vars)
|
|
216
|
+
fetchArrow.body = buildTrackedFetchBody(fetchArrow.body, vars, appendingRequest)
|
|
176
217
|
} else if (fetchArrow.body.type === 'BlockStatement') {
|
|
177
|
-
if (!injectTrackingIntoCachedBody(fetchArrow.body, vars)) {
|
|
218
|
+
if (!injectTrackingIntoCachedBody(fetchArrow.body, vars, appendingRequest)) {
|
|
178
219
|
return false
|
|
179
220
|
}
|
|
180
221
|
} else {
|
|
181
222
|
return false
|
|
182
223
|
}
|
|
183
224
|
|
|
184
|
-
setPersistDataDuringLoading(attributes, vars)
|
|
225
|
+
setPersistDataDuringLoading(attributes, vars, !!appendingRequest)
|
|
185
226
|
|
|
186
227
|
return true
|
|
187
228
|
}
|
|
@@ -199,7 +240,11 @@ export function applyLoadingStateToDataProvider(
|
|
|
199
240
|
* Returns `false` when the body is not the shape this module knows how to wrap,
|
|
200
241
|
* so the caller emits no state declarations rather than half-wiring it.
|
|
201
242
|
*/
|
|
202
|
-
function injectTrackingIntoCachedBody(
|
|
243
|
+
function injectTrackingIntoCachedBody(
|
|
244
|
+
body: types.BlockStatement,
|
|
245
|
+
vars: LoadingStateVars,
|
|
246
|
+
appendingRequest?: types.Expression
|
|
247
|
+
): boolean {
|
|
203
248
|
// Idempotent: a second pass over the same AST must not double-count.
|
|
204
249
|
if (containsIdentifier(body, vars.setIsFetchingVar)) {
|
|
205
250
|
return false
|
|
@@ -210,7 +255,11 @@ function injectTrackingIntoCachedBody(body: types.BlockStatement, vars: LoadingS
|
|
|
210
255
|
return false
|
|
211
256
|
}
|
|
212
257
|
|
|
213
|
-
const tracked = buildTrackedFetchBody(
|
|
258
|
+
const tracked = buildTrackedFetchBody(
|
|
259
|
+
networkChain.statement.argument as types.Expression,
|
|
260
|
+
vars,
|
|
261
|
+
appendingRequest
|
|
262
|
+
)
|
|
214
263
|
body.body.splice(networkChain.index, 1, ...tracked.body)
|
|
215
264
|
markCacheHitAsNewest(body, vars)
|
|
216
265
|
|
|
@@ -363,10 +412,14 @@ function findMemoizedFetchDataArrow(
|
|
|
363
412
|
* `fetchExpression` is the `fetch(...).then(...).then(...)` chain the generator
|
|
364
413
|
* emitted, so `.finally` is always available on it — the caller only reaches
|
|
365
414
|
* here for a call-expression body.
|
|
415
|
+
*
|
|
416
|
+
* An appending list also records, as the flag goes up, whether this request
|
|
417
|
+
* only appends — `setDs_N_isAppending(<appendingRequest>)`, batched with it.
|
|
366
418
|
*/
|
|
367
419
|
function buildTrackedFetchBody(
|
|
368
420
|
fetchExpression: types.Expression,
|
|
369
|
-
vars: LoadingStateVars
|
|
421
|
+
vars: LoadingStateVars,
|
|
422
|
+
appendingRequest?: types.Expression
|
|
370
423
|
): types.BlockStatement {
|
|
371
424
|
const inFlightCount = types.memberExpression(
|
|
372
425
|
types.identifier(vars.inFlightRefVar),
|
|
@@ -456,6 +509,15 @@ function buildTrackedFetchBody(
|
|
|
456
509
|
types.numericLiteral(1)
|
|
457
510
|
)
|
|
458
511
|
),
|
|
512
|
+
...(appendingRequest
|
|
513
|
+
? [
|
|
514
|
+
types.expressionStatement(
|
|
515
|
+
types.callExpression(types.identifier(vars.setIsAppendingVar), [
|
|
516
|
+
types.cloneNode(appendingRequest, true),
|
|
517
|
+
])
|
|
518
|
+
),
|
|
519
|
+
]
|
|
520
|
+
: []),
|
|
459
521
|
types.expressionStatement(
|
|
460
522
|
types.callExpression(types.identifier(vars.setIsFetchingVar), [types.booleanLiteral(true)])
|
|
461
523
|
),
|
|
@@ -467,13 +529,23 @@ function buildTrackedFetchBody(
|
|
|
467
529
|
])
|
|
468
530
|
}
|
|
469
531
|
|
|
470
|
-
/**
|
|
471
|
-
|
|
472
|
-
|
|
532
|
+
/**
|
|
533
|
+
* `persistDataDuringLoading={!ds_N_isFetching}` — or, for an appending list,
|
|
534
|
+
* `{!ds_N_isFetching || ds_N_isAppending}` — replacing any existing value.
|
|
535
|
+
*/
|
|
536
|
+
function setPersistDataDuringLoading(
|
|
537
|
+
// tslint:disable-next-line:no-any
|
|
538
|
+
attributes: any[],
|
|
539
|
+
vars: LoadingStateVars,
|
|
540
|
+
appends: boolean
|
|
541
|
+
): void {
|
|
542
|
+
const notFetching = types.unaryExpression('!', types.identifier(vars.isFetchingVar), true)
|
|
473
543
|
const attribute = types.jsxAttribute(
|
|
474
544
|
types.jsxIdentifier('persistDataDuringLoading'),
|
|
475
545
|
types.jsxExpressionContainer(
|
|
476
|
-
|
|
546
|
+
appends
|
|
547
|
+
? types.logicalExpression('||', notFetching, types.identifier(vars.isAppendingVar))
|
|
548
|
+
: notFetching
|
|
477
549
|
)
|
|
478
550
|
)
|
|
479
551
|
|