mirror of
https://github.com/amark/gun.git
synced 2025-03-30 15:08:33 +00:00
event emitter finished!
This commit is contained in:
parent
f84beca9ed
commit
0ed28b81e2
42
gun.js
42
gun.js
@ -135,29 +135,54 @@
|
||||
Type.time.is = function(t){ return t? t instanceof Date : (+new Date().getTime()) }
|
||||
}(Util));
|
||||
;(function(exports, add, emit){ // On event emitter generic javascript utility.
|
||||
function Act(fn, at, on){
|
||||
function Act(tag, fn, at, on, ctx){
|
||||
this.tag = tag;
|
||||
this.fn = fn;
|
||||
this.at = at;
|
||||
this.on = on;
|
||||
this.ctx = ctx;
|
||||
}
|
||||
Act.chain = Act.prototype;
|
||||
Act.chain.stun = function(){
|
||||
this.halt = true;
|
||||
if(!arguments.length){
|
||||
return this.halt = true;
|
||||
}
|
||||
var act = this, on = act.on, halt = {
|
||||
resume: function(arg){
|
||||
act.halt = false;
|
||||
act.ctx.on(act.tag, (arguments.length?
|
||||
1 === arguments.length? arg : Array.prototype.slice.call(arguments)
|
||||
: halt.arg), halt.end, halt.as, act);
|
||||
}, arg: on.arg,
|
||||
end: on.end,
|
||||
as: on.as
|
||||
};
|
||||
act.halt = 1;
|
||||
return halt.resume;
|
||||
}
|
||||
Act.chain.off = function(){
|
||||
this.fn = noop;
|
||||
}
|
||||
function noop(){};
|
||||
function Event(tag, arg, at, t){
|
||||
function Event(tag, arg, at, as, skip){
|
||||
var ctx = this, ons = ctx.ons || (ctx.ons = {}), on = ons[tag] || (ons[tag] = {s: []}), act, mem;
|
||||
if(arg instanceof Function){
|
||||
on.s.push(act = new Act(arg, at, ctx));
|
||||
on.s.push(act = new Act(tag, arg, at, on, ctx));
|
||||
if(add){ add(tag, act, on, ctx) }
|
||||
return;
|
||||
}
|
||||
if(emit){ emit(tag, arg, on, ctx) }
|
||||
var i = 0, acts = on.s, l = acts.length, arr = (arg instanceof Array), off, act;
|
||||
on.arg = arg;
|
||||
on.end = at;
|
||||
on.as = as;
|
||||
var i = 0, acts = on.s, l = acts.length, arr = (arg instanceof Array), gap, off, act;
|
||||
for(i; i < l; i++){ act = acts[i];
|
||||
if(skip){
|
||||
if(skip === act){
|
||||
skip = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(!arr){
|
||||
act.fn.call(act.at, arg, act);
|
||||
} else {
|
||||
@ -167,6 +192,9 @@
|
||||
off = true;
|
||||
}
|
||||
if(act.halt){
|
||||
if(1 === act.halt){
|
||||
gap = true;
|
||||
}
|
||||
act.halt = false;
|
||||
break;
|
||||
}
|
||||
@ -180,8 +208,8 @@
|
||||
}
|
||||
on.s = still;
|
||||
}
|
||||
if(at && at instanceof Function){
|
||||
at(arg, t);
|
||||
if(!gap && at && at instanceof Function){
|
||||
at.call(as, arg);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -477,10 +477,10 @@ describe('Gun', function(){
|
||||
});
|
||||
e.on('foo', 1);
|
||||
});
|
||||
it('double go', function(done){
|
||||
var on = Gun.on.create();
|
||||
on('foo', function(a){
|
||||
var go = this.stop(go);
|
||||
it('double resume', function(done){
|
||||
var e = {on: Gun.on};
|
||||
e.on('foo', function(a, ev){
|
||||
var resume = ev.stun(resume);
|
||||
setTimeout(function(){
|
||||
if(1 === a){
|
||||
done.first1 = true;
|
||||
@ -489,10 +489,10 @@ describe('Gun', function(){
|
||||
if(2 === a){
|
||||
done.first2 = true;
|
||||
}
|
||||
go();
|
||||
resume();
|
||||
},10);
|
||||
});
|
||||
on('foo').event(function(a){
|
||||
e.on('foo', function(a, ev){
|
||||
done.second = true;
|
||||
if(1 === a){
|
||||
expect(done.first2).to.not.be.ok();
|
||||
@ -505,115 +505,117 @@ describe('Gun', function(){
|
||||
}
|
||||
}
|
||||
});
|
||||
on('foo').emit(1);
|
||||
on('foo').emit(2);
|
||||
e.on('foo', 1);
|
||||
e.on('foo', 2);
|
||||
});
|
||||
it('double go different event', function(done){
|
||||
var on = Gun.on.create();
|
||||
on('foo', function(a){
|
||||
var go = this.stop(go);
|
||||
it('double resume different event', function(done){
|
||||
var e = {on: Gun.on};
|
||||
e.on('foo', function(a, ev){
|
||||
var resume = ev.stun(resume);
|
||||
setTimeout(function(){
|
||||
done.first1 = true;
|
||||
go();
|
||||
resume();
|
||||
},10);
|
||||
});
|
||||
on('foo').event(function(a){
|
||||
e.on('foo', function(a){
|
||||
if(1 === a){
|
||||
expect(done.first1).to.be.ok();
|
||||
done();
|
||||
}
|
||||
});
|
||||
on('foo').emit(1);
|
||||
on('bar').emit(2);
|
||||
e.on('foo', 1);
|
||||
e.on('bar', 2);
|
||||
});
|
||||
it('go params', function(done){
|
||||
var on = Gun.on.create();
|
||||
on('foo', function(a){
|
||||
var go = this.stop(go);
|
||||
it('resume params', function(done){
|
||||
var e = {on: Gun.on};
|
||||
e.on('foo', function(a, ev){
|
||||
var resume = ev.stun(resume);
|
||||
setTimeout(function(){
|
||||
expect(done.second).to.not.be.ok();
|
||||
go(0);
|
||||
resume(0);
|
||||
},10);
|
||||
});
|
||||
on('foo').event(function(a){
|
||||
e.on('foo', function(a){
|
||||
done.second = true;
|
||||
expect(a).to.be(0);
|
||||
done();
|
||||
});
|
||||
on('foo').emit(1);
|
||||
e.on('foo', 1);
|
||||
});
|
||||
it('map', function(done){
|
||||
var on = Gun.on.create();
|
||||
on('foo', function(a){
|
||||
var go = this.stop(go);
|
||||
var e = {on: Gun.on};
|
||||
e.on('foo', function(a, ev){
|
||||
var resume = ev.stun(resume);
|
||||
Gun.obj.map(a.it, function(v,f){
|
||||
setTimeout(function(){
|
||||
var emit = {field: 'where', soul: f};
|
||||
go(emit);
|
||||
resume(emit);
|
||||
},10);
|
||||
})
|
||||
});
|
||||
on('foo').event(function(a){
|
||||
var go = this.stop(go);
|
||||
e.on('foo', function(a, ev){
|
||||
var resume = ev.stun(resume);
|
||||
setTimeout(function(){
|
||||
go({node: a.soul});
|
||||
resume({node: a.soul});
|
||||
},100);
|
||||
});
|
||||
on('foo').end(function(a){
|
||||
e.on('foo', function(a){
|
||||
if('a' == a.node){
|
||||
done.a = true;
|
||||
} else {
|
||||
expect(done.a).to.be.ok();
|
||||
done();
|
||||
}
|
||||
}).emit({field: 'where', it: {a: 1, b: 2}});
|
||||
});
|
||||
e.on('foo', {field: 'where', it: {a: 1, b: 2}});
|
||||
});
|
||||
it('map synchronous', function(done){
|
||||
var on = Gun.on.create();
|
||||
on('foo', function(a){
|
||||
var go = this.stop(go);
|
||||
var e = {on: Gun.on};
|
||||
e.on('foo', function(a, ev){
|
||||
var resume = ev.stun(resume);
|
||||
Gun.obj.map(a.node, function(v,f){
|
||||
//setTimeout(function(){
|
||||
var emit = {field: 'where', soul: f};
|
||||
go(emit);
|
||||
resume(emit);
|
||||
//},10);
|
||||
})
|
||||
});
|
||||
on('foo').event(function(a){
|
||||
var go = this.stop(go);
|
||||
e.on('foo', function(a, ev){
|
||||
var resume = ev.stun(resume);
|
||||
setTimeout(function(){
|
||||
go({node: a.soul});
|
||||
resume({node: a.soul});
|
||||
},100);
|
||||
});
|
||||
on('foo').end(function(a){
|
||||
e.on('foo', {field: 'where', node: {a: 1, b: 2}}, function(a){
|
||||
expect(this.hi).to.be(1);
|
||||
if('a' == a.node){
|
||||
done.a = true;
|
||||
} else {
|
||||
expect(done.a).to.be.ok();
|
||||
done();
|
||||
}
|
||||
}).emit({field: 'where', node: {a: 1, b: 2}});
|
||||
}, {hi: 1});
|
||||
});
|
||||
it('map synchronous async', function(done){
|
||||
var on = Gun.on.create();
|
||||
on('foo', function(a){
|
||||
var e = {on: Gun.on};
|
||||
e.on('foo', function(a){
|
||||
expect(a.b).to.be(5);
|
||||
done.first = true;
|
||||
});
|
||||
on('foo').event(function(a){
|
||||
e.on('foo', function(a, ev){
|
||||
expect(a.b).to.be(5);
|
||||
done.second = true;
|
||||
var go = this.stop(go);
|
||||
var resume = ev.stun(resume);
|
||||
setTimeout(function(){
|
||||
go({c: 9});
|
||||
resume({c: 9});
|
||||
},100);
|
||||
});
|
||||
on('foo').end(function(a){
|
||||
e.on('foo', {b: 5}, function(a){
|
||||
expect(a.c).to.be(9);
|
||||
expect(done.first).to.be.ok();
|
||||
expect(done.second).to.be.ok();
|
||||
done();
|
||||
}).emit({b: 5});
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('Gun Safety', function(){
|
||||
|
Loading…
x
Reference in New Issue
Block a user