Guest User

Untitled

a guest
Jul 15th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. exports.createEmitter = function createEmitter ({ shouldPrime, initialValue }) {
  2. let currentValue = initialValue
  3. let listeners = []
  4.  
  5. return {
  6. emit: value => () => {
  7. currentValue = value
  8. listeners.forEach(listener => listener(currentValue))
  9. },
  10.  
  11. subscribe () {
  12. let listener
  13. return {
  14. effect (dispatch) {
  15. listener = dispatch
  16. listeners.push(listener)
  17. if (shouldPrime) {
  18. listener(currentValue)
  19. }
  20. },
  21. cancel () {
  22. listeners = listeners.filter(l => l !== listener)
  23. }
  24. }
  25. }
  26. }
  27. }
Add Comment
Please, Sign In to add comment