Skip to content

Commit

Permalink
Avoid recreating ref content (#1)
Browse files Browse the repository at this point in the history
This removes the default value for apiHost but most importantly avoids
recreating a killswitch when the hook is rerun
  • Loading branch information
charlesdemers authored Mar 27, 2023
1 parent 61548e0 commit 45d3351
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const { isOk } = useKillswitch({
the correct behavior to your users.

- `apiHost`
The host of the killswitch back-end. Defaults to: `https://killswitch.mirego.com`
The host of the killswitch back-end.

- `useNativeUI`
Use native alerts to display messages. Defaults to `true`
Expand Down
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function App() {
'a351227e97198cb4d71e6433ef7afcc185b49b8c364874b5129aacd08b576f59',
language: 'fr',
version: '0.10.0',
apiHost: 'https://killswitch.mirego.com',
});

return (
Expand Down
34 changes: 23 additions & 11 deletions src/use-killswitch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import usePrevious from './hooks/use-previous';
import { useAppState } from './hooks/use-app-state';
import Killswitch from './killswitch';
Expand All @@ -8,7 +8,7 @@ interface UseKillswitchOptions {
androidApiKey: string;
language: string;
version: string;
apiHost?: string;
apiHost: string;
useNativeUI?: boolean;
timeout?: number;
}
Expand All @@ -18,23 +18,35 @@ export function useKillswitch({
androidApiKey,
language,
version,
apiHost = 'https://killswitch.mirego.com',
apiHost,
useNativeUI = true,
timeout = 2000,
}: UseKillswitchOptions) {
const killswitch = useRef(
new Killswitch({ iosApiKey, androidApiKey, apiHost, useNativeUI, timeout })
);

const killswitchRef = useRef<Killswitch | null>(null);
const appState = useAppState();
const previousAppState = usePrevious(appState);

const [isOk, setIsOk] = useState<boolean | null>(null);

const getKillswitch = useCallback(() => {
if (killswitchRef.current !== null) return killswitchRef.current;

const killswitch = new Killswitch({
iosApiKey,
androidApiKey,
apiHost,
useNativeUI,
timeout,
});

killswitchRef.current = killswitch;

return killswitch;
}, [androidApiKey, apiHost, iosApiKey, timeout, useNativeUI]);

useEffect(() => {
async function run() {
if (previousAppState !== 'active' && appState === 'active') {
const { isOk: newIsOk } = await killswitch.current.check(
const { isOk: newIsOk } = await getKillswitch().check(
language,
version
);
Expand All @@ -44,7 +56,7 @@ export function useKillswitch({
}

run();
}, [appState, language, previousAppState, version]);
}, [appState, getKillswitch, language, previousAppState, version]);

return { isOk, killswitch: killswitch.current };
return { isOk, killswitch: getKillswitch() };
}

0 comments on commit 45d3351

Please sign in to comment.