Skip to content
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

Enabled #63

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const { isOk } = useKillswitch({
androidApiKey: androidApiKey,
language: myAppLanguage,
version: myAppVersion,
enabled: true,
});
```

Expand Down Expand Up @@ -63,6 +64,9 @@ const { isOk } = useKillswitch({
- `timeout`
A number of milliseconds to wait for the back-end before returning `isOk = true`. Defaults to `2000`

- `enabled`
Toggle if you want to disabled. Defaults to `true`

## License

react-native-killswitch is © 2023 [Mirego](https://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](./LICENSE.md) file.
Expand Down
13 changes: 12 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 @@ -65,6 +66,16 @@ describe('useKillswitch()', () => {

await waitFor(() => screen.getByText('is ok'));
}, 8000);

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

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

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

await waitFor(() => screen.getByText('is ok'));
}, 8000);
});

describe('when it receives an "alert" signal', () => {
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() };
}