-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
scrollSeekSystem.ts
44 lines (40 loc) · 1.45 KB
/
scrollSeekSystem.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
import * as u from './urx'
import { ListRange } from './interfaces'
import { stateFlagsSystem } from './stateFlagsSystem'
import { ScrollSeekConfiguration } from './interfaces'
export const scrollSeekSystem = u.system(
([{ scrollVelocity }]) => {
const isSeeking = u.statefulStream(false)
const rangeChanged = u.stream<ListRange>()
const scrollSeekConfiguration = u.statefulStream<ScrollSeekConfiguration | undefined | false>(false)
u.connect(
u.pipe(
scrollVelocity,
u.withLatestFrom(scrollSeekConfiguration, isSeeking, rangeChanged),
u.filter(([_, config]) => !!config),
u.map(([speed, config, isSeeking, range]) => {
const { exit, enter } = config as ScrollSeekConfiguration
if (isSeeking) {
if (exit(speed, range)) {
return false
}
} else {
if (enter(speed, range)) {
return true
}
}
return isSeeking
}),
u.distinctUntilChanged()
),
isSeeking
)
u.subscribe(
u.pipe(u.combineLatest(isSeeking, scrollVelocity, rangeChanged), u.withLatestFrom(scrollSeekConfiguration)),
([[isSeeking, velocity, range], config]) => isSeeking && config && config.change && config.change(velocity, range)
)
return { isSeeking, scrollSeekConfiguration, scrollVelocity, scrollSeekRangeChanged: rangeChanged }
},
u.tup(stateFlagsSystem),
{ singleton: true }
)