40ea4aed622c3705b26b94ae64eeb3f62da19d0443e69a5381b2338369746a7a5dfee6b31fa96f0556da833b3d79193d9d6483dc19bc8cfc84fb8e66bddb04 954 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * not type checking this file because flow doesn't play well with
  3. * dynamically accessing methods on Array prototype
  4. */
  5. import { def } from '../util/index'
  6. const arrayProto = Array.prototype
  7. export const arrayMethods = Object.create(arrayProto)
  8. const methodsToPatch = [
  9. 'push',
  10. 'pop',
  11. 'shift',
  12. 'unshift',
  13. 'splice',
  14. 'sort',
  15. 'reverse'
  16. ]
  17. /**
  18. * Intercept mutating methods and emit events
  19. */
  20. methodsToPatch.forEach(function (method) {
  21. // cache original method
  22. const original = arrayProto[method]
  23. def(arrayMethods, method, function mutator (...args) {
  24. const result = original.apply(this, args)
  25. const ob = this.__ob__
  26. let inserted
  27. switch (method) {
  28. case 'push':
  29. case 'unshift':
  30. inserted = args
  31. break
  32. case 'splice':
  33. inserted = args.slice(2)
  34. break
  35. }
  36. if (inserted) ob.observeArray(inserted)
  37. // notify change
  38. ob.dep.notify()
  39. return result
  40. })
  41. })