-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActiveStackModel.js
More file actions
151 lines (135 loc) · 5.2 KB
/
ActiveStackModel.js
File metadata and controls
151 lines (135 loc) · 5.2 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
angular.module('ActiveStack.Model', [], function($provide) {
$provide.factory('ActiveStackModel', function($log, ActiveStackDomain) {
ActiveStackModel = function() {
Decorate.withEventDispatcher(this);
var self = this;
var cache = {};
var timeoutID = null;
var emitContext = [];
/**
* Found that we needed to queue up the refreshes
* since they were happening so often. This way
* the data will only refresh every 200 millis
*/
this.notifyChanged = function(context) {
if(!timeoutID){
emitContext.push(context)
var that = this;
timeoutID = setTimeout(function(){
emitContext = [];
timeoutID = null;
that.emit('changed');
},200);
}
else{
emitContext.push(context);
}
}
this.filterIncoming = function(o) {
var ret = walkAndReplace(o);
self.notifyChanged(o);
return ret;
}
this.removeObject = function(o) {
var cacheKey = getCacheKey(o);
if (cacheKey) {
// Find the object in the ALL array to remove
var className = getClassName(o);
for(var i in ActiveStackDomain[className].prototype._all){
var ob = ActiveStackDomain[className].prototype._all[i];
if(o.ID && o.ID == ob.ID)
delete ActiveStackDomain[className].prototype._all[i];
}
o.removeReferences();
removeFromCache(cacheKey);
}
}
this.findObjectByClassIdPair = function(cip){
return findObject(getCacheKey(cip));
}
this.findObject = function(cn, id) {
return findObject(cn + id);
}
function walkAndReplace(o) {
if (o == null) {
return null;
} else if (Array.isArray(o)) {
var list = [];
for (var i = 0; i < o.length; i++) {
var object = o[i];
var typed = walkAndReplace(object);
list.push(typed);
}
return list;
} else if (typeof o == "object") {
var newO = {};
var cacheKey = getCacheKey(o);
if (cacheKey != null) {
newO = findObject(cacheKey);
if (newO == null) {
var className = getClassName(o);
if (className) {
newO = new ActiveStackDomain[className]();
}
}
newO.isProxy = false;
}
for (var key in o) {
var value = o[key];
newO[key] = walkAndReplace(value);
}
addOrReplaceCache(newO);
return newO;
}
return o;
}
function findObject(cacheKey) {
return cache[cacheKey];
}
var numCached = 0;
var cacheCounts = {};
function addOrReplaceCache(o) {
var cacheKey = getCacheKey(o);
if (cacheKey != null) {
var isAdd = findObject(cacheKey) == null;
if(isAdd){
var className = getClassName(o);
// console.log(className+" Added to model");
if(ActiveStackDomain[className])
ActiveStackDomain[className].prototype._all.push(o);
}
if(!cacheCounts[cacheKey]) cacheCounts[cacheKey] = 0;
cacheCounts[cacheKey]++;
cache[cacheKey] = o;
// self.notifyChanged();
if(numCached %100==0){
console.log("Num Cached: "+(++numCached));
}
}
}
function removeFromCache(cacheKey) {
delete cache[cacheKey];
self.notifyChanged(cacheKey);
}
function getCacheKey(o) {
var ID = o.ID;
var cn = getClassName(o);
if (ID && cn) {
if (ActiveStackDomain[cn] != undefined) {
return cn + ID;
}
}
return null;
}
function getClassName(o) {
var cn = o.cn || o.className;
if (cn) {
var parts = cn.split(".");
return parts[parts.length-1];
}
return null;
}
};
return new ActiveStackModel();
});
});