ExperimentalThe EventTarget to listen on (window, document, an element, etc.)
The event name (e.g. "resize", "click", "keydown")
The event handler callback
Optionaloptions: boolean | AddEventListenerOptionsOptional addEventListener options (passive, capture, once, signal)
An IDisposable that removes the event listener when disposed
import { on } from "@needle-tools/engine";
const sub = on(window, "resize", (ev) => {
// ev is typed as UIEvent
console.log("resized", ev.target);
});
// Later: clean up
sub.dispose();
import { Behaviour, on } from "@needle-tools/engine";
export class MyComponent extends Behaviour {
onEnable() {
this.autoCleanup(on(window, "resize", (ev) => { /* UIEvent */ }));
this.autoCleanup(on(document, "keydown", (ev) => { /* KeyboardEvent */ }));
this.autoCleanup(on(this.context.domElement, "click", (ev) => { /* MouseEvent */ }));
}
// All listeners removed automatically on disable!
}
ExperimentalSubscribe to a DOM event on any EventTarget (window, document, HTML elements, etc.) and return an IDisposable that removes the listener when disposed.
Provides full TypeScript event type inference — the callback parameter
is automatically typed based on the event name (e.g. "resize" → UIEvent,
"click" → MouseEvent).
Use with DisposableStore.add for automatic lifecycle cleanup in components.
The EventTarget to listen on (window, document, an element, etc.)
The event name (e.g. "resize", "click", "keydown")
The event handler callback
Optionaloptions: boolean | AddEventListenerOptionsOptional addEventListener options (passive, capture, once, signal)
An IDisposable that removes the event listener when disposed
import { on } from "@needle-tools/engine";
const sub = on(window, "resize", (ev) => {
// ev is typed as UIEvent
console.log("resized", ev.target);
});
// Later: clean up
sub.dispose();
import { Behaviour, on } from "@needle-tools/engine";
export class MyComponent extends Behaviour {
onEnable() {
this.autoCleanup(on(window, "resize", (ev) => { /* UIEvent */ }));
this.autoCleanup(on(document, "keydown", (ev) => { /* KeyboardEvent */ }));
this.autoCleanup(on(this.context.domElement, "click", (ev) => { /* MouseEvent */ }));
}
// All listeners removed automatically on disable!
}
ExperimentalSubscribe to a DOM event on any EventTarget (window, document, HTML elements, etc.) and return an IDisposable that removes the listener when disposed.
Provides full TypeScript event type inference — the callback parameter
is automatically typed based on the event name (e.g. "resize" → UIEvent,
"click" → MouseEvent).
Use with DisposableStore.add for automatic lifecycle cleanup in components.
The EventTarget to listen on (window, document, an element, etc.)
The event name (e.g. "resize", "click", "keydown")
The event handler callback
Optionaloptions: boolean | AddEventListenerOptionsOptional addEventListener options (passive, capture, once, signal)
An IDisposable that removes the event listener when disposed
import { on } from "@needle-tools/engine";
const sub = on(window, "resize", (ev) => {
// ev is typed as UIEvent
console.log("resized", ev.target);
});
// Later: clean up
sub.dispose();
import { Behaviour, on } from "@needle-tools/engine";
export class MyComponent extends Behaviour {
onEnable() {
this.autoCleanup(on(window, "resize", (ev) => { /* UIEvent */ }));
this.autoCleanup(on(document, "keydown", (ev) => { /* KeyboardEvent */ }));
this.autoCleanup(on(this.context.domElement, "click", (ev) => { /* MouseEvent */ }));
}
// All listeners removed automatically on disable!
}
ExperimentalSubscribe to a DOM event on any EventTarget (window, document, HTML elements, etc.) and return an IDisposable that removes the listener when disposed.
Provides full TypeScript event type inference — the callback parameter
is automatically typed based on the event name (e.g. "resize" → UIEvent,
"click" → MouseEvent).
Use with DisposableStore.add for automatic lifecycle cleanup in components.
The EventTarget to listen on (window, document, an element, etc.)
The event name (e.g. "resize", "click", "keydown")
The event handler callback
Optionaloptions: boolean | AddEventListenerOptionsOptional addEventListener options (passive, capture, once, signal)
An IDisposable that removes the event listener when disposed
import { on } from "@needle-tools/engine";
const sub = on(window, "resize", (ev) => {
// ev is typed as UIEvent
console.log("resized", ev.target);
});
// Later: clean up
sub.dispose();
import { Behaviour, on } from "@needle-tools/engine";
export class MyComponent extends Behaviour {
onEnable() {
this.autoCleanup(on(window, "resize", (ev) => { /* UIEvent */ }));
this.autoCleanup(on(document, "keydown", (ev) => { /* KeyboardEvent */ }));
this.autoCleanup(on(this.context.domElement, "click", (ev) => { /* MouseEvent */ }));
}
// All listeners removed automatically on disable!
}
Subscribe to a DOM event on any EventTarget (window, document, HTML elements, etc.) and return an IDisposable that removes the listener when disposed.
Provides full TypeScript event type inference — the callback parameter is automatically typed based on the event name (e.g.
"resize"→UIEvent,"click"→MouseEvent).Use with DisposableStore.add for automatic lifecycle cleanup in components.