-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KEEP-1063: handle data-entries limit
- Loading branch information
1 parent
46871e7
commit d0facd5
Showing
6 changed files
with
107 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function* chunk<T>(array: T[], size: number) { | ||
for (let i = 0; i < array.length; i += size) { | ||
yield array.slice(i, i + size); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import invariant from 'tiny-invariant'; | ||
|
||
import { chunk } from './chunk'; | ||
|
||
export async function fetchInBatches<I, T>( | ||
allItems: I[], | ||
chunkSize: number, | ||
fetchFn: (items: I[]) => Promise<T[]> | ||
) { | ||
const result: T[] = []; | ||
|
||
for (const items of chunk(allItems, chunkSize)) { | ||
const data = await fetchFn(items); | ||
invariant(Array.isArray(data)); | ||
|
||
result.push(...data); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,3 @@ | ||
import { DataTransactionEntry } from '@waves/ts-types'; | ||
|
||
export function capitalize(str: string | undefined) { | ||
return str ? str.charAt(0).toUpperCase() + str.slice(1) : str; | ||
} | ||
|
||
export function reduceDataEntries(entries: DataTransactionEntry[]) { | ||
return entries.reduce<Record<string, DataTransactionEntry['value']>>( | ||
(data, item) => { | ||
data[item.key] = item.value; | ||
return data; | ||
}, | ||
{} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { DataTransactionEntry } from '@waves/ts-types'; | ||
|
||
import { fetchInBatches } from '../_core/fetchInBatches'; | ||
|
||
export function dataEntriesToRecord<T extends DataTransactionEntry>( | ||
entries: T[] | ||
) { | ||
return entries.reduce<Record<string, T['value']>>((data, item) => { | ||
data[item.key] = item.value; | ||
return data; | ||
}, {}); | ||
} | ||
|
||
const NODE_DATA_KEYS_REQUEST_LIMIT = 1000; | ||
|
||
export function fetchDataEntries<T extends DataTransactionEntry>( | ||
url: string, | ||
allKeys: string[] | ||
) { | ||
return fetchInBatches(allKeys, NODE_DATA_KEYS_REQUEST_LIMIT, keys => | ||
fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ keys }), | ||
}).then( | ||
(response): Promise<T[]> => | ||
response.ok | ||
? response.json() | ||
: response.text().then(text => Promise.reject(new Error(text))) | ||
) | ||
); | ||
} |