observe.js 316B

123456789101112131415161718
  1. export default function () {
  2. const listeners = []
  3. const listen = (f) => {
  4. const p = listeners.length
  5. listeners.push(f)
  6. return () => {
  7. listeners.splice(p, 1)
  8. }
  9. }
  10. const notify = (...args) => {
  11. listeners.forEach(f => f(...args))
  12. }
  13. return { listen, notify }
  14. }