Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions packages/react/src/context/use-context-mutator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useCallback, useContext } from 'react';
import { useCallback, useContext, useEffect, useState } from 'react';
import type { EvaluationContext } from '@openfeature/web-sdk';
import { OpenFeature } from '@openfeature/web-sdk';
import { Context } from '../internal';

export type ContextMutationOptions = {
/**
* Mutate the default context instead of the domain scoped context applied at the `<OpenFeatureProvider/>`.
* Note, if the `<OpenFeatureProvider/>` has no domain specified, the default is used.
* By default, will use the domain set on `<OpenFeatureProvider/>` (or the domain associated with the client set on `<OpenFeatureProvider/>`).
* See the {@link https://openfeature.dev/docs/reference/technologies/client/web/#manage-evaluation-context-for-domains|documentation} for more information.
* @default false
*/
Expand All @@ -33,7 +33,28 @@ export type ContextMutation = {
* @returns {ContextMutation} context-aware function(s) to mutate evaluation context
*/
export function useContextMutator(options: ContextMutationOptions = { defaultContext: false }): ContextMutation {
const { domain } = useContext(Context) || {};
const { client } = useContext(Context) || {};
const domain = client?.metadata.domain;

// TODO: Replace this warning with a thrown error in a future major release,
// to match the behavior of `useOpenFeatureProvider` + `useOpenFeatureClient`,
// when `defaultContext` isn't explicitly set to true.
const [warned, setWarned] = useState(false);
useEffect(() => {
if (options.defaultContext || domain) {
if (warned) {
setWarned(false);
}
return;
}

if (!warned) {
console.warn(
'[useContextMutator] No domain available from OpenFeature context; are you using <OpenFeatureProvider/>? setContext will mutate the default context, as if `defaultContext: true` were set. This may result in a thrown error in the future.',
);
setWarned(true);
}
}, [warned]);

const setContext = useCallback(
async (
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/internal/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { normalizeOptions } from '.';
* **DO NOT EXPORT PUBLICLY**
* @internal
*/
export const Context = React.createContext<
{ client: Client; domain?: string; options: ReactFlagEvaluationOptions } | undefined
>(undefined);
export const Context = React.createContext<{ client: Client; options: ReactFlagEvaluationOptions } | undefined>(
undefined,
);

/**
* Get a normalized copy of the options used for this OpenFeatureProvider, see {@link normalizeOptions}.
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/provider/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ type ProviderProps = {
export function OpenFeatureProvider({ client, domain, children, ...options }: ProviderProps) {
const stableClient = React.useMemo(() => client || OpenFeature.getClient(domain), [client, domain]);

return <Context.Provider value={{ client: stableClient, options, domain }}>{children}</Context.Provider>;
return <Context.Provider value={{ client: stableClient, options }}>{children}</Context.Provider>;
}
2 changes: 1 addition & 1 deletion packages/react/src/provider/use-open-feature-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export function useOpenFeatureProvider(): Provider {
throw new MissingContextError('No OpenFeature context available');
}

return OpenFeature.getProvider(openFeatureContext.domain);
return OpenFeature.getProvider(openFeatureContext.client.metadata.domain);
}
41 changes: 41 additions & 0 deletions packages/react/test/provider.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ describe('OpenFeatureProvider', () => {
); // delay init by 100ms
};

beforeEach(async () => {
await OpenFeature.clearContexts();
});

describe('useOpenFeatureClient', () => {
const DOMAIN = 'useOpenFeatureClient';

Expand Down Expand Up @@ -230,6 +234,43 @@ describe('OpenFeatureProvider', () => {
expect(screen.getByText('Will says aloha')).toBeInTheDocument();
});

it('should update context when a client is set', async () => {
const DOMAIN = 'mutate-context-client';
OpenFeature.setProvider(DOMAIN, suspendingProvider(), {});

const client = OpenFeature.getClient(DOMAIN);

const changed = jest.fn();
client.addHandler(ProviderEvents.ContextChanged, changed);

render(
<OpenFeatureProvider client={client}>
<React.Suspense fallback={<div>{FALLBACK}</div>}>
<TestComponent name="Will" />
</React.Suspense>
</OpenFeatureProvider>,
);

await waitFor(() => {
expect(screen.getByText('Will says hi')).toBeInTheDocument();
});

act(() => {
fireEvent.click(screen.getByText('Update Context'));
});
expect(screen.getByText('Updating context...')).toBeInTheDocument();

await waitFor(
() => {
expect(screen.getByText('Update Context')).toBeInTheDocument();
},
{ timeout: DELAY * 2 },
);
expect(changed).toHaveBeenCalledTimes(1);

expect(screen.getByText('Will says aloha')).toBeInTheDocument();
});

it('should update nested contexts', async () => {
const DOMAIN1 = 'Wills Domain';
const DOMAIN2 = 'Todds Domain';
Expand Down