UNPKG

configcat-react

Version:

ConfigCat is a configuration as a service that lets you manage your features and configurations without actually deploying new code.

167 lines (166 loc) 7.45 kB
"use client"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { PollingMode, getClient } from "configcat-common"; import React, { Component } from "react"; import { LocalStorageCache } from "./Cache"; import { ensureConfigCatContext } from "./ConfigCatContext"; import { HttpConfigFetcher } from "./ConfigFetcher"; import CONFIGCAT_SDK_VERSION from "./Version"; var ConfigCatProvider = /** @class */ (function (_super) { __extends(ConfigCatProvider, _super); function ConfigCatProvider(props) { var _this = this; var _a; _this = _super.call(this, props) || this; var client = (!isServerContext() ? _this.initializeConfigCatClient() : new ConfigCatClientStub()); var providers = (_a = client.$reactSdk_providers) !== null && _a !== void 0 ? _a : (client.$reactSdk_providers = new Set()); providers.add(_this); _this.state = { client: client }; return _this; } ConfigCatProvider.prototype.componentDidMount = function () { var _this = this; this.configChangedHandler = function (newConfig) { return _this.reactConfigChanged(newConfig); }; this.state.client.waitForReady().then(function () { if (!_this.configChangedHandler) { // If the component was unmounted before client initialization finished, we have nothing left to do. return; } _this.state.client.on("configChanged", _this.configChangedHandler); _this.clientReady(); }); }; ConfigCatProvider.prototype.componentWillUnmount = function () { if (this.configChangedHandler) { this.state.client.off("configChanged", this.configChangedHandler); delete this.configChangedHandler; } var providers = this.state.client.$reactSdk_providers; if ((providers === null || providers === void 0 ? void 0 : providers.delete(this)) && !providers.size) { this.state.client.dispose(); } }; ConfigCatProvider.prototype.initializeConfigCatClient = function () { var _a = this.props, pollingMode = _a.pollingMode, options = _a.options, sdkKey = _a.sdkKey; var configCatKernel = LocalStorageCache.setup({ configFetcher: new HttpConfigFetcher(), sdkType: "ConfigCat-React", sdkVersion: CONFIGCAT_SDK_VERSION, }); return getClient(sdkKey, pollingMode !== null && pollingMode !== void 0 ? pollingMode : PollingMode.AutoPoll, options, configCatKernel); }; ConfigCatProvider.prototype.reactConfigChanged = function (_newConfig) { this.setState({ lastUpdated: new Date() }); }; ConfigCatProvider.prototype.clientReady = function () { this.setState({ lastUpdated: new Date() }); }; ConfigCatProvider.prototype.render = function () { var context = ensureConfigCatContext(this.props.id); return (React.createElement(context.Provider, { value: this.state }, this.props.children)); }; return ConfigCatProvider; }(Component)); function isServerContext() { return typeof XMLHttpRequest === "undefined"; } function serverContextNotSupported() { return new Error("ConfigCat SDK functionality is not available in server context. " + "If you need it in both server and client contexts, please consider using the JS SSR SDK instead of React SDK."); } function providerIdText(providerId) { return providerId ? " with id=\"".concat(providerId, "\"") : " without id attribute"; } export function createConfigCatProviderError(methodName, providerId) { return Error("".concat(methodName, " must be used in ConfigCatProvider").concat(providerIdText(providerId), "!")); } var ConfigCatClientStub = /** @class */ (function () { function ConfigCatClientStub() { this.isOffline = true; } ConfigCatClientStub.prototype.getValueAsync = function (_key, _defaultValue, _user) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.getValueDetailsAsync = function (_key, _defaultValue, _user) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.getAllKeysAsync = function () { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.getAllValuesAsync = function (_user) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.getAllValueDetailsAsync = function (_user) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.getKeyAndValueAsync = function (_variationId) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.forceRefreshAsync = function () { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.waitForReady = function () { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.snapshot = function () { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.setDefaultUser = function (_defaultUser) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.clearDefaultUser = function () { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.setOnline = function () { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.setOffline = function () { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.dispose = function () { }; ConfigCatClientStub.prototype.addListener = function (_eventName, _listener) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.on = function (_eventName, _listener) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.once = function (_eventName, _listener) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.removeListener = function (_eventName, _listener) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.off = function (_eventName, _listener) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.removeAllListeners = function (_eventName) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.listeners = function (_eventName) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.listenerCount = function (_eventName) { throw serverContextNotSupported(); }; ConfigCatClientStub.prototype.eventNames = function () { throw serverContextNotSupported(); }; return ConfigCatClientStub; }()); export default ConfigCatProvider;