40
40
</template >
41
41
42
42
<script lang="ts">
43
- import { defineComponent , onMounted , PropType , ref } from " vue" ;
44
- import { fromEvent , fromEventPattern , Observable } from " rxjs" ;
45
- import { pluck , share , switchMapTo } from " rxjs/operators" ;
43
+ import { defineComponent , PropType , ref } from " vue" ;
46
44
import { useObservable } from " @vueuse/rxjs" ;
47
- import { fromProp , fromResizeObserver } from " ./utilites" ;
48
- import { InternalItem , PageProvider , pipeline } from " ./pipeline" ;
45
+ import { fromProp , fromResizeObserver , fromWindowScroll } from " ./utilites" ;
46
+ import { PageProvider , pipeline } from " ./pipeline" ;
49
47
50
48
export default defineComponent ({
51
49
name: " Grid" ,
@@ -69,62 +67,33 @@ export default defineComponent({
69
67
},
70
68
},
71
69
setup(props ) {
72
- // region: props
73
- const length$ = fromProp (props , " length" );
74
- const pageSize$ = fromProp (props , " pageSize" );
75
- const pageProvider$ = fromProp (props , " pageProvider" );
76
- // endregion
77
-
78
- // region: refs
70
+ // template refs
79
71
const rootRef = ref <Element >(document .createElement (" div" ));
80
72
const probeRef = ref <Element >(document .createElement (" div" ));
81
- // endregion
82
-
83
- // region: rendering triggers
84
- // a stream of root elements when scrolling
85
- // @ts-expect-error Rxjs has a typing bug on fromEvent() with resultSelector
86
- // which is fixed in https://github.com/ReactiveX/rxjs/pull/6447
87
- const scroll$: Observable <Element > = fromEventPattern (onMounted ).pipe (
88
- // use share() to push the "mounted" event from vue, instead of pulling:
89
- share (),
90
- switchMapTo (
91
- fromEvent <UIEvent , Element >(
92
- window ,
93
- " scroll" ,
94
- { passive: true , capture: true },
95
- () => rootRef .value
96
- )
97
- )
98
- );
99
-
100
- // a stream of root elements when it is resized
101
- const rootResize$: Observable <Element > = fromResizeObserver (rootRef ).pipe (
102
- pluck (" target" )
103
- );
104
-
105
- // a stream of item size measurements when it is changed
106
- const itemRect$: Observable <DOMRectReadOnly > = fromResizeObserver (
107
- probeRef
108
- ).pipe (pluck (" contentRect" ));
109
- // endregion
110
73
111
- // region: data to render
74
+ // data to render
112
75
const {
113
76
buffer$, // the items in the current scanning window
114
77
contentHeight$, // the height of the whole list
115
- } = pipeline (
116
- itemRect$ ,
117
- length$ ,
118
- pageProvider$ ,
119
- pageSize$ ,
120
- rootResize$ ,
121
- scroll$
122
- );
123
- const buffer = useObservable <InternalItem []>(buffer$ );
124
- const contentHeight = useObservable <number >(contentHeight$ );
125
- // endregion
78
+ } = pipeline ({
79
+ // streams of prop
80
+ length$: fromProp (props , " length" ),
81
+ pageProvider$: fromProp (props , " pageProvider" ),
82
+ pageSize$: fromProp (props , " pageSize" ),
83
+ // a stream of item size measurements when it is changed
84
+ itemRect$: fromResizeObserver (probeRef , " contentRect" ),
85
+ // a stream of root elements when it is resized
86
+ rootResize$: fromResizeObserver (rootRef , " target" ),
87
+ // a stream of root elements when scrolling
88
+ scroll$: fromWindowScroll (() => rootRef .value ),
89
+ });
126
90
127
- return { rootRef , probeRef , buffer , contentHeight };
91
+ return {
92
+ rootRef ,
93
+ probeRef ,
94
+ buffer: useObservable (buffer$ ),
95
+ contentHeight: useObservable (contentHeight$ ),
96
+ };
128
97
},
129
98
});
130
99
</script >
0 commit comments