1
1
import type { ViewBase } from '../../ui/core/view-base' ;
2
2
import { DOMEvent } from '../dom-events/dom-event' ;
3
3
4
- import { Observable as ObservableDefinition , WrappedValue as WrappedValueDefinition } from '.' ;
5
-
6
4
/**
7
5
* Base event data.
8
6
*/
@@ -51,7 +49,7 @@ let _wrappedIndex = 0;
51
49
* By default property change will not be fired for a same object.
52
50
* By wrapping object into a WrappedValue instance `same object restriction` will be passed.
53
51
*/
54
- export class WrappedValue implements WrappedValueDefinition {
52
+ export class WrappedValue {
55
53
/**
56
54
* Creates an instance of WrappedValue object.
57
55
* @param wrapped - the real value which should be wrapped.
@@ -96,7 +94,7 @@ const _globalEventHandlers: {
96
94
* Please note that should you be using the `new Observable({})` constructor, it is **obsolete** since v3.0,
97
95
* and you have to migrate to the "data/observable" `fromObject({})` or the `fromObjectRecursive({})` functions.
98
96
*/
99
- export class Observable implements ObservableDefinition {
97
+ export class Observable implements EventTarget {
100
98
/**
101
99
* String value used when hooking to propertyChange event.
102
100
*/
@@ -186,27 +184,27 @@ export class Observable implements ObservableDefinition {
186
184
* @param thisArg An optional parameter which when set will be used as "this" in callback method call.
187
185
* @param options An optional parameter. If passed as a boolean, configures the useCapture value. Otherwise, specifies options.
188
186
*/
189
- public addEventListener ( eventNames : string , callback : ( data : EventData ) => void , thisArg ?: any , options ?: AddEventListenerOptions | boolean ) : void {
187
+ public addEventListener ( eventNames : string , callback : EventListenerOrEventListenerObject | ( ( data : EventData ) => void ) , thisArg ?: any , options ?: AddEventListenerOptions | boolean ) : void {
190
188
if ( typeof eventNames !== 'string' ) {
191
189
throw new TypeError ( 'Events name(s) must be string.' ) ;
192
190
}
193
191
194
192
if ( typeof callback !== 'function' ) {
195
- throw new TypeError ( 'callback must be function.' ) ;
193
+ throw new TypeError ( 'Callback must be function.' ) ;
196
194
}
197
195
198
196
const events = eventNames . trim ( ) . split ( eventDelimiterPattern ) ;
199
197
for ( let i = 0 , l = events . length ; i < l ; i ++ ) {
200
198
const event = events [ i ] ;
201
199
const list = this . getEventList ( event , true ) ;
202
- if ( Observable . _indexOfListener ( list , callback , thisArg , options ) >= 0 ) {
200
+ if ( Observable . _indexOfListener ( list , callback as ( data : EventData ) => void , thisArg , options ) >= 0 ) {
203
201
// Don't allow addition of duplicate event listeners.
204
202
continue ;
205
203
}
206
204
207
205
// TODO: Performance optimization - if we do not have the thisArg specified, do not wrap the callback in additional object (ObserveEntry)
208
206
list . push ( {
209
- callback,
207
+ callback : callback as ( data : EventData ) => void ,
210
208
thisArg,
211
209
...normalizeEventOptions ( options ) ,
212
210
} ) ;
@@ -220,7 +218,7 @@ export class Observable implements ObservableDefinition {
220
218
* @param thisArg An optional parameter which when set will be used to refine search of the correct callback which will be removed as event listener.
221
219
* @param options An optional parameter. If passed as a boolean, configures the useCapture value. Otherwise, specifies options.
222
220
*/
223
- public removeEventListener ( eventNames : string , callback ?: ( data : EventData ) => void , thisArg ?: any , options ?: EventListenerOptions | boolean ) : void {
221
+ public removeEventListener ( eventNames : string , callback ?: EventListenerOrEventListenerObject | ( ( data : EventData ) => void ) , thisArg ?: any , options ?: EventListenerOptions | boolean ) : void {
224
222
if ( typeof eventNames !== 'string' ) {
225
223
throw new TypeError ( 'Events name(s) must be string.' ) ;
226
224
}
@@ -236,14 +234,16 @@ export class Observable implements ObservableDefinition {
236
234
}
237
235
238
236
const list = this . getEventList ( event , false ) ;
239
- if ( list ) {
240
- const index = Observable . _indexOfListener ( list , callback , thisArg , options ) ;
241
- if ( index >= 0 ) {
242
- list . splice ( index , 1 ) ;
243
- }
244
- if ( list . length === 0 ) {
245
- delete this . _observers [ event ] ;
246
- }
237
+ if ( ! list ) {
238
+ continue ;
239
+ }
240
+
241
+ const index = Observable . _indexOfListener ( list , callback as ( data : EventData ) => void , thisArg , options ) ;
242
+ if ( index >= 0 ) {
243
+ list . splice ( index , 1 ) ;
244
+ }
245
+ if ( list . length === 0 ) {
246
+ delete this . _observers [ event ] ;
247
247
}
248
248
}
249
249
}
@@ -260,13 +260,13 @@ export class Observable implements ObservableDefinition {
260
260
this . removeEventListener ( eventName , callback , thisArg , options ) ;
261
261
}
262
262
263
- public static removeEventListener ( eventName : string , callback ?: ( data : EventData ) => void , thisArg ?: any , options ?: EventListenerOptions | boolean ) : void {
263
+ public static removeEventListener ( eventName : string , callback ?: EventListenerOrEventListenerObject | ( ( data : EventData ) => void ) , thisArg ?: any , options ?: EventListenerOptions | boolean ) : void {
264
264
if ( typeof eventName !== 'string' ) {
265
265
throw new TypeError ( 'Event must be string.' ) ;
266
266
}
267
267
268
268
if ( callback && typeof callback !== 'function' ) {
269
- throw new TypeError ( 'callback must be function.' ) ;
269
+ throw new TypeError ( 'Callback, if provided, must be function.' ) ;
270
270
}
271
271
272
272
const eventClass = this . name === 'Observable' ? '*' : this . name ;
@@ -278,7 +278,7 @@ export class Observable implements ObservableDefinition {
278
278
279
279
const events = _globalEventHandlers [ eventClass ] [ eventName ] ;
280
280
if ( callback ) {
281
- const index = Observable . _indexOfListener ( events , callback , thisArg , options ) ;
281
+ const index = Observable . _indexOfListener ( events , callback as ( data : EventData ) => void , thisArg , options ) ;
282
282
if ( index >= 0 ) {
283
283
events . splice ( index , 1 ) ;
284
284
}
@@ -299,13 +299,13 @@ export class Observable implements ObservableDefinition {
299
299
}
300
300
}
301
301
302
- public static addEventListener ( eventName : string , callback : ( data : EventData ) => void , thisArg ?: any , options ?: AddEventListenerOptions | boolean ) : void {
302
+ public static addEventListener ( eventName : string , callback : EventListenerOrEventListenerObject | ( ( data : EventData ) => void ) , thisArg ?: any , options ?: AddEventListenerOptions | boolean ) : void {
303
303
if ( typeof eventName !== 'string' ) {
304
304
throw new TypeError ( 'Event must be string.' ) ;
305
305
}
306
306
307
307
if ( typeof callback !== 'function' ) {
308
- throw new TypeError ( 'callback must be function.' ) ;
308
+ throw new TypeError ( 'Callback must be function.' ) ;
309
309
}
310
310
311
311
const eventClass = this . name === 'Observable' ? '*' : this . name ;
@@ -317,13 +317,13 @@ export class Observable implements ObservableDefinition {
317
317
}
318
318
319
319
const list = _globalEventHandlers [ eventClass ] [ eventName ] ;
320
- if ( Observable . _indexOfListener ( list , callback , thisArg , options ) >= 0 ) {
320
+ if ( Observable . _indexOfListener ( list , callback as ( data : EventData ) => void , thisArg , options ) >= 0 ) {
321
321
// Don't allow addition of duplicate event listeners.
322
322
return ;
323
323
}
324
324
325
325
_globalEventHandlers [ eventClass ] [ eventName ] . push ( {
326
- callback,
326
+ callback : callback as ( data : EventData ) => void ,
327
327
thisArg,
328
328
...normalizeEventOptions ( options ) ,
329
329
} ) ;
@@ -384,6 +384,21 @@ export class Observable implements ObservableDefinition {
384
384
} ) ;
385
385
}
386
386
387
+ dispatchEvent ( event : DOMEvent ) : boolean {
388
+ const data = {
389
+ eventName : event . type ,
390
+ object : this ,
391
+ detail : event . detail ,
392
+ } ;
393
+
394
+ return event . dispatchTo ( {
395
+ target : this ,
396
+ data,
397
+ getGlobalEventHandlersPreHandling : ( ) => this . _getGlobalEventHandlers ( data , 'First' ) ,
398
+ getGlobalEventHandlersPostHandling : ( ) => this . _getGlobalEventHandlers ( data , '' ) ,
399
+ } ) ;
400
+ }
401
+
387
402
private _getGlobalEventHandlers ( data : EventData , eventType : 'First' | '' ) : ListenerEntry [ ] {
388
403
const eventClass = data . object ?. constructor ?. name ;
389
404
const globalEventHandlersForOwnClass = _globalEventHandlers [ eventClass ] ?. [ `${ data . eventName } ${ eventType } ` ] ?? [ ] ;
0 commit comments