2021-04-16 04:34:29 +00:00
|
|
|
import createDebug from 'debug';
|
|
|
|
|
import DataLoader from 'dataloader';
|
|
|
|
|
import LRUCache from 'lru-cache';
|
2017-10-19 08:00:21 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
const debug = createDebug('graphbrainz:extensions/mediawiki');
|
2017-10-19 08:00:21 +00:00
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
export default function createLoader(options) {
|
2021-04-16 04:34:29 +00:00
|
|
|
const { client } = options;
|
|
|
|
|
const cache = new LRUCache({
|
2017-10-19 08:00:21 +00:00
|
|
|
max: options.cacheSize,
|
|
|
|
|
maxAge: options.cacheTTL,
|
2017-11-07 05:54:56 +00:00
|
|
|
dispose(key) {
|
2021-04-16 04:34:29 +00:00
|
|
|
debug(`Removed from cache. key=${key}`);
|
|
|
|
|
},
|
|
|
|
|
});
|
2017-10-19 08:00:21 +00:00
|
|
|
// Make the cache Map-like.
|
2021-04-16 04:34:29 +00:00
|
|
|
cache.delete = cache.del;
|
|
|
|
|
cache.clear = cache.reset;
|
2017-10-19 08:00:21 +00:00
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
return new DataLoader(
|
2021-04-16 04:34:29 +00:00
|
|
|
(keys) => {
|
|
|
|
|
return Promise.allSettled(
|
|
|
|
|
keys.map((key) => client.imageInfo(key))
|
|
|
|
|
).then((results) =>
|
|
|
|
|
results.map((result) => result.reason || result.value)
|
|
|
|
|
);
|
2017-11-07 05:54:56 +00:00
|
|
|
},
|
2018-08-04 19:04:19 +00:00
|
|
|
{ batch: false, cacheMap: cache }
|
2021-04-16 04:34:29 +00:00
|
|
|
);
|
2017-10-19 08:00:21 +00:00
|
|
|
}
|