-
Notifications
You must be signed in to change notification settings - Fork 920
/
ImageCache.js
162 lines (131 loc) · 3.15 KB
/
ImageCache.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
var EventEmitter = require('events');
var NOOP = function () {};
function Img (src) {
this._originalSrc = src;
this._img = new Image();
this._img.onload = this.emit.bind(this, 'load');
this._img.onerror = this.emit.bind(this, 'error');
this._img.crossOrigin = true;
this._img.src = src;
// The default impl of events emitter will throw on any 'error' event unless
// there is at least 1 handler. Logging anything in this case is unnecessary
// since the browser console will log it too.
this.on('error', NOOP);
// Default is just 10.
this.setMaxListeners(100);
}
Object.assign(Img.prototype, EventEmitter.prototype, {
/**
* Pooling owner looks for this
*/
destructor: function () {
// Make sure we aren't leaking callbacks.
this.removeAllListeners();
},
/**
* Retrieve the original image URL before browser normalization
*
* @return {String}
*/
getOriginalSrc: function () {
return this._originalSrc;
},
/**
* Retrieve a reference to the underyling <img> node.
*
* @return {HTMLImageElement}
*/
getRawImage: function () {
return this._img;
},
/**
* Retrieve the loaded image width
*
* @return {Number}
*/
getWidth: function () {
return this._img.naturalWidth;
},
/**
* Retrieve the loaded image height
*
* @return {Number}
*/
getHeight: function () {
return this._img.naturalHeight;
},
/**
* @return {Bool}
*/
isLoaded: function () {
return this._img.naturalHeight > 0;
}
});
var kInstancePoolLength = 300;
var _instancePool = {
length: 0,
// Keep all the nodes in memory.
elements: {
},
// Push with 0 frequency
push: function (hash, data) {
this.length++;
this.elements[hash] = {
hash: hash, // Helps identifying
freq: 0,
data: data
};
},
get: function (path) {
var element = this.elements[path];
if( element ){
element.freq++;
return element.data;
}
return null;
},
// used to explicitely remove the path
removeElement: function (path) {
// Now almighty GC can claim this soul
var element = this.elements[path];
delete this.elements[path];
this.length--;
return element;
},
_reduceLeastUsed: function (least, currentHash) {
var current = _instancePool.elements[currentHash];
if( least.freq > current.freq ){
return current;
}
return least;
},
popLeastUsed: function () {
var reducer = _instancePool._reduceLeastUsed;
var minUsed = Object.keys(this.elements).reduce(reducer, { freq: Infinity });
if( minUsed.hash ){
return this.removeElement(minUsed.hash);
}
return null;
}
};
var ImageCache = {
/**
* Retrieve an image from the cache
*
* @return {Img}
*/
get: function (src) {
var image = _instancePool.get(src);
if (!image) {
// Awesome LRU
image = new Img(src);
if (_instancePool.length >= kInstancePoolLength) {
_instancePool.popLeastUsed().destructor();
}
_instancePool.push(image.getOriginalSrc(), image);
}
return image;
}
};
module.exports = ImageCache;