Skip to content

Commit

Permalink
add enabled field
Browse files Browse the repository at this point in the history
  • Loading branch information
David authored and David committed Jul 25, 2024
1 parent 1e77b28 commit 7da5e93
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
15 changes: 14 additions & 1 deletion src/__tests__/use-killswitch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import { useKillswitch } from '../use-killswitch';
import fetchMock from 'jest-fetch-mock';
import spyOnAlert from '../__utils__/spy-on-alert';

function App() {
function App({ enabled = true }: { enabled?: boolean }) {
const { isOk } = useKillswitch({
iosApiKey: 'apiKey',
androidApiKey: 'apiKey',
language: 'en',
version: '1.0.0',
apiHost: 'https://killswitch.mirego.com',
timeout: 200,
enabled,
});

return (
Expand Down Expand Up @@ -53,6 +54,18 @@ describe('useKillswitch()', () => {
appStateSpy.mockRestore();
});

it('should display "is ok" when enabled is false', async () => {
fetchMock.mockResponseOnce(JSON.stringify({ isOk: true }));

const { rerender, getByTestId } = render(<App />);

rerender(<App enabled={false} />);

await waitFor(() => {
expect(getByTestId('killswitch-text').props.children).toBe('is ok');
});
});

describe('when it receives an "ok" signal', () => {
beforeEach(() => {
fetchMock.mockResponse((_request) => {
Expand Down
12 changes: 10 additions & 2 deletions src/killswitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface KillswitchOptions {
androidApiKey: string;
useNativeUI?: boolean;
timeout?: number;
enabled?: boolean;
}

class Killswitch {
Expand All @@ -50,6 +51,7 @@ class Killswitch {
androidApiKey: string;
useNativeUI: boolean;
timeout: number;
enabled: boolean;

behavior?: z.infer<typeof KillswitchBehavior>;

Expand All @@ -59,12 +61,14 @@ class Killswitch {
androidApiKey,
useNativeUI = true,
timeout = 2000,
enabled = true,
}: KillswitchOptions) {
this.apiHost = apiHost;
this.iosApiKey = iosApiKey;
this.androidApiKey = androidApiKey;
this.useNativeUI = useNativeUI;
this.timeout = timeout;
this.enabled = enabled;
}

get isOk() {
Expand All @@ -79,7 +83,8 @@ class Killswitch {
return this.behavior?.action === KillswitchBehaviorAction.KILL;
}

async check(language: string, version: string) {
async check(language: string, version: string, enabled: boolean) {
if (!enabled) return { isOk: true };
try {
const payload = await this.fetch(language, version);

Expand Down Expand Up @@ -120,7 +125,6 @@ class Killswitch {
signal,
}
);

return response.json();
} finally {
clearTimeout(timeout);
Expand All @@ -134,6 +138,10 @@ class Killswitch {
return;
}

if (!this.enabled) {
return;
}

if (this.isAlert || this.isKill) {
return Alert.alert('', this.behavior.message, this.buildAlertButtons(), {
cancelable: false,
Expand Down
16 changes: 12 additions & 4 deletions src/use-killswitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface UseKillswitchOptions {
apiHost: string;
useNativeUI?: boolean;
timeout?: number;
enabled?: boolean;
}

export function useKillswitch({
Expand All @@ -20,6 +21,7 @@ export function useKillswitch({
version,
apiHost,
useNativeUI = true,
enabled = true,
timeout = 2000,
}: UseKillswitchOptions) {
const killswitchRef = useRef<Killswitch | null>(null);
Expand All @@ -28,35 +30,41 @@ export function useKillswitch({
const [isOk, setIsOk] = useState<boolean | null>(null);

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

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

killswitchRef.current = killswitch;

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

useEffect(() => {
async function run() {
if (previousAppState !== 'active' && appState === 'active') {
const { isOk: newIsOk } = await getKillswitch().check(
language,
version
version,
enabled
);

setIsOk(newIsOk);
}
}

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

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

0 comments on commit 7da5e93

Please sign in to comment.