@hutkev
Feb 2013
// Promises/A
interface Promise {
then(
callback: (value:any) -> void,
errback?: (err:any) -> void)
) : Promise
resolve(value: any) : void
reject(value: any) : void
}
// Underlying abstraction
interface Event {
value: any; // One time set, multiple get
next: (event: Event) -> void; // Stack the callbacks
}
var tester = new Tester() var obj = Proxy.create(new ForwardHandler(tester))
interface Handler /* harmony:proxies style */{ defineProperty(name: string, desc: PropertyDescriptor): void; delete (name: string): bool; getPropertyNames(name: string): string[]; getOwnPropertyNames(name: string): string[]; getOwnPropertyDescriptor(name: string): PropertyDescriptor; getPropertyDescriptor(name: string): PropertyDescriptor; }
Handler:
addProperty(name: string, writeCB: (any) => void ) {
var that = this;
var desc: PropertyDescriptor = {
get: function () {
return that.props[name].value;
},
set: function(value) {
writeCB(value);
that.props[name].value = value;
}
}
this.props[name] = { value: null, pd: desc };
}
My Object:
constructor () {
this.handler = new VirtualHandler(this);
this.handler.addProperty("foo", function (value) {
console.log('New foo: ' + value);
});
return Proxy.create(this.handler,this);
}
obj = {a:0};
Object.observe(obj, function(record) {
console.log(record);
});
obj.a = 1 // {type: 'updated', object: obj, name: 'a', oldValue: 1}
obj.b = true // {type: 'new', object: obj, name: 'b' }
delete obj.b // {type: 'delete', object: obj, name: 'b'}
var shared = require('shared')
var store = shared.createStore({host: 'mongohost'});
store.apply(function(db) {
if (db.counter === undefined)
db.counter = 0;
else
db.counter++;
}, function(err) {
// Error handle
});
Each object is one document in MongoDB
References link objects in the DB
Garbage collection cleans dead objects up as needed
function push(queue, item, cb) {
store.apply(function(db) {
if (db[queue] === undefined)
db.queue = [];
db.queue.push(item);
}, function (err) {
cb(err)
}
}
function pop(queue, cb) {
store.apply(function (db) {
if (db[queue] !== undefined)
return db[queue].shift();
else
return null;
}, function (err, ret) {
cb(err, ret);
});
}