UNPKG

configcat-react

Version:

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

197 lines (196 loc) 9.23 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 { Internals, LocalStorageConfigCache, PollingMode, XmlHttpRequestConfigFetcher } from "@configcat/sdk"; import React, { Component } from "react"; import { ensureConfigCatContext } from "./ConfigCatContext"; import CONFIGCAT_SDK_VERSION from "./Version"; var ConfigCatProvider = /** @class */ (function (_super) { __extends(ConfigCatProvider, _super); function ConfigCatProvider(props) { var _this = this; var _a, _b; _this = _super.call(this, props) || this; var client = (!isServerContext() ? _this.initializeConfigCatClient() : new ConfigCatClientStub()); var providers = (_a = client._configCatReactSdkProviders) !== null && _a !== void 0 ? _a : (client._configCatReactSdkProviders = new Set()); providers.add(_this); _this.state = { client: client, defaultUser: (_b = _this.props.options) === null || _b === void 0 ? void 0 : _b.defaultUser }; return _this; } ConfigCatProvider.prototype.componentDidMount = function () { var _this = this; var client = this.state.client; // Monkey-patch client's `clearDefaultUser` and `setDefaultUser` methods to detect default user changes. // eslint-disable-next-line @typescript-eslint/unbound-method this.originalClearDefaultUser = client.clearDefaultUser; client.clearDefaultUser = function () { if (_this.originalClearDefaultUser && _this.state.client === client) { _this.originalClearDefaultUser.call(client); _this.setState({ defaultUser: void 0 }); } }; // eslint-disable-next-line @typescript-eslint/unbound-method this.originalSetDefaultUser = client.setDefaultUser; client.setDefaultUser = function (defaultUser) { if (_this.originalSetDefaultUser && _this.state.client === client) { _this.originalSetDefaultUser.call(client, defaultUser); _this.setState({ defaultUser: defaultUser }); } }; // Wire up config data change detection. this.configChangedHandler = function (newConfig) { return _this.reactConfigChanged(newConfig); }; client.waitForReady().then(function () { // If the component was unmounted before client initialization finished, we have nothing left to do. if (_this.configChangedHandler && _this.state.client === client) { client.on("configChanged", _this.configChangedHandler); _this.clientReady(); } }); }; ConfigCatProvider.prototype.componentWillUnmount = function () { var client = this.state.client; // Stop config data change detection. if (this.configChangedHandler) { client.off("configChanged", this.configChangedHandler); this.configChangedHandler = void 0; } // Restore monkey-patched client methods. if (this.originalClearDefaultUser) { client.clearDefaultUser = this.originalClearDefaultUser; this.originalClearDefaultUser = void 0; } if (this.originalSetDefaultUser) { client.setDefaultUser = this.originalSetDefaultUser; this.originalSetDefaultUser = void 0; } // Dispose client if no longer in use. var providers = client._configCatReactSdkProviders; if ((providers === null || providers === void 0 ? void 0 : providers.delete(this)) && !providers.size) { client.dispose(); } }; ConfigCatProvider.prototype.initializeConfigCatClient = function () { var _a = this.props, pollingMode = _a.pollingMode, options = _a.options, sdkKey = _a.sdkKey; var configCatKernel = { sdkType: "ConfigCat-React", sdkVersion: CONFIGCAT_SDK_VERSION, eventEmitterFactory: function () { return new Internals.DefaultEventEmitter(); }, defaultCacheFactory: LocalStorageConfigCache["tryGetFactory"](), configFetcherFactory: XmlHttpRequestConfigFetcher["getFactory"](), }; return Internals.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;