Skip to content

Instantly share code, notes, and snippets.

@MarcoWorms
Created October 13, 2016 16:54
Show Gist options
  • Save MarcoWorms/aa9939ff16d4fe6fe29a2550403a2215 to your computer and use it in GitHub Desktop.
Save MarcoWorms/aa9939ff16d4fe6fe29a2550403a2215 to your computer and use it in GitHub Desktop.
const Immutable = require('immutable')
const add = (channels, name) => {
return channels.set(name, Immutable.List())
}
const addAction = (channels, name, action) => {
return channels.set(name, channels.get(name).push(action))
}
const addListener = (channels, name, action) => {
if (!channels.has(name)) {
const newChannels = add(channels, name)
return addAction(newChannels, name, action)
}
return addAction(...args)
}
const doActions = (channels, name, data) => {
if (channels.has(name)) {
channels.get(name).forEach((action) => {
action(data)
})
}
}
let channels = Immutable.Map()
module.exports = {
subscribe: (name, action) => {
channels = addListener(channels, name, action)
},
emit: (name, data) => {
doActions(channels, name, data)
}
}
const channels = require('./channels')
const logger = console.log.bind(console)
channels.subscribe('channel1', logger)
channels.emit('channel1', 'hello world!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment