-
Notifications
You must be signed in to change notification settings - Fork 49
chore: add js docs for context mutator hook #1045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export * from './context'; | ||
| export * from './is-equal'; | ||
| export * from './options'; | ||
| export * from './suspense'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './use-context-mutator'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { useCallback, useContext, useRef } from 'react'; | ||
| import type { EvaluationContext } from '@openfeature/web-sdk'; | ||
| import { OpenFeature } from '@openfeature/web-sdk'; | ||
| import { Context } from '../common'; | ||
|
|
||
| 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. | ||
| * See the {@link https://openfeature.dev/docs/reference/technologies/client/web/#manage-evaluation-context-for-domains|documentation} for more information. | ||
| * @default false | ||
| */ | ||
| defaultContext?: boolean; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wichopy @lukas-reining I've changed this from 1 - I eventually want to return 2 functions from this hook, 1 for |
||
| }; | ||
|
|
||
| export type ContextMutation = { | ||
| /** | ||
| * A function to set the desired context (see: {@link ContextMutationOptions} for details). | ||
| * There's generally no need to await the result of this function; flag evaluation hooks will re-render when the context is updated. | ||
| * This promise never rejects. | ||
| * @param updatedContext | ||
| * @returns Promise for awaiting the context update | ||
| */ | ||
| setContext: (updatedContext: EvaluationContext) => Promise<void>; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wichopy @lukas-reining one thing perhaps worth talking about before we release is that we could make this return IMO we should keep the promise exposed, but I wanted to point it out. |
||
| }; | ||
|
|
||
| /** | ||
| * Get function(s) for mutating the evaluation context associated with this domain, or the default context if `global: true`. | ||
toddbaert marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * See the {@link https://openfeature.dev/docs/reference/technologies/client/web/#targeting-and-context|documentation} for more information. | ||
| * @param {ContextMutationOptions} options options for the generated function | ||
| * @returns {ContextMutation} function(s) to mutate context | ||
| */ | ||
| export function useContextMutator(options: ContextMutationOptions = { defaultContext: false }): ContextMutation { | ||
| const { domain } = useContext(Context) || {}; | ||
| const previousContext = useRef<null | EvaluationContext>(null); | ||
|
|
||
| const setContext = useCallback(async (updatedContext: EvaluationContext) => { | ||
| if (previousContext.current !== updatedContext) { | ||
| if (!domain || options?.defaultContext) { | ||
| OpenFeature.setContext(updatedContext); | ||
| } else { | ||
| OpenFeature.setContext(domain, updatedContext); | ||
| } | ||
| previousContext.current = updatedContext; | ||
| } | ||
| }, [domain]); | ||
|
|
||
| return { | ||
| setContext, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export * from './evaluation'; | ||
| export * from './query'; | ||
| export * from './provider'; | ||
| export * from './context'; | ||
| // re-export the web-sdk so consumers can access that API from the react-sdk | ||
| export * from '@openfeature/web-sdk'; |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.