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. })