Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
menglexing committed Sep 11, 2017
1 parent e2d3248 commit c65eb0a
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions Event.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// https://github.com/huya-fed/Event

var Callbacks = $.Callbacks
/**
* Class Event
* https://github.com/huya-fed/Event
*/

var arr = []
var slice = arr.slice
Expand All @@ -13,13 +14,21 @@
return typeof s === 'string'
}

function E () {
return {
fired: false,
data: [],
callbacks: new $.Callbacks()
}
}

function Event () {
var events = {}

function on (name, callback, useCache) {
if ( !isString(name) || !isFunction(callback) ) return this;

var e = events[name] || ( events[name] = {fired: false, data: null, callbacks: new Callbacks()} )
var e = events[name] || ( events[name] = new E() )

e.callbacks.add(callback)

Expand All @@ -30,25 +39,19 @@
return this
}

function emit (name, dataA, dataB, dataC) {
function emit (name) {
if ( !isString(name) ) return this;

var e = events[name] || ( events[name] = {fired: false, data: null, callbacks: new Callbacks()} )
var e = events[name] || ( events[name] = new E() )

e.fired = true
e.data = slice.call(arguments, 1)
e.callbacks.fireWith(this, e.data)

if (name !== 'ALL') {
e.callbacks.fireWith(this, e.data)
emit.apply( this, ['ALL'].concat( slice.call(arguments) ) )
}

// emit('ALL')
var eAll = events['ALL'] || ( events['ALL'] = {fired: false, data: null, callbacks: new Callbacks()} )

eAll.fired = true
eAll.data = slice.call(arguments, 0)
eAll.callbacks.fireWith(this, eAll.data)

return this
}

Expand Down Expand Up @@ -99,4 +102,4 @@
}
}

return Event
return Event

0 comments on commit c65eb0a

Please sign in to comment.