-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.js
79 lines (67 loc) · 1.71 KB
/
get.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
var gets=function(paths,opts,cb) { //get many data with one call
if (!paths) return ;
if (typeof paths=='string') {
paths=[paths];
}
var engine=this, output=[], taskqueue=[];
if (opts.syncable) opts.syncable=false;
var makecb=function(path){
return function(data){
if (!(data && typeof data =='object' && data.__empty)) output.push(data);
engine.get(path,opts,taskqueue.shift());
//setTimeout(function (){
// engine.get(path,opts,taskqueue.shift());
//}, 0);
};
};
for (var i=0;i<paths.length;i++) {
if (typeof paths[i]=="null") { //this is only a place holder for key data already in client cache
output.push(null);
} else {
taskqueue.push(makecb(paths[i]));
}
};
taskqueue.push(function(data){
output.push(data);
cb.apply(engine.context||engine,[output,paths]); //return to caller
});
taskqueue.shift()({__empty:true}); //run the task
}
var get=function(path,opts,cb) {
var engine=this;
if (typeof opts=="function") {
context=cb;
cb=opts;
opts={recursive:false};
}
opts=opts||{};
if (!path) {
cb&&cb([null]);
return null;
}
if (typeof cb!="function") {
if (typeof path[0] =="object") {
return path.map(function(p){
return engine.kdb.get(p,opts);
})
}
else return engine.kdb.get(path,opts);
}
if (typeof path==="string") {
path=[path];
}
if (typeof path[0] =="string") {
return engine.kdb.get(path,opts,function(data){
cb(data,engine,path);//return top level keys
});
} else if (typeof path[0] =="object") {
return gets.call(engine,path,opts,function(data){
cb(data,engine,path);//return top level keys
});
} else {
engine.kdb.get([],opts,function(data){
cb(data,engine);//return top level keys
});
}
};
module.exports=get;