Skip to content

Instantly share code, notes, and snippets.

@ssddi456
Last active December 11, 2015 16:59
Show Gist options
  • Save ssddi456/4631345 to your computer and use it in GitHub Desktop.
Save ssddi456/4631345 to your computer and use it in GitHub Desktop.
function orderedFlow ( order ) {
this.order = order;
this.listnerId = uuid('orderedFlow.');
this.dataStack = [];
this.index = 0;
this.interval = 0;
this.maxListen= 500;
this.nextTime = 0;
this.timeout = undefined;
var self = this;
this.listner = function(e,data){
if(e.timestamp < this.nextTime ){
return;
}
if( self.timeout != undefined && e.timestamp > self.timeout ){
self.reset();
}
if(e.type == self.order[self.index]){
self.index ++;
self.nextTime = e.timestamp + self.interval;
self.dataStack.push(data);
} else {
self.reset();
}
if( self.order[self.index] == undefined){
flow.fire(self.dataStack);
self.reset();
}
}
this.order.forEach(function(name){
flow.on(name,self.listner);
});
this.reset();
};
ofproto = orderedFlow.prototype;
ofproto.bind = function(cb){
flow.on(this.listnerId,cb);
return this;
};
ofproto.once = function(cb){
flow.on(this.listnerId,cb);
return this;
};
ofproto.unbind =function(){
this.order.forEach(function(name){
flow.unbind(name);
});
};
ofproto.reset = function(){
this.index = 0;
this.nextTime = 0;
this.timeout = undefined;
this.dataStack= [];
};
@ssddi456
Copy link
Author

a order flow implement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment