D
Size: a a a
D
D
ДЖ
ДЖ
D
const pubSub = () => {
const handlers = {}
const dispatch = (event, data) => {
if(handlers[eventName]) {
handlers[eventName].forEach(h => h(data))
}
}
const on = (eventName, handler) => {
if (!handlers[eventName]) handlers[eventName] = []
handlers[eventName].push(handler)
}
const off = (eventName, handler) => {
if(handlers[eventName]){
handlers[eventName] = handlers[eventName].filter(h => h!== handler)
}
}
return {
on, off, handlers, dispatch
},
}
const someThing = () => {
const {handlers, on, off, dispatch} = pubSub()
const getData = () => {
console.log('getting data')
dispatch('data.ready', data)
}
const othertData = () => {
console.log('getting other data')
dispatch('other.data.ready', data)
}
return {on, off, otherData}
}
const something = someThing()
something.on('data.ready', ()=>{
...
}}
something.getData();
D
D
ДЖ
ДЖ
АК
j
DT
ДЖ
W
ДЖ
IS
IS
W