All files decorators.ts

100% Statements 22/22
100% Branches 3/3
100% Functions 10/10
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 454x               4x 1x 1x       4x 7x 7x       4x   8x   58x 12x 12x       58x           4x 4x 4x 4x   4x   4x  
import { StateController } from "./StateController";
 
 
interface EventClass {
    eventType:string;
}
 
 
export const windowEvent = (eventClass:EventClass) =>
    (controllerClass:StateController, propertyKey:string) => 
        eventDecorator("window", eventClass, controllerClass, propertyKey);
 
 
 
export const hostEvent = (eventClass:EventClass) =>
    (controllerClass:StateController, propertyKey:string) => 
        eventDecorator("host", eventClass, controllerClass, propertyKey);
 
 
 
const eventDecorator = (eventTarget:string, eventClass:EventClass,
    controllerClass:StateController, propertyKey:string) => {
        controllerClass.hostConnected = new Proxy(controllerClass.hostConnected, {
            apply: (hostConnected, thisArg:StateController, args) => {
                (eventTarget === "window" ? window : thisArg.host)
                    .addEventListener(eventClass.eventType, async (event:Event) => {
                        thisArg[propertyKey](event);
                }, {
                    signal: thisArg.abortController.signal
                } as EventListenerOptions);
                return hostConnected.apply(thisArg);
            }
        });
    };
 
 
export const stateProperty = () =>
    (target: StateController, propertyKey: string) => {
        const ctor = target.constructor as typeof StateController;
        if (ctor.stateProperties === StateController.stateProperties) {
            // shadow the static state properties field
            ctor.stateProperties = [];
        }
        ctor.stateProperties.push(propertyKey);
    };