UNPKG

react-singletons

Version:

React Singletons brings the singleton pattern to React components. This module allows applications to create components that live outside of your application with shared states for popups and overlays.

54 lines 2.34 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); var React = __importStar(require("react")); var ReactDom = __importStar(require("react-dom")); var SingletonComponentWrapper_1 = require("../Components/SingletonComponentWrapper"); var Singleton = /** @class */ (function () { function Singleton(component) { var _this = this; this.component = component; this.unmountDelayHandle = -1; this.updateDelayHandle = -1; ReactDom.render(React.createElement(SingletonComponentWrapper_1.SingletonComponentWrapper, { referenceCallback: function (_instance) { return (_this.scwInstance = _instance); }, wrappedComponent: this.component }), document.body.appendChild(document.createElement("div"))); } Singleton.prototype.mount = function (props) { if (typeof this.scwInstance !== "undefined") this.scwInstance.setState({ wrappedProps: props, shouldBeMounted: true }); return this; }; Singleton.prototype.update = function (props, delayMs) { var _this = this; window.clearTimeout(this.updateDelayHandle); if (typeof delayMs !== "undefined") this.updateDelayHandle = window.setTimeout(function () { return _this.update(props); }, delayMs); else if (typeof this.scwInstance !== "undefined") this.scwInstance.setState({ wrappedProps: props }); return this; }; Singleton.prototype.unmount = function (delayMs) { var _this = this; window.clearTimeout(this.unmountDelayHandle); if (typeof delayMs !== "undefined") this.unmountDelayHandle = window.setTimeout(function () { return _this.unmount(); }, delayMs); else if (typeof this.scwInstance !== "undefined") this.scwInstance.setState({ shouldBeMounted: false }); return this; }; return Singleton; }()); exports.Singleton = Singleton; //# sourceMappingURL=Singleton.js.map