@flipfeatureflag/react 0.1.9 → 0.1.11
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/package.json +7 -4
- package/src/index.tsx +0 -174
- package/tsconfig.json +0 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flipfeatureflag/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "flipFeatureFlag React SDK",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -20,7 +20,10 @@
|
|
|
20
20
|
"react": ">=17"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@flipfeatureflag/core": "0.1.
|
|
24
|
-
"@flipfeatureflag/js": "0.1.
|
|
25
|
-
}
|
|
23
|
+
"@flipfeatureflag/core": "0.1.10",
|
|
24
|
+
"@flipfeatureflag/js": "0.1.11"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
]
|
|
26
29
|
}
|
package/src/index.tsx
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import React, { PropsWithChildren, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
-
import { FlipFlagClient, FlipFlagClientOptions, FlagsStatus, SdkFlagEvaluation } from "@flipfeatureflag/core";
|
|
3
|
-
import { createClient as createBrowserClient } from "@flipfeatureflag/js";
|
|
4
|
-
|
|
5
|
-
interface FlipFlagProviderProps {
|
|
6
|
-
client?: FlipFlagClient;
|
|
7
|
-
config?: FlipFlagClientOptions;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const FlipFlagContext = React.createContext<FlipFlagClient | null>(null);
|
|
11
|
-
const FlipFlagStatusContext = React.createContext<FlagsStatus>({ ready: false });
|
|
12
|
-
|
|
13
|
-
export function FlipFlagProvider({ client, config, children }: PropsWithChildren<FlipFlagProviderProps>) {
|
|
14
|
-
const configKey = useMemo(() => {
|
|
15
|
-
if (!config) {
|
|
16
|
-
return "";
|
|
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 {
|
|
46
|
-
if (!config) {
|
|
47
|
-
throw new Error("flipFeatureFlagProvider requires client or config");
|
|
48
|
-
}
|
|
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;
|
|
64
|
-
|
|
65
|
-
const [status, setStatus] = useState<FlagsStatus>(resolvedClient.getStatus());
|
|
66
|
-
|
|
67
|
-
useEffect(() => {
|
|
68
|
-
let mounted = true;
|
|
69
|
-
|
|
70
|
-
const handleUpdate = () => {
|
|
71
|
-
if (mounted) {
|
|
72
|
-
setStatus(resolvedClient.getStatus());
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
resolvedClient.on("ready", handleUpdate);
|
|
77
|
-
resolvedClient.on("update", handleUpdate);
|
|
78
|
-
resolvedClient.on("error", handleUpdate);
|
|
79
|
-
|
|
80
|
-
void resolvedClient.start();
|
|
81
|
-
|
|
82
|
-
return () => {
|
|
83
|
-
mounted = false;
|
|
84
|
-
resolvedClient.off("ready", handleUpdate);
|
|
85
|
-
resolvedClient.off("update", handleUpdate);
|
|
86
|
-
resolvedClient.off("error", handleUpdate);
|
|
87
|
-
if (ownsClient) {
|
|
88
|
-
resolvedClient.stop();
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
}, [resolvedClient, ownsClient]);
|
|
92
|
-
|
|
93
|
-
return (
|
|
94
|
-
<FlipFlagContext.Provider value={resolvedClient}>
|
|
95
|
-
<FlipFlagStatusContext.Provider value={status}>{children}</FlipFlagStatusContext.Provider>
|
|
96
|
-
</FlipFlagContext.Provider>
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function useFlipFlagClient(): FlipFlagClient {
|
|
101
|
-
const client = React.useContext(FlipFlagContext);
|
|
102
|
-
if (!client) {
|
|
103
|
-
throw new Error("useFlipFlagClient must be used within flipFeatureFlagProvider");
|
|
104
|
-
}
|
|
105
|
-
return client;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function useFlagsStatus(): FlagsStatus {
|
|
109
|
-
return React.useContext(FlipFlagStatusContext);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export interface FlagValueResult<T = SdkFlagEvaluation["value"]> {
|
|
113
|
-
value: T;
|
|
114
|
-
enabled: boolean;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function toFlagResult(evaluation: SdkFlagEvaluation, known: boolean): FlagValueResult {
|
|
118
|
-
const enabled = known && evaluation.reason !== "disabled";
|
|
119
|
-
return {
|
|
120
|
-
value: evaluation.value,
|
|
121
|
-
enabled,
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export function useFlag(
|
|
126
|
-
flagKey: string,
|
|
127
|
-
defaultValue: SdkFlagEvaluation["value"] = false,
|
|
128
|
-
): FlagValueResult {
|
|
129
|
-
const client = useFlipFlagClient();
|
|
130
|
-
const [result, setResult] = useState(() =>
|
|
131
|
-
toFlagResult(client.getVariant(flagKey, defaultValue), client.hasFlag(flagKey)),
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
useEffect(() => {
|
|
135
|
-
const update = () => {
|
|
136
|
-
setResult(toFlagResult(client.getVariant(flagKey, defaultValue), client.hasFlag(flagKey)));
|
|
137
|
-
};
|
|
138
|
-
client.on("update", update);
|
|
139
|
-
return () => client.off("update", update);
|
|
140
|
-
}, [client, flagKey, defaultValue]);
|
|
141
|
-
|
|
142
|
-
return result;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export function useVariant(flagKey: string, defaultValue: SdkFlagEvaluation["value"] = false): SdkFlagEvaluation {
|
|
146
|
-
const client = useFlipFlagClient();
|
|
147
|
-
const [variant, setVariant] = useState(() => client.getVariant(flagKey, defaultValue));
|
|
148
|
-
|
|
149
|
-
useEffect(() => {
|
|
150
|
-
const update = () => {
|
|
151
|
-
setVariant(client.getVariant(flagKey, defaultValue));
|
|
152
|
-
};
|
|
153
|
-
client.on("update", update);
|
|
154
|
-
return () => client.off("update", update);
|
|
155
|
-
}, [client, flagKey, defaultValue]);
|
|
156
|
-
|
|
157
|
-
return variant;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export function useAllFlags(): Record<string, SdkFlagEvaluation> {
|
|
161
|
-
const client = useFlipFlagClient();
|
|
162
|
-
const [flags, setFlags] = useState(() => client.getAllFlags());
|
|
163
|
-
|
|
164
|
-
useEffect(() => {
|
|
165
|
-
const update = () => setFlags(client.getAllFlags());
|
|
166
|
-
client.on("update", update);
|
|
167
|
-
return () => client.off("update", update);
|
|
168
|
-
}, [client]);
|
|
169
|
-
|
|
170
|
-
return flags;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
export type { FlipFlagClientOptions } from "@flipfeatureflag/core";
|
|
174
|
-
export { FlipFlagClient } from "@flipfeatureflag/core";
|