Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(EventStack): fix erroneous removal of non-empty EventPool #2992

Merged
merged 5 commits into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/lib/eventStack/EventPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,10 @@ export default class EventPool {
}

/**
* @param {String} eventType
* @return {Boolean}
*/
hasHandlers(eventType) {
const handlerSet = this.handlerSets.get(eventType)

if (handlerSet) return handlerSet.hasHandlers()
return false
hasHandlers() {
return this.handlerSets.size > 0
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EventPool has handlers if it has any EventHandlerSet.


/**
Expand Down
6 changes: 3 additions & 3 deletions src/lib/eventStack/EventTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ export default class EventTarget {
if (pool) {
const newPool = pool.removeHandlers(eventType, eventHandlers)

if (newPool.hasHandlers(eventType)) {
this.removeTargetHandler(eventType)
if (newPool.hasHandlers()) {
this.pools.set(poolName, newPool)
} else {
this.removeTargetHandler(eventType)
this.pools.delete(poolName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EventPool should not added if it's empty

}

this.removeTargetHandler(eventType)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need always to call removeTargetHandler().


if (this.pools.size > 0) this.addTargetHandler(eventType)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/eventStack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

The `EventStack` solves two design problems:
1. Reduces the number of connected listeners to DOM nodes compared to `element.addListener()`.
1. Respects event ordering. Example, two modals are open and you only want the top modal to close on document click.
2. Respects event ordering. Example, two modals are open and you only want the top modal to close on document click.

## EventStack

Expand Down
10 changes: 6 additions & 4 deletions test/specs/lib/eventStack/EventPool-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ describe('EventPool', () => {
})

describe('hasHandlers', () => {
const pool = EventPool.createByType('default', 'click', [() => {}])

it('returns "true" if has handlers', () => {
pool.hasHandlers('click').should.have.be.true()
const pool = EventPool.createByType('default', 'click', [() => {}])

pool.hasHandlers().should.have.be.true()
})

it('returns "false" if has not handlers', () => {
pool.hasHandlers('mousedown').should.have.be.false()
const pool = new EventPool('default', new Map())

pool.hasHandlers().should.have.be.false()
})
})

Expand Down
16 changes: 15 additions & 1 deletion test/specs/lib/eventStack/EventStack-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('EventStack', () => {
handler.should.have.been.calledOnce()
})

it('unsubscribes but leaves eventTarget if it contains handlers', () => {
it('unsubscribes and leaves eventTarget if it contains handlers', () => {
const clickHandler = sandbox.spy()
const keyHandler = sandbox.spy()

Expand All @@ -104,6 +104,20 @@ describe('EventStack', () => {
keyHandler.should.have.not.been.called()
})

it('unsubscribes and leaves an eventPool if contains handlers', () => {
const firstHandler = sandbox.spy()
const secondHandler = sandbox.spy()

eventStack.sub('click', firstHandler)
eventStack.unsub('mouseup', firstHandler)
eventStack.sub('click', secondHandler)

domEvent.click(document)

firstHandler.should.have.been.calledOnce()
secondHandler.should.have.been.calledOnce()
})

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

Expand Down