Skip to content

Commit a842377

Browse files
author
dmp42
committed
F
1 parent 9fca599 commit a842377

File tree

2 files changed

+260
-0
lines changed

2 files changed

+260
-0
lines changed

pukefile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def build():
253253
list.merge(['src/jsboot/types/eventdispatcher.js'])
254254

255255
# Not exactly "core" per-se
256+
list.merge(['src/jsboot/types/mutable.js'])
256257
list.merge(['src/jsboot/controllers/idle.js'])
257258
list.merge(['src/jsboot/controllers/singleapp.js'])
258259
list.merge(['src/jsboot/utils/storage.js'])

src/jsboot/types/mutable.js

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
jsBoot.use('jsBoot.types.EventDispatcher').as('dispatcher');
2+
3+
// An object that when mutating (eg: via a call to the change method) will dispatch a change event
4+
jsBoot.pack('jsBoot.types', function(api) {
5+
/*global Ember*/
6+
'use strict';
7+
8+
// XXX clarify this shit
9+
// XXX What's wrong with Ember.Object.prototype?
10+
this.Mutable = function() {
11+
if (typeof Ember != 'undefined') {
12+
var em = new Ember.Object();
13+
for (var i in em) {
14+
if (i != 'constructor' && i != 'set')
15+
this[i] = em[i];
16+
}
17+
}
18+
api.dispatcher.apply(this);
19+
};
20+
21+
this.Mutable.prototype = Object.create(api.dispatcher.prototype);
22+
23+
// XXX The persistence of "change" is only here for Roxee backward compat
24+
this.Mutable.prototype.set = this.Mutable.prototype.change = function(key, value) {
25+
var ov = (typeof Ember != 'undefined') ? this.get(key) : this[key];
26+
if (value == ov)
27+
return;
28+
this.dispatchEvent(this.CHANGE, {key: key, oldValue: ov, newValue: value});
29+
if (typeof Ember != 'undefined') {
30+
Ember.set(this, key, value);
31+
}else {
32+
this[key] = value;
33+
}
34+
};
35+
36+
37+
// Should not be constructed before the store is ready
38+
/*
39+
this.StoreMutable = function(storeObject){
40+
this.Mutable.apply(this);
41+
42+
Object.keys(storeObject).forEach(function(key){
43+
// XXX miss typage instanciation
44+
this[key] = storeObject[key];
45+
}, this);
46+
47+
// Should save on user-idle, or on logout / shutdown
48+
this.addEventListener(this.AFTER_MUTATION, function(){
49+
// Reset / repopulate store object
50+
Object.keys(storeObject).forEach(function(key){
51+
// XXX miss typage serialization
52+
if(key in this)
53+
storeObject[key] = this[key];
54+
else
55+
delete storeObject[key];
56+
});
57+
}, this);
58+
};
59+
60+
StoreMutable.prototype = Object.create(Mutable.prototype);
61+
*/
62+
63+
});
64+
65+
66+
67+
/*
68+
var des = function(valuesHolder){
69+
};
70+
return {
71+
toto: {
72+
enumerable: true,
73+
configurable: false,
74+
writable: true,
75+
value: 'whatever'
76+
},
77+
titi: {
78+
enumerable: true,
79+
configurable: false,
80+
value: 'toto',
81+
parse: function(value){
82+
return 'parse: ' + value;
83+
},
84+
serialize: function(value){
85+
return 'serialize: ' + value;
86+
}
87+
}
88+
};
89+
90+
var TypedMutable = function(descriptor){
91+
var proxy = Object.create({}, descriptor);
92+
Object.keys(descriptor).forEach(function(key){
93+
Object.defineProperty(this, key, {
94+
enumerable: descriptor[key].enumerable,
95+
configurable: true,
96+
get: function(){
97+
if(descriptor[key].parse){
98+
return descriptor[key].parse(proxy[key]);
99+
return proxy[key];
100+
},
101+
set: descriptor[key].writable ? function(value){
102+
if(descriptor[key].serialize){
103+
proxy[key] = descriptor[key].serialize(value);
104+
return;
105+
}
106+
switch(typeof descriptor[key].value){
107+
case 'number':
108+
proxy[key] = parseInt(value, 10);
109+
break;
110+
case 'boolean':
111+
proxy[key] = !!value;
112+
break;
113+
case 'string':
114+
proxy[key] = '' + value;
115+
break;
116+
case 'object':
117+
// XXX code injection risk?
118+
proxy[key] = value; // Object.create({}, value);
119+
break;
120+
case 'function':
121+
// Custom filtering method
122+
}
123+
} : undefined
124+
});
125+
}, this);
126+
};
127+
128+
var a1 = new TypedMutable(des);
129+
var a2 = new TypedMutable(des);
130+
// throw "toto"
131+
*/
132+
133+
/*
134+
jsBoot.use('jsBoot.types.Mutable');
135+
jsBoot.pack('jsBoot.types', function(api) {
136+
'use strict';
137+
138+
this.TypedMutable = function(descriptor, initialMesh) {
139+
api.Mutable.apply(this);
140+
141+
this.isTyped = true;
142+
143+
var privatePool = {};
144+
var lastMesh = {};
145+
146+
Object.keys(descriptor).forEach(function(i) {
147+
var item = descriptor[i];
148+
switch (typeof item) {
149+
case 'number':
150+
this[i] = parseInt(item, 10);
151+
break;
152+
case 'boolean':
153+
this[i] = (item == 'true');
154+
break;
155+
case 'string':
156+
this[i] = '' + item;
157+
break;
158+
case 'object':
159+
// May be null, an array, or an object-object
160+
this[i] = item;
161+
break;
162+
case 'function':
163+
Object.defineProperty(this, i, {
164+
enumerable: true,
165+
configurable: true,
166+
get: function() {
167+
// XXX super dirty and dangerous - cause of the bind
168+
// Verify this in IE and other non-bindable browsers
169+
if (item.isDirty || item.constructor != Function)
170+
return item(lastMesh[i]);
171+
else {
172+
if (typeof privatePool[i] == 'undefined') {
173+
privatePool[i] = new item(lastMesh[i] || null);
174+
}
175+
return privatePool[i];
176+
}
177+
},
178+
set: function(value) {
179+
privatePool[i] = value;
180+
// XXX dirty trix to let polymorph descriptors do whatever job need be be done
181+
if (item.isDirty)
182+
privatePool[i] = item(value);
183+
}
184+
});
185+
break;
186+
}
187+
}, this);
188+
189+
this.free = function() {
190+
privatePool = {};
191+
};
192+
193+
this.toObject = function() {
194+
var ret = {};
195+
Object.keys(descriptor).forEach(function(i) {
196+
ret[i] = (!!this[i] && (typeof this[i] == 'object') && ('toObject' in this[i])) ? this[i].toObject() : this[i];
197+
}, this);
198+
return ret;
199+
};
200+
201+
this.fromObject = function(networkMesh) {
202+
if (typeof networkMesh != 'object')
203+
networkMesh = {id: networkMesh};
204+
Object.keys(networkMesh).forEach(function(i) {
205+
if (!(i in descriptor))
206+
return;
207+
var item = networkMesh[i];
208+
switch (typeof descriptor[i]) {
209+
case 'number':
210+
this.set(i, parseInt(item, 10));
211+
break;
212+
case 'boolean':
213+
this.set(i, (item == 'true'));
214+
break;
215+
case 'string':
216+
this.set(i, '' + item);
217+
break;
218+
case 'object':
219+
// May be null, an array, or an object-object
220+
this.set(i, item);
221+
break;
222+
case 'function':
223+
if (typeof privatePool[i] != 'undefined') {
224+
if (descriptor[i].constructor == Function) {
225+
if (!!privatePool[i]) {
226+
privatePool[i].fromObject(networkMesh[i]);
227+
}else {
228+
lastMesh[i] = networkMesh[i];
229+
}
230+
}else
231+
this.set(i, descriptor[i](networkMesh[i]));
232+
}else
233+
// Merge and override lastMesh otherwise, to be used for later construction
234+
lastMesh[i] = networkMesh[i];
235+
236+
237+
// if(typeof privatePool[i] != 'undefined')
238+
// if('fromObject' in privatePool[i])
239+
// privatePool[i].fromObject(networkMesh[i]);
240+
// else
241+
// this.set(i, descriptor[i](networkMesh[i]));
242+
// else
243+
// // Merge and override lastMesh otherwise, to be used for later construction
244+
// lastMesh[i] = networkMesh[i];
245+
break;
246+
default:
247+
this.set(i, item);
248+
throw new Error('UNTYPED_MESH', 'Mesh is not typed properly ' + i + ' ' + descriptor[i] + ' ' + item);
249+
}
250+
}, this);
251+
};
252+
253+
if (initialMesh)
254+
this.fromObject(initialMesh);
255+
};
256+
257+
});
258+
259+
*/

0 commit comments

Comments
 (0)