Skip to content

Commit 83da43a

Browse files
committed
feat(event-bus): add support for Symbol type for the eventName parameter
1 parent 12e4e65 commit 83da43a

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

Test/event-bus-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,34 @@ const lileiCallback1 = (...payload) => {
1414
console.log("lileiCallback1:", payload)
1515
}
1616

17+
const symbolCallback1 = (...payload) => {
18+
console.log("symbolCallback1:", payload)
19+
}
20+
21+
const s1 = Symbol('symbol1')
22+
1723
eventBus.on("why", whyCallback1)
1824
eventBus.on("why", whyCallback2)
1925
eventBus.on('lilei', lileiCallback1)
26+
eventBus.on(s1, whyCallback1)
2027
eventBus.once("why", (...payload) => {
2128
console.log("why once:", payload)
2229
})
2330

2431
setTimeout(() => {
2532
eventBus.emit("why", "abc", "cba", "nba")
2633
eventBus.emit("lilei", "abc", "cba", "nba")
34+
eventBus.emit(s1, 111, 222, 333)
2735
}, 1000);
2836

2937
setTimeout(() => {
3038
eventBus.off("why", whyCallback1)
3139
eventBus.off("lilei", lileiCallback1)
40+
eventBus.off(s1, whyCallback1)
3241
}, 2000);
3342

3443
setTimeout(() => {
3544
eventBus.emit("why")
3645
eventBus.emit("lilei")
46+
eventBus.emit(s1)
3747
}, 3000);

src/event-bus.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ class HYEventBus {
44
}
55

66
on(eventName, eventCallback, thisArg) {
7-
if (typeof eventName !== "string") {
8-
throw new TypeError("the event name must be string type")
7+
if (typeof eventName !== "string" && typeof eventName !== "symbol") {
8+
throw new TypeError("the event name must be string type or symbol type")
99
}
1010

1111
if (typeof eventCallback !== "function") {
1212
throw new TypeError("the event callback must be function type")
1313
}
14-
14+
1515
let hanlders = this.eventBus[eventName]
1616
if (!hanlders) {
1717
hanlders = []
@@ -26,14 +26,14 @@ class HYEventBus {
2626
}
2727

2828
once(eventName, eventCallback, thisArg) {
29-
if (typeof eventName !== "string") {
30-
throw new TypeError("the event name must be string type")
29+
if (typeof eventName !== "string" && typeof eventName !== "symbol") {
30+
throw new TypeError("the event name must be string type or symbol type")
3131
}
3232

3333
if (typeof eventCallback !== "function") {
3434
throw new TypeError("the event callback must be function type")
3535
}
36-
36+
3737
const tempCallback = (...payload) => {
3838
this.off(eventName, tempCallback)
3939
eventCallback.apply(thisArg, payload)
@@ -43,8 +43,8 @@ class HYEventBus {
4343
}
4444

4545
emit(eventName, ...payload) {
46-
if (typeof eventName !== "string") {
47-
throw new TypeError("the event name must be string type")
46+
if (typeof eventName !== "string" && typeof eventName !== "symbol") {
47+
throw new TypeError("the event name must be string type or symbol type")
4848
}
4949

5050
const handlers = this.eventBus[eventName] || []
@@ -55,8 +55,8 @@ class HYEventBus {
5555
}
5656

5757
off(eventName, eventCallback) {
58-
if (typeof eventName !== "string") {
59-
throw new TypeError("the event name must be string type")
58+
if (typeof eventName !== "string" && typeof eventName !== "symbol") {
59+
throw new TypeError("the event name must be string type or symbol type")
6060
}
6161

6262
if (typeof eventCallback !== "function") {

0 commit comments

Comments
 (0)