-
Notifications
You must be signed in to change notification settings - Fork 4
feat: allow context to be passed as null #29
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
base: chore_update_some_packages
Are you sure you want to change the base?
Changes from all commits
3ef0c45
115e1d1
1c66c59
cb87e0d
2b397db
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import { useState, type FC, type ReactNode } from "react"; | ||
|
|
||
| import absmartly from "@absmartly/javascript-sdk"; | ||
| import absmartly, { Context, SDK } from "@absmartly/javascript-sdk"; | ||
|
|
||
| import { _SdkContext } from "../../hooks/useABSmartly"; | ||
| import type { | ||
|
|
@@ -25,31 +25,51 @@ type SDKProviderWithContext = { | |
| contextOptions?: never; | ||
| }; | ||
|
|
||
| type SDKProviderProps = SDKProviderNoContext | SDKProviderWithContext; | ||
| type SDKProviderNullContext = { | ||
| context: null; | ||
| children?: ReactNode; | ||
| sdkOptions: SDKOptionsType; | ||
| contextOptions?: never; | ||
| }; | ||
|
|
||
| type SDKProviderProps = | ||
| | SDKProviderNoContext | ||
| | SDKProviderWithContext | ||
| | SDKProviderNullContext; | ||
|
|
||
| export const SDKProvider: FC<SDKProviderProps> = ({ | ||
| sdkOptions, | ||
| contextOptions, | ||
| context, | ||
| children, | ||
| }) => { | ||
| const sdk = context | ||
| ? context["_sdk"] | ||
| : new absmartly.SDK({ retries: 5, timeout: 3000, ...sdkOptions }); | ||
| const sdk: SDK = | ||
| context == null | ||
| ? new absmartly.SDK({ retries: 5, timeout: 3000, ...sdkOptions }) | ||
| : context["_sdk"]; | ||
|
|
||
| const [providedContext, setProvidedContext] = useState( | ||
| context ? context : sdk.createContext(contextOptions), | ||
| const [providedContext, setProvidedContext] = useState<Context | null>( | ||
| context === null | ||
| ? null | ||
| : context === undefined | ||
| ? sdk.createContext(contextOptions) | ||
| : context, | ||
| ); | ||
|
|
||
| const resetContext = async ( | ||
| params: ContextRequestType, | ||
| contextOptions?: ContextOptionsType, | ||
| ) => { | ||
| try { | ||
| if (providedContext == null) { | ||
|
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. [Suggestion]: I think this implies the only method of replacing the I think a better approach would be to have an useEffect hook listening to possible changes in the context, so that when the context passed to the provider is no longer export const SDKProvider: FC<SDKProviderProps> = ({
sdkOptions,
contextOptions,
context,
children,
}) => {
let contextInitialValue = context;
let sdkInitialValue = context ? context['_sdk'] : null;
if (context === undefined) {
sdkInitialValue = new absmartly.SDK({ retries: 5, timeout: 3000, ...sdkOptions });
contextInitialValue = sdk.createContext(contextOptions);
}
const [context, setContext] = useState<Context | null>(contextInitialValue);
const [sdk, setSdk] = useState<SDK | null>(sdkInitialValue);
useEffect(() => {
if (!!context) {
setContext(context);
setSdk(context['_sdk']);
};
}, [context]);
};You could probably improve performance a bit by memoizing values and such, but this is the gist of it :) |
||
| setProvidedContext(sdk.createContext(params, contextOptions)); | ||
| return; | ||
| } | ||
|
|
||
| await providedContext.ready(); | ||
|
|
||
| const contextData = providedContext.data(); | ||
| const oldContextOptions = providedContext._opts; | ||
| const oldContextOptions = providedContext["_opts"]; | ||
|
|
||
| const combinedContextOptions = { | ||
| ...oldContextOptions, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Question]: Wouldn't it be better to also keep the SDK
nulluntil a non-nullcontextis passed (or requested viaresetContext)?