-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
initialTopMostItemIndexSystem.ts
48 lines (43 loc) · 1.4 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
import * as u from '@virtuoso.dev/urx'
import { empty } from './AATree'
import { sizeSystem } from './sizeSystem'
import { domIOSystem } from './domIOSystem'
import { scrollToIndexSystem } from './scrollToIndexSystem'
import { propsReadySystem } from './propsReadySystem'
export const initialTopMostItemIndexSystem = u.system(
([{ sizes, listRefresh }, { scrollTop }, { scrollToIndex }, { didMount }]) => {
const scrolledToInitialItem = u.statefulStream(true)
const initialTopMostItemIndex = u.statefulStream(0)
u.connect(
u.pipe(
didMount,
u.withLatestFrom(initialTopMostItemIndex),
u.filter(([_, index]) => index !== 0),
u.mapTo(false)
),
scrolledToInitialItem
)
u.subscribe(
u.pipe(
listRefresh,
u.withLatestFrom(scrolledToInitialItem, sizes),
u.filter(([, scrolledToInitialItem, { sizeTree }]) => {
return !empty(sizeTree) && !scrolledToInitialItem
}),
u.withLatestFrom(initialTopMostItemIndex)
),
([, initialTopMostItemIndex]) => {
u.handleNext(scrollTop, () => {
u.publish(scrolledToInitialItem, true)
})
u.publish(scrollToIndex, initialTopMostItemIndex)
}
)
return {
scrolledToInitialItem,
initialTopMostItemIndex,
}
},
u.tup(sizeSystem, domIOSystem, scrollToIndexSystem, propsReadySystem),
{ singleton: true }
)