From 2d3b23468aa08d5e8037f3b12409df2a01b9e8ce Mon Sep 17 00:00:00 2001 From: Lajos Szoke <63732287+laliconfigcat@users.noreply.github.com> Date: Thu, 9 Mar 2023 13:37:42 +0100 Subject: [PATCH] strict mode fixes (#23) * strict mode fixes * test fixes * v2.2.0 * 2.2.0 * function call --- package-lock.json | 4 +- package.json | 2 +- samples/react-sdk-sample/src/ConfigCatHOC.tsx | 11 +++++ src/ConfigCatProvider.tsx | 40 +++++++++---------- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index b7048ea..3feadc4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "configcat-react", - "version": "2.0.0", + "version": "2.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "configcat-react", - "version": "2.0.0", + "version": "2.2.0", "license": "MIT", "dependencies": { "configcat-common": "^7.0.1", diff --git a/package.json b/package.json index 442ab43..0021447 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "configcat-react", - "version": "2.1.0", + "version": "2.2.0", "scripts": { "build": "npm run build:esm && npm run build:cjs", "build:esm": "tsc -p tsconfig.build.esm.json && gulp esm", diff --git a/samples/react-sdk-sample/src/ConfigCatHOC.tsx b/samples/react-sdk-sample/src/ConfigCatHOC.tsx index 473a557..b7cda42 100644 --- a/samples/react-sdk-sample/src/ConfigCatHOC.tsx +++ b/samples/react-sdk-sample/src/ConfigCatHOC.tsx @@ -11,7 +11,18 @@ class ButtonClassComponent extends React.Component< this.state = { isEnabled: false, loading: true }; } + componentDidUpdate(prevProps: any) { + // To achieve hot reload on config.json updates. + if (prevProps?.lastUpdated !== this.props.lastUpdated) { + this.evaluateFeatureFlag(); + } + } + componentDidMount() { + this.evaluateFeatureFlag(); + } + + evaluateFeatureFlag() { this.props .getValue("isAwesomeFeatureEnabled", false) .then((v: boolean) => this.setState({ isEnabled: v, loading: false })); diff --git a/src/ConfigCatProvider.tsx b/src/ConfigCatProvider.tsx index c7ce9d2..7c3653f 100644 --- a/src/ConfigCatProvider.tsx +++ b/src/ConfigCatProvider.tsx @@ -20,6 +20,8 @@ type ConfigCatProviderState = { lastUpdated?: Date; }; +const initializedClients = new Map(); + class ConfigCatProvider extends Component< /* eslint-disable @typescript-eslint/indent */ PropsWithChildren, @@ -34,12 +36,25 @@ class ConfigCatProvider extends Component< this.state = { client }; } + componentDidMount(): void { + this.state?.client?.on("clientReady", () => this.clientReady()); + this.state?.client?.on("configChanged", newConfig => this.reactConfigChanged(newConfig)); + } + componentWillUnmount(): void { - this.state?.client?.dispose(); + this.state?.client?.removeListener("clientReady", () => this.clientReady()); + this.state?.client?.removeListener("configChanged", newConfig => this.reactConfigChanged(newConfig)); + + initializedClients.set(this.props.sdkKey, (initializedClients.get(this.props.sdkKey) ?? 1) - 1); + + if (initializedClients.get(this.props.sdkKey) === 0) { + this.state?.client?.dispose(); + initializedClients.delete(this.props.sdkKey); + } } private initializeConfigCatClient() { - let { pollingMode, options } = this.props; + const { pollingMode, options } = this.props; const { sdkKey } = this.props; const configCatKernel = { configFetcher: new HttpConfigFetcher(), @@ -48,32 +63,15 @@ class ConfigCatProvider extends Component< sdkVersion: CONFIGCAT_SDK_VERSION }; - pollingMode ??= PollingMode.AutoPoll; - - options ??= {}; - const userSetupHooks = options.setupHooks; - options.setupHooks = hooks => { - hooks.on("clientReady", () => this.clientReady()); - hooks.on("configChanged", newConfig => this.reactConfigChanged(newConfig)); - userSetupHooks?.(hooks); - }; - - return configcatcommon.getClient(sdkKey, pollingMode, options, configCatKernel); + initializedClients.set(sdkKey, (initializedClients.get(sdkKey) ?? 0) + 1); + return configcatcommon.getClient(sdkKey, pollingMode ?? PollingMode.AutoPoll, options, configCatKernel); } reactConfigChanged(newConfig: ProjectConfig): void { - if (!this.state) { - // Initialization phase will set state anyway. - return; - } this.setState({ lastUpdated: new Date(newConfig.Timestamp) }); } clientReady(): void { - if (!this.state) { - // Initialization phase will set state anyway. - return; - } this.setState({ lastUpdated: new Date() }); }