@tmrw-realityos/charm / Exports / 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
| Name | Type |
|---|---|
T | extends EventHandler |
Table of contents
Constructors
Accessors
Methods
Constructors
constructor
• new ROSEvent<T>(): ROSEvent<T>
Type parameters
| Name | Type |
|---|---|
T | extends EventHandler<any[]> |
Returns
ROSEvent<T>
Accessors
dispatch
• get dispatch(): (...args: Parameters<T>) => void
Returns
fn
▸ (...args): void
Parameters
| Name | Type |
|---|---|
...args | Parameters<T> |
Returns
void
Defined in
packages/charm/src/helpers/events.ts:75
Methods
on
▸ on(callback): RemoveEventListener
Register an event listener.
Parameters
| Name | Type |
|---|---|
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);
});