Skip to content

Commit

Permalink
Add possibility to pass options to addEventListener
Browse files Browse the repository at this point in the history
This allows users to pass options to [`addEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Parameters). This is especially usefull when using passive event listeners which can greatly improve scrolling performance. See atom-minimap/minimap#683
  • Loading branch information
lgeiger committed Feb 15, 2019
1 parent 74796e2 commit 6f0ac87
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/mixins/disposable-events.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Mixin = require 'mixto'

module.exports =
class DisposableEvents extends Mixin
addDisposableEventListener: (object, event, listener) ->
object.addEventListener event, listener
addDisposableEventListener: (object, event, listener, options) ->
object.addEventListener event, listener, options
new Disposable -> object.removeEventListener event, listener
12 changes: 6 additions & 6 deletions src/mixins/events-delegation.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ module.exports =
class EventsDelegation extends Mixin
DisposableEvents.includeInto(this)

subscribeTo: (object, selector, events) ->
subscribeTo: (object, selector, events, options) ->
unless object instanceof HTMLElement
[object, selector, events] = [this, object, selector]
[object, selector, events, options] = [this, object, selector, events]

[events, selector] = [selector, NO_SELECTOR] if typeof selector is 'object'
[events, selector, options] = [selector, NO_SELECTOR, events] if typeof selector is 'object'

@eventsMap ?= new WeakMap
@disposablesMap ?= new WeakMap
Expand All @@ -26,7 +26,7 @@ class EventsDelegation extends Mixin
eachPair events, (event, callback) =>
unless eventsForObject[event]?
eventsForObject[event] = {}
disposablesForObject[event] = @createEventListener(object, event)
disposablesForObject[event] = @createEventListener(object, event, options)

eventsForObject[event][selector] = callback

Expand All @@ -53,7 +53,7 @@ class EventsDelegation extends Mixin
@eventsMap.delete(object)
@disposablesMap.delete(object)

createEventListener: (object, event) ->
createEventListener: (object, event, options) ->
listener = (e) =>
return unless eventsForObject = @eventsMap.get(object)?[event]

Expand All @@ -64,7 +64,7 @@ class EventsDelegation extends Mixin
eventsForObject[NO_SELECTOR]?(e) unless e.isPropagationStopped
return true

@addDisposableEventListener object, event, listener
@addDisposableEventListener object, event, listener, options

eachSelectorFromTarget: (event, target, eventsForObject) ->
@nodeAndItsAncestors target, (node) =>
Expand Down

0 comments on commit 6f0ac87

Please sign in to comment.