-
Notifications
You must be signed in to change notification settings - Fork 104
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,55 +100,62 @@ function celoGoldExchangeRateHistoryChannel(lastTimeUpdated: number) { | |
} | ||
|
||
const now = Date.now() | ||
// timestamp + 1 is used because .startAt is inclusive | ||
const startAt = Math.max(lastTimeUpdated + 1, now - MAX_HISTORY_RETENTION) | ||
|
||
return eventChannel((emit: any) => { | ||
const emitter = (snapshot: FirebaseDatabaseTypes.DataSnapshot) => { | ||
const singleItemEmitter = (snapshot: FirebaseDatabaseTypes.DataSnapshot) => { | ||
emit([snapshot.val()]) | ||
} | ||
let cancelFunction = () => {} | ||
const listenForNewElements = (newElementsStartAt: number) => { | ||
cancelFunction = firebase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this called There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
.database() | ||
.ref(`${EXCHANGE_RATES}/cGLD/cUSD`) | ||
.orderByChild('timestamp') | ||
.startAt(newElementsStartAt) | ||
.on('child_added', singleItemEmitter, errorCallback) | ||
} | ||
const fullListEmitter = (snapshot: FirebaseDatabaseTypes.DataSnapshot) => { | ||
const result: ExchangeRate[] = [] | ||
snapshot.forEach((childSnapshot: FirebaseDatabaseTypes.DataSnapshot) => { | ||
tarikbellamine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result.push(childSnapshot.val()) | ||
return undefined | ||
}) | ||
emit(result) | ||
if (result.length) { | ||
emit(result) | ||
listenForNewElements(result[result.length - 1].timestamp + 1) | ||
} else { | ||
listenForNewElements(startAt) | ||
} | ||
} | ||
|
||
// timestamp + 1 is used because .startAt is inclusive | ||
const startAt = lastTimeUpdated + 1 || now - MAX_HISTORY_RETENTION | ||
|
||
const onValueChange = firebase | ||
firebase | ||
.database() | ||
.ref(`${EXCHANGE_RATES}/cGLD/cUSD`) | ||
.orderByChild('timestamp') | ||
.startAt(startAt) | ||
.on(VALUE_CHANGE_HOOK, emitter, errorCallback) | ||
|
||
const cancel = () => { | ||
firebase | ||
.database() | ||
.ref(`${EXCHANGE_RATES}/cGLD/cUSD`) | ||
.orderByChild('timestamp') | ||
.startAt(startAt) | ||
.off(VALUE_CHANGE_HOOK, onValueChange) | ||
} | ||
.once(VALUE_CHANGE_HOOK, fullListEmitter, errorCallback) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated to this PR but why is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, so I find it confusing that |
||
|
||
return cancel | ||
return cancelFunction | ||
}) | ||
} | ||
|
||
export function* subscribeToCeloGoldExchangeRateHistory() { | ||
yield call(waitForFirebaseAuth) | ||
const history = yield select(exchangeHistorySelector) | ||
const chan = yield call(celoGoldExchangeRateHistoryChannel, history.lastTimeUpdated) | ||
const channel = yield call(celoGoldExchangeRateHistoryChannel, history.lastTimeUpdated) | ||
try { | ||
while (true) { | ||
const exchangeRates = yield take(chan) | ||
const exchangeRates = yield take(channel) | ||
const now = getRemoteTime() | ||
yield put(updateCeloGoldExchangeRateHistory(exchangeRates, now)) | ||
} | ||
} catch (error) { | ||
Logger.error(`${TAG}@subscribeToCeloGoldExchangeRateHistory`, error) | ||
} finally { | ||
if (yield cancelled()) { | ||
chan.close() | ||
channel.close() | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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 nestedThere was a problem hiding this comment.
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 theeventChannel
:/