styled-components
Version:
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
29 lines (24 loc) • 689 B
JavaScript
/**
* Creates a broadcast that can be listened to, i.e. simple event emitter
*
* @see https://github.com/ReactTraining/react-broadcast
*/
const createBroadcast = (initialValue) => {
let listeners = []
let currentValue = initialValue
return {
publish(value) {
currentValue = value
listeners.forEach(listener => listener(currentValue))
},
subscribe(listener) {
listeners.push(listener)
// Publish to this subscriber once immediately.
listener(currentValue)
// eslint-disable-next-line no-return-assign
return () =>
listeners = listeners.filter(item => item !== listener)
},
}
}
export default createBroadcast