Last active
April 27, 2018 17:02
-
-
Save nikolaswise/522124eee3fe3b57ac193f625c9318f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const channels = { | |
z: { | |
channel: 'z', | |
description: 'Getting nothing but static, getting nothing but static / Static in my attic from Channel Z', | |
emits: 'Space Junk', | |
listeners: [] | |
} | |
} | |
const check = channel => typeof channels[channel] == 'object' | |
const register = object => { | |
if (check(object.channel)) { | |
return false | |
} else { | |
channels[object.channel] = object | |
return true | |
} | |
} | |
const on = (channel, cb) => { | |
if (check(channel)) { | |
if (channels[channel].listeners.includes(cb)) { | |
return false | |
} | |
channels[channel].listeners.push(cb) | |
return true | |
} else { | |
return false | |
} | |
} | |
const emit = (channel, ...params) => { | |
if (!channels[channel]) { | |
return false | |
} else { | |
channels[channel].listeners.map(fn => { | |
fn(...params) | |
}) | |
return true | |
} | |
} | |
const off = (channel, cb) => { | |
if (!channels[channel]) { | |
return false | |
} else { | |
channels[channel].listeners = channels[channel].listeners.filter(fn => fn != cb) | |
return true | |
} | |
} | |
const bus = { | |
channels: channels, | |
register: register, | |
check: check, | |
on: on, | |
emit: emit, | |
off: off | |
} | |
export default bus |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment