Created
October 13, 2016 16:54
-
-
Save MarcoWorms/aa9939ff16d4fe6fe29a2550403a2215 to your computer and use it in GitHub Desktop.
This file contains 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 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) | |
} | |
} |
This file contains 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 = 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