Emitter¶
Overview¶
Event & listener abstraction for a single "event" type. The type provides extra functionality beyond just notifying listeners. It adds PhET-iO instrumentation capabilities as well as validation. For the lightest-weight, fastest solution with the smallest memory footprint, see TinyEmitter
.
@author Sam Reid (PhET Interactive Simulations) @author Michael Kauzmann (PhET Interactive Simulations)
Class Emitter¶
Constructor¶
new Emitter( providedOptions? : EmitterOptions )¶
Instance Methods¶
emit( ...args : T )¶
Emit to notify listeners
dispose()¶
Disposes an Emitter. All listeners are removed.
addListener( listener : TEmitterListener<T>, options? : DisposerOptions )¶
Adds a listener which will be called during emit.
removeListener( listener : TEmitterListener<T> )¶
Removes a listener
removeAllListeners()¶
Removes all the listeners
hasListener( listener : TEmitterListener<T> ) : boolean¶
Checks whether a listener is registered with this Emitter
hasListeners() : boolean¶
Returns true if there are any listeners.
getListenerCount() : number¶
Returns the number of listeners.
debug( name : string ) : TEmitterListener<T>¶
Convenience function for debugging a Property's value. It prints the new value on registration and when changed. @param name - debug name to be printed on the console @returns - the handle to the listener added in case it needs to be removed later
Static Properties¶
EmitterIO¶
(readonly)
PhET-iO Type for Emitter.
Providing validators to instrumented Emitters: Instrumented Emitters should have their validators
for each argument passed via EmitterIO (the phetioType). To provide validators, there are two methods. First, by default each IOType has its own validator that will be used. So specifying an argument object like { type: NumberIO }
will automatically use NumberIO.validator
as the validator. This can be overridden with the validator
key (second option), like { type: NumberIO, validator: { isValidValue: v=> typeof v === 'number' && v < 5 } }NOTE: currently the implementation is either/or, if a validator is provided via the
validatorkey, the validator from the
type` will be ignored. see https://github.com/phetsims/axon/issues/204 for more details.
@author Sam Reid (PhET Interactive Simulations) @author Michael Kauzmann (PhET Interactive Simulations) @author Andrew Adare (PhET Interactive Simulations)
Type EmitterOptions¶
Pick<TinyEmitterOptions, "reentrantNotificationStrategy"> & StrictOmit<PhetioDataHandlerOptions, "phetioOuterType">
Source Code¶
See the source for Emitter.ts in the axon repository.