This repository was archived by the owner on Aug 9, 2024. It is now read-only.

Description
Case
Sometimes you need to calculate data for every record in KV
Worth mentioning:
- Calculated data might be anything. E.g. it can be a sum of two fields, some string based on certain conditions. Literally anything. So API should be low-level rather than something rigid like
createSwitch etc.
Solution
import { createListApi, createMapApi } from '@keyval/core'
type StatsWidgetState = {
widgetId: string;
ethValue: number;
txPerDay: number
}
const $statsWidgets = createListApi<StatsWidgetState>({
keygen: item => item.widgetId
})
const $roles = createMap($statsWidgets, {
fn: stats => {
if (stats.ethValue > 10) { return 'whale' }
if (stats.txPerDay > 10) { return 'day_trader' }
return 'stalling'
}
})
Also, there's an optional source field
const $roles = createMap($statsWidget, {
source: $preferredRole,
fn: (stats, preferredRole) => {
const roles = []
/* calculate roles */
if (roles.includes(preferredRole)) { return preferredRole }
return roles[0] ?? 'stalling'
}
})