@domx/statecontroller
Version:
A StateController base class for handling data state changes on a LitElement
38 lines • 2.22 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { StateController } from "./StateController";
export const windowEvent = (eventClass, options) => (controllerClass, propertyKey) => eventDecorator("window", eventClass, controllerClass, propertyKey, options);
export const hostEvent = (eventClass, options) => (controllerClass, propertyKey) => eventDecorator("host", eventClass, controllerClass, propertyKey, options);
const eventDecorator = (eventTarget, eventClass, controllerClass, propertyKey, options) => {
controllerClass.hostConnected = new Proxy(controllerClass.hostConnected, {
apply: (hostConnected, thisArg, args) => {
(eventTarget === "window" ? window : thisArg.host)
.addEventListener(eventClass.eventType, (event) => __awaiter(void 0, void 0, void 0, function* () {
if (!options || !options.capture === false) {
event.stopImmediatePropagation();
}
thisArg.refreshState();
thisArg[propertyKey](event);
}), {
signal: thisArg.abortController.signal
});
return hostConnected.apply(thisArg);
}
});
};
export const stateProperty = () => (target, propertyKey) => {
const ctor = target.constructor;
if (ctor.stateProperties === StateController.stateProperties) {
// shadow the static state properties field
ctor.stateProperties = [];
}
ctor.stateProperties.push(propertyKey);
};
//# sourceMappingURL=decorators.js.map