@@ -35,10 +35,15 @@ export type PagingCtx<E extends keyof Misskey.Endpoints = keyof Misskey.Endpoint
35
35
36
36
baseId ?: MisskeyEntity [ 'id' ] ;
37
37
direction ?: 'newer' | 'older' ;
38
+
39
+ // 一部のAPIはさらに遡れる場合でもパフォーマンス上の理由でlimit以下の結果を返す場合があり、その場合はsafe、それ以外はlimitにすることを推奨
40
+ canFetchDetection ?: 'safe' | 'limit' ;
38
41
} ;
39
42
40
43
export function usePagination < Endpoint extends keyof Misskey . Endpoints , T = Misskey . Endpoints [ Endpoint ] [ 'res' ] extends ( infer I ) [ ] ? I : never > ( props : {
41
44
ctx : PagingCtx < Endpoint > ;
45
+ autoInit ?: boolean ;
46
+ autoReInit ?: boolean ;
42
47
useShallowRef ?: boolean ;
43
48
} ) {
44
49
const items = props . useShallowRef ? shallowRef < T [ ] > ( [ ] ) : ref < T [ ] > ( [ ] ) ;
@@ -49,8 +54,9 @@ export function usePagination<Endpoint extends keyof Misskey.Endpoints, T = Miss
49
54
const canFetchOlder = ref ( false ) ;
50
55
const error = ref ( false ) ;
51
56
52
- // パラメータに何らかの変更があった際、再読込したい(チャンネル等のIDが変わったなど)
53
- watch ( ( ) => [ props . ctx . endpoint , props . ctx . params ] , init , { deep : true } ) ;
57
+ if ( props . autoReInit !== false ) {
58
+ watch ( ( ) => [ props . ctx . endpoint , props . ctx . params ] , init , { deep : true } ) ;
59
+ }
54
60
55
61
function getNewestId ( ) : string | null | undefined {
56
62
// 様々な要因により並び順は保証されないのでソートが必要
@@ -92,12 +98,20 @@ export function usePagination<Endpoint extends keyof Misskey.Endpoints, T = Miss
92
98
if ( i === 3 ) item . _shouldInsertAd_ = true ;
93
99
}
94
100
95
- if ( res . length === 0 || props . ctx . noPaging ) {
96
- pushItems ( res ) ;
97
- canFetchOlder . value = false ;
98
- } else {
99
- pushItems ( res ) ;
100
- canFetchOlder . value = true ;
101
+ pushItems ( res ) ;
102
+
103
+ if ( props . ctx . canFetchDetection === 'limit' ) {
104
+ if ( res . length < FIRST_FETCH_LIMIT ) {
105
+ canFetchOlder . value = false ;
106
+ } else {
107
+ canFetchOlder . value = true ;
108
+ }
109
+ } else if ( props . ctx . canFetchDetection === 'safe' || props . ctx . canFetchDetection == null ) {
110
+ if ( res . length === 0 || props . ctx . noPaging ) {
111
+ canFetchOlder . value = false ;
112
+ } else {
113
+ canFetchOlder . value = true ;
114
+ }
101
115
}
102
116
103
117
error . value = false ;
@@ -130,15 +144,22 @@ export function usePagination<Endpoint extends keyof Misskey.Endpoints, T = Miss
130
144
if ( i === 10 ) item . _shouldInsertAd_ = true ;
131
145
}
132
146
133
- if ( res . length === 0 ) {
134
- canFetchOlder . value = false ;
135
- fetchingOlder . value = false ;
136
- } else {
137
- pushItems ( res ) ;
138
- canFetchOlder . value = true ;
139
- fetchingOlder . value = false ;
147
+ pushItems ( res ) ;
148
+
149
+ if ( props . ctx . canFetchDetection === 'limit' ) {
150
+ if ( res . length < FIRST_FETCH_LIMIT ) {
151
+ canFetchOlder . value = false ;
152
+ } else {
153
+ canFetchOlder . value = true ;
154
+ }
155
+ } else if ( props . ctx . canFetchDetection === 'safe' || props . ctx . canFetchDetection == null ) {
156
+ if ( res . length === 0 ) {
157
+ canFetchOlder . value = false ;
158
+ } else {
159
+ canFetchOlder . value = true ;
160
+ }
140
161
}
141
- } , err => {
162
+ } ) . finally ( ( ) => {
142
163
fetchingOlder . value = false ;
143
164
} ) ;
144
165
}
@@ -232,9 +253,11 @@ export function usePagination<Endpoint extends keyof Misskey.Endpoints, T = Miss
232
253
}
233
254
}
234
255
235
- onMounted ( ( ) => {
236
- init ( ) ;
237
- } ) ;
256
+ if ( props . autoInit !== false ) {
257
+ onMounted ( ( ) => {
258
+ init ( ) ;
259
+ } ) ;
260
+ }
238
261
239
262
return {
240
263
items : items as DeepReadonly < ShallowRef < T [ ] > > ,
0 commit comments