Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix the fetching of exchange rates #255

Merged
merged 4 commits into from
Apr 20, 2021
Merged

Fix the fetching of exchange rates #255

merged 4 commits into from
Apr 20, 2021

Conversation

gnardini
Copy link
Contributor

Description

We had a bug in the fetching of the exchange rates that made us fetch the whole thing every time there was an update, which is every 30 minutes. The number of elements to fetch each time depended on how much time it passed since the last use of the app, for new users and for users that didn't open the app in a couple months we would load all the history for three months.

We were also fetching the whole exchange rate history from Firebase and filtering it in memory instead of only fetching the latest three months. We tried to filter, but we were sending 1 as startAt which returns everything.

With the current changes, we load everything since the max between the last update and three months ago and then we listen only for new items, we don't reload everything again.

Other changes

  • Added a migration to delete the exchange rate history from Redux since it was likely that it was repeated a bunch of times.

Tested

Mostly manually, by making changes and adding and modifying items in the RTDB to see that the behavior was the expected one.

Related issues

Backwards compatibility

N/A

@gnardini gnardini requested a review from a team April 19, 2021 21:05
Copy link
Contributor

@tarikbellamine tarikbellamine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Left some small nits and questions.

.startAt(startAt)
.off(VALUE_CHANGE_HOOK, onValueChange)
}
.once(VALUE_CHANGE_HOOK, fullListEmitter, errorCallback)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR but why is VALUE_CHANGE_HOOK = 'value'? I would assume it would equal child_added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you pass value it returns the whole collection/object in one go. child_added is used so that the callback is called once for each child immediately and then whenever a new child is added it's called again. There's also a child_updated, child_moved and child_removed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so I find it confusing that VALUE_CHANGE_HOOK doesn't mean return values when there is a change but instead means return all values. Maybe just me though

}
let cancelFunction = () => {}
const listenForNewElements = (newElementsStartAt: number) => {
cancelFunction = firebase
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this called cancelFunction?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was doing this wrongly. The idea was to return a function that is called when channel is canceled so you stop listening, but I was not doing it correctly, we need to call the off function in the Firebase node ref. Switched to the correct implementation


return eventChannel((emit: any) => {
const emitter = (snapshot: FirebaseDatabaseTypes.DataSnapshot) => {
const singleItemEmitter = (snapshot: FirebaseDatabaseTypes.DataSnapshot) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we define these outside of the eventChannel? It makes it a little more difficult to follow when all these functions are nested

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't because it uses the emit thing that is returned in the eventChannel :/

@gnardini gnardini added the automerge Have PR merge automatically when checks pass label Apr 20, 2021
@mergify mergify bot merged commit a0e44d8 into main Apr 20, 2021
@mergify mergify bot deleted the exchange-rates-firebase branch April 20, 2021 22:46
@ValoraQA
Copy link

Hi @gnardini @tarikbellamine Can you please let us know if we need to test anything in this task if yes can you please provide more info to test this.
Thanks!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
automerge Have PR merge automatically when checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider not persisting exchange rate history
4 participants