diff --git a/README.md b/README.md
index ed09f39..1bde319 100644
--- a/README.md
+++ b/README.md
@@ -33,6 +33,7 @@ const { isOk } = useKillswitch({
androidApiKey: androidApiKey,
language: myAppLanguage,
version: myAppVersion,
+ enabled: true,
});
```
@@ -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.
diff --git a/src/__tests__/use-killswitch.test.tsx b/src/__tests__/use-killswitch.test.tsx
index b85c771..6786896 100644
--- a/src/__tests__/use-killswitch.test.tsx
+++ b/src/__tests__/use-killswitch.test.tsx
@@ -12,7 +12,7 @@ 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',
@@ -20,6 +20,7 @@ function App() {
version: '1.0.0',
apiHost: 'https://killswitch.mirego.com',
timeout: 200,
+ enabled,
});
return (
@@ -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();
+
+ rerender();
+
+ await waitFor(() => screen.getByText('is ok'));
+ }, 8000);
});
describe('when it receives an "alert" signal', () => {
diff --git a/src/killswitch.ts b/src/killswitch.ts
index 5d870c0..db2ef65 100644
--- a/src/killswitch.ts
+++ b/src/killswitch.ts
@@ -42,6 +42,7 @@ interface KillswitchOptions {
androidApiKey: string;
useNativeUI?: boolean;
timeout?: number;
+ enabled?: boolean;
}
class Killswitch {
@@ -50,6 +51,7 @@ class Killswitch {
androidApiKey: string;
useNativeUI: boolean;
timeout: number;
+ enabled: boolean;
behavior?: z.infer;
@@ -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() {
@@ -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);
@@ -120,7 +125,6 @@ class Killswitch {
signal,
}
);
-
return response.json();
} finally {
clearTimeout(timeout);
@@ -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,
diff --git a/src/use-killswitch.ts b/src/use-killswitch.ts
index 63b972e..779c9f1 100644
--- a/src/use-killswitch.ts
+++ b/src/use-killswitch.ts
@@ -11,6 +11,7 @@ interface UseKillswitchOptions {
apiHost: string;
useNativeUI?: boolean;
timeout?: number;
+ enabled?: boolean;
}
export function useKillswitch({
@@ -20,6 +21,7 @@ export function useKillswitch({
version,
apiHost,
useNativeUI = true,
+ enabled = true,
timeout = 2000,
}: UseKillswitchOptions) {
const killswitchRef = useRef(null);
@@ -28,7 +30,11 @@ export function useKillswitch({
const [isOk, setIsOk] = useState(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,
@@ -36,19 +42,21 @@ export function useKillswitch({
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);
@@ -56,7 +64,7 @@ export function useKillswitch({
}
run();
- }, [appState, getKillswitch, language, previousAppState, version]);
+ }, [appState, getKillswitch, language, previousAppState, version, enabled]);
return { isOk, killswitch: getKillswitch() };
}