Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Documentation/Support] using mapping the result of a selector with another selector. #717

Open
MylesWardell opened this issue Apr 8, 2024 · 2 comments

Comments

@MylesWardell
Copy link

MylesWardell commented Apr 8, 2024

Background

I have a balance adapter that has 10 elements and a market adapter that has many more (let's say 100). Now I want to create derived data that gets the fiat balance by multiplying my balance adapter by its USD market.

// Typing on phone so might get syntax wrong
const selectBalaceCurrencies = balanceAdapter.getAll() // returns ['USD', 'EUR']
 
const selectBalaceByCode = balanceAdapter.getEntityById() // returns {code: 'USD', available: 10 }

const selectPriceByCode = priceAdapter.getEntityById() // Returns { askPrice, dailyChange, ...ext }

// What I want to do is
const selectLiveBalance = (state: RootState) => {
 const currencies = selectCurrencies(state)
 const liveBalance = currencies.map((code) => {
  const balance = selectBalaceByCode(state, code)
  const price = selectPriceByCode(state, code)
  return balance.available * price.askPrice
 }
}

I have managed to merge the second two into one selector that's caching properly selectLiveBalanceByCode but I can't for the life of me work out how to get that to merge with the first selector and only update when one of the selected codes change not when any state changes or I could do it when any reference to price changes (by selecting the object adapter.getEntityRecord() //the one that returns an object but then my balance updates when a market price changes for something that we don't have a balance for.

Goal

I want to only update my derived balance when the selectPriceByCode (merged with balance it's selectLiveBalanceByCode) updates with values from selectBalanceCurrencies

I have given up for the day but can't stop thinking about it (hence typing this on phone instead of at desk)

Hope their is a solution and I have provided enough details

@MylesWardell
Copy link
Author

Related to this issue potentially #360

@MylesWardell
Copy link
Author

MylesWardell commented May 16, 2024

I have been forced to use this package as I could not find a solution. Luckily it works well! Unfortunately it has not had a commit in 6 years. https://github.com/taskworld/rereselect

Example solution

export const selectLiveBalance = makeSelector((query) => query(selectBalanceCurrencies).map((code) => query(selectLiveBalanceByCode(code))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant