Files
freeCodeCamp/api-server/server/models/donation.js
Josh Soref 004b99bf8f chore: fix typos in spelling (#38100)
* spelling: accidentally

* spelling: announce

* spelling: assembly

* spelling: avoid

* spelling: backend

* spelling: because

* spelling: claimed

* spelling: candidate

* spelling: certification

* spelling: certified

* spelling: challenge

* spelling: circular

* spelling: it isn't

* spelling: coins

* spelling: combination

* spelling: compliant

* spelling: containers

* spelling: concise

* spelling: deprecated

* spelling: development

* spelling: donor

* spelling: error

* spelling: everything

* spelling: exceed

* spelling: exist

* spelling: falsy

* spelling: faulty

* spelling: forward

* spelling: handle

* spelling: indicates

* spelling: initial

* spelling: integers

* spelling: issealed

* spelling: javascript

* spelling: length

* spelling: maximum

* spelling: minimum

* spelling: mutable

* spelling: notifier

* spelling: coordinate

* spelling: passport

* spelling: perform

* spelling: permuter

* spelling: placeholder

* spelling: progressively

* spelling: semantic

* spelling: submission

* spelling: submit

* spelling: translations

* spelling: turquoise

* spelling: visualization

* spelling: without

* spelling: registration

* spelling: representation
2020-02-08 23:59:10 +05:30

78 lines
2.2 KiB
JavaScript

import { Observable } from 'rx';
import debug from 'debug';
import { reportError } from '../middlewares/error-reporter';
import InMemoryCache from '../utils/in-memory-cache';
const log = debug('fcc:boot:donate');
const fiveMinutes = 1000 * 60 * 5;
export default function(Donation) {
let activeDonationUpdateInterval = null;
const activeDonationCountCacheTTL = fiveMinutes;
const activeDonationCountCache = InMemoryCache(0, reportError);
const activeDonationsQuery$ = () =>
Donation.find$({
// eslint-disable-next-line no-undefined
where: { endDate: undefined }
}).map(instances => instances.length);
function cleanUp() {
if (activeDonationUpdateInterval) {
clearInterval(activeDonationUpdateInterval);
}
return;
}
process.on('exit', cleanUp);
Donation.on('dataSourceAttached', () => {
Donation.find$ = Observable.fromNodeCallback(Donation.find.bind(Donation));
Donation.findOne$ = Observable.fromNodeCallback(
Donation.findOne.bind(Donation)
);
seedTheCache()
.then(setupCacheUpdateInterval)
.catch(err => {
const errMsg = `Error caught seeding the cache: ${err.message}`;
err.message = errMsg;
reportError(err);
});
});
function seedTheCache() {
return new Promise((resolve, reject) =>
Observable.defer(activeDonationsQuery$).subscribe(count => {
log('activeDonor count: %d', count);
activeDonationCountCache.update(() => count);
return resolve();
}, reject)
);
}
function setupCacheUpdateInterval() {
activeDonationUpdateInterval = setInterval(
() =>
Observable.defer(activeDonationsQuery$).subscribe(
count => {
log('activeDonor count: %d', count);
return activeDonationCountCache.update(() => count);
},
err => {
const errMsg = `Error caught updating the cache: ${err.message}`;
err.message = errMsg;
reportError(err);
}
),
activeDonationCountCacheTTL
);
return null;
}
function getCurrentActiveDonationCount$() {
return Observable.of(activeDonationCountCache.get());
}
Donation.getCurrentActiveDonationCount$ = getCurrentActiveDonationCount$;
}