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) • 8.89 kB
JavaScript
"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 __());
};
})();
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 configcat_common_1 = require("configcat-common");
var react_1 = __importStar(require("react"));
var Cache_1 = require("./Cache");
var ConfigCatContext_1 = require("./ConfigCatContext");
var ConfigFetcher_1 = require("./ConfigFetcher");
var Version_1 = __importDefault(require("./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 = Cache_1.LocalStorageCache.setup({
configFetcher: new ConfigFetcher_1.HttpConfigFetcher(),
sdkType: "ConfigCat-React",
sdkVersion: Version_1.default,
});
return (0, configcat_common_1.getClient)(sdkKey, pollingMode !== null && pollingMode !== void 0 ? pollingMode : configcat_common_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;