Skip to content

Commit

Permalink
Issue menacher#37 Moved CodecChain functions to prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
menacher committed Jan 30, 2013
1 parent ae78fff commit c817856
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
65 changes: 33 additions & 32 deletions jetclient-js/src/jet-0.1.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

// Functions
// Creates a new event object
jet.nevent = function (eventType, payload, date){
jet.NEvent = function (eventType, payload, date){
return {
type : eventType,
source : payload,
Expand All @@ -45,37 +45,34 @@

// Creates a login event object to login to remote jetserver
jet.LoginEvent = function (config){
return jet.nevent(jet.LOG_IN,[config.user,config.pass,config.connectionKey]);
return jet.NEvent(jet.LOG_IN,[config.user,config.pass,config.connectionKey]);
};

// If using a differnt protocol, then use this codec chain, to decode and encode incoming and outgoing requests. Something like a Chain of Responsibility pattern.
jet.CodecChain = function (){
var me = this;
me.chain = [];
for(var i = 0; i < arguments.length; i++) {
me.chain.push(arguments[i]);
}

me.add = function (func){
me.chain.push(func);
};
this.chain = [];
};

me.remove = function (func){
var index = me.chain.indexOf(func);
while(index != -1){
me.chain.splice(index,1);
index = me.chain.indexOf(func);
}
};
jet.CodecChain.prototype.add = function (func){
if (func && typeof(func) === 'function') {
this.chain.push(func);
} else{
throw new Error("Parameter:" + func + " is not of type function.");
}
return this;
};

me.tranform = function transform(message){
for(var i = 0; i < me.chain.length(); i++){
message = me.chain[i].transform(message);
}
return message;
};
jet.CodecChain.prototype.remove = function (func){
removeFromArray(this.chain,func);
};

jet.CodecChain.prototype.tranform = function (message){
for(var i = 0; i < this.chain.length(); i++){
message = this.chain[i].transform(message);
}
return message;
};

// Default codes which use JSON to decode and encode messages.
jet.Codecs = {
encoder : {transform: function (e){ return JSON.stringify(e)}},
Expand Down Expand Up @@ -153,14 +150,7 @@
};

me.removeHandler = function(eventName, handler){
var handlers = callbacks[eventName];
if (handlers instanceof Array){
var index = handlers.indexOf(handler);
while(index != -1){
handlers.splice(index,1);
index = handlers.indexOf(handler);
}
}
removeFromArray(callbacks[eventName], handler);
};

me.clearHandlers = function (){
Expand Down Expand Up @@ -201,4 +191,15 @@
dispatch(evt.type, evt);
}
}

function removeFromArray(chain, func){
if(chain instanceof Array){
var index = chain.indexOf(func);
while(index != -1){
chain.splice(index,1);
index = chain.indexOf(func);
}
}
}

}( window.jet = window.jet || {}));
4 changes: 2 additions & 2 deletions jetclient-js/test/jetclient.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
}

function defender(session){
session.send(jet.nevent(jet.NETWORK_MESSAGE, [2,1]));
session.send(jet.NEvent(jet.NETWORK_MESSAGE, [2,1]));
}
function zombie(session){
session.send(jet.nevent(jet.NETWORK_MESSAGE, [1,2]));
session.send(jet.NEvent(jet.NETWORK_MESSAGE, [1,2]));
}
}

Expand Down

0 comments on commit c817856

Please sign in to comment.