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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { ref, shallowRef, triggerRef } from 'vue';
import * as Misskey from 'misskey-js';
import type { ComputedRef, Ref, ShallowRef, UnwrapRef } from 'vue';
import { misskeyApi } from '@/utility/misskey-api.js';
const MAX_ITEMS = 30;
const MAX_QUEUE_ITEMS = 100;
const FIRST_FETCH_LIMIT = 15;
const SECOND_FETCH_LIMIT = 30;
export type MisskeyEntity = {
id: string;
createdAt: string;
_shouldInsertAd_?: boolean;
};
type AbsEndpointType = {
req: unknown;
res: unknown;
};
type FilterByEpRes<E extends Record<string, AbsEndpointType>> = {
[K in keyof E]: E[K]['res'] extends Array<{ id: string }> ? K : never
}[keyof E];
export type PaginatorCompatibleEndpointPaths = FilterByEpRes<Misskey.Endpoints>;
export type PaginatorCompatibleEndpoints = {
[K in PaginatorCompatibleEndpointPaths]: Misskey.Endpoints[K];
};
export type ExtractorFunction<P extends IPaginator, T> = (item: UnwrapRef<P['items']>[number]) => T;
export interface IPaginator<T = unknown, _T = T & MisskeyEntity> {
/**
* 外部から直接操作しないでください
*/
items: Ref<_T[]> | ShallowRef<_T[]>;
queuedAheadItemsCount: Ref<number>;
fetching: Ref<boolean>;
fetchingOlder: Ref<boolean>;
fetchingNewer: Ref<boolean>;
canFetchOlder: Ref<boolean>;
canFetchNewer: Ref<boolean>;
canSearch: boolean;
error: Ref<boolean>;
computedParams: ComputedRef<Misskey.Endpoints[PaginatorCompatibleEndpointPaths]['req'] | null | undefined> | null;
initialId: MisskeyEntity['id'] | null;
initialDate: number | null;
initialDirection: 'newer' | 'older';
noPaging: boolean;
searchQuery: Ref<null | string>;
order: Ref<'newest' | 'oldest'>;
init(): Promise<void>;
reload(): Promise<void>;
fetchOlder(): Promise<void>;
fetchNewer(options?: { toQueue?: boolean }): Promise<void>;
trim(trigger?: boolean): void;
unshiftItems(newItems: (_T)[]): void;
pushItems(oldItems: (_T)[]): void;
prepend(item: _T): void;
enqueue(item: _T): void;
releaseQueue(): void;
removeItem(id: string): void;
updateItem(id: string, updater: (item: _T) => _T): void;
}
export class Paginator<
Endpoint extends PaginatorCompatibleEndpointPaths,
E extends PaginatorCompatibleEndpoints[Endpoint] = PaginatorCompatibleEndpoints[Endpoint],
T extends E['res'][number] & MisskeyEntity = E['res'][number] & MisskeyEntity,
SRef extends boolean = false,
> implements IPaginator {
/**
* 外部から直接操作しないでください
*/
public items: SRef extends true ? ShallowRef<T[]> : Ref<T[]>;
public queuedAheadItemsCount = ref(0);
public fetching = ref(true);
public fetchingOlder = ref(false);
public fetchingNewer = ref(false);
public canFetchOlder = ref(false);
public canFetchNewer = ref(false);
public canSearch = false;
public error = ref(false);
private endpoint: Endpoint;
private limit: number;
private params: E['req'] | (() => E['req']);
public computedParams: ComputedRef<E['req'] | null | undefined> | null;
public initialId: MisskeyEntity['id'] | null = null;
public initialDate: number | null = null;
// 初回読み込み時、initialIdを基準にそれより新しいものを取得するか古いものを取得するか
// newer: initialIdより新しいものを取得する
// older: initialIdより古いものを取得する (default)
public initialDirection: 'newer' | 'older';
private offsetMode: boolean;
public noPaging: boolean;
public searchQuery = ref<null | string>('');
private searchParamName: keyof E['req'] | 'search';
private canFetchDetection: 'safe' | 'limit' | null = null;
private aheadQueue: T[] = [];
private useShallowRef: SRef;
// 配列内の要素をどのような順序で並べるか
// newest: 新しいものが先頭 (default)
// oldest: 古いものが先頭
// NOTE: このようなプロパティを用意してこっち側で並びを管理せずに、Setで持っておき参照者側が好きに並び変えるような設計の方がすっきりしそうなものの、Vueのレンダリングのたびに並び替え処理が発生することになったりしそうでパフォーマンス上の懸念がある
public order: Ref<'newest' | 'oldest'>;
constructor(endpoint: Endpoint, props: {
limit?: number;
params?: E['req'] | (() => E['req']);
computedParams?: ComputedRef<E['req'] | null | undefined>;
/**
* 検索APIのような、ページング不可なエンドポイントを利用する場合
* (そのようなAPIをこの関数で使うのは若干矛盾してるけど)
*/
noPaging?: boolean;
offsetMode?: boolean;
initialId?: MisskeyEntity['id'];
initialDate?: number | null;
initialDirection?: 'newer' | 'older';
order?: 'newest' | 'oldest';
// 一部のAPIはさらに遡れる場合でもパフォーマンス上の理由でlimit以下の結果を返す場合があり、その場合はsafe、それ以外はlimitにすることを推奨
canFetchDetection?: 'safe' | 'limit';
useShallowRef?: SRef;
canSearch?: boolean;
searchParamName?: keyof E['req'];
}) {
this.endpoint = endpoint;
this.useShallowRef = (props.useShallowRef ?? false) as SRef;
if (this.useShallowRef) {
this.items = shallowRef<T[]>([]);
} else {
this.items = ref<T[]>([]) as Ref<T[]>;
}
this.limit = props.limit ?? FIRST_FETCH_LIMIT;
this.params = props.params ?? {};
this.computedParams = props.computedParams ?? null;
this.order = ref(props.order ?? 'newest');
this.initialId = props.initialId ?? null;
this.initialDate = props.initialDate ?? null;
this.initialDirection = props.initialDirection ?? 'older';
this.canFetchDetection = props.canFetchDetection ?? null;
this.noPaging = props.noPaging ?? false;
this.offsetMode = props.offsetMode ?? false;
this.canSearch = props.canSearch ?? false;
this.searchParamName = props.searchParamName ?? 'search';
this.getNewestId = this.getNewestId.bind(this);
this.getOldestId = this.getOldestId.bind(this);
this.init = this.init.bind(this);
this.reload = this.reload.bind(this);
this.fetchOlder = this.fetchOlder.bind(this);
this.fetchNewer = this.fetchNewer.bind(this);
this.unshiftItems = this.unshiftItems.bind(this);
this.pushItems = this.pushItems.bind(this);
this.prepend = this.prepend.bind(this);
this.enqueue = this.enqueue.bind(this);
this.releaseQueue = this.releaseQueue.bind(this);
this.removeItem = this.removeItem.bind(this);
this.updateItem = this.updateItem.bind(this);
}
private getNewestId(): string | null | undefined {
// 様々な要因により並び順は保証されないのでソートが必要
if (this.aheadQueue.length > 0) {
return this.aheadQueue.map(x => x.id).sort().at(-1);
}
return this.items.value.map(x => x.id).sort().at(-1);
}
private getOldestId(): string | null | undefined {
// 様々な要因により並び順は保証されないのでソートが必要
return this.items.value.map(x => x.id).sort().at(0);
}
public async init(): Promise<void> {
this.items.value = [];
this.aheadQueue = [];
this.queuedAheadItemsCount.value = 0;
this.fetching.value = true;
const data: E['req'] = {
...(typeof this.params === 'function' ? this.params() : this.params),
...(this.computedParams ? this.computedParams.value : {}),
...(this.searchQuery.value != null && this.searchQuery.value.trim() !== '' ? { [this.searchParamName]: this.searchQuery.value } : {}),
limit: this.limit ?? FIRST_FETCH_LIMIT,
allowPartial: true,
...((this.initialId == null && this.initialDate == null) && this.initialDirection === 'newer' ? {
sinceId: '0',
} : this.initialDirection === 'newer' ? {
sinceId: this.initialId ?? undefined,
sinceDate: this.initialDate ?? undefined,
} : (this.initialId || this.initialDate) && this.initialDirection === 'older' ? {
untilId: this.initialId ?? undefined,
untilDate: this.initialDate ?? undefined,
} : {}),
};
const apiRes = (await misskeyApi(this.endpoint, data).catch(_ => {
this.error.value = true;
this.fetching.value = false;
return null;
})) as T[] | null;
if (apiRes == null) {
return;
}
// 逆順で返ってくるので
if ((this.initialId || this.initialDate) && this.initialDirection === 'newer') {
apiRes.reverse();
}
for (let i = 0; i < apiRes.length; i++) {
const item = apiRes[i];
if (i === 3) item._shouldInsertAd_ = true;
}
this.pushItems(apiRes);
if (this.canFetchDetection === 'limit') {
if (apiRes.length < FIRST_FETCH_LIMIT) {
(this.initialDirection === 'older' ? this.canFetchOlder : this.canFetchNewer).value = false;
} else {
(this.initialDirection === 'older' ? this.canFetchOlder : this.canFetchNewer).value = true;
}
} else if (this.canFetchDetection === 'safe' || this.canFetchDetection == null) {
if (apiRes.length === 0 || this.noPaging) {
(this.initialDirection === 'older' ? this.canFetchOlder : this.canFetchNewer).value = false;
} else {
(this.initialDirection === 'older' ? this.canFetchOlder : this.canFetchNewer).value = true;
}
}
this.error.value = false;
this.fetching.value = false;
}
public reload(): Promise<void> {
return this.init();
}
public async fetchOlder(): Promise<void> {
if (!this.canFetchOlder.value || this.fetching.value || this.fetchingOlder.value || this.items.value.length === 0) return;
this.fetchingOlder.value = true;
const data: E['req'] = {
...(typeof this.params === 'function' ? this.params() : this.params),
...(this.computedParams ? this.computedParams.value : {}),
...(this.searchQuery.value != null && this.searchQuery.value.trim() !== '' ? { [this.searchParamName]: this.searchQuery.value } : {}),
limit: SECOND_FETCH_LIMIT,
...(this.offsetMode ? {
offset: this.items.value.length,
} : {
untilId: this.getOldestId(),
}),
};
const apiRes = (await misskeyApi<T[]>(this.endpoint, data).catch(_ => {
return null;
})) as T[] | null;
this.fetchingOlder.value = false;
if (apiRes == null) {
return;
}
for (let i = 0; i < apiRes.length; i++) {
const item = apiRes[i];
if (i === 10) item._shouldInsertAd_ = true;
}
if (this.order.value === 'oldest') {
this.unshiftItems(apiRes.toReversed(), false);
} else {
this.pushItems(apiRes);
}
if (this.canFetchDetection === 'limit') {
if (apiRes.length < FIRST_FETCH_LIMIT) {
this.canFetchOlder.value = false;
} else {
this.canFetchOlder.value = true;
}
} else if (this.canFetchDetection === 'safe' || this.canFetchDetection == null) {
if (apiRes.length === 0) {
this.canFetchOlder.value = false;
} else {
this.canFetchOlder.value = true;
}
}
}
public async fetchNewer(options: {
toQueue?: boolean;
} = {}): Promise<void> {
this.fetchingNewer.value = true;
const data: E['req'] = {
...(typeof this.params === 'function' ? this.params() : this.params),
...(this.computedParams ? this.computedParams.value : {}),
...(this.searchQuery.value != null && this.searchQuery.value.trim() !== '' ? { [this.searchParamName]: this.searchQuery.value } : {}),
limit: SECOND_FETCH_LIMIT,
...(this.offsetMode ? {
offset: this.items.value.length,
} : {
sinceId: this.getNewestId(),
}),
};
const apiRes = (await misskeyApi<T[]>(this.endpoint, data).catch(_ => {
return null;
})) as T[] | null;
this.fetchingNewer.value = false;
if (apiRes == null || apiRes.length === 0) {
this.canFetchNewer.value = false;
// 余計なre-renderを防止するためここで終了
return;
}
if (options.toQueue) {
this.aheadQueue.unshift(...apiRes.toReversed());
if (this.aheadQueue.length > MAX_QUEUE_ITEMS) {
this.aheadQueue = this.aheadQueue.slice(0, MAX_QUEUE_ITEMS);
}
this.queuedAheadItemsCount.value = this.aheadQueue.length;
} else {
if (this.order.value === 'oldest') {
this.pushItems(apiRes);
} else {
this.unshiftItems(apiRes.toReversed(), false);
}
}
if (this.canFetchDetection === 'limit') {
if (apiRes.length < FIRST_FETCH_LIMIT) {
this.canFetchNewer.value = false;
} else {
this.canFetchNewer.value = true;
}
}
// canFetchDetectionが'safe'の場合・apiRes.length === 0 の場合は apiRes.length === 0 の場合に canFetchNewer.value = false になるが、
// 余計な re-render を防ぐために上部で処理している。そのため、ここでは何もしない
}
public trim(trigger = true): void {
if (this.items.value.length >= MAX_ITEMS) this.canFetchOlder.value = true;
this.items.value = this.items.value.slice(0, MAX_ITEMS);
if (this.useShallowRef && trigger) triggerRef(this.items);
}
public unshiftItems(newItems: T[], trim = true): void {
if (newItems.length === 0) return; // これやらないと余計なre-renderが走る
this.items.value.unshift(...newItems.filter(x => !this.items.value.some(y => y.id === x.id))); // ストリーミングやポーリングのタイミングによっては重複することがあるため
if (trim) this.trim(true);
if (this.useShallowRef) triggerRef(this.items);
}
public pushItems(oldItems: T[]): void {
if (oldItems.length === 0) return; // これやらないと余計なre-renderが走る
this.items.value.push(...oldItems);
if (this.useShallowRef) triggerRef(this.items);
}
public prepend(item: T): void {
if (this.items.value.some(x => x.id === item.id)) return;
this.items.value.unshift(item);
this.trim(false);
if (this.useShallowRef) triggerRef(this.items);
}
public enqueue(item: T): void {
this.aheadQueue.unshift(item);
if (this.aheadQueue.length > MAX_QUEUE_ITEMS) {
this.aheadQueue.pop();
}
this.queuedAheadItemsCount.value = this.aheadQueue.length;
}
public releaseQueue(): void {
if (this.aheadQueue.length === 0) return; // これやらないと余計なre-renderが走る
this.unshiftItems(this.aheadQueue);
this.aheadQueue = [];
this.queuedAheadItemsCount.value = 0;
}
public removeItem(id: string): void {
// TODO: queueからも消す
const index = this.items.value.findIndex(x => x.id === id);
if (index !== -1) {
this.items.value.splice(index, 1);
if (this.useShallowRef) triggerRef(this.items);
}
}
public updateItem(id: string, updater: (item: T) => T): void {
// TODO: queueのも更新
const index = this.items.value.findIndex(x => x.id === id);
if (index !== -1) {
const item = this.items.value[index]!;
this.items.value[index] = updater(item);
if (this.useShallowRef) triggerRef(this.items);
}
}
}
|