demarches-normaliennes/app/javascript/controllers/application_controller.ts

101 lines
2.9 KiB
TypeScript
Raw Normal View History

import { Controller } from '@hotwired/stimulus';
import debounce from 'debounce';
2023-01-12 18:49:46 +01:00
import invariant from 'tiny-invariant';
export type Detail = Record<string, unknown>;
// see: https://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
const FOCUS_EVENTS = ['focus', 'blur'];
export class ApplicationController extends Controller {
#debounced = new Map<() => void, ReturnType<typeof debounce>>();
protected debounce(fn: () => void, interval: number): void {
this.globalDispatch('debounced:added');
let debounced = this.#debounced.get(fn);
if (!debounced) {
2022-10-25 10:56:22 +02:00
const wrapper = () => {
fn.bind(this)();
this.#debounced.delete(fn);
if (this.#debounced.size == 0) {
this.globalDispatch('debounced:empty');
}
2022-10-25 10:56:22 +02:00
};
debounced = debounce(wrapper.bind(this), interval);
this.#debounced.set(fn, debounced);
}
debounced();
}
protected cancelDebounce(fn: () => void) {
this.#debounced.get(fn)?.clear();
}
2022-05-06 19:44:33 +02:00
protected globalDispatch<T = Detail>(type: string, detail?: T): void {
this.dispatch(type, {
2022-08-31 15:57:37 +02:00
detail: detail as object,
prefix: '',
target: document.documentElement
});
}
2023-01-12 18:49:46 +01:00
protected on<HandlerEvent extends Event = Event>(
target: EventTarget,
eventName: string,
handler: (event: HandlerEvent) => void
): void;
protected on<HandlerEvent extends Event = Event>(
eventName: string,
handler: (event: HandlerEvent) => void
2023-01-12 18:49:46 +01:00
): void;
protected on<HandlerEvent extends Event = Event>(
targetOrEventName: EventTarget | string,
eventNameOrHandler: string | ((event: HandlerEvent) => void),
handler?: (event: HandlerEvent) => void
2022-05-06 19:44:33 +02:00
): void {
2023-01-12 18:49:46 +01:00
if (typeof targetOrEventName == 'string') {
invariant(typeof eventNameOrHandler != 'string', 'handler is required');
this.onTarget(
this.element,
targetOrEventName,
eventNameOrHandler,
FOCUS_EVENTS.includes(targetOrEventName)
);
2023-01-12 18:49:46 +01:00
} else {
invariant(
typeof eventNameOrHandler == 'string',
'event name is required'
);
2023-01-12 18:49:46 +01:00
invariant(handler, 'handler is required');
this.onTarget(targetOrEventName, eventNameOrHandler, handler);
}
2022-05-06 19:44:33 +02:00
}
protected onGlobal<HandlerEvent extends Event = Event>(
eventName: string,
handler: (event: HandlerEvent) => void
): void {
this.onTarget(document.documentElement, eventName, handler);
}
private onTarget<HandlerEvent extends Event = Event>(
target: EventTarget,
eventName: string,
handler: (event: HandlerEvent) => void,
capture?: boolean
): void {
const disconnect = this.disconnect;
const callback = (event: Event): void => {
handler(event as HandlerEvent);
};
target.addEventListener(eventName, callback, capture);
this.disconnect = () => {
target.removeEventListener(eventName, callback, capture);
disconnect.call(this);
};
}
}