|
23 | 23 | * |
24 | 24 | */ |
25 | 25 |
|
26 | | -import { Sub } from 'tea-cup-core'; |
| 26 | +import {Sub} from 'tea-cup-core'; |
27 | 27 |
|
28 | 28 | type Listener<E> = (ev: E) => any; |
29 | | - |
30 | | -type ListenerMap<T> = { |
31 | | - [K in keyof T]?: Listener<T[K]>; |
32 | | -}; |
33 | | - |
34 | | -function setListener<T, K extends keyof T>(o: ListenerMap<T>, k: K, v: Listener<T[K]> | undefined) { |
35 | | - o[k] = v; |
36 | | -} |
37 | | - |
38 | | -function getListener<T, K extends keyof T>(o: ListenerMap<T>, k: K): Listener<T[K]> | undefined { |
39 | | - return o[k]; |
40 | | -} |
41 | | - |
42 | | -type SubsMap<Map, Msg> = { |
43 | | - [K in keyof Map]?: ReadonlyArray<DocSub<K, Map, Msg>>; |
44 | | -}; |
45 | | - |
46 | | -function addOneSub<K extends keyof Map, Map, Msg>( |
47 | | - sub: DocSub<K, Map, Msg>, |
48 | | - subs: SubsMap<Map, Msg>[K], |
49 | | -): SubsMap<Map, Msg>[K] { |
50 | | - const result = new Array().concat(subs); |
51 | | - result.push(sub); |
52 | | - return result; |
53 | | -} |
54 | | - |
55 | | -function removeOneSub<K extends keyof Map, Map, Msg>( |
56 | | - sub: DocSub<K, Map, Msg>, |
57 | | - subs: SubsMap<Map, Msg>[K], |
58 | | -): SubsMap<Map, Msg>[K] | undefined { |
59 | | - const result = new Array().concat(subs).filter((s) => s !== sub); |
60 | | - return result.length !== 0 ? result : undefined; |
61 | | -} |
62 | | - |
63 | | -function getSubs<K extends keyof Map, Map, Msg>(o: SubsMap<Map, Msg>, k: K): SubsMap<Map, Msg>[K] | undefined { |
64 | | - return o[k]; |
65 | | -} |
66 | | - |
67 | | -function setSubs<K extends keyof Map, Map, Msg>(o: SubsMap<Map, Msg>, k: K, v: SubsMap<Map, Msg>[K] | undefined) { |
68 | | - o[k] = v; |
69 | | -} |
| 29 | +type ListenerOptions = boolean | AddEventListenerOptions; |
| 30 | +type Mapper<E, Msg> = (ev: E) => Msg; |
70 | 31 |
|
71 | 32 | class DocSub<K extends keyof Map, Map, Msg> extends Sub<Msg> { |
72 | | - constructor( |
73 | | - private readonly documentEvents: EventMapEvents<Map, Msg>, |
74 | | - private readonly key: K, |
75 | | - private readonly mapper: (t: Map[K]) => Msg, |
76 | | - ) { |
77 | | - super(); |
78 | | - } |
| 33 | + private readonly listener: Listener<Map[K]>; |
| 34 | + |
| 35 | + constructor( |
| 36 | + private readonly documentEvents: EventMapEvents<Map, Msg>, |
| 37 | + private readonly key: K, |
| 38 | + private readonly mapper: Mapper<Map[K], Msg>, |
| 39 | + private readonly options?: ListenerOptions |
| 40 | + ) { |
| 41 | + super(); |
| 42 | + this.listener = (e) => this.dispatch(this.mapper(e)); |
| 43 | + } |
79 | 44 |
|
80 | | - protected onInit() { |
81 | | - super.onInit(); |
82 | | - this.documentEvents.addSub({ key: this.key, sub: this }); |
83 | | - } |
| 45 | + protected onInit() { |
| 46 | + super.onInit(); |
| 47 | + this.documentEvents.doAddListener(this.key, this.listener, this.options) |
| 48 | + } |
84 | 49 |
|
85 | | - protected onRelease() { |
86 | | - super.onRelease(); |
87 | | - this.documentEvents.removeSub(this.key, this); |
88 | | - } |
| 50 | + protected onRelease() { |
| 51 | + super.onRelease(); |
| 52 | + this.documentEvents.doRemoveListener(this.key, this.listener, this.options) |
| 53 | + } |
89 | 54 |
|
90 | | - event(e: Map[K]) { |
91 | | - this.dispatch(this.mapper(e)); |
92 | | - } |
| 55 | + event(e: Map[K]) { |
| 56 | + this.dispatch(this.mapper(e)); |
| 57 | + } |
93 | 58 | } |
94 | 59 |
|
95 | 60 | abstract class EventMapEvents<Map, Msg> { |
96 | | - private readonly listeners: ListenerMap<Map> = {}; |
97 | | - private readonly subs: SubsMap<Map, Msg> = {}; |
98 | | - |
99 | | - constructor() {} |
100 | | - |
101 | | - public release() { |
102 | | - const listeners = this.listeners; |
103 | | - const keys = Object.keys(listeners) as Array<keyof typeof listeners>; |
104 | | - keys.forEach((key) => this.releaseListener(key)); |
105 | | - } |
106 | | - |
107 | | - abstract doAddListener<K extends keyof Map>(key: K, listener: (ev: Map[K]) => any): void; |
108 | | - |
109 | | - abstract doRemoveListener<K extends keyof Map>(key: K, listener: (ev: Map[K]) => any): void; |
110 | | - |
111 | | - addSub<K extends keyof Map>({ key, sub }: { key: K; sub: DocSub<K, Map, Msg> }) { |
112 | | - this.initListener(key); |
113 | | - const subs: SubsMap<Map, Msg>[K] = getSubs(this.subs, key) ?? []; |
114 | | - setSubs(this.subs, key, addOneSub(sub, subs)); |
115 | | - } |
116 | | - |
117 | | - removeSub<K extends keyof Map, M>(key: K, sub: DocSub<K, Map, Msg>) { |
118 | | - const list: SubsMap<Map, Msg>[K] = getSubs(this.subs, key) ?? []; |
119 | | - const list1 = removeOneSub(sub, list); |
120 | | - setSubs(this.subs, key, list1); |
121 | | - if (!list1) { |
122 | | - this.releaseListener(key); |
123 | | - } |
124 | | - } |
125 | | - |
126 | | - private initListener<K extends keyof Map>(key: K) { |
127 | | - if (!this.listeners[key]) { |
128 | | - const listener = (ev: Map[K]) => { |
129 | | - const subs: SubsMap<Map, Msg>[K] = getSubs(this.subs, key) ?? []; |
130 | | - new Array().concat(subs).forEach((s: DocSub<K, Map, Msg>) => s.event(ev)); |
131 | | - return {}; |
132 | | - }; |
133 | | - setListener(this.listeners, key, listener); |
134 | | - this.doAddListener(key, listener); |
135 | | - } |
136 | | - } |
137 | 61 |
|
138 | | - private releaseListener<K extends keyof Map>(key: K) { |
139 | | - const listener: Listener<Map[K]> | undefined = getListener(this.listeners, key); |
140 | | - if (listener) { |
141 | | - setListener(this.listeners, key, undefined); |
142 | | - this.doRemoveListener(key, listener); |
| 62 | + abstract doAddListener<K extends keyof Map>(key: K, |
| 63 | + listener: (ev: Map[K]) => any, |
| 64 | + options?: boolean | AddEventListenerOptions): void; |
| 65 | + |
| 66 | + abstract doRemoveListener<K extends keyof Map>(key: K, |
| 67 | + listener: (ev: Map[K]) => any, |
| 68 | + options?: boolean | AddEventListenerOptions): void; |
| 69 | + |
| 70 | + /** |
| 71 | + * Subscribe to an event. |
| 72 | + * @param key the event type to subscribe to. |
| 73 | + * @param mapper map the event to a message. |
| 74 | + * @param options options for this listener |
| 75 | + */ |
| 76 | + public on<K extends keyof Map, Msg>(key: K, |
| 77 | + mapper: (e: Map[K]) => Msg, |
| 78 | + options?: boolean | AddEventListenerOptions): Sub<Msg> { |
| 79 | + return new DocSub<K, Map, Msg>(this, key, mapper, options); |
143 | 80 | } |
144 | | - } |
145 | | - |
146 | | - /** |
147 | | - * Subscribe to an event. |
148 | | - * @param key the event type to subscribe to. |
149 | | - * @param mapper map the event to a message. |
150 | | - */ |
151 | | - public on<K extends keyof Map, Msg>(key: K, mapper: (e: Map[K]) => Msg): Sub<Msg> { |
152 | | - return new DocSub<K, Map, Msg>(this, key, mapper); |
153 | | - } |
154 | 81 | } |
155 | 82 |
|
156 | 83 | /** |
157 | 84 | * Subscribe to document events. |
158 | 85 | */ |
159 | 86 | export class DocumentEvents<Msg> extends EventMapEvents<DocumentEventMap, Msg> { |
160 | | - doAddListener<K extends keyof DocumentEventMap>(key: K, listener: (ev: DocumentEventMap[K]) => any): void { |
161 | | - document.addEventListener(key, listener); |
162 | | - } |
| 87 | + doAddListener<K extends keyof DocumentEventMap>(key: K, |
| 88 | + listener: (ev: DocumentEventMap[K]) => any, |
| 89 | + options?: boolean | AddEventListenerOptions): void { |
| 90 | + document.addEventListener(key, listener, options); |
| 91 | + } |
163 | 92 |
|
164 | | - doRemoveListener<K extends keyof DocumentEventMap>(key: K, listener: (ev: DocumentEventMap[K]) => any): void { |
165 | | - document.removeEventListener(key, listener); |
166 | | - } |
| 93 | + doRemoveListener<K extends keyof DocumentEventMap>(key: K, |
| 94 | + listener: (ev: DocumentEventMap[K]) => any, |
| 95 | + options?: boolean | AddEventListenerOptions): void { |
| 96 | + document.removeEventListener(key, listener, options); |
| 97 | + } |
167 | 98 | } |
168 | 99 |
|
169 | 100 | /** |
170 | 101 | * Bonus, WindowEvents |
171 | 102 | */ |
172 | 103 | export class WindowEvents<Msg> extends EventMapEvents<WindowEventMap, Msg> { |
173 | | - doAddListener<K extends keyof WindowEventMap>(key: K, listener: (ev: WindowEventMap[K]) => any): void { |
174 | | - window.addEventListener(key, listener); |
175 | | - } |
| 104 | + doAddListener<K extends keyof WindowEventMap>(key: K, |
| 105 | + listener: (ev: WindowEventMap[K]) => any, |
| 106 | + options?: boolean | AddEventListenerOptions): void { |
| 107 | + window.addEventListener(key, listener, options); |
| 108 | + } |
176 | 109 |
|
177 | | - doRemoveListener<K extends keyof WindowEventMap>(key: K, listener: (ev: WindowEventMap[K]) => any): void { |
178 | | - window.removeEventListener(key, listener); |
179 | | - } |
| 110 | + doRemoveListener<K extends keyof WindowEventMap>(key: K, |
| 111 | + listener: (ev: WindowEventMap[K]) => any, |
| 112 | + options?: boolean | AddEventListenerOptions): void { |
| 113 | + window.removeEventListener(key, listener, options); |
| 114 | + } |
180 | 115 | } |
0 commit comments