forked from choojs/nanocache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (39 loc) · 1.54 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
var assert = require('assert')
module.exports = ChooComponentCache
function ChooComponentCache (state, emit) {
assert.equal(typeof state, 'object', 'ChooComponentCache: state should be type object')
assert.equal(typeof emit, 'function', 'ChooComponentCache: state should be type function')
this.state = state
this.emit = emit
this.cache = {}
}
ChooComponentCache.prototype.prune = function () {
var keys = Object.keys(this.cache)
for (var id, i = 0, len = keys.length; i < len; i++) {
id = keys[i]
if (!this.cache[id].element) delete this.cache[id]
}
}
ChooComponentCache.prototype.render = function (Component) {
assert.equal(typeof Component, 'function', 'ChooComponentCache.render: Component should be type function')
var args = []
for (var i = 1, len = arguments.length; i < len; i++) {
args.push(arguments[i])
}
assert.equal(typeof Component.id, 'function', 'ChooComponentCache.render: Component.id should be type function')
var id = Component.id.apply(Component, args)
assert.equal(typeof id, 'string', 'ChooComponentCache.render: Component.id should return type string')
var el = this.cache[id]
if (!el) {
var ext = args.slice(0)
ext.unshift(Component, id, this.state, this.emit)
el = newCall.apply(newCall, ext)
this.cache[id] = el
}
return el.render.apply(el, args)
}
// Because you can't call `new` and `.apply()` at the same time. This is a mad
// hack, but hey it works so we gonna go for it. Whoop.
function newCall (Cls) {
return new (Cls.bind.apply(Cls, arguments)) // eslint-disable-line
}