Skip to content

Commit

Permalink
fix(all): rename ".all" to "._allEvents"
Browse files Browse the repository at this point in the history
ensure not conflicting with other tools and things

BREAKING CHANGE: rename ".all" to "._allEvents"
  • Loading branch information
tunnckoCore committed Mar 19, 2017
1 parent de43658 commit d760ecd
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 55 deletions.
26 changes: 13 additions & 13 deletions dist/dush.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* const dush = require('dush')
* const emitter = dush()
*
* console.log(emitter.all) // => {}
* console.log(emitter._allEvents) // => {}
* console.log(emitter.on) // => Function
* console.log(emitter.once) // => Function
* console.log(emitter.off) // => Function
Expand All @@ -32,7 +32,7 @@
*/

function dush () {
var all = Object.create(null);
var _allEvents = Object.create(null);
var app = {
/**
* > An listeners map of all registered events
Expand All @@ -51,16 +51,16 @@ function dush () {
* emitter.on('foo', () => {})
* emitter.on('bar', () => {})
*
* console.log(emitter.all)
* console.log(emitter._allEvents)
* // => { foo: [Function, Function], bar: [Functon] }
* ```
*
* @name .all
* @type {Object} `all` a key/value store of all events and their listeners
* @name ._allEvents
* @type {Object} `_allEvents` a key/value store of all events and their listeners
* @api public
*/

all: all,
_allEvents: _allEvents,

/**
* > Invokes `plugin` function immediately, which is passed
Expand Down Expand Up @@ -126,7 +126,7 @@ function dush () {
*/

on: function on (name, handler, once) {
var e = app.all[name] || (app.all[name] = []);
var e = app._allEvents[name] || (app._allEvents[name] = []);

function func () {
if (!func.called) {
Expand Down Expand Up @@ -216,13 +216,13 @@ function dush () {
*/

off: function off (name, handler) {
if (handler && app.all[name]) {
if (handler && app._allEvents[name]) {
var fnStr = handler.toString();
app.all[name] = app.all[name].filter(function (func) { return func.sourceString !== fnStr; });
app._allEvents[name] = app._allEvents[name].filter(function (func) { return func.sourceString !== fnStr; });
} else if (name) {
app.all[name] = [];
app._allEvents[name] = [];
} else {
app.all = Object.create(null);
app._allEvents = Object.create(null);
}

return app
Expand Down Expand Up @@ -264,8 +264,8 @@ function dush () {
emit: function emit (name) {
if (name !== '*') {
var args = [].slice.call(arguments);
(app.all[name] || []).map(function (handler) { handler.apply(handler, args.slice(1)); });
(app.all['*'] || []).map(function (handler) { handler.apply(handler, args); });
(app._allEvents[name] || []).map(function (handler) { handler.apply(handler, args.slice(1)); });
(app._allEvents['*'] || []).map(function (handler) { handler.apply(handler, args); });
}

return app
Expand Down
26 changes: 13 additions & 13 deletions dist/dush.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* const dush = require('dush')
* const emitter = dush()
*
* console.log(emitter.all) // => {}
* console.log(emitter._allEvents) // => {}
* console.log(emitter.on) // => Function
* console.log(emitter.once) // => Function
* console.log(emitter.off) // => Function
Expand All @@ -30,7 +30,7 @@
*/

function dush () {
var all = Object.create(null);
var _allEvents = Object.create(null);
var app = {
/**
* > An listeners map of all registered events
Expand All @@ -49,16 +49,16 @@ function dush () {
* emitter.on('foo', () => {})
* emitter.on('bar', () => {})
*
* console.log(emitter.all)
* console.log(emitter._allEvents)
* // => { foo: [Function, Function], bar: [Functon] }
* ```
*
* @name .all
* @type {Object} `all` a key/value store of all events and their listeners
* @name ._allEvents
* @type {Object} `_allEvents` a key/value store of all events and their listeners
* @api public
*/

all: all,
_allEvents: _allEvents,

/**
* > Invokes `plugin` function immediately, which is passed
Expand Down Expand Up @@ -124,7 +124,7 @@ function dush () {
*/

on: function on (name, handler, once) {
var e = app.all[name] || (app.all[name] = []);
var e = app._allEvents[name] || (app._allEvents[name] = []);

function func () {
if (!func.called) {
Expand Down Expand Up @@ -214,13 +214,13 @@ function dush () {
*/

off: function off (name, handler) {
if (handler && app.all[name]) {
if (handler && app._allEvents[name]) {
var fnStr = handler.toString();
app.all[name] = app.all[name].filter(function (func) { return func.sourceString !== fnStr; });
app._allEvents[name] = app._allEvents[name].filter(function (func) { return func.sourceString !== fnStr; });
} else if (name) {
app.all[name] = [];
app._allEvents[name] = [];
} else {
app.all = Object.create(null);
app._allEvents = Object.create(null);
}

return app
Expand Down Expand Up @@ -262,8 +262,8 @@ function dush () {
emit: function emit (name) {
if (name !== '*') {
var args = [].slice.call(arguments);
(app.all[name] || []).map(function (handler) { handler.apply(handler, args.slice(1)); });
(app.all['*'] || []).map(function (handler) { handler.apply(handler, args); });
(app._allEvents[name] || []).map(function (handler) { handler.apply(handler, args.slice(1)); });
(app._allEvents['*'] || []).map(function (handler) { handler.apply(handler, args); });
}

return app
Expand Down
2 changes: 1 addition & 1 deletion dist/dush.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/dush.umd.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/dush.umd.js.map

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* const dush = require('dush')
* const emitter = dush()
*
* console.log(emitter.all) // => {}
* console.log(emitter._allEvents) // => {}
* console.log(emitter.on) // => Function
* console.log(emitter.once) // => Function
* console.log(emitter.off) // => Function
Expand All @@ -32,7 +32,7 @@
*/

export default function dush () {
let all = Object.create(null)
let _allEvents = Object.create(null)
const app = {
/**
* > An listeners map of all registered events
Expand All @@ -51,16 +51,16 @@ export default function dush () {
* emitter.on('foo', () => {})
* emitter.on('bar', () => {})
*
* console.log(emitter.all)
* console.log(emitter._allEvents)
* // => { foo: [Function, Function], bar: [Functon] }
* ```
*
* @name .all
* @type {Object} `all` a key/value store of all events and their listeners
* @name ._allEvents
* @type {Object} `_allEvents` a key/value store of all events and their listeners
* @api public
*/

all,
_allEvents,

/**
* > Invokes `plugin` function immediately, which is passed
Expand Down Expand Up @@ -126,7 +126,7 @@ export default function dush () {
*/

on (name, handler, once) {
let e = app.all[name] || (app.all[name] = [])
let e = app._allEvents[name] || (app._allEvents[name] = [])

function func () {
if (!func.called) {
Expand Down Expand Up @@ -216,13 +216,13 @@ export default function dush () {
*/

off (name, handler) {
if (handler && app.all[name]) {
if (handler && app._allEvents[name]) {
const fnStr = handler.toString()
app.all[name] = app.all[name].filter((func) => func.sourceString !== fnStr)
app._allEvents[name] = app._allEvents[name].filter((func) => func.sourceString !== fnStr)
} else if (name) {
app.all[name] = []
app._allEvents[name] = []
} else {
app.all = Object.create(null)
app._allEvents = Object.create(null)
}

return app
Expand Down Expand Up @@ -264,8 +264,8 @@ export default function dush () {
emit (name) {
if (name !== '*') {
var args = [].slice.call(arguments);
(app.all[name] || []).map((handler) => { handler.apply(handler, args.slice(1)) });
(app.all['*'] || []).map((handler) => { handler.apply(handler, args) })
(app._allEvents[name] || []).map((handler) => { handler.apply(handler, args.slice(1)) });
(app._allEvents['*'] || []).map((handler) => { handler.apply(handler, args) })
}

return app
Expand Down
28 changes: 14 additions & 14 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var test = require('mukla')
var dush = require('./dist/dush.common')
var app = dush()

test('should return an instance with methods and `.all` object', function (done) {
test.strictEqual(typeof app.all, 'object')
test('should return an instance with methods and `._allEvents` object', function (done) {
test.strictEqual(typeof app._allEvents, 'object')
test.strictEqual(typeof app.use, 'function')
test.strictEqual(typeof app.on, 'function')
test.strictEqual(typeof app.off, 'function')
Expand All @@ -23,7 +23,7 @@ test('should return an instance with methods and `.all` object', function (done)
done()
})

test('should instace has .all object that contains all handlers', function (done) {
test('should instace has ._allEvents object that contains all handlers', function (done) {
var fn = function () {}

app.on('aaa', fn)
Expand All @@ -33,10 +33,10 @@ test('should instace has .all object that contains all handlers', function (done
app.on('ccc', fn)
app.on('ccc', fn)

test.deepStrictEqual(Object.keys(app.all), ['aaa', 'bbb', 'ccc'])
test.strictEqual(app.all.aaa.length, 2)
test.strictEqual(app.all.bbb.length, 1)
test.strictEqual(app.all.ccc.length, 3)
test.deepStrictEqual(Object.keys(app._allEvents), ['aaa', 'bbb', 'ccc'])
test.strictEqual(app._allEvents.aaa.length, 2)
test.strictEqual(app._allEvents.bbb.length, 1)
test.strictEqual(app._allEvents.ccc.length, 3)
app.emit('zzz')
done()
})
Expand Down Expand Up @@ -106,7 +106,7 @@ test('should .off("foo", fn) remove the handler', function (done) {

test.strictEqual(called, 0)
test.strictEqual(second, 1)
test.strictEqual(app.all.qux.length, 1)
test.strictEqual(app._allEvents.qux.length, 1)
done()
})

Expand All @@ -116,7 +116,7 @@ test('should .off("foo") remove all "foo" handlers', function (done) {
.on('zzz', function () {})
.off('zzz')

test.strictEqual(app.all.zzz.length, 0)
test.strictEqual(app._allEvents.zzz.length, 0)
done()
})

Expand Down Expand Up @@ -278,14 +278,14 @@ test('should `.off()` remove all listeners', function (done) {
app.once('b', fixture)
app.on('c', fixture)

var evts = Object.keys(app.all)
var evts = Object.keys(app._allEvents)
test.strictEqual(evts.length, 3)
test.strictEqual(app.all.a.length, 3)
test.strictEqual(app.all.b.length, 2)
test.strictEqual(app.all.c.length, 1)
test.strictEqual(app._allEvents.a.length, 3)
test.strictEqual(app._allEvents.b.length, 2)
test.strictEqual(app._allEvents.c.length, 1)

app.off()
var allEvents = Object.keys(app.all)
var allEvents = Object.keys(app._allEvents)
test.strictEqual(allEvents.length, 0)
done()
})

0 comments on commit d760ecd

Please sign in to comment.