main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | import type { Controller } from './Controller'
|
---|
2 | import { GestureKey } from './types'
|
---|
3 | import { toDomEventType } from './utils/events'
|
---|
4 |
|
---|
5 | export class EventStore {
|
---|
6 | private _listeners = new Set<() => void>()
|
---|
7 | private _ctrl: Controller
|
---|
8 | private _gestureKey?: GestureKey
|
---|
9 | constructor(ctrl: Controller, gestureKey?: GestureKey) {
|
---|
10 | this._ctrl = ctrl
|
---|
11 | this._gestureKey = gestureKey
|
---|
12 | }
|
---|
13 |
|
---|
14 | add(
|
---|
15 | element: EventTarget,
|
---|
16 | device: string,
|
---|
17 | action: string,
|
---|
18 | handler: (event: any) => void,
|
---|
19 | options?: AddEventListenerOptions
|
---|
20 | ) {
|
---|
21 | const listeners = this._listeners
|
---|
22 | const type = toDomEventType(device, action)
|
---|
23 | const _options = this._gestureKey ? this._ctrl.config[this._gestureKey]!.eventOptions : {}
|
---|
24 | const eventOptions = { ..._options, ...options }
|
---|
25 | element.addEventListener(type, handler, eventOptions)
|
---|
26 | const remove = () => {
|
---|
27 | element.removeEventListener(type, handler, eventOptions)
|
---|
28 | listeners.delete(remove)
|
---|
29 | }
|
---|
30 | listeners.add(remove)
|
---|
31 | return remove
|
---|
32 | }
|
---|
33 |
|
---|
34 | clean() {
|
---|
35 | this._listeners.forEach((remove) => remove())
|
---|
36 | this._listeners.clear() // just for safety
|
---|
37 | }
|
---|
38 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.