Skip to content

Commit

Permalink
feat(events): Switch to event delegation
Browse files Browse the repository at this point in the history
This switches to using event delegation and filtering of the
event source to allow things to continue to work under the same semantics, but greating
reducing the cost of the operation.

References #4
  • Loading branch information
TylorS committed Dec 29, 2015
1 parent a8bd6fa commit 4c9ff0f
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions src/events.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
import {empty} from 'most'
import fromEvent from './fromEvent'
import {makeIsStrictlyInRootScope} from './select'

const makeEventsSelector =
element$ =>
(type, useCapture = false) => {
if (typeof type !== `string`) {
throw new Error(`DOM drivers events() expects argument to be a ` +
`string representing the event type to listen for.`)
}
return element$
.map(
elements =>
elements ?
fromEvent(type, elements, useCapture) :
empty()
)
.switch()
.multicast()
let matchesSelector
try {
matchesSelector = require(`matches-selector`)
} catch (err) {
matchesSelector = () => {}
}

function makeEventsSelector(element$, selector) {
return function eventsSelector(type, useCapture = false) {
if (typeof type !== `string`) {
throw new Error(`DOM drivers events() expects argument to be a ` +
`string representing the event type to listen for.`)
}
return element$
.map(elements => {
if (!elements) {
return empty()
}

if (matchesSelector(elements, selector.join(` `))) {
return fromEvent(type, elements, useCapture)
}

return fromEvent(type, elements, useCapture)
.filter(ev => {
if (matchesSelector(ev.srcElement, selector.join(` `)) ||
matchesSelector(ev.srcElement, selector.join(``)))
{
return makeIsStrictlyInRootScope(selector)(ev.srcElement)
}
return false
})
})
.switch()
.multicast()
}
}

export {makeEventsSelector}
export default makeEventsSelector

0 comments on commit 4c9ff0f

Please sign in to comment.