Skip to content

Commit

Permalink
0.1.3
Browse files Browse the repository at this point in the history
- unsubscribing not existing callback doesn't throw error anymore.
- add procedureCb method.
- add unsubscribeProcedure method.
  • Loading branch information
Choi KyuWoo committed Mar 14, 2016
1 parent 4ccf465 commit 6dc65fa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
39 changes: 31 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class CoMediator {
}
return;
}
throw 'no such subscriber symbol';
}
}

Expand Down Expand Up @@ -112,6 +111,19 @@ class CoMediator {
});
}


/***
* procedure call with callback
*
* @param channel
* @param resolveCallback
* @param rejectCallback
* @param data
*/
procedureCb(channel, resolveCallback, rejectCallback, ...data) {
this.procedure(channel, ...data).then(resolveCallback).catch(rejectCallback);
}

/**
* subscribe procedure
*
Expand All @@ -120,20 +132,31 @@ class CoMediator {
* @returns {Symbol}
*/
subscribeProcedure(channel, callback) {
let procedureSubscribers = INTERNAL.get(this).procedureSubscribers;
Object.getOwnPropertySymbols(procedureSubscribers).forEach(function (subscriberSymbol) {
let procedureSubscriber = procedureSubscribers[subscriberSymbol];
if (procedureSubscriber.channel === channel) {
delete procedureSubscribers[subscriberSymbol];
}
});
this.unsubscribeProcedure(channel);
let subscriberSymbol = Symbol();
INTERNAL.get(this).procedureSubscribers[subscriberSymbol] = {
channel: channel,
callback: callback
};
return subscriberSymbol;
}

/**
* un-subscribe procedure
*
* @param channel
*/
unsubscribeProcedure(channel) {
let procedureSubscribers = INTERNAL.get(this).procedureSubscribers;
let symbols = Object.getOwnPropertySymbols(procedureSubscribers);
for (let i = 0; i < symbols.length; i += 1) {
let subscriber = procedureSubscribers[symbols[i]];
if (subscriber.channel === channel) {
delete procedureSubscribers[symbols[i]];
return;
}
}
}
}

export default CoMediator;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "co-mediator",
"version": "0.1.2",
"version": "0.1.3",
"description": "co mediator",
"homepage": "https://github.com/PAIO-CO-KR/co-mediator",
"author": {
Expand Down
23 changes: 7 additions & 16 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('co-mediator', function () {
cm.publish('test', testData1, testData2);
});

it('subscribed callback function shouldn\' be called after unsubscribe', function (done) {
it('subscribed callback function shouldn\'t be called after unsubscribe', function (done) {
let cm = new CoMediator();
let testData = 'a string';
let subscriberSymbol = cm.subscribe('test', function* () {
Expand All @@ -102,20 +102,6 @@ describe('co-mediator', function () {
}, 10);
});

it('trying to unsubscribe not existing subscriber should issue an error', function (done) {
let cm = new CoMediator();
let subscriberSymbol = cm.subscribe('test', function* () {});
cm.unsubscribe(subscriberSymbol);
try {
cm.unsubscribe(subscriberSymbol);
} catch (e) {
assert(e === 'no such subscriber symbol');
done();
return;
}
done('error should be issued');
});

it('procedure should be registered/called with param and returns result', function (done) {
let cm = new CoMediator();
cm.subscribeProcedure('ch', function* () {
Expand All @@ -135,7 +121,12 @@ describe('co-mediator', function () {
})
.catch(function (e) {
assert(e === 'no procedure found', 'should throw error');
done();
cm.procedureCb('ch', function (val2) {
assert(val2 === 'result', 'should return result');
done();
}, function (e2) {
done('proper call should not be fell in with exeption' + e2);
}, 'param');
});
})
.catch(function (e) {
Expand Down

0 comments on commit 6dc65fa

Please sign in to comment.