This repository was archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender_cache.coffee
68 lines (57 loc) · 1.79 KB
/
render_cache.coffee
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Hash = require '../foundation/hash/hash'
module.exports = class RenderCache extends Hash
maximumLength: 4
constructor: ->
super
@keyQueue = []
viewForOptions: (options) ->
if Batman.config.cacheViews || options.cache || options.viewClass::cache
@getOrSet options, =>
@_newViewFromOptions(Batman.extend {}, options)
else
@_newViewFromOptions(options)
_newViewFromOptions: (options) -> new options.viewClass(options)
@wrapAccessor (core) ->
cache: false
get: (key) ->
result = core.get.call(@, key)
# Bubble the result up to the top of the queue
@_addOrBubbleKey(key) if result
result
set: (key, value) ->
result = core.set.apply(@, arguments)
result.set 'cached', true
@_addOrBubbleKey(key)
@_evictExpiredKeys()
result
unset: (key) ->
result = core.unset.apply(@, arguments)
result.set 'cached', false
@_removeKeyFromQueue(key)
result
equality: (incomingOptions, storageOptions) ->
return false unless Object.keys(incomingOptions).length == Object.keys(storageOptions).length
for key of incomingOptions when !(key in ['view'])
return false if incomingOptions[key] != storageOptions[key]
return true
reset: ->
for key in @keyQueue.slice(0)
@unset(key)
return
_addOrBubbleKey: (key) ->
@_removeKeyFromQueue(key)
@keyQueue.unshift key
_removeKeyFromQueue: (key) ->
for queuedKey, index in @keyQueue
if @equality(queuedKey, key)
@keyQueue.splice(index, 1)
break
key
_evictExpiredKeys: ->
if @length > @maximumLength
currentKeys = @keyQueue.slice(0)
for i in [@maximumLength...currentKeys.length]
key = currentKeys[i]
if !@get(key).isInDOM
@unset(key)
return