Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exports.createEmitter = function createEmitter ({ shouldPrime, initialValue }) {
- let currentValue = initialValue
- let listeners = []
- return {
- emit: value => () => {
- currentValue = value
- listeners.forEach(listener => listener(currentValue))
- },
- subscribe () {
- let listener
- return {
- effect (dispatch) {
- listener = dispatch
- listeners.push(listener)
- if (shouldPrime) {
- listener(currentValue)
- }
- },
- cancel () {
- listeners = listeners.filter(l => l !== listener)
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment