UNPKG

configcat-react

Version:

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

227 lines (226 loc) 10.6 kB
"use client"; "use strict"; 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 __()); }; })(); var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createConfigCatProviderError = void 0; var sdk_1 = require("@configcat/sdk"); var react_1 = __importStar(require("react")); var ConfigCatContext_1 = require("./ConfigCatContext"); var Version_1 = __importDefault(require("./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: Version_1.default, eventEmitterFactory: function () { return new sdk_1.Internals.DefaultEventEmitter(); }, defaultCacheFactory: sdk_1.LocalStorageConfigCache["tryGetFactory"](), configFetcherFactory: sdk_1.XmlHttpRequestConfigFetcher["getFactory"](), }; return sdk_1.Internals.getClient(sdkKey, pollingMode !== null && pollingMode !== void 0 ? pollingMode : sdk_1.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 = (0, ConfigCatContext_1.ensureConfigCatContext)(this.props.id); return (react_1.default.createElement(context.Provider, { value: this.state }, this.props.children)); }; return ConfigCatProvider; }(react_1.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"; } function createConfigCatProviderError(methodName, providerId) { return Error("".concat(methodName, " must be used in ConfigCatProvider").concat(providerIdText(providerId), "!")); } exports.createConfigCatProviderError = createConfigCatProviderError; 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; }()); exports.default = ConfigCatProvider;