Skip to content

Commit 046a4d9

Browse files
committed
Throw on sub without listener; log pub/sub/unsub
1 parent 6a82066 commit 046a4d9

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pubsub
22

3-
`pubsub` is a small, fast JavaScript library that implements [Publish/Subscribe](http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) pattern for web applications.
3+
`pubsub` is a small, fast JavaScript library that implements the [Publish/Subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) pattern for web applications.
44

55
## Usage
66

src/pubsub.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class pubsub {
4545
}
4646

4747
if (this.options.log) {
48-
this.options.log(event, args);
48+
this.options.log('pub', event, args);
4949
}
5050

5151
for (j = 0, jlen = eventArray.length; j < jlen; j++) {
@@ -75,15 +75,23 @@ class pubsub {
7575
*/
7676
sub(eventStr, method, thisArg, flags) {
7777
if (!eventStr) {
78-
throw new Error('pubsub.sub() received empty event string');
78+
throw new Error('sub(): received empty event string');
7979
}
8080

81-
if (thisArg && this.options.strict) {
81+
if (!method) {
82+
throw new Error('sub(): listener is undefined');
83+
}
84+
85+
if (this.options.strict && thisArg) {
8286
console.warn(
8387
`[Deprecated] sub(): thisArg is deprecated; bind method manually instead. Re: ${eventStr}`
8488
);
8589
}
8690

91+
if (this.options.log) {
92+
this.options.log('sub', eventStr, method, flags);
93+
}
94+
8795
var events;
8896

8997
if (Array.isArray(eventStr)) {
@@ -148,6 +156,10 @@ class pubsub {
148156
console.warn(`[Deprecated] unsub(): called without a listener. Re: ${eventStr}`);
149157
}
150158

159+
if (this.options.log) {
160+
this.options.log('unsub', eventStr, method, flags);
161+
}
162+
151163
var events = eventStr.split(/\s+/),
152164
event,
153165
eventArray,

0 commit comments

Comments
 (0)