UNPKG

stencil-quantum

Version:

Experience the quantum realm of stencil.

37 lines (36 loc) 1.45 kB
import { getElement } from "@stencil/core"; import { throwQuantum } from "../error"; import { Provider } from "../provider"; import { hookComponent } from "../utils"; export function Implement(config, key) { return function (prototype, propertyName) { const opts = config === null || config === void 0 ? void 0 : config.get(key); let defaultValue = prototype[propertyName] || (opts === null || opts === void 0 ? void 0 : opts.default); delete prototype[propertyName]; hookComponent(prototype, "componentWillLoad", obj => { const el = getElement(obj); const provider = Provider.create(el, key, defaultValue !== null && defaultValue !== void 0 ? defaultValue : obj[propertyName], opts === null || opts === void 0 ? void 0 : opts.namespace, opts === null || opts === void 0 ? void 0 : opts.debug); try { provider.attach(el); provider.hook(el); } catch (err) { throwQuantum(el, err); } Object.defineProperty(obj, propertyName, { get: () => provider.retrieve(), set: (v) => provider.provide((...args) => v.apply(obj, args)), enumerable: true, configurable: true }); hookComponent(prototype, "disconnectedCallback", o => { if (o === obj) provider.pause(); }); hookComponent(prototype, "connectedCallback", o => { if (o === obj) provider.pause(false); }); }); }; }