forked from butterproviders/butter-provider
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
36 lines (30 loc) · 907 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var memoize = require('memoizee');
var _ = require('lodash')
var Provider = function () {
var memopts = {
maxAge: 10 * 60 * 1000,
/* 10 minutes */
preFetch: 0.5,
/* recache every 5 minutes */
primitive: true
};
this.memfetch = memoize(this.fetch.bind(this), memopts);
this.fetch = this._fetch.bind(this);
this.detail = memoize(this.detail.bind(this), _.extend(memopts, {
async: true
}));
};
Provider.prototype._fetch = function (filters) {
filters.toString = this.toString;
var promise = this.memfetch(filters),
_this = this;
promise.catch(function (error) {
// Delete the cached result if we get an error so retry will work
_this.memfetch.delete(filters);
});
return promise;
};
Provider.prototype.toString = function (arg) {
return JSON.stringify(this);
};
module.exports = Provider