-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
53 lines (40 loc) · 1.57 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
var Provider = require('butter-provider')
var ConfigDir = require('utils-configdir')
var fx = require('mkdir-recursive')
var PromiseUtils = require('./promises')
var inherits = require('util').inherits
function _make_cached_call (provider, cache, update) {
return function () {
var promises = [
provider.apply(this, arguments)
.then(data => {
update.apply(this, [data])
return data
}),
cache.apply(this, arguments)
]
return PromiseUtils.firstSettled(promises)
}
}
var CacheProvider = function () {
CacheProvider.super_.apply(this, arguments)
this.configDir = ConfigDir('Butter/cache/butter-provider-' + this.config.name)
fx.mkdirSync(this.configDir)
this._fetch_uncached = this.fetch
this.fetch = _make_cached_call.apply(this, [this._fetch_uncached,
this.fetchFromDB, this.updateFetch])
this._detail_uncached = this.detail
this.detail = _make_cached_call.apply(this, [this._detail_uncached,
this.detailFromDB, this.updateDetail])
}
inherits(CacheProvider, Provider)
function UNIMPLEMENTED () {
console.error('UNIMPLEMENTED')
return Promise.reject('UNIMPLEMENTED')
}
CacheProvider.prototype.fetchFromDB = UNIMPLEMENTED
CacheProvider.prototype.updateFetch = UNIMPLEMENTED
CacheProvider.prototype.detailFromDB = UNIMPLEMENTED
CacheProvider.prototype.updateDetail = UNIMPLEMENTED
module.exports = CacheProvider