-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·146 lines (119 loc) · 3.18 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
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
var inherit = require('util').inherits;
var EventEmitter = require('EventEmitter');
var defaultTtl = 15000;
var defaultMaxParallel = 5;
var emptyImg = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII=';
function ImagePreloader() {
this.urlList = {};
this.imgList = {};
this._keyMap = [];
this.loading = 0;
this.loaded = 0;
this.error = 0;
this.errKeys = [];
this.ttl = defaultTtl;
this.maxParallel = defaultMaxParallel;
this.placeholderImgData = emptyImg;
}
inherit(ImagePreloader, EventEmitter);
module.exports = ImagePreloader;
function loadNext() {
var that = this;
var ttlTimeout;
var processed = false;
var done = this.loaded + this.error + this.loading;
// let's not go over the limit
if (this.loading >= this.maxParallel) {
return;
}
// no more pictures to process
if (this._keyMap.length <= done) {
return;
}
this.loading++;
var key = this._keyMap[done];
var img = this.imgList[key] = new Image();
var url = this.urlList[key];
function onLoad() {
callback();
}
function onError() {
callback('unable to load picture');
}
function onTimeout() {
callback('picture loading timed out');
}
function clearTtlTimeout() {
if (ttlTimeout) {
clearTimeout(ttlTimeout);
ttlTimeout = null;
}
}
function callback(err) {
img.removeEventListener('load', onLoad);
img.removeEventListener('error', onError);
img.removeEventListener('abort', onError);
clearTtlTimeout();
var toLoad = that._keyMap.length;
if (!processed) {
if (err) {
that.imgList[key].src = that.placeholderImgData;
that.error++;
that.errKeys.push(key);
that.emit('error', {loaded: that.loaded, error: that.error, total: toLoad, errorMsg: err, currentKey: key});
} else {
that.loaded++;
that.emit('loaded', {loaded: that.loaded, error: that.error, total: toLoad, currentKey: key});
}
that.loading--;
processed = true;
}
if (that.loaded + that.error === toLoad) {
that.emit('finished', {loaded: that.loaded, error: that.error, errKeys: that.errKeys, total: toLoad, images: that.imgList});
} else {
loadNext.call(that);
}
}
img.addEventListener('load', onLoad, false);
img.addEventListener('error', onError, false);
img.addEventListener('abort', onError, false);
ttlTimeout = setTimeout(onTimeout, this.ttl);
if (url) {
img.src = url;
} else {
onError();
}
loadNext.call(this);
}
ImagePreloader.prototype.add = function (urlList) {
for (var key in urlList) {
if (urlList.hasOwnProperty(key)) {
this.urlList[key] = urlList[key];
}
}
};
ImagePreloader.prototype.start = function (options) {
options = options || {};
if (options.ttl !== undefined) {
this.ttl = options.ttl;
} else {
this.ttl = defaultTtl;
}
if (options.maxParallel !== undefined) {
this.maxParallel = options.maxParallel;
} else {
this.maxParallel = defaultMaxParallel;
}
if (options.placeholderImgData !== undefined) {
this.placeholderImgData = options.placeholderImgData;
} else {
this.placeholderImgData = emptyImg;
}
this.imgList = {};
this._keyMap = Object.keys(this.urlList);
this.loading = 0;
this.loaded = 0;
this.error = 0;
this.errKeys = [];
loadNext.call(this);
};