Appearance
@tmrw-realityos/charm • Docs
@tmrw-realityos/charm / ROSEvent
Class: ROSEvent<T>
Simple, type safe, multicast events.
Leaves out the complexity of event bubbling and cancellation.
Uses a naming convention of "on" to register an event listener and "off" to remove it.
Example
typescript
class EventHost {
#currentValue = 0;
#valueChanged= new ROSEvent<(value: number) => void>();
mutateState() {
this.#currentValue = 10;
this.#valueChanged.dispatch(this.#currentValue);
}
}
// Observer
const host = new EventHost();
const offChanged = host.#valueChanged.on((value) => {
console.log("Value changed to", value);
});
function exit() {
// remove event listener
if (offChanged) offChanged();
}
Type Parameters
• T extends EventHandler
Constructors
new ROSEvent()
new ROSEvent<
T
>():ROSEvent
<T
>
Returns
ROSEvent
<T
>
Accessors
dispatch
get
dispatch(): (...args
) =>void
Internal
Returns
Function
Parameters
• ...args: Parameters
<T
>
Returns
void
Defined in
packages/charm/src/helpers/events.ts:75
Methods
on()
on(
callback
):RemoveEventListener
Register an event listener.
Parameters
• callback: T
Returns
RemoveEventListener
A function that will remove the event listener when called.
Parameter
callback - The function to be called when the event is dispatched.
Example
typescript
const offChanged = host.#onChanged.on((value) => {
console.log("Value changed to", value);
});