UNPKG

stencil-quantum

Version:

Experience the quantum realm of stencil.

38 lines (37 loc) 1.5 kB
import { Provider } from "../provider"; import { getElement } from "@stencil/core"; import { hookComponent } from "../utils"; import { throwQuantum } from "../error"; export function Provide(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); provider.mutable = !!(opts === null || opts === void 0 ? void 0 : opts.mutable); try { provider.attach(el); provider.hook(el); } catch (err) { throwQuantum(el, err); } Object.defineProperty(obj, propertyName, { get: () => provider.retrieve(), set: (v) => provider.provide(v), enumerable: true, configurable: true }); hookComponent(prototype, "disconnectedCallback", o => { if (o === obj) provider.pause(); }); hookComponent(prototype, "connectedCallback", o => { if (o === obj) provider.pause(false); }); }); }; }