Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Test/event-bus-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,34 @@ const lileiCallback1 = (...payload) => {
console.log("lileiCallback1:", payload)
}

const symbolCallback1 = (...payload) => {
console.log("symbolCallback1:", payload)
}

const s1 = Symbol('symbol1')

eventBus.on("why", whyCallback1)
eventBus.on("why", whyCallback2)
eventBus.on('lilei', lileiCallback1)
eventBus.on(s1, whyCallback1)
eventBus.once("why", (...payload) => {
console.log("why once:", payload)
})

setTimeout(() => {
eventBus.emit("why", "abc", "cba", "nba")
eventBus.emit("lilei", "abc", "cba", "nba")
eventBus.emit(s1, 111, 222, 333)
}, 1000);

setTimeout(() => {
eventBus.off("why", whyCallback1)
eventBus.off("lilei", lileiCallback1)
eventBus.off(s1, whyCallback1)
}, 2000);

setTimeout(() => {
eventBus.emit("why")
eventBus.emit("lilei")
eventBus.emit(s1)
}, 3000);
20 changes: 10 additions & 10 deletions src/event-bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class HYEventBus {
}

on(eventName, eventCallback, thisArg) {
if (typeof eventName !== "string") {
throw new TypeError("the event name must be string type")
if (typeof eventName !== "string" && typeof eventName !== "symbol") {
throw new TypeError("the event name must be string type or symbol type")
}

if (typeof eventCallback !== "function") {
throw new TypeError("the event callback must be function type")
}

let hanlders = this.eventBus[eventName]
if (!hanlders) {
hanlders = []
Expand All @@ -26,14 +26,14 @@ class HYEventBus {
}

once(eventName, eventCallback, thisArg) {
if (typeof eventName !== "string") {
throw new TypeError("the event name must be string type")
if (typeof eventName !== "string" && typeof eventName !== "symbol") {
throw new TypeError("the event name must be string type or symbol type")
}

if (typeof eventCallback !== "function") {
throw new TypeError("the event callback must be function type")
}

const tempCallback = (...payload) => {
this.off(eventName, tempCallback)
eventCallback.apply(thisArg, payload)
Expand All @@ -43,8 +43,8 @@ class HYEventBus {
}

emit(eventName, ...payload) {
if (typeof eventName !== "string") {
throw new TypeError("the event name must be string type")
if (typeof eventName !== "string" && typeof eventName !== "symbol") {
throw new TypeError("the event name must be string type or symbol type")
}

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

off(eventName, eventCallback) {
if (typeof eventName !== "string") {
throw new TypeError("the event name must be string type")
if (typeof eventName !== "string" && typeof eventName !== "symbol") {
throw new TypeError("the event name must be string type or symbol type")
}

if (typeof eventCallback !== "function") {
Expand Down