Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

RFC: grabItems (naming wanted) #24

Open
Kelin2025 opened this issue Jul 7, 2023 · 1 comment
Open

RFC: grabItems (naming wanted) #24

Kelin2025 opened this issue Jul 7, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@Kelin2025
Copy link
Member

Case

Sometimes you need to gather data from multiple KVs based on one

Basically like JOIN from SQL

Solution

export const $txWidget = grabItems(Widget.model.$kv, {
  source: {
    deposit: Deposit.model.$kv,
    withdraw: Withdraw.model.$kv,
    earn: Earn.model.$kv,
  },
  filter: {
    deposit: (deposit, widget) => widget.txHashes.includes(deposit.tx_hash),
    withdraw: (deposit, widget) => widget.txHashes.includes(deposit.tx_hash),
    earn:(deposit, widget) => widget.txHashes.includes(deposit.tx_hash),
  },
  orderBy: (a, b, widgetState) => a.value.timestamp - b.value.timestamp
})

Returned store will have the following shape:

{
  [wigetId]: {
    item: Widget,
    children: { 
      type: 'deposit' | 'withdraw' | 'earn', 
      value: Deposit | Withdraw | Earn 
    }[]
  }
}
@Kelin2025 Kelin2025 added the enhancement New feature or request label Jul 7, 2023
@Kelin2025 Kelin2025 self-assigned this Jul 7, 2023
This was referenced Jul 7, 2023
@Kelin2025
Copy link
Member Author

What if:

const txWidget = grabItems(Widget.model.$kv, {
  source: {
    deposit: Deposit.model.$kv,
    withdraw: Withdraw.model.$kv,
    earn: Earn.model.$kv,
  },
  filter: {
    deposit: (deposit, widget) => widget.txHashes.includes(deposit.tx_hash),
    withdraw: (deposit, widget) => widget.txHashes.includes(deposit.tx_hash),
    earn:(deposit, widget) => widget.txHashes.includes(deposit.tx_hash),
  },
  orderBy: (a, b, widgetState) => a.value.timestamp - b.value.timestamp
})

txWidget.state.store // { ref: { [widgetId]: { item: Widget, children: { ... }[] }
txWidget.found.deposit.state.store // { ref: { [depositId]: Deposit } }
txWidget.found.withdraw.state.store // { ref: { [withdrawId]: Withdraw } }
txWidget.found.earn.state.store // { ref: { [earnId]: Earn } }

And we can compute them just by using createSelection with filter and fn 🤔

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant