@flipfeatureflag/react 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -46,15 +46,53 @@ var import_jsx_runtime = require("react/jsx-runtime");
46
46
  var FlipFlagContext = import_react.default.createContext(null);
47
47
  var FlipFlagStatusContext = import_react.default.createContext({ ready: false });
48
48
  function FlipFlagProvider({ client, config, children }) {
49
- const { resolvedClient, ownsClient } = (0, import_react.useMemo)(() => {
50
- if (client) {
51
- return { resolvedClient: client, ownsClient: false };
49
+ const configKey = (0, import_react.useMemo)(() => {
50
+ if (!config) {
51
+ return "";
52
52
  }
53
+ return [
54
+ config.url,
55
+ config.sdkKey,
56
+ config.env ?? "",
57
+ String(config.refreshIntervalMs ?? ""),
58
+ String(config.metricsIntervalMs ?? ""),
59
+ String(config.disableRefresh ?? false),
60
+ String(config.disableMetrics ?? false)
61
+ ].join("|");
62
+ }, [
63
+ config?.url,
64
+ config?.sdkKey,
65
+ config?.env,
66
+ config?.refreshIntervalMs,
67
+ config?.metricsIntervalMs,
68
+ config?.disableRefresh,
69
+ config?.disableMetrics
70
+ ]);
71
+ const clientRef = (0, import_react.useRef)(null);
72
+ const ownsRef = (0, import_react.useRef)(false);
73
+ const configKeyRef = (0, import_react.useRef)(configKey);
74
+ if (client) {
75
+ clientRef.current = client;
76
+ ownsRef.current = false;
77
+ configKeyRef.current = configKey;
78
+ } else {
53
79
  if (!config) {
54
80
  throw new Error("flipFeatureFlagProvider requires client or config");
55
81
  }
56
- return { resolvedClient: (0, import_js.createClient)(config), ownsClient: true };
57
- }, [client, config]);
82
+ if (!clientRef.current || configKeyRef.current !== configKey) {
83
+ if (clientRef.current && ownsRef.current) {
84
+ clientRef.current.stop();
85
+ }
86
+ clientRef.current = (0, import_js.createClient)(config);
87
+ ownsRef.current = true;
88
+ configKeyRef.current = configKey;
89
+ }
90
+ }
91
+ const resolvedClient = clientRef.current;
92
+ if (!resolvedClient) {
93
+ throw new Error("flipFeatureFlagProvider requires client or config");
94
+ }
95
+ const ownsClient = ownsRef.current;
58
96
  const [status, setStatus] = (0, import_react.useState)(resolvedClient.getStatus());
59
97
  (0, import_react.useEffect)(() => {
60
98
  let mounted = true;
package/dist/index.mjs CHANGED
@@ -1,20 +1,58 @@
1
1
  // src/index.tsx
2
- import React, { useEffect, useMemo, useState } from "react";
2
+ import React, { useEffect, useMemo, useRef, useState } from "react";
3
3
  import { createClient as createBrowserClient } from "@flipfeatureflag/js";
4
4
  import { FlipFlagClient } from "@flipfeatureflag/core";
5
5
  import { jsx } from "react/jsx-runtime";
6
6
  var FlipFlagContext = React.createContext(null);
7
7
  var FlipFlagStatusContext = React.createContext({ ready: false });
8
8
  function FlipFlagProvider({ client, config, children }) {
9
- const { resolvedClient, ownsClient } = useMemo(() => {
10
- if (client) {
11
- return { resolvedClient: client, ownsClient: false };
9
+ const configKey = useMemo(() => {
10
+ if (!config) {
11
+ return "";
12
12
  }
13
+ return [
14
+ config.url,
15
+ config.sdkKey,
16
+ config.env ?? "",
17
+ String(config.refreshIntervalMs ?? ""),
18
+ String(config.metricsIntervalMs ?? ""),
19
+ String(config.disableRefresh ?? false),
20
+ String(config.disableMetrics ?? false)
21
+ ].join("|");
22
+ }, [
23
+ config?.url,
24
+ config?.sdkKey,
25
+ config?.env,
26
+ config?.refreshIntervalMs,
27
+ config?.metricsIntervalMs,
28
+ config?.disableRefresh,
29
+ config?.disableMetrics
30
+ ]);
31
+ const clientRef = useRef(null);
32
+ const ownsRef = useRef(false);
33
+ const configKeyRef = useRef(configKey);
34
+ if (client) {
35
+ clientRef.current = client;
36
+ ownsRef.current = false;
37
+ configKeyRef.current = configKey;
38
+ } else {
13
39
  if (!config) {
14
40
  throw new Error("flipFeatureFlagProvider requires client or config");
15
41
  }
16
- return { resolvedClient: createBrowserClient(config), ownsClient: true };
17
- }, [client, config]);
42
+ if (!clientRef.current || configKeyRef.current !== configKey) {
43
+ if (clientRef.current && ownsRef.current) {
44
+ clientRef.current.stop();
45
+ }
46
+ clientRef.current = createBrowserClient(config);
47
+ ownsRef.current = true;
48
+ configKeyRef.current = configKey;
49
+ }
50
+ }
51
+ const resolvedClient = clientRef.current;
52
+ if (!resolvedClient) {
53
+ throw new Error("flipFeatureFlagProvider requires client or config");
54
+ }
55
+ const ownsClient = ownsRef.current;
18
56
  const [status, setStatus] = useState(resolvedClient.getStatus());
19
57
  useEffect(() => {
20
58
  let mounted = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flipfeatureflag/react",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "flipFeatureFlag React SDK",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -20,7 +20,7 @@
20
20
  "react": ">=17"
21
21
  },
22
22
  "dependencies": {
23
- "@flipfeatureflag/core": "0.1.4",
24
- "@flipfeatureflag/js": "0.1.5"
23
+ "@flipfeatureflag/core": "0.1.5",
24
+ "@flipfeatureflag/js": "0.1.6"
25
25
  }
26
26
  }
package/src/index.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import React, { PropsWithChildren, useEffect, useMemo, useState } from "react";
1
+ import React, { PropsWithChildren, useEffect, useMemo, useRef, useState } from "react";
2
2
  import { FlipFlagClient, FlipFlagClientOptions, FlagsStatus, SdkFlagEvaluation } from "@flipfeatureflag/core";
3
3
  import { createClient as createBrowserClient } from "@flipfeatureflag/js";
4
4
 
@@ -11,15 +11,56 @@ const FlipFlagContext = React.createContext<FlipFlagClient | null>(null);
11
11
  const FlipFlagStatusContext = React.createContext<FlagsStatus>({ ready: false });
12
12
 
13
13
  export function FlipFlagProvider({ client, config, children }: PropsWithChildren<FlipFlagProviderProps>) {
14
- const { resolvedClient, ownsClient } = useMemo(() => {
15
- if (client) {
16
- return { resolvedClient: client, ownsClient: false };
14
+ const configKey = useMemo(() => {
15
+ if (!config) {
16
+ return "";
17
17
  }
18
+ return [
19
+ config.url,
20
+ config.sdkKey,
21
+ config.env ?? "",
22
+ String(config.refreshIntervalMs ?? ""),
23
+ String(config.metricsIntervalMs ?? ""),
24
+ String(config.disableRefresh ?? false),
25
+ String(config.disableMetrics ?? false),
26
+ ].join("|");
27
+ }, [
28
+ config?.url,
29
+ config?.sdkKey,
30
+ config?.env,
31
+ config?.refreshIntervalMs,
32
+ config?.metricsIntervalMs,
33
+ config?.disableRefresh,
34
+ config?.disableMetrics,
35
+ ]);
36
+
37
+ const clientRef = useRef<FlipFlagClient | null>(null);
38
+ const ownsRef = useRef(false);
39
+ const configKeyRef = useRef(configKey);
40
+
41
+ if (client) {
42
+ clientRef.current = client;
43
+ ownsRef.current = false;
44
+ configKeyRef.current = configKey;
45
+ } else {
18
46
  if (!config) {
19
47
  throw new Error("flipFeatureFlagProvider requires client or config");
20
48
  }
21
- return { resolvedClient: createBrowserClient(config), ownsClient: true };
22
- }, [client, config]);
49
+ if (!clientRef.current || configKeyRef.current !== configKey) {
50
+ if (clientRef.current && ownsRef.current) {
51
+ clientRef.current.stop();
52
+ }
53
+ clientRef.current = createBrowserClient(config);
54
+ ownsRef.current = true;
55
+ configKeyRef.current = configKey;
56
+ }
57
+ }
58
+
59
+ const resolvedClient = clientRef.current;
60
+ if (!resolvedClient) {
61
+ throw new Error("flipFeatureFlagProvider requires client or config");
62
+ }
63
+ const ownsClient = ownsRef.current;
23
64
 
24
65
  const [status, setStatus] = useState<FlagsStatus>(resolvedClient.getStatus());
25
66