Skip to content

Commit

Permalink
fix(off/once): better .off and .once methods
Browse files Browse the repository at this point in the history
now ".on" can work as ".once" if third argument true

fix #8 and fix #9
  • Loading branch information
tunnckoCore committed Mar 19, 2017
1 parent 4e087be commit f5cbef1
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 52 deletions.
32 changes: 19 additions & 13 deletions dist/dush.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @api public
*/

function dush () {
module.exports = function dush () {
var all = Object.create(null);
var app = {
/**
Expand Down Expand Up @@ -123,10 +123,21 @@ function dush () {
* @api public
*/

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

function func () {
if (!func.called) {
app.off(name, func);
handler.apply(undefined, arguments);
func.called = true;
}
}

var fn = once ? func : handler;
fn.sourceString = handler.toString();

e.push(fn);
return app
},

Expand Down Expand Up @@ -163,12 +174,8 @@ function dush () {
*/

once: function once (name, handler) {
function fn (a, b, c) {
app.off(name, fn);
handler(a, b, c);
}

return app.on(name, fn)
app.on(name, handler, true);
return app
},

/**
Expand Down Expand Up @@ -208,7 +215,8 @@ function dush () {

off: function off (name, handler) {
if (handler && all[name]) {
all[name].splice(all[name].indexOf(handler) >>> 0, 1);
var fnStr = handler.toString();
all[name] = all[name].filter(function (func) { return func.sourceString !== fnStr; });
} else {
all[name] = [];
}
Expand Down Expand Up @@ -261,6 +269,4 @@ function dush () {
};

return app
}

module.exports = dush;
};
32 changes: 19 additions & 13 deletions dist/dush.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @api public
*/

function dush () {
module.exports = function dush () {
var all = Object.create(null);
var app = {
/**
Expand Down Expand Up @@ -121,10 +121,21 @@ function dush () {
* @api public
*/

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

function func () {
if (!func.called) {
app.off(name, func);
handler.apply(undefined, arguments);
func.called = true;
}
}

var fn = once ? func : handler;
fn.sourceString = handler.toString();

e.push(fn);
return app
},

Expand Down Expand Up @@ -161,12 +172,8 @@ function dush () {
*/

once: function once (name, handler) {
function fn (a, b, c) {
app.off(name, fn);
handler(a, b, c);
}

return app.on(name, fn)
app.on(name, handler, true);
return app
},

/**
Expand Down Expand Up @@ -206,7 +213,8 @@ function dush () {

off: function off (name, handler) {
if (handler && all[name]) {
all[name].splice(all[name].indexOf(handler) >>> 0, 1);
var fnStr = handler.toString();
all[name] = all[name].filter(function (func) { return func.sourceString !== fnStr; });
} else {
all[name] = [];
}
Expand Down Expand Up @@ -259,6 +267,4 @@ function dush () {
};

return app
}

export default dush;
};
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.
Loading

0 comments on commit f5cbef1

Please sign in to comment.