-
Notifications
You must be signed in to change notification settings - Fork 290
/
artwork.js
53 lines (53 loc) · 1.67 KB
/
artwork.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
(function (root, factory){
if(typeof define === 'function' && define.amd){
// AMD. Register as an anonymous module.
define(['async-arrays'], factory);
}else if (typeof module === 'object' && module.exports){
module.exports = factory(require('async-arrays'));
}else{
// Browser globals (root is window)
root.AsciiArtArtwork = factory(root.AsyncArrays);
}
}(this, function(arrays){
var sources = [];
var request;
var cache;
return {
addSource : function(source){
sources.push(source);
source.useRequest(request);
},
useRequest : function(instance){
request = instance;
sources.forEach(function(source){
source.useRequest(instance);
});
},
useCache : function(instance){
cache = instance;
},
search : function(query, cb){
var results = [];
arrays.forEachEmission(sources, function(source, index, done){
source.search(query, function(err, res){
if(err) return cb(err);
res.forEach(function(item){
results.push(item);
});
done();
})
}, function(){
cb(null, results);
});
},
get : function(src, path, file, cb){
var source = src;
if(typeof source === 'string'){
source = sources.find(function(item){
return item.name === src;
});
}
source.fetch(path, file, cb);
}
};
}));