Skip to content

Commit

Permalink
mini-signals: Remove once functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
vovkasm committed Dec 17, 2016
1 parent 7e9f9a7 commit 63caf2c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 75 deletions.
22 changes: 2 additions & 20 deletions src/mini-signals.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ export class MiniSignalBinding {
* MiniSignalBinding constructor.
* @constructs MiniSignalBinding
* @param {Function} fn Event handler to be called.
* @param {Boolean} [once=false] Should this listener be removed after dispatch
* @param {Mixed} [thisArg] The context of the callback function.
* @api private
*/
constructor (fn, once = false, thisArg) {
constructor (fn, thisArg) {
this._fn = fn
this._once = once
this._thisArg = thisArg
this._next = this._prev = this._owner = null
}
Expand Down Expand Up @@ -109,7 +107,6 @@ export class MiniSignal {
if (!node) return false

while (node) {
if (node._once) this.detach(node)
node._fn.apply(node._thisArg, arguments)
node = node._next
}
Expand All @@ -129,22 +126,7 @@ export class MiniSignal {
if (typeof fn !== 'function') {
throw new Error('MiniSignal#add(): First arg must be a Function.')
}
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, false, thisArg))
}

/**
* Register a new listener that will be executed only once.
*
* @param {Function} fn Callback function.
* @param {Mixed} [thisArg] The context of the callback function.
* @returns {MiniSignalBinding} The MiniSignalBinding node that was added.
* @api public
*/
once (fn, thisArg = null) {
if (typeof fn !== 'function') {
throw new Error('MiniSignal#once(): First arg must be a Function.')
}
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, true, thisArg))
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, thisArg))
}

/**
Expand Down
55 changes: 0 additions & 55 deletions test/mini-signals.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,6 @@ describe('MiniSignal', function tests () {
}
})

describe('MiniSignal#once', function () {
let e, context

beforeEach(function () {
e = new MiniSignal()
context = { bar: 'baz' }
})

it('should throw error for incorrect types', function () {
expect(function () { e.once() }).throw('MiniSignal#once(): First arg must be a Function.')
expect(function () { e.once(123) }).throw('MiniSignal#once(): First arg must be a Function.')
expect(function () { e.once(true) }).throw('MiniSignal#once(): First arg must be a Function.')
expect(e.handlers()).to.be.empty
})

it('should not invoke twice', function () {
var cb = function (bar) {
expect(bar).to.be.equal('bar')
expect(this).to.be.equal(context)
expect(arguments).to.have.lengthOf(1)
e.dispatch('bar')
}.bind(context)

e.once(cb)

e.dispatch('bar')
})
})

describe('MiniSignal#add', function () {
let e

Expand Down Expand Up @@ -298,32 +269,6 @@ describe('MiniSignal', function tests () {

expect(pattern.join(';')).to.equal('foo1;foo2;foo3')
})

it('emits to all event listeners, removes once', function () {
var e = new MiniSignal()
var pattern = []

function foo1 () {
pattern.push('foo1')
}

function foo2 () {
pattern.push('foo2')
}

function foo3 () {
pattern.push('foo3')
}

e.add(foo1)
e.once(foo2)
e.add(foo3)

e.dispatch()
e.dispatch()

expect(pattern.join(';')).to.equal('foo1;foo2;foo3;foo1;foo3')
})
})

describe('MiniSignal#handlers', function () {
Expand Down

0 comments on commit 63caf2c

Please sign in to comment.