Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: leshy/comm-4
base: c06b99cdb6
...
head fork: leshy/comm-4
compare: d8ddc3a51e
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 101 additions and 53 deletions.
  1. +101 −53 app.js
View
154 app.js
@@ -1,5 +1,5 @@
// notes from comm framework 3:
-
+//
// Permissions - it could accept a JSON schema instead of just attribute list
//
// What if multiple talkers respond to a message? List of messages should be sent in one shot.
@@ -9,20 +9,23 @@
// it should be more symmetric. I'd like for clients to be able to create new objects.
//
// abstract permissions/output/input/persist
+//
// there should be no difference between objectsync receiving an object from db or from remote host
// another reason for symetric input/output
+//
// make it so that comm elements can be remote/persistable objects
// figure out the initialization (phonebook for example)
// callbacks are super ugly
+//
var Backbone = require('backbone');
-var _ = require('underscore')
+var _ = require('underscore');
var decorators = require('decorators');
var decorate = decorators.decorate;
// converts retarded magical arguments object to an Array object
-function toArray(arg) { return Array.prototype.slice.call(arg) }
+function toArray(arg) { return Array.prototype.slice.call(arg); }
// defaults attribute inheritance, and automatic super.initialize calls
@@ -31,25 +34,25 @@ function toArray(arg) { return Array.prototype.slice.call(arg) }
var args = Array.prototype.slice.call(arguments),
child = this;
- var initf = []
- var defaults = {}
+ var initf = [];
+ var defaults = {};
if (child.prototype.defaults) {
- defaults = _.clone(child.prototype.defaults)
+ defaults = _.clone(child.prototype.defaults);
}
_.each(args, function (superc) {
// did I receive a dictionary or an object/backbone model?
- if (superc.prototype) { superc = superc.prototype }
+ if (superc.prototype) { superc = superc.prototype; }
// inherit defaults
if (superc.defaults) {
- defaults = _.extend(defaults,superc.defaults)
+ defaults = _.extend(defaults,superc.defaults);
}
// build a list of initialize functions if you find more then one
if (superc.initialize) {
(initf.length) || initf.push(child.prototype.initialize);
- initf.push(superc.initialize)
+ initf.push(superc.initialize);
}
child = child.extend(superc);
@@ -58,11 +61,11 @@ function toArray(arg) { return Array.prototype.slice.call(arg) }
// construct a combined init function
if (initf.length) {
child = child.extend({ initialize : function(attributes,options) {
- var self = this
- _.map(initf,function(initf) { initf.call(self,attributes,options) })
- }})
+ var self = this;
+ _.map(initf,function(initf) { initf.call(self,attributes,options); });
+ }});
}
- child.prototype.defaults = defaults
+ child.prototype.defaults = defaults;
return child;
}
@@ -74,15 +77,62 @@ function toArray(arg) { return Array.prototype.slice.call(arg) }
function triggerOnce(event,f) {
var self = this;
this.bind(event,function() {
- self.unbind(event,f)
- f.apply(this,toArray(arguments))
- })
+ self.unbind(event,f);
+ f.apply(this,toArray(arguments));
+ });
}
Backbone.Model.triggerOnce = triggerOnce;
})();
+// simple object that matches json blobs and executes callbacks
+// maybe I should upgrade this to use json schemas as patterns
+var SubscriptionMan = Backbone.Model.extend4000({
+ initialize: function() {
+ this.subscriptions = [];
+ },
+ subscribe: function(msg,f,name) {
+ if (!name) { name = function() { f() }; }
+ this.subscriptions.push({pattern: msg, f: f, name: name});
+ return name;
+ },
+
+ unsubscribe: function(name) {
+ this.subscriptions = _.filter(this.subscriptions, function(sub) { return ((sub.name != name) && (sub.f != name)); });
+ },
+
+ oneshot: function(msg,f) {
+ var self = this;
+ function ff() {
+ self.unsubscribe(ff); f.apply(this,attributes);
+ }
+ this.subscribe(msg, ff,ff );
+ return function() { self.unsubscribe(ff); };
+ },
+
+ _matches: function(msg) {
+ function checkmatch(msg,pattern) {
+ for (var property in pattern) {
+ if (msg[property] == undefined) { return false; }
+ if (pattern[property] != true) { if (msg[property] != pattern[property]) { return false; } }
+ }
+ return true;
+ }
+
+ var res = [];
+
+ this.subscriptions.forEach(function(matcher) {
+ var pattern = matcher.pattern;
+ if (checkmatch(msg,pattern)) { res.push (matcher.f); }
+ });
+ return res;
+ },
+
+ event: function(msg) {
+ this._matches(msg).forEach( function(f) { f(msg); } );
+ }
+});
// a node that connects to other nodes via 'plugs'
@@ -92,58 +142,58 @@ var GenericGraphNode = Backbone.Model.extend({
defaults: { name: 'node' },
initialize: function() {
- var self = this
+ var self = this;
// simple decorator to check if plug exists for plug accepting functions
var plugfundecorator = function() {
var args = toArray(arguments); var f = args.shift(); var plugname = _.first(args);
if (!self.plugs[plugname]) { throw "graph node can't find plug named '" + plugname + "'"; return; }
- return f(args)
- }
+ return f(args);
+ };
_.map(['get', 'add', 'remove', 'has'], function(fname) {
- self.fname = decorate(plugfundecorator,self.fname)
- })
+ self.fname = decorate(plugfundecorator,self.fname);
+ });
- this.plugs = {}
+ this.plugs = {};
},
name: function() {
- return this.get('name')
+ return this.get('name');
},
addplug: function(plugplural, plugsingular, nomodels) {
var self = this;
- if (!plugsingular) { plugsingular = plugplural }
- this.plugs[plugplural] = true
+ if (!plugsingular) { plugsingular = plugplural; }
+ this.plugs[plugplural] = true;
- this[ plugplural ] = new Backbone.Collection()
+ this[ plugplural ] = new Backbone.Collection();
- this[ 'add' + plugsingular ] = decorate(decorators.multiArg,function(obj) { return self.plugadd.call(self,plugplural,obj) })
- this[ 'del' + plugsingular ] = decorate(decorators.multiArg,function(obj) { return self.plugremove.call(self,plugplural,obj) })
- this[ 'has' + plugsingular ] = function(obj) { return self.plughas.call(self,plugplural,obj) }
- this[ 'get' + plugsingular ] = function() { return _.first(self.plugget.call(self,plugplural)) }
- this[ 'get' + plugplural ] = function() { return self.plugget.call(self,plugplural) }
+ this[ 'add' + plugsingular ] = decorate(decorators.multiArg,function(obj) { return self.plugadd.call(self,plugplural,obj); });
+ this[ 'del' + plugsingular ] = decorate(decorators.multiArg,function(obj) { return self.plugremove.call(self,plugplural,obj); });
+ this[ 'has' + plugsingular ] = function(obj) { return self.plughas.call(self,plugplural,obj); };
+ this[ 'get' + plugsingular ] = function() { return _.first(self.plugget.call(self,plugplural)); };
+ this[ 'get' + plugplural ] = function() { return self.plugget.call(self,plugplural); };
},
plugget: function(plug) {
- return this[plug].models
+ return this[plug].models;
},
plugadd: function(plug,obj) {
//console.log(this.get('name'), 'add', plug,obj.get('name'))
- if (!this.plughas(plug,obj)) { this[plug].add(obj) }
+ if (!this.plughas(plug,obj)) { this[plug].add(obj); }
},
plugremove: function(plug,obj) {
- this[plug].remove(obj)
+ this[plug].remove(obj);
},
plughas: function(plug, obj) {
- return (this[plug].indexOf(obj) != -1)
+ return (this[plug].indexOf(obj) != -1);
}
-})
+});
// GraphNode specializes GenericGraphNode by adding 'children' and 'parents' plugs
@@ -151,54 +201,52 @@ var GraphNode = GenericGraphNode.extend4000({
initialize: function() {
var self = this;
- this.addplug('parents','parent')
- this.addplug('children','child')
+ this.addplug('parents','parent');
+ this.addplug('children','child');
this.parents.on('add',function(obj) {
- obj.addchild(self)
- })
+ obj.addchild(self);
+ });
this.children.on('add',function(obj) {
- obj.addparent(self)
- })
+ obj.addparent(self);
+ });
}
-})
+});
var CommNode = GraphNode.extend4000({
MsgIn: function(message) {
return _.flatten(_.union(
[ this.Respond(message) ],
- this.children.map(function(child) { return child.MsgIn(message) })
+ this.children.map(function(child) { return child.MsgIn(message); })
));
},
MsgOut: function(message) {
- return _.flatten(this.parents.map(function(parent) { parent.MsgOut(message) }))
+ return _.flatten(this.parents.map(function(parent) { parent.MsgOut(message); }));
},
Respond: function(message) {
// override me
}
-})
+});
var ObjectSync = CommNode.extend4000({
initialize: function() {
var self = this;
- this.addplug('obj')
+ this.addplug('obj');
this.obj.on('flush',function(model,changes) {
//self.MsgOut(changes)
- })
+ });
}
-})
+});
var DbObjectSync = ObjectSync.extend4000({
- initialize: function() {},
- query: function() {},
-})
+});
@@ -209,5 +257,5 @@ var RemoteModel = Backbone.Model.extend4000({
},
flush: function() {
}
-})
+});

No commit comments for this range

Something went wrong with that request. Please try again.