all files / src/scroll/ event.js

96.43% Statements 27/28
86.67% Branches 13/15
100% Functions 6/6
96.43% Lines 27/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55      100× 94×     100×                                 624× 624× 554×     70× 70× 70× 75× 75× 75× 75×          
import { spliceOne } from '../util/spliceOne'
 
export function eventMixin(BScroll) {
  BScroll.prototype.on = function (type, fn, context = this) {
    if (!this._events[type]) {
      this._events[type] = []
    }
 
    this._events[type].push([fn, context])
  }
 
  BScroll.prototype.once = function (type, fn, context = this) {
    function magic() {
      this.off(type, magic)
 
      fn.apply(context, arguments)
    }
    // To expose the corresponding function method in order to execute the off method
    magic.fn = fn
 
    this.on(type, magic)
  }
 
  BScroll.prototype.off = function (type, fn) {
    let _events = this._events[type]
    Iif (!_events) {
      return
    }
 
    let count = _events.length
    while (count--) {
      if (_events[count][0] === fn || (_events[count][0] && _events[count][0].fn === fn)) {
        spliceOne(_events, count)
      }
    }
  }
 
  BScroll.prototype.trigger = function (type) {
    let events = this._events[type]
    if (!events) {
      return
    }
 
    let len = events.length
    let eventsCopy = [...events]
    for (let i = 0; i < len; i++) {
      let event = eventsCopy[i]
      let [fn, context] = event
      Eif (fn) {
        fn.apply(context, [].slice.call(arguments, 1))
      }
    }
  }
}