Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2020-03-16 实现简易版 EventEmitter #10

Open
fanmingfei opened this issue Mar 16, 2020 · 4 comments
Open

2020-03-16 实现简易版 EventEmitter #10

fanmingfei opened this issue Mar 16, 2020 · 4 comments

Comments

@fanmingfei
Copy link
Member

const ee = new EventEmitter()
// 绑定事件
ee.on('someAction', (a, b, c) => {
  console.log('action1',a,b,c)
)

// 绑定事件
ee.on('someAction', (a, b, c)=>{
  console.log('action2', a, b, c)
})

// 绑定单次事件
ee.once('someAction', (a, b, c)=> {
  console.log('actionOnce', a, b, c)
})

// 触发事件
ee.emit('someAction', 1, 2, 3)
// 输出 action1 1 2 3
// 输出 action2 1 2 3
// acitonOnce 1 2 3

// 再次触发事件
ee.emit('someAction', 3,4,5)
// 输出 action1 3 4 5
// 输出 action2 3 4 5
@Vince-9
Copy link

Vince-9 commented Mar 16, 2020

class EventEmitter {
  constructor() {
    this.evePool = [];
  }

  on(e, cb) {
    if (!this.evePool[e]) this.evePool[e] = [];
    this.evePool[e].push(cb);
  }

  once(e, cb) {
    if (!this.evePool[e]) this.evePool[e] = [];
    const fn = (...args) => {
      cb(...args);
      this.evePool[e] = this.evePool[e].filter(f => f !== fn);
    }
    this.evePool[e].push(fn);
  }

  emit(e, ...args) {
    if (this.evePool[e])
      this.evePool[e].forEach(cb => {
        cb(...args);
      });
  }
}

@xiaosen7
Copy link

xiaosen7 commented Mar 16, 2020

class EventEmitter {
    constructor() {
        this.events = Object.create(null);
    }
    on(key, handler) {

        (this.events[key] || (this.events[key] = [])).push(handler);
    }
    once(key, handler) {

        const _handler = (...args) => {
            handler(...args);
            this.off(key, _handler)
        }

        (this.events[key] || (this.events[key] = [])).push(_handler);
    }
    off(key, handler) {
        const events = this.events[key];
        if (events) {
            this.events[key] = events.filter(h => h !== handler);
        }

    }
    emit(key, ...args) {
        const  _events = this.events[key];

        if (_events) {
            _events.forEach(h => h(...args));
        }
    }
}

@LiZhaji
Copy link

LiZhaji commented Mar 16, 2020

class EventEmitter{
  _cache = {}
  _onceCache = {}
  on(key, callback){
    const events =  this._cache[key] = this._cache[key]?.length ? this._cache[key] : []
    events.push(callback)
  }
  once(key, callback){
    const events =  this._onceCache[key] = this._onceCache[key]?.length ? this._onceCache[key] : []
    events.push(callback)
  }
  emit(key, ...args){
    this._cache[key].forEach(event => event(...args))
    this._onceCache[key]?.length && this._onceCache[key].forEach(event => event(...args))
    this._onceCache[key] = []

  }
}

@markmcsong
Copy link

class EventEmitter {
  constructor() {
    this.events = []
  }
  on(ev, callback) {
    if (!this.events[ev]) this.events[ev] = []
    this.events[ev].push(callback)
  }
  once(ev, callback) {
    // 执行完解除绑定(包装处理)
    const wrapCb = (...args) => { 
      callback.apply(null, args)
      this.off(ev, wrapCb)
    }
    this.on(ev, wrapCb)
  }
  off(ev, callback) {
    this.events[ev] = this.events[ev] && this.events[ev].filter(fn => fn !== callback)
  }
  emit(ev, ...args) {
    this.events[ev] && this.events[ev].forEach(fn => fn.apply(null, args))
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants