UNPKG

stencil-wormhole

Version:

Pass props down component trees easily via wormholes.

34 lines (33 loc) 1.28 kB
import { getElement } from "@stencil/core"; import { createDeferredPromise } from "./deferred"; export var openWormhole = function (Component, props, isBlocking) { if (isBlocking === void 0) { isBlocking = true; } var isConstructor = (Component.constructor.name === 'Function'); var Proto = isConstructor ? Component.prototype : Component; var componentWillLoad = Proto.componentWillLoad; Proto.componentWillLoad = function () { var _this = this; var el = getElement(this); var onOpen = createDeferredPromise(); var event = new CustomEvent('openWormhole', { bubbles: true, composed: true, detail: { consumer: this, fields: props, updater: function (prop, value) { var target = (prop in el) ? el : _this; target[prop] = value; }, onOpen: onOpen, }, }); el.dispatchEvent(event); var willLoad = function () { if (componentWillLoad) { return componentWillLoad.call(_this); } }; return isBlocking ? onOpen.promise.then(function () { return willLoad(); }) : (willLoad()); }; };