-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
initialTopMostItemIndexSystem.ts
72 lines (66 loc) · 2.52 KB
/
initialTopMostItemIndexSystem.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import * as u from './urx'
import { empty } from './AATree'
import { sizeSystem } from './sizeSystem'
import { domIOSystem } from './domIOSystem'
import { scrollToIndexSystem } from './scrollToIndexSystem'
import { propsReadySystem } from './propsReadySystem'
import { FlatIndexLocationWithAlign } from './interfaces'
import { skipFrames } from './utils/skipFrames'
export function getInitialTopMostItemIndexNumber(location: number | FlatIndexLocationWithAlign, totalCount: number): number {
const lastIndex = totalCount - 1
const index = typeof location === 'number' ? location : location.index === 'LAST' ? lastIndex : location.index
return index
}
export const initialTopMostItemIndexSystem = u.system(
([{ sizes, listRefresh, defaultItemSize }, { scrollTop }, { scrollToIndex, scrollTargetReached }, { didMount }]) => {
const scrolledToInitialItem = u.statefulStream(true)
const initialTopMostItemIndex = u.statefulStream<number | FlatIndexLocationWithAlign>(0)
const initialItemFinalLocationReached = u.statefulStream(true)
u.connect(
u.pipe(
didMount,
u.withLatestFrom(initialTopMostItemIndex),
u.filter(([_, location]) => !!location),
u.mapTo(false)
),
scrolledToInitialItem
)
u.connect(
u.pipe(
didMount,
u.withLatestFrom(initialTopMostItemIndex),
u.filter(([_, location]) => !!location),
u.mapTo(false)
),
initialItemFinalLocationReached
)
u.subscribe(
u.pipe(
u.combineLatest(listRefresh, didMount),
u.withLatestFrom(scrolledToInitialItem, sizes, defaultItemSize, initialItemFinalLocationReached),
u.filter(([[, didMount], scrolledToInitialItem, { sizeTree }, defaultItemSize, scrollScheduled]) => {
return didMount && (!empty(sizeTree) || u.isDefined(defaultItemSize)) && !scrolledToInitialItem && !scrollScheduled
}),
u.withLatestFrom(initialTopMostItemIndex)
),
([, initialTopMostItemIndex]) => {
u.handleNext(scrollTargetReached, () => {
u.publish(initialItemFinalLocationReached, true)
})
skipFrames(4, () => {
u.handleNext(scrollTop, () => {
u.publish(scrolledToInitialItem, true)
})
u.publish(scrollToIndex, initialTopMostItemIndex)
})
}
)
return {
scrolledToInitialItem,
initialTopMostItemIndex,
initialItemFinalLocationReached,
}
},
u.tup(sizeSystem, domIOSystem, scrollToIndexSystem, propsReadySystem),
{ singleton: true }
)