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
*/
@@ -187,27 +185,27 @@ export class Observable implements ObservableDefinition {
187
185
* @param thisArg An optional parameter which when set will be used as "this" in callback method call.
188
186
* @param options An optional parameter. If passed as a boolean, configures the useCapture value. Otherwise, specifies options.
189
187
*/
190
- public addEventListener ( eventNames : string , callback : ( data : EventData ) => void , thisArg ?: any , options ?: AddEventListenerOptions | boolean ) : void {
188
+ public addEventListener ( eventNames : string , callback : EventListenerOrEventListenerObject | ( ( data : EventData ) => void ) , thisArg ?: any , options ?: AddEventListenerOptions | boolean ) : void {
191
189
if ( typeof eventNames !== 'string' ) {
192
190
throw new TypeError ( 'Events name(s) must be string.' ) ;
193
191
}
194
192
195
193
if ( typeof callback !== 'function' ) {
196
- throw new TypeError ( 'callback must be function.' ) ;
194
+ throw new TypeError ( 'Callback must be function.' ) ;
197
195
}
198
196
199
197
const events = eventNames . trim ( ) . split ( eventDelimiterPattern ) ;
200
198
for ( let i = 0 , l = events . length ; i < l ; i ++ ) {
201
199
const event = events [ i ] ;
202
200
const list = this . getEventList ( event , true ) ;
203
- if ( Observable . _indexOfListener ( list , callback , thisArg , options ) >= 0 ) {
201
+ if ( Observable . _indexOfListener ( list , callback as ( data : EventData ) => void , thisArg , options ) >= 0 ) {
204
202
// Don't allow addition of duplicate event listeners.
205
203
continue ;
206
204
}
207
205
208
206
// TODO: Performance optimization - if we do not have the thisArg specified, do not wrap the callback in additional object (ObserveEntry)
209
207
list . push ( {
210
- callback,
208
+ callback : callback as ( data : EventData ) => void ,
211
209
thisArg,
212
210
capture : getCaptureFromOptions ( options ) ,
213
211
passive : getPassiveFromOptions ( options ) ,
@@ -222,7 +220,7 @@ export class Observable implements ObservableDefinition {
222
220
* @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.
223
221
* @param options An optional parameter. If passed as a boolean, configures the useCapture value. Otherwise, specifies options.
224
222
*/
225
- public removeEventListener ( eventNames : string , callback ?: ( data : EventData ) => void , thisArg ?: any , options ?: EventListenerOptions | boolean ) : void {
223
+ public removeEventListener ( eventNames : string , callback ?: EventListenerOrEventListenerObject | ( ( data : EventData ) => void ) , thisArg ?: any , options ?: EventListenerOptions | boolean ) : void {
226
224
if ( typeof eventNames !== 'string' ) {
227
225
throw new TypeError ( 'Events name(s) must be string.' ) ;
228
226
}
@@ -238,14 +236,16 @@ export class Observable implements ObservableDefinition {
238
236
}
239
237
240
238
const list = this . getEventList ( event , false ) ;
241
- if ( list ) {
242
- const index = Observable . _indexOfListener ( list , callback , thisArg , options ) ;
243
- if ( index >= 0 ) {
244
- list . splice ( index , 1 ) ;
245
- }
246
- if ( list . length === 0 ) {
247
- delete this . _observers [ event ] ;
248
- }
239
+ if ( ! list ) {
240
+ continue ;
241
+ }
242
+
243
+ const index = Observable . _indexOfListener ( list , callback as ( data : EventData ) => void , thisArg , options ) ;
244
+ if ( index >= 0 ) {
245
+ list . splice ( index , 1 ) ;
246
+ }
247
+ if ( list . length === 0 ) {
248
+ delete this . _observers [ event ] ;
249
249
}
250
250
}
251
251
}
@@ -263,13 +263,13 @@ export class Observable implements ObservableDefinition {
263
263
this . removeEventListener ( eventName , callback , thisArg , options ) ;
264
264
}
265
265
266
- public static removeEventListener ( eventName : string , callback ?: ( data : EventData ) => void , thisArg ?: any , options ?: EventListenerOptions | boolean ) : void {
266
+ public static removeEventListener ( eventName : string , callback ?: EventListenerOrEventListenerObject | ( ( data : EventData ) => void ) , thisArg ?: any , options ?: EventListenerOptions | boolean ) : void {
267
267
if ( typeof eventName !== 'string' ) {
268
268
throw new TypeError ( 'Event must be string.' ) ;
269
269
}
270
270
271
271
if ( callback && typeof callback !== 'function' ) {
272
- throw new TypeError ( 'callback must be function.' ) ;
272
+ throw new TypeError ( 'Callback, if provided, must be function.' ) ;
273
273
}
274
274
275
275
const eventClass = this . name === 'Observable' ? '*' : this . name ;
@@ -281,7 +281,7 @@ export class Observable implements ObservableDefinition {
281
281
282
282
const events = _globalEventHandlers [ eventClass ] [ eventName ] ;
283
283
if ( callback ) {
284
- const index = Observable . _indexOfListener ( events , callback , thisArg , options ) ;
284
+ const index = Observable . _indexOfListener ( events , callback as ( data : EventData ) => void , thisArg , options ) ;
285
285
if ( index >= 0 ) {
286
286
events . splice ( index , 1 ) ;
287
287
}
@@ -302,13 +302,13 @@ export class Observable implements ObservableDefinition {
302
302
}
303
303
}
304
304
305
- public static addEventListener ( eventName : string , callback : ( data : EventData ) => void , thisArg ?: any , options ?: AddEventListenerOptions | boolean ) : void {
305
+ public static addEventListener ( eventName : string , callback : EventListenerOrEventListenerObject | ( ( data : EventData ) => void ) , thisArg ?: any , options ?: AddEventListenerOptions | boolean ) : void {
306
306
if ( typeof eventName !== 'string' ) {
307
307
throw new TypeError ( 'Event must be string.' ) ;
308
308
}
309
309
310
310
if ( typeof callback !== 'function' ) {
311
- throw new TypeError ( 'callback must be function.' ) ;
311
+ throw new TypeError ( 'Callback must be function.' ) ;
312
312
}
313
313
314
314
const eventClass = this . name === 'Observable' ? '*' : this . name ;
@@ -320,13 +320,13 @@ export class Observable implements ObservableDefinition {
320
320
}
321
321
322
322
const list = _globalEventHandlers [ eventClass ] [ eventName ] ;
323
- if ( Observable . _indexOfListener ( list , callback , thisArg , options ) >= 0 ) {
323
+ if ( Observable . _indexOfListener ( list , callback as ( data : EventData ) => void , thisArg , options ) >= 0 ) {
324
324
// Don't allow addition of duplicate event listeners.
325
325
return ;
326
326
}
327
327
328
328
_globalEventHandlers [ eventClass ] [ eventName ] . push ( {
329
- callback,
329
+ callback : callback as ( data : EventData ) => void ,
330
330
thisArg,
331
331
capture : getCaptureFromOptions ( options ) ,
332
332
passive : getPassiveFromOptions ( options ) ,
@@ -388,6 +388,21 @@ export class Observable implements ObservableDefinition {
388
388
} ) ;
389
389
}
390
390
391
+ dispatchEvent ( event : DOMEvent ) : boolean {
392
+ const data = {
393
+ eventName : event . type ,
394
+ object : this ,
395
+ detail : event . detail ,
396
+ } ;
397
+
398
+ return event . dispatchTo ( {
399
+ target : this ,
400
+ data,
401
+ getGlobalEventHandlersPreHandling : ( ) => this . _getGlobalEventHandlers ( data , 'First' ) ,
402
+ getGlobalEventHandlersPostHandling : ( ) => this . _getGlobalEventHandlers ( data , '' ) ,
403
+ } ) ;
404
+ }
405
+
391
406
private _getGlobalEventHandlers ( data : EventData , eventType : 'First' | '' ) : ListenerEntry [ ] {
392
407
const eventClass = data . object ?. constructor ?. name ;
393
408
const globalEventHandlersForOwnClass = _globalEventHandlers [ eventClass ] ?. [ `${ data . eventName } ${ eventType } ` ] ?? [ ] ;
0 commit comments