Skip to content

Commit 414ebba

Browse files
committed
refactor: improve scroll prop architecture and consistency
1 parent d7d2db1 commit 414ebba

File tree

4 files changed

+51
-37
lines changed

4 files changed

+51
-37
lines changed

src/inertia.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ export class Inertia<Pages> {
253253
finalProps = { ...pageProps }
254254
}
255255

256-
let result
257256
if (requestInfo.partialComponent === component) {
258257
const only = requestInfo.onlyProps
259258
const except = requestInfo.exceptProps ?? []
@@ -267,30 +266,20 @@ export class Inertia<Pages> {
267266
debug('building props for a partial reload %O', requestInfo)
268267
debug('cherry picking props %s', cherryPickProps)
269268

270-
result = await buildPartialRequestProps(
269+
return buildPartialRequestProps(
271270
finalProps,
272271
cherryPickProps,
273-
this.ctx.containerResolver
272+
this.ctx.containerResolver,
273+
requestInfo.scrollMergeIntent
274274
)
275275
} else {
276276
debug('building props for a standard visit %O', requestInfo)
277-
result = await buildStandardVisitProps(finalProps, this.ctx.containerResolver)
278-
}
279-
280-
const prependProps: string[] = []
281-
const mergeProps: string[] = [...(result.mergeProps || [])]
282-
const scrollProps = result.scrollProps ?? {}
283-
for (const [key, propInfo] of Object.entries(scrollProps)) {
284-
const scrolPropPath = `${key}.${propInfo.wrapper}`
285-
286-
if (requestInfo.scrollMergeIntent === 'prepend') {
287-
prependProps.push(scrolPropPath)
288-
} else if (requestInfo.scrollMergeIntent === 'append') {
289-
mergeProps.push(scrolPropPath)
290-
}
277+
return buildStandardVisitProps(
278+
finalProps,
279+
this.ctx.containerResolver,
280+
requestInfo.scrollMergeIntent
281+
)
291282
}
292-
293-
return { ...result, mergeProps, prependProps }
294283
}
295284

296285
/**

src/props.ts

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,12 @@ export function isScrollProp<T extends UnPackedPageProps>(
387387
* Reads from the "metadata" key produced by AdonisJS transformers
388388
* rather than inspecting the raw paginator instance.
389389
*/
390-
function resolveScrollMetadata(serialized: any, pageName: string, wrapper: string): ScrollMetadata {
390+
function resolveScrollMetadata(serialized: any, pageName: string): ScrollMetadata {
391391
const meta = serialized?.metadata ?? {}
392392
const currentPage: number | null = meta.currentPage ?? null
393393
const lastPage: number | null = meta.lastPage ?? null
394394
return {
395395
pageName,
396-
wrapper,
397396
currentPage,
398397
nextPage:
399398
currentPage !== null && lastPage !== null && currentPage < lastPage ? currentPage + 1 : null,
@@ -441,13 +440,16 @@ async function unpackPropValue(
441440
*/
442441
export async function buildStandardVisitProps(
443442
pageProps: PageProps,
444-
containerResolver: ContainerResolver<any>
443+
containerResolver: ContainerResolver<any>,
444+
scrollMergeIntent?: string
445445
) {
446446
const mergeProps: string[] = []
447447
const deepMergeProps: string[] = []
448448
const newProps: ComponentProps = {}
449449
const deferredProps: { [group: string]: string[] } = {}
450+
const prependProps: string[] = []
450451
const scrollProps: { [key: string]: ScrollMetadata } = {}
452+
const scrollResolvers = new Map<string, (jsonValue: any) => void>()
451453
const unpackedValues: Array<{
452454
key: string
453455
value: UnPackedPageProps | (() => AsyncOrSync<UnPackedPageProps>)
@@ -485,6 +487,16 @@ export async function buildStandardVisitProps(
485487
* so that scrollProps metadata can be derived after serialization.
486488
*/
487489
if (isScrollProp(value)) {
490+
const scrollPath = `${key}.${value.wrapper}`
491+
492+
if (scrollMergeIntent === 'prepend') {
493+
prependProps.push(scrollPath)
494+
} else if (scrollMergeIntent === 'append') {
495+
mergeProps.push(scrollPath)
496+
}
497+
scrollResolvers.set(key, (jsonValue) => {
498+
scrollProps[key] = resolveScrollMetadata(jsonValue, value.pageName)
499+
})
488500
unpackedValues.push({ key, value: value.value })
489501
continue
490502
}
@@ -551,27 +563,28 @@ export async function buildStandardVisitProps(
551563
.then((r) => unpackPropValue(r, containerResolver))
552564
.then((jsonValue) => {
553565
newProps[key] = jsonValue
566+
if (scrollResolvers.has(key)) {
567+
scrollResolvers.get(key)!(jsonValue)
568+
}
554569
})
555570
} else {
556571
return unpackPropValue(value, containerResolver).then((jsonValue) => {
557572
newProps[key] = jsonValue
573+
if (scrollResolvers.has(key)) {
574+
scrollResolvers.get(key)!(jsonValue)
575+
}
558576
})
559577
}
560578
})
561579
)
562580

563-
for (const [key, value] of Object.entries(pageProps)) {
564-
if (isObject(value) && isScrollProp(value)) {
565-
scrollProps[key] = resolveScrollMetadata(newProps[key], value.pageName, value.wrapper)
566-
}
567-
}
568-
569581
return {
570582
props: newProps,
571583
mergeProps,
572584
deepMergeProps,
573585
deferredProps,
574586
scrollProps,
587+
prependProps,
575588
}
576589
}
577590

@@ -602,11 +615,14 @@ export async function buildStandardVisitProps(
602615
export async function buildPartialRequestProps(
603616
pageProps: PageProps,
604617
cherryPickProps: string[],
605-
containerResolver: ContainerResolver<any>
618+
containerResolver: ContainerResolver<any>,
619+
scrollMergeIntent?: string
606620
) {
607621
const mergeProps: string[] = []
608622
const deepMergeProps: string[] = []
623+
const prependProps: string[] = []
609624
const scrollProps: { [key: string]: ScrollMetadata } = {}
625+
const scrollResolvers = new Map<string, (jsonValue: any) => void>()
610626
const newProps: ComponentProps = {}
611627
const unpackedValues: Array<{
612628
key: string
@@ -651,6 +667,16 @@ export async function buildPartialRequestProps(
651667
* Unpack scroll prop value and track pageName for metadata derivation
652668
*/
653669
if (isScrollProp(value)) {
670+
const scrollPath = `${key}.${value.wrapper}`
671+
672+
if (scrollMergeIntent === 'prepend') {
673+
prependProps.push(scrollPath)
674+
} else if (scrollMergeIntent === 'append') {
675+
mergeProps.push(scrollPath)
676+
}
677+
scrollResolvers.set(key, (jsonValue) => {
678+
scrollProps[key] = resolveScrollMetadata(jsonValue, value.pageName)
679+
})
654680
unpackedValues.push({ key, value: value.value })
655681
continue
656682
}
@@ -708,26 +734,27 @@ export async function buildPartialRequestProps(
708734
.then((r) => unpackPropValue(r, containerResolver))
709735
.then((jsonValue) => {
710736
newProps[key] = jsonValue
737+
if (scrollResolvers.has(key)) {
738+
scrollResolvers.get(key)!(jsonValue)
739+
}
711740
})
712741
} else {
713742
return unpackPropValue(value, containerResolver).then((jsonValue) => {
714743
newProps[key] = jsonValue
744+
if (scrollResolvers.has(key)) {
745+
scrollResolvers.get(key)!(jsonValue)
746+
}
715747
})
716748
}
717749
})
718750
)
719751

720-
for (const [key, value] of Object.entries(pageProps)) {
721-
if (isObject(value) && isScrollProp(value)) {
722-
scrollProps[key] = resolveScrollMetadata(newProps[key], value.pageName, value.wrapper)
723-
}
724-
}
725-
726752
return {
727753
props: newProps,
728754
mergeProps,
729755
deepMergeProps,
730756
deferredProps: {},
731757
scrollProps,
758+
prependProps,
732759
}
733760
}

src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ export type ScrollProp<T extends UnPackedPageProps> = {
145145
*/
146146
export type ScrollMetadata = {
147147
pageName: string
148-
wrapper: string
149148
currentPage: number | null
150149
nextPage: number | null
151150
previousPage: number | null

tests/inertia_page.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2169,7 +2169,6 @@ test.group('Inertia.page | scroll helper', () => {
21692169
"nextPage": 2,
21702170
"pageName": "page",
21712171
"previousPage": null,
2172-
"wrapper": "data",
21732172
},
21742173
},
21752174
"url": "",

0 commit comments

Comments
 (0)