[]previous and next page

chainEvents

Combines several handlers for one event name on one element into one handler that calls them in order with the same event.

Combines several handlers for one event name on one element into one handler that calls them in order with the same event. The compiler uses it when an element has, for example, both onInput and a text input's onChange: the delegated slot holds one function.

Import#

ts
import { chainEvents } from "@shadow-js/web";

Signature#

ts
function chainEvents(
  ...handlers: Array<(this: EventTarget, event: Event) => unknown>
): (this: EventTarget, event: Event) => void

Parameters#

Name Description
handlers The handlers, in source order. None gives a handler that does nothing.

Returns#

A handler that calls each of them with the same this and event. An error thrown by one handler propagates and the later handlers do not run.

Examples#

ts
import { addEventListener, chainEvents } from "@shadow-js/web";

const input = document.createElement("input");
const onInput = chainEvents(
  (event) => console.log("validate", event.type),
  (event) => console.log("save", event.type),
);
addEventListener(input, "input", onInput);