Skip to content

Commit bc67b25

Browse files
committed
move state-keeping of active flags to browser sdk
1 parent 1b0dcab commit bc67b25

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

packages/browser-sdk/src/client.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,12 +852,31 @@ export class ReflagClient {
852852
}
853853

854854
/**
855-
* Set the active flags from React SDK.
855+
* Register an active flag from React SDK.
856856
* @internal
857857
*/
858-
setActiveFlags(flags: Set<string>) {
859-
this.activeFlags = flags;
860-
this.hooks.trigger("activeFlagsUpdated", flags);
858+
registerActiveFlag(flagKey: string) {
859+
const wasActive = this.activeFlags.has(flagKey);
860+
this.activeFlags.add(flagKey);
861+
862+
// Only trigger update if flag wasn't already active
863+
if (!wasActive) {
864+
this.hooks.trigger("activeFlagsUpdated", this.activeFlags);
865+
}
866+
}
867+
868+
/**
869+
* Unregister an active flag from React SDK.
870+
* @internal
871+
*/
872+
unregisterActiveFlag(flagKey: string) {
873+
const wasActive = this.activeFlags.has(flagKey);
874+
this.activeFlags.delete(flagKey);
875+
876+
// Only trigger update if flag was previously active
877+
if (wasActive) {
878+
this.hooks.trigger("activeFlagsUpdated", this.activeFlags);
879+
}
861880
}
862881

863882
/**

packages/react-sdk/src/index.tsx

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ type ProviderContextType = {
123123
isLoading: boolean;
124124
};
125125
provider: boolean;
126-
activeFlags: Set<string>;
127126
registerActiveFlag: (flagKey: string) => void;
128127
unregisterActiveFlag: (flagKey: string) => void;
129128
};
@@ -134,7 +133,6 @@ const ProviderContext = createContext<ProviderContextType>({
134133
isLoading: false,
135134
},
136135
provider: false,
137-
activeFlags: new Set(),
138136
registerActiveFlag: () => {
139137
// No-op default implementation
140138
},
@@ -187,7 +185,6 @@ export function ReflagProvider({
187185
}: ReflagProps) {
188186
const [featuresLoading, setFlagsLoading] = useState(true);
189187
const [rawFlags, setRawFlags] = useState<RawFlags>({});
190-
const [activeFlags, setActiveFlags] = useState<Set<string>>(new Set());
191188

192189
const clientRef = useRef<ReflagClient>();
193190
const contextKeyRef = useRef<string>();
@@ -236,22 +233,11 @@ export function ReflagProvider({
236233
}, [contextKey]);
237234

238235
const registerActiveFlag = useCallback((flagKey: string) => {
239-
setActiveFlags((prev) => {
240-
const newSet = new Set(prev).add(flagKey);
241-
// Sync with browser SDK client
242-
clientRef.current?.setActiveFlags(newSet);
243-
return newSet;
244-
});
236+
clientRef.current?.registerActiveFlag(flagKey);
245237
}, []);
246238

247239
const unregisterActiveFlag = useCallback((flagKey: string) => {
248-
setActiveFlags((prev) => {
249-
const newSet = new Set(prev);
250-
newSet.delete(flagKey);
251-
// Sync with browser SDK client
252-
clientRef.current?.setActiveFlags(newSet);
253-
return newSet;
254-
});
240+
clientRef.current?.unregisterActiveFlag(flagKey);
255241
}, []);
256242

257243
const context: ProviderContextType = {
@@ -261,7 +247,6 @@ export function ReflagProvider({
261247
},
262248
client: clientRef.current,
263249
provider: true,
264-
activeFlags,
265250
registerActiveFlag,
266251
unregisterActiveFlag,
267252
};

0 commit comments

Comments
 (0)