-
Notifications
You must be signed in to change notification settings - Fork 16
/
connect.js
69 lines (52 loc) · 1.56 KB
/
connect.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
var behavior = require("./behavior");
/**
*
* @param {Array<String,Behavior,function>} behaviors - An array of behavior names or custom behaviors.
* The order of named execution gets run in order.
* @param {Object} options
* @hide
*/
var connect = function(behaviors, options){
behaviors = behaviors.map(function(behavior, index){
var sortedIndex = -1;
if(typeof behavior === "string") {
sortedIndex = connect.order.indexOf(behavior);
behavior = behavior.map[behavior];
} else if(behavior.isBehavior) {
sortedIndex = connect.order.indexOf(behavior.behaviorName);
} else {
behavior = connect.behavior(behavior);
}
return {
originalIndex: index,
sortedIndex: sortedIndex,
behavior: behavior
};
});
behaviors.sort(function(b1, b2){
// if both have a sorted index
if(~b1.sortedIndex && ~b2.sortedIndex) {
return b1.sortedIndex - b2.sortedIndex;
}
return b1.originalIndex - b2.originalIndex;
});
behaviors = behaviors.map(function(b){
return b.behavior;
});
var behavior = connect.base( connect.behavior("options",function(){return options; })() );
behaviors.forEach(function(behave){
behavior = behave(behavior);
});
if(behavior.init) {
behavior.init();
}
return behavior;
};
connect.order = ["data/localstorage-cache","data/url","data/parse","cache-requests","data/combine-requests",
"constructor","constructor/store","can/map","can/ref",
"fall-through-cache",
"data/worker","real-time",
"data/callbacks-cache","data/callbacks","constructor/callbacks-once"
];
connect.behavior = behavior;
module.exports= connect;