Skip to content

Instantly share code, notes, and snippets.

@luqmaan
Last active March 28, 2017 19:27
Show Gist options
  • Save luqmaan/13c106009bd0bd61d3f411e31c6e0c9a to your computer and use it in GitHub Desktop.
Save luqmaan/13c106009bd0bd61d3f411e31c6e0c9a to your computer and use it in GitHub Desktop.
export function* getMissingRates(orderNumbers, labelType) {
const ratesDeltaByOrderNumber = {};
const orderNumbersMissingRates = yield select(orderNumbersMissingRatesSelector, {orderNumbers, labelType});
if (!orderNumbersMissingRates.length) {
return;
}
yield put(startUpdatingRates(orderNumbersMissingRates, labelType));
const ratesDeltaList = yield orderNumbersMissingRates.map(
(orderNumber) => call(fetchRates, orderNumber, labelType),
);
ratesDeltaList.forEach((ratesDelta, i) => {
const orderNumber = orderNumbersMissingRates[i];
ratesDeltaByOrderNumber[orderNumber] = ratesDelta;
});
}
export function* getMissingRates(orderNumbers, labelType) {
const ratesDeltaByOrderNumber = {};
const orderNumbersMissingRates = yield select(orderNumbersMissingRatesSelector, {orderNumbers, labelType});
if (!orderNumbersMissingRates.length) {
return;
}
yield put(startUpdatingRates(orderNumbersMissingRates, labelType));
try {
for (let i = 0; i < orderNumbersMissingRates.length; i++) {
const orderNumber = orderNumbersMissingRates[i];
const ratesDelta = yield call(fetchRates, orderNumber, labelType);
ratesDeltaByOrderNumber[orderNumber] = ratesDelta;
}
}
finally {
// Commit the finished rates, even if the generator was cancelled.
// This way we don't have to refresh rates we've already loaded
yield put(updateRatesByOrderNumber(labelType, ratesDeltaByOrderNumber));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment