-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add js docs for context mutator hook (#1045)
Minor refactors, but mostly adding js-doc after #1031. --------- Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
- Loading branch information
Showing
13 changed files
with
69 additions
and
54 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
packages/react/src/provider/context.ts → packages/react/src/common/context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './use-context-mutator'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
||
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>; | ||
}; | ||
|
||
/** | ||
* Get function(s) for mutating the evaluation context associated with this domain, or the default context if `defaultContext: true`. | ||
* 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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters