Skip to content

Commit

Permalink
feat: ref-based off-ing, fixes #22 and #42
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed May 16, 2024
2 parents 8f9cbc7 + ccebceb commit c7809c7
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 27 deletions.
12 changes: 4 additions & 8 deletions dist/dush.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,12 @@ function dush () {
var e = app._allEvents[name] || (app._allEvents[name] = []);

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

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

e.push(fn);
return app
Expand Down Expand Up @@ -223,9 +220,8 @@ function dush () {

off: function off (name, handler) {
if (handler && app._allEvents[name]) {
var fnStr = handler.toString();
app._allEvents[name] = app._allEvents[name].filter(
function (func) { return func.__sourceString !== fnStr; }
function (func) { return func !== handler && func !== handler.fn; }
);
} else if (name) {
app._allEvents[name] = [];
Expand Down
12 changes: 4 additions & 8 deletions dist/dush.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,12 @@ function dush () {
var e = app._allEvents[name] || (app._allEvents[name] = []);

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

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

e.push(fn);
return app
Expand Down Expand Up @@ -221,9 +218,8 @@ function dush () {

off: function off (name, handler) {
if (handler && app._allEvents[name]) {
var fnStr = handler.toString();
app._allEvents[name] = app._allEvents[name].filter(
function (func) { return func.__sourceString !== fnStr; }
function (func) { return func !== handler && func !== handler.fn; }
);
} else if (name) {
app._allEvents[name] = [];
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"rollup-plugin-gzip": "1.3.0",
"rollup-plugin-uglify": "2.0.1",
"standard": "10.0.3",
"standard-version": "4.2.0"
"standard-version": "^4.4.0"
},
"files": [
"dist/"
Expand Down
12 changes: 4 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,12 @@ export default function dush () {
let e = app._allEvents[name] || (app._allEvents[name] = [])

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

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

e.push(fn)
return app
Expand Down Expand Up @@ -223,9 +220,8 @@ export default function dush () {

off (name, handler) {
if (handler && app._allEvents[name]) {
const fnStr = handler.toString()
app._allEvents[name] = app._allEvents[name].filter(
(func) => func.__sourceString !== fnStr
(func) => func !== handler && func !== handler.fn
)
} else if (name) {
app._allEvents[name] = []
Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,25 @@ test('should `.off()` remove all listeners', function (done) {
test.strictEqual(allEvents.length, 0)
done()
})

test('should `.off()` be able to differentiate between similar handler functions', function (done) {
var emitter = dush()
var calls = 0
var funcA = function () {
calls++
}
var funcB = function () {
calls++
}
emitter.on('a', funcA)
emitter.on('a', funcB)
emitter.emit('a')
test.strictEqual(calls, 2)
emitter.off('a', funcB)
emitter.emit('a')
test.strictEqual(calls, 3)
emitter.off('a', funcA)
emitter.emit('a')
test.strictEqual(calls, 3)
done()
})

0 comments on commit c7809c7

Please sign in to comment.