Skip to content

Commit

Permalink
strict mode fixes (#23)
Browse files Browse the repository at this point in the history
* strict mode fixes

* test fixes

* v2.2.0

* 2.2.0

* function call
  • Loading branch information
laliconfigcat authored Mar 9, 2023
1 parent 02fd55b commit 2d3b234
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
11 changes: 11 additions & 0 deletions samples/react-sdk-sample/src/ConfigCatHOC.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
Expand Down
40 changes: 19 additions & 21 deletions src/ConfigCatProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type ConfigCatProviderState = {
lastUpdated?: Date;
};

const initializedClients = new Map<string, number>();

class ConfigCatProvider extends Component<
/* eslint-disable @typescript-eslint/indent */
PropsWithChildren<ConfigCatProviderProps>,
Expand All @@ -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(),
Expand All @@ -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() });
}

Expand Down

0 comments on commit 2d3b234

Please sign in to comment.