-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.js
52 lines (43 loc) · 967 Bytes
/
image.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 EventEmitter = require('events').EventEmitter
, util = require('util');
/**
* Image instance.
*
* @constructor
* @param {String} url URL we want to connect to
* @api private
*/
/* istanbul ignore next */
function Image() {
if (!this) return new Image();
this.url = null;
EventEmitter.call(this);
setTimeout(function timeout() {
if (this.onerror) this.on('error', this.onerror);
if (this.onload) this.on('load', this.onload);
}.bind(this));
}
util.inherits(Image, EventEmitter);
Object.defineProperty(Image.prototype, 'src', {
set: function set(url) {
this.url = url;
},
get: function get() {
return this.url;
}
});
//
// Assign Event Listeners.
//
['error', 'load'].forEach(function each(type) {
Object.defineProperty(Image.prototype, 'on'+ type, {
set: function set(fn) {
if ('function' === typeof fn) this.on(type, fn);
}
});
});
//
// Expose module.
//
module.exports = Image;