|
1 | 1 | import fetchJSON from '../../utils/fetchJSON';
|
2 | 2 | import { parseResponse } from '../schemas/error';
|
3 |
| -import { SmesherStatesResponseSchema } from '../schemas/smesherStates'; |
| 3 | +import { |
| 4 | + IdentityStateInfo, |
| 5 | + SmesherStatesResponseSchema, |
| 6 | +} from '../schemas/smesherStates'; |
| 7 | +import SortOrder from '../sortOrder'; |
4 | 8 |
|
5 |
| -// eslint-disable-next-line import/prefer-default-export |
6 |
| -export const fetchSmesherStates = (rpc: string, limit = 100) => { |
7 |
| - const params = new URLSearchParams({ limit: limit.toString() }); |
| 9 | +const PER_PAGE = 100; |
| 10 | + |
| 11 | +export const fetchSmesherStatesChunk = ( |
| 12 | + rpc: string, |
| 13 | + limit = PER_PAGE, |
| 14 | + order = SortOrder.DESC, |
| 15 | + to = new Date(), |
| 16 | + from: Date | undefined = undefined |
| 17 | +) => { |
| 18 | + const params = new URLSearchParams({ |
| 19 | + limit: limit.toString(), |
| 20 | + order: order.toString(), |
| 21 | + to: to.toISOString(), |
| 22 | + ...(from ? { from: from.toISOString() } : {}), |
| 23 | + }); |
8 | 24 | return fetchJSON(
|
9 | 25 | `${rpc}/spacemesh.v2beta1.SmeshingIdentitiesService/States?${params}`,
|
10 | 26 | {
|
11 | 27 | method: 'GET',
|
12 | 28 | }
|
13 | 29 | )
|
14 | 30 | .then(parseResponse(SmesherStatesResponseSchema))
|
15 |
| - .then((res) => res.identities); |
| 31 | + .then((res) => res.states); |
| 32 | +}; |
| 33 | + |
| 34 | +export type SmesherStates = IdentityStateInfo[]; |
| 35 | + |
| 36 | +export type SmesherStatesSetter = (states: SmesherStates) => void; |
| 37 | +export const fetchSmesherStatesWithCallback = (setter: SmesherStatesSetter) => { |
| 38 | + let isInProcess = false; |
| 39 | + // Datetime range of oldest and newest fetched states |
| 40 | + // Used to fetch all events within the specified range |
| 41 | + let fetched: [Date, Date] = [new Date(), new Date()]; |
| 42 | + |
| 43 | + return async ( |
| 44 | + rpc: string, |
| 45 | + order = SortOrder.ASC, |
| 46 | + to = new Date(), |
| 47 | + from: Date | undefined = undefined |
| 48 | + ) => { |
| 49 | + if (isInProcess) return; |
| 50 | + |
| 51 | + const fetchNext = async (chunkTo: Date, chunkFrom?: Date) => { |
| 52 | + const res = await fetchSmesherStatesChunk( |
| 53 | + rpc, |
| 54 | + PER_PAGE, |
| 55 | + order, |
| 56 | + chunkTo, |
| 57 | + chunkFrom |
| 58 | + ); |
| 59 | + const len = res.length; |
| 60 | + |
| 61 | + const first = res[0]; |
| 62 | + const last = res[len - 1]; |
| 63 | + if (!first?.time) { |
| 64 | + throw new Error( |
| 65 | + `Smesher event (first) supposed to have timestamp: ${JSON.stringify( |
| 66 | + first |
| 67 | + )}` |
| 68 | + ); |
| 69 | + } |
| 70 | + if (!last?.time) { |
| 71 | + throw new Error( |
| 72 | + `Smesher event (last) supposed to have timestamp: ${JSON.stringify( |
| 73 | + last |
| 74 | + )}` |
| 75 | + ); |
| 76 | + } |
| 77 | + fetched = |
| 78 | + order === SortOrder.ASC |
| 79 | + ? [new Date(first.time), new Date(last.time)] |
| 80 | + : [new Date(last.time), new Date(first.time)]; |
| 81 | + |
| 82 | + setter(res); |
| 83 | + if (len === PER_PAGE) { |
| 84 | + if (order === SortOrder.ASC) { |
| 85 | + await fetchNext(to, fetched[1]); |
| 86 | + } else { |
| 87 | + await fetchNext(fetched[0], from); |
| 88 | + } |
| 89 | + } else { |
| 90 | + isInProcess = false; |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + isInProcess = true; |
| 95 | + await fetchNext(to, from); |
| 96 | + }; |
16 | 97 | };
|
0 commit comments