Skip to content

Commit e72ce60

Browse files
authored
fix(frontend): tweak use-pagination behaviour
1 parent 59fc18f commit e72ce60

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

packages/frontend/src/composables/use-pagination.ts

+42-19
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ export type PagingCtx<E extends keyof Misskey.Endpoints = keyof Misskey.Endpoint
3535

3636
baseId?: MisskeyEntity['id'];
3737
direction?: 'newer' | 'older';
38+
39+
// 一部のAPIはさらに遡れる場合でもパフォーマンス上の理由でlimit以下の結果を返す場合があり、その場合はsafe、それ以外はlimitにすることを推奨
40+
canFetchDetection?: 'safe' | 'limit';
3841
};
3942

4043
export function usePagination<Endpoint extends keyof Misskey.Endpoints, T = Misskey.Endpoints[Endpoint]['res'] extends (infer I)[] ? I : never>(props: {
4144
ctx: PagingCtx<Endpoint>;
45+
autoInit?: boolean;
46+
autoReInit?: boolean;
4247
useShallowRef?: boolean;
4348
}) {
4449
const items = props.useShallowRef ? shallowRef<T[]>([]) : ref<T[]>([]);
@@ -49,8 +54,9 @@ export function usePagination<Endpoint extends keyof Misskey.Endpoints, T = Miss
4954
const canFetchOlder = ref(false);
5055
const error = ref(false);
5156

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+
}
5460

5561
function getNewestId(): string | null | undefined {
5662
// 様々な要因により並び順は保証されないのでソートが必要
@@ -92,12 +98,20 @@ export function usePagination<Endpoint extends keyof Misskey.Endpoints, T = Miss
9298
if (i === 3) item._shouldInsertAd_ = true;
9399
}
94100

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+
}
101115
}
102116

103117
error.value = false;
@@ -130,15 +144,22 @@ export function usePagination<Endpoint extends keyof Misskey.Endpoints, T = Miss
130144
if (i === 10) item._shouldInsertAd_ = true;
131145
}
132146

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+
}
140161
}
141-
}, err => {
162+
}).finally(() => {
142163
fetchingOlder.value = false;
143164
});
144165
}
@@ -232,9 +253,11 @@ export function usePagination<Endpoint extends keyof Misskey.Endpoints, T = Miss
232253
}
233254
}
234255

235-
onMounted(() => {
236-
init();
237-
});
256+
if (props.autoInit !== false) {
257+
onMounted(() => {
258+
init();
259+
});
260+
}
238261

239262
return {
240263
items: items as DeepReadonly<ShallowRef<T[]>>,

0 commit comments

Comments
 (0)