it.should("listen once to an event", function () { var ret = new comb.Promise(), called = 0; db.listenOnce("myChannel").chain(function (payload) { assert.equal(payload, "hello1"); called++; ret.callback(); }); return when( db.notify("myChannel", "hello1"), db.notify("myChannel", "hello2"), db.notify("myChannel", "hello3"), ret ).chain(function () { assert.equal(called, 1); called = 0; db.listenOnce("myChannel").chain(function (payload) { assert.equal(payload, "hello1"); called++; }); return db.notify("myChannel", "hello1") .chain(function () { return db.notify("myChannel", "hello2"); }) .chain(function () { return db.notify("myChannel", "hello3"); }) .chain(function () { assert.equal(called, 1); }); }); });
db.listen("myChannel", function (msg) { try { assert.deepEqual(msg, {msg: "hello"}); db.unListen("myChannel").chain(function () { assert.deepEqual(db.sqls, ['LISTEN my_channel', "NOTIFY my_channel , '{\"msg\":\"hello\"}'", "UNLISTEN my_channel"]); assert.deepEqual(db.__listeners, {}); ret.callback(); }).addErrback(ret); } catch (e) { ret.errback(e); } }).chain(function () {
it.should("listen to a channel", function () { var ret = new comb.Promise(); db.listen("myChannel", function (msg) { try { assert.deepEqual(msg, {msg: "hello"}); db.unListen("myChannel").chain(function () { assert.deepEqual(db.sqls, ['LISTEN my_channel', "NOTIFY my_channel , '{\"msg\":\"hello\"}'", "UNLISTEN my_channel"]); assert.deepEqual(db.__listeners, {}); ret.callback(); }).addErrback(ret); } catch (e) { ret.errback(e); } }).chain(function () { assert.isTrue("my_channel" in db.__listeners); assert.deepEqual(db.sqls, ['LISTEN my_channel']); return db.notify("myChannel", {msg: "hello"}); }).addErrback(ret); return ret; });
db.listenOnce("myChannel").chain(function (payload) { assert.equal(payload, "hello1"); called++; ret.callback(); });
db.unListen("myChannel").chain(function () { assert.deepEqual(db.sqls, ['LISTEN my_channel', "NOTIFY my_channel , '{\"msg\":\"hello\"}'", "UNLISTEN my_channel"]); assert.deepEqual(db.__listeners, {}); ret.callback(); }).addErrback(ret);