Skip to content

Commit

Permalink
Redesign. Eats a lot of memory
Browse files Browse the repository at this point in the history
  • Loading branch information
holywarez committed Sep 27, 2009
1 parent e85367d commit f08175a
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 140 deletions.
38 changes: 1 addition & 37 deletions public/javascripts/lib/inplace/meta.js
Expand Up @@ -2,48 +2,12 @@ var $inplace = $inplace || {};

(function($) {
$inplace.Meta = {
__template: function(options) {
this.options = options;
},

create: function(options) {
var instance = new this.__template(options);
if('function'==typeof(this.__constructor)) {
this.__constructor.apply(instance, options);
}
instance.meta = this;
return instance;
},

clone: function() {
return $.extend(true, {}, this);
},

hasMethod: function(name, handler) {
var proto = this.__template.prototype;
this[name] = function(methodHandler) {
if("function" != typeof(methodHandler)) return this;
proto[name] = methodHandler;
};

// @handler argument is optional
this[name](handler);

return this;
},

hasConstructor: function(constructor) {
this.__constructor = constructor;
return this;
},

meta: function(obj) {
$.extend(this, obj);
return this;
},

extend: function(obj) {
$.extend(this.__template.prototype, obj);
$.extend(this, obj);
return this;
}
};
Expand Down
162 changes: 74 additions & 88 deletions public/javascripts/lib/inplace/nodes.js
@@ -1,102 +1,88 @@
(function($) {
$inplace.MessageTrapException = { type: 'message-trap' };

$inplace.Message = $inplace.Meta.clone()
.hasConstructor(function(options) {
this.topic = options.topic;
this.sender = options.sender;
this.data = options.data;
})
.meta({
newMessage: function(topic, sender, data) {
return this.create({ topic: topic, sender: sender, data: data });
}
});
$inplace.Message = function(topic, sender, data) {
this.topic = topic;
this.sender = sender;
this.data = data;
};

$inplace.Node = $inplace.Meta.clone()
.hasMethod('appendChild')

.hasMethod('_processMessage')
.hasMethod('_processChildMessage')
.hasMethod('childMessageHandler')
.hasMethod('messageHandler')

.hasMethod('sendMessage')
.hasMethod('sendParentMessage')

.hasMethod('subscribe')
.hasMethod('subscribeUpstream')
.extend({

.hasConstructor(function(options) {
this.__childs = [];
this.__downstreamHandlers = {};
this.__upstreamHandlers = {};
})
hasConstructor: function(options) {
this.__childs = [];
this.__downstreamHandlers = {};
this.__upstreamHandlers = {};
},

.appendChild(function(child) {
this.__childs.push(child);
child.parentNode = this;
return this;
})
appendChild: function(child) {
this.__childs.push(child);
child.parentNode = this;
return this;
},

.messageHandler(function(msg){
var handlers = this.__downstreamHandlers[msg.topic];
if(handlers) {
$.each(handlers, function(handler) {
handler(msg);
});
}
})
.childMessageHandler(function(msg){
var handlers = this.__upstreamHandlers[msg.topic];
if(handlers) {
$.each(handlers, function(handler) {
handler(msg);
});
}
})
messageHandler: function(msg){
var handlers = this.__downstreamHandlers[msg.topic];
if(handlers) {
$.each(handlers, function(handler) {
handler(msg);
});
}
},

childMessageHandler: function(msg){
var handlers = this.__upstreamHandlers[msg.topic];
if(handlers) {
$.each(handlers, function(handler) {
handler(msg);
});
}
},

.sendMessage(function(msg) {
this._processMessage(msg);
})
sendMessage: function(msg) {
this._processMessage(msg);
},

.sendParentMessage(function(msg){
if(!this.parentNode) return this;
this.parentNode._processChildMessage(msg);
return this;
})
sendParentMessage: function(msg){
if(!this.parentNode) return this;
this.parentNode._processChildMessage(msg);
return this;
},

._processChildMessage(function(msg) {
try {
this.childMessageHandler(msg);
if(!this.parentNode) return;
this.parentNode._processChildMessage(msg);
} catch (exc) {
console.log('Exception while sending message to parent: '+exc.type);
}
})

._processMessage(function(msg) {
try {
this.messageHandler(msg);
if(this.__childs) {
$.each(this.__childs, function(child) {
child._processMessage(msg);
});
}
} catch (exc) {
console.log("Exception while processing broadcast message: "+exc.type);
}
})
.subscribe(function(topic, handler) {
this.__downstreamHandlers[topic] = this.__downstreamHandlers[topic] || [];
this.__downstreamHandlers[topic].push(handler);
})
.subscribeUpstream(function(topic, handler) {
this.__upstreamHandlers[topic] = this.__upstreamHandlers[topic] || [];
this.__upstreamHandlers[topic].push(handler);
});
_processChildMessage: function(msg) {
try {
this.childMessageHandler(msg);
if(!this.parentNode) return;
this.parentNode._processChildMessage(msg);
} catch (exc) {
console.log('Exception while sending message to parent: '+exc.type);
}
},

_processMessage: function(msg) {
try {
this.messageHandler(msg);
if(this.__childs) {
$.each(this.__childs, function(child) {
child._processMessage(msg);
});
}
} catch (exc) {
console.log("Exception while processing broadcast message: "+exc.type);
}
},

subscribe: function(topic, handler) {
this.__downstreamHandlers[topic] = this.__downstreamHandlers[topic] || [];
this.__downstreamHandlers[topic].push(handler);
},

subscribeUpstream: function(topic, handler) {
this.__upstreamHandlers[topic] = this.__upstreamHandlers[topic] || [];
this.__upstreamHandlers[topic].push(handler);
}
});


})(jQuery);
Expand Down
82 changes: 67 additions & 15 deletions public/javascripts/lib/inplace/states.js
@@ -1,21 +1,73 @@
(function($){
$inplace.states = {};
$inplace.states.Transition = function(event, target, handlers) {
this.event = event;
this.target = target;
this.handlers = handlers;
};

$inplace.State = $inplace.Node.clone()
.meta({
hasState: function(stateName, behaviour) {
var behaviour = behaviour || $inplace.State;
this.__states = this.__states || {};
var state = this.__states[stateName];
if(!state) {
state = behaviour.clone();
this.appendChild(state); /// Aghtung!!! this is not a meta method!
this.__states[stateName] = state;
}
return state;
},
.extend({
hasState: function(stateName, behaviour) {
var behaviour = behaviour || $inplace.State;
this.__states = this.__states || {};
//this.__pstates = this.__pstates || {};

parentState: function() {
return this.parentNode();
var state = this.__states[stateName];
if(!state) { // ?
state = behaviour.clone();
this.appendChild(state);
this.__states[stateName] = state;
//this.__pstates[state] = stateName;
}
return state;
},

_hasTransition: function(from, description) {
this.__transitions = this.__transitions || {};
var trans = this.__transitions[from];
if(!trans) { // ?
this.__transitions[from] = trans = {};
}
});

// continue here
},

hasTransition: function(transitionDescription) {
this.parentState()._hasTransition(this, transitionDescription);
return this;
},

hasTransitions: function(descriptions) {
var _this = this;
$.each(descriptions, function(description) {
_this.hasTransition(description);
});

return this;
},

parentState: function() {
return this.parentNode;
},

setDefaultState: function(stateName) {
this.defaultState = stateName;
return this;
},

makeHandlerMaker: function(handlerName) {
this.__handlers = this.__handlers || {};
this[handlerName] = function(handler) {
if(handler) {
this.__handlers[handlerName] = handler;
} else {
this.__handlers[handlerName].apply(this);
}
return this;
};
return this;
}
});

})(jQuery);

0 comments on commit f08175a

Please sign in to comment.