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
11 changes: 10 additions & 1 deletion Test/event-bus-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,40 @@ const whyCallback1 = (...payload) => {
}

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

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

const ztCallback1 = (...payload) => {
console.log('ztCallback1', payload)
}

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

setTimeout(() => {
eventBus.emit("why", "abc", "cba", "nba")
eventBus.emit("lilei", "abc", "cba", "nba")
eventBus.emit("zzt", "abc", "cba", "nba")
}, 1000);

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

setTimeout(() => {
eventBus.emit("why")
eventBus.emit("lilei")
eventBus.emit('zzt')
}, 3000);
11 changes: 11 additions & 0 deletions src/event-bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ class HYEventBus {
delete this.eventBus[eventName]
}
}

clear(...eventNames) {
if (!eventNames.length) {
this.eventBus = {}
return
}
if (eventNames.some(eventName => typeof eventName !== 'string')) {
throw new TypeError("the event name must be string type")
}
eventNames.forEach(eventName => delete this.eventBus[eventName])
}
}

module.exports = HYEventBus