Skip to content

Commit 70f09e4

Browse files
committed
commets, reuse domain from context value
Signed-off-by: Will Chou <[email protected]>
1 parent fedcaf3 commit 70f09e4

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

packages/react/src/provider/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { NormalizedOptions, ReactFlagEvaluationOptions, normalizeOptions } from
77
* DO NOT EXPORT PUBLICLY
88
* @internal
99
*/
10-
export const Context = React.createContext<{ client: Client; options: ReactFlagEvaluationOptions } | undefined>(undefined);
10+
export const Context = React.createContext<{ client: Client; domain?: string; options: ReactFlagEvaluationOptions } | undefined>(undefined);
1111

1212
/**
1313
* Get a normalized copy of the options used for this OpenFeatureProvider, see {@link normalizeOptions}.
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1+
import { useContext } from 'react';
12
import { OpenFeature, EvaluationContext } from '@openfeature/web-sdk';
2-
3-
type DomainContextMutator = (domain: string, updatedContext: EvaluationContext) => Promise<void>;
3+
import { Context } from './context';
44

55
/**
6-
*
6+
*
77
* A hook for accessing context mutating functions.
8-
*
8+
*
99
*/
1010
export function useContextMutator() {
11+
async function mutateContext(updatedContext: EvaluationContext): Promise<void> {
12+
const { domain } = useContext(Context) || {};
1113

12-
async function mutateContext(domainOrUpdatedContext: string | EvaluationContext, updatedContextOrUndefined?: EvaluationContext): Promise<void> {
13-
if (typeof domainOrUpdatedContext === 'string' && updatedContextOrUndefined) {
14-
await OpenFeature.setContext(domainOrUpdatedContext, updatedContextOrUndefined);
15-
} else if (typeof domainOrUpdatedContext !== 'string') {
16-
OpenFeature.setContext(domainOrUpdatedContext);
14+
if (!domain) {
15+
throw new Error('No domain set for your context');
1716
}
17+
18+
OpenFeature.setContext(domain, updatedContext);
1819
}
19-
20+
2021
return {
21-
mutateContext,
22+
mutateContext,
2223
};
23-
}
24+
}

0 commit comments

Comments
 (0)