typed-emitter
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -5,3 +5,9 @@ module.exports = { | ||
indent: ['error', 4], | ||
quotes: ['error', 'single'] | ||
quotes: ['error', 'single'], | ||
// demoted to es5, so don't use the new sugar | ||
'no-var': 0, | ||
'object-shorthand': 0, | ||
'vars-on-top': 0, | ||
'prefer-arrow-callback': 0, | ||
}, | ||
@@ -12,4 +18,5 @@ globals: { | ||
parserOptions: { | ||
ecmaVersion: 5, | ||
sourceType: 'script', | ||
}, | ||
}; |
/* eslint no-underscore-dangle: 0 */ | ||
'use strict'; | ||
const each = require('lodash/forEach'); | ||
class Emitter { | ||
constructor() { | ||
this._listeners = {}; | ||
this._onQueue = []; | ||
this._offQueue = []; | ||
this._triggerStack = []; | ||
} | ||
var each = require('lodash/forEach'); | ||
var defaults = require('lodash/defaults'); | ||
on(ctx, ev, fn) { | ||
function Emitter() { | ||
this._listeners = {}; | ||
this._onQueue = []; | ||
this._offQueue = []; | ||
this._triggerStack = []; | ||
} | ||
Emitter.prototype = { | ||
on: function on(ctx, ev, fn) { | ||
if (!this._listeners[ev]) { | ||
@@ -18,3 +20,3 @@ this._listeners[ev] = []; | ||
const listener = { ctx, ev, fn }; | ||
var listener = { ctx: ctx, ev: ev, fn: fn }; | ||
if (!this._triggering) { | ||
@@ -25,9 +27,11 @@ this._listeners[ev].push(listener); | ||
} | ||
} | ||
off(ctx) { | ||
return this; | ||
}, | ||
off: function off(ctx) { | ||
if (!this._triggering) { | ||
each(this._listeners, listeners => { | ||
for (let i = 0, len = listeners.length; i < len; i++) { | ||
const listener = listeners[i]; | ||
each(this._listeners, function offListeners(listeners) { | ||
for (var i = 0, len = listeners.length; i < len; i++) { | ||
var listener = listeners[i]; | ||
if (listener.ctx === ctx) { | ||
@@ -43,8 +47,10 @@ listeners.splice(i, 1); | ||
} | ||
} | ||
trigger(ev) { | ||
return this; | ||
}, | ||
trigger: function trigger(ev) { | ||
if (this._triggering) { | ||
this._triggerStack.unshift(ev); | ||
return; | ||
return this; | ||
} | ||
@@ -54,7 +60,7 @@ | ||
const type = ev.type; | ||
const listeners = this._listeners[type]; | ||
var type = ev.type; | ||
var listeners = this._listeners[type]; | ||
if (listeners) { | ||
for (let i = 0, len = listeners.length; i < len; i++) { | ||
const listener = listeners[i]; | ||
for (var i = 0, len = listeners.length; i < len; i++) { | ||
var listener = listeners[i]; | ||
listener.fn.call(listener.ctx, ev); | ||
@@ -66,5 +72,19 @@ } | ||
this._processQueue(); | ||
} | ||
_processQueue() { | ||
return this; | ||
}, | ||
factory: function factory(evProps) { | ||
var that = this; | ||
var triggerer = function triggerer(ev) { | ||
return that.trigger(defaults({}, evProps, ev)); | ||
}; | ||
triggerer.trigger = triggerer; | ||
triggerer.factory = function triggererFactory(subEvProps) { | ||
return that.factory(defaults({}, evProps, subEvProps)); | ||
}; | ||
return triggerer; | ||
}, | ||
_processQueue: function _processQueue() { | ||
if (this._triggering) { | ||
@@ -74,3 +94,3 @@ return; | ||
each(this._offQueue, ctx => { | ||
each(this._offQueue, function offQueue(ctx) { | ||
this.off(ctx); | ||
@@ -80,3 +100,3 @@ }); | ||
each(this._onQueue, listener => { | ||
each(this._onQueue, function onQueue(listener) { | ||
this.on(listener.ctx, listener.ev, listener.fn); | ||
@@ -87,8 +107,8 @@ }); | ||
if (this._triggerStack.length) { | ||
const ev = this._triggerStack.pop(); | ||
var ev = this._triggerStack.pop(); | ||
this.trigger(ev); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
module.exports = Emitter; |
{ | ||
"name": "typed-emitter", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Basic pub/sub/emitter class for React apps, inspired by Flux", | ||
@@ -5,0 +5,0 @@ "main": "emitter.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5017
107