确定API 以及 测试用例
- EventHub#on
- EventHub#off
- EventHub#emit
/src/index.ts
class EventHub {
cache = {}
on(eventName, fn) {
this.cache[eventName] = this.cache[eventName] || []
this.cache[eventName].push(fn)
}
emit(eventName, data?) {
(this.cache[eventName] || []).forEach(fn => fn(data))
}
off(eventName, fn) {
this.cache[eventName] = this.cache[eventName] || []
let index = indexOf(this.cache[eventName], fn)
if (index === -1) return
this.cache[eventName].splice(index, 1)
}
}
export default EventHub
/* 帮助函数indexOf */
function indexOf(array, item) {
if (array === undefined) return -1
let index = -1
for (let i = 0; i < array.length; i++) {
if (array[i] === item) {
index = i
break
}
}
return index
}
/test/index.ts
import EventHub from '../src/index';
const test1 = (message) => {
const eventHub = new EventHub()
console.assert(eventHub instanceof Object === true, "eventHub 是个对象")
console.log(message)
}
const test2 = (message) => {
const eventHub = new EventHub()
let called = false
eventHub.on("xxx", (data) => {
called = true
console.log("男篮", data)
})
eventHub.emit("xxx", "迈特凯")
setTimeout(() => {
console.assert(called)
console.log(message)
}, 1000)
}
const test3 = (message) => {
const eventHub = new EventHub()
let called = false
const fn = () => {
called = true
console.log("yyy")
}
eventHub.on("yyy", fn)
eventHub.off("yyy", fn)
eventHub.emit("yyy")
setTimeout(() => {
console.assert(called)
console.log(message)
}, 1000)
}
test1('EventHub可以创建对象')
test2('on 之后 emit会触发on的回调函数')
test3('off卸载监听')
$ ts-node test/index.ts
优化TS
/src/index.ts
class EventHub {
private cache: { [key: string]: Array<(data?: unknown) => void> } = {}
on(eventName: string, fn: (data?: unknown) => void) {
this.cache[eventName] = this.cache[eventName] || []
this.cache[eventName].push(fn)
}
emit(eventName: string, data?: unknown) {
(this.cache[eventName] || []).forEach(fn => fn(data))
}
off(eventName: string, fn: (data?: unknown) => void) {
this.cache[eventName] = this.cache[eventName] || []
let index = indexOf(this.cache[eventName], fn)
if (index === -1) return
this.cache[eventName].splice(index, 1)
}
}
export default EventHub
/* */
function indexOf(array: Array<(data?: unknown) => void>, item: (data?: unknown) => void) {
if (array === undefined) return -1
let index = -1
for (let i = 0; i < array.length; i++) {
if (array[i] === item) {
index = i
break
}
}
return index
}
/test/index.ts
import EventHub from '../src/index';
type TestCase = (message: string) => void
const test1: TestCase = (message) => {
const eventHub = new EventHub()
console.assert(eventHub instanceof Object === true, "eventHub 是个对象")
console.log(message)
}
const test2: TestCase = (message) => {
const eventHub = new EventHub()
let called = false
eventHub.on("xxx", (data) => {
called = true
console.log("男篮", data)
})
eventHub.emit("xxx", "迈特凯")
setTimeout(() => {
console.assert(called)
console.log(message)
}, 1000)
}
const test3: TestCase = (message) => {
const eventHub = new EventHub()
let called = false
const fn = () => {
called = true
console.log("yyy")
}
eventHub.on("yyy", fn)
eventHub.off("yyy", fn)
eventHub.emit("yyy")
setTimeout(() => {
console.assert(called)
console.log(message)
}, 1000)
}
test1('EventHub可以创建对象')
test2('on 之后 emit会触发on的回调函数')
test3('off卸载监听')