Skip to content

Commit

Permalink
fix(eventStack): correct handling of removed targets (#2117)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter authored and levithomason committed Sep 25, 2017
1 parent c003fe9 commit 34ecc05
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
31 changes: 15 additions & 16 deletions src/lib/eventStack/eventStack.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
import _ from 'lodash'

import isBrowser from '../isBrowser'
import EventTarget from './EventTarget'
import normalizeTarget from './normalizeTarget'

class EventStack {
_eventTargets = {}
_targets = []
constructor() {
this._targets = new Map()
}

// ------------------------------------
// Target utils
// ------------------------------------

_find = (target) => {
_find = (target, autoCreate = true) => {
const normalized = normalizeTarget(target)
let index = this._targets.indexOf(normalized)

if (index !== -1) return this._eventTargets[index]
if (this._targets.has(normalized)) return this._targets.get(normalized)
if (!autoCreate) return

index = this._targets.push(normalized) - 1
this._eventTargets[index] = new EventTarget(normalized)
const eventTarget = new EventTarget(normalized)
this._targets.set(normalized, eventTarget)

return this._eventTargets[index]
return eventTarget
}

_remove = (target) => {
const normalized = normalizeTarget(target)
const index = this._targets.indexOf(normalized)

this._targets = _.without(this._targets, normalized)
delete this._eventTargets[index]
this._targets.delete(normalized)
}

// ------------------------------------
Expand All @@ -49,10 +46,12 @@ class EventStack {
if (!isBrowser) return

const { target = document, pool = 'default' } = options
const eventTarget = this._find(target)
const eventTarget = this._find(target, false)

eventTarget.unsub(name, handlers, pool)
if (eventTarget.empty()) this._remove(target)
if (eventTarget) {
eventTarget.unsub(name, handlers, pool)
if (eventTarget.empty()) this._remove(target)
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions test/specs/lib/eventStack/eventStack-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { domEvent, sandbox } from 'test/utils'

describe('eventStack', () => {
afterEach(() => {
eventStack._eventTargets = {}
eventStack._targets = []
eventStack._targets = new Map()
})

describe('sub', () => {
Expand Down Expand Up @@ -68,5 +67,18 @@ describe('eventStack', () => {
clickHandler.should.have.been.calledOnce()
keyHandler.should.have.not.been.called()
})

it('unsubscribes from same event multiple times', () => {
const handler = sandbox.spy()

eventStack.sub('click', handler)
domEvent.click(document)

eventStack.unsub('click', handler)
eventStack.unsub('click', handler)
domEvent.click(document)

handler.should.have.been.calledOnce()
})
})
})

0 comments on commit 34ecc05

Please sign in to comment.