-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
103 lines (97 loc) · 3.03 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
var List = require('complex-list');
function inherit(a,b){
a.prototype = Object.create(b.prototype,{constructor:{
value:a,
enumerable:false,
configurable:false,
writable:false
}});
}
function depChecker(targetpropfunc,targetdep,depfromlist,depfromlistcontainer){
targetdep.resolve(targetpropfunc(depfromlist));
if(targetdep.resolved()){
return depfromlistcontainer;
}
}
function addToListOfUnresolved(lobj,unres){
lobj.list.push(unres.name);
}
function DependableList(){
List.call(this);
this.unresolved = new List();
}
inherit(DependableList,List);
DependableList.prototype.destroy = function(){
this.unresolved.destroy();
this.unresolved = null;
List.prototype.destroy.call(this);
};
DependableList.dependableConstructor = function(){
return Dependable;
};
DependableList.prototype.createDependable = function(){
return new Dependable();
};
DependableList.prototype.getDependencyProperty = function(dependable){
return dependable.name;
};
DependableList.prototype.plainAddReverse = function(afterdependable,dependable){
this.plainAdd(dependable,afterdependable);
};
DependableList.prototype.plainAdd = function(dependable,afterdependable){
var listforremove = new List(), listforadd = new List();
var newdependablecontainer = List.prototype.add.call(this,dependable,afterdependable);
this.unresolved.traverse(this.lateResolve.bind(this,listforremove,listforadd,newdependablecontainer,dependable));
listforremove.traverse(this.unresolved.removeOne.bind(this.unresolved));
listforadd.traverse(this.plainAddReverse.bind(this,newdependablecontainer));
listforremove.destroy();
listforadd.destroy();
};
DependableList.prototype.add = function(dependable){
var depcheck = this.traverseConditionally(depChecker.bind(null,this.getDependencyProperty.bind(this),dependable));
if(depcheck){
this.plainAdd(dependable,depcheck);
return;
}
if(dependable.resolved()){
this.plainAdd(dependable);
}else{
this.unresolved.add(dependable);
}
};
DependableList.prototype.lateResolve = function(listforremove,listforadd,newdependablecontainer,newdependable,unresolveddependable,unresolvedcontainer){
unresolveddependable.resolve(newdependable.name);
if(unresolveddependable.resolved()){
listforremove.add(unresolvedcontainer);
listforadd.add(unresolveddependable);
}
};
DependableList.prototype.listOfUnresolved = function(){
var lobj = {list:[]};
this.unresolved.traverse(addToListOfUnresolved.bind(null,lobj));
return lobj.list.join(',');
};
function Dependable(){
this.dependencies = {};
this.unresolved = {};
}
Dependable.prototype.destroy = function(){
this.unresolved = null;
this.dependencies = null;
};
Dependable.prototype.cloneDependencies = function(obj){
for(var i in obj){
this.dependencies[i] = obj[i];
this.unresolved[i] = true;
}
};
Dependable.prototype.resolve = function(depname){
delete this.unresolved[depname];
};
Dependable.prototype.resolved = function(){
for(var i in this.unresolved){
return false;
}
return true;
};
module.exports = DependableList;