UNPKG

stencil-wormhole

Version:

Pass props down component trees easily via wormholes.

37 lines (36 loc) 1.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.openWormhole = void 0; var core_1 = require("@stencil/core"); var deferred_1 = require("./deferred"); exports.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 = core_1.getElement(this); var onOpen = deferred_1.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()); }; };