2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2020-02-12 21:30:58 +00:00
|
|
|
import { SocketStatus } from '../../types/SocketStatus';
|
|
|
|
import { trigger } from '../../shims/events';
|
2020-12-16 01:57:34 +00:00
|
|
|
import { assignWithNoUnnecessaryAllocation } from '../../util/assignWithNoUnnecessaryAllocation';
|
2024-03-13 20:44:13 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2020-02-12 21:30:58 +00:00
|
|
|
|
|
|
|
// State
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type NetworkStateType = ReadonlyDeep<{
|
2020-02-12 21:30:58 +00:00
|
|
|
isOnline: boolean;
|
2024-03-12 19:52:02 +00:00
|
|
|
isOutage: boolean;
|
2020-02-12 21:30:58 +00:00
|
|
|
socketStatus: SocketStatus;
|
2021-05-06 00:09:29 +00:00
|
|
|
challengeStatus: 'required' | 'pending' | 'idle';
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
const SET_NETWORK_STATUS = 'network/SET_NETWORK_STATUS';
|
2020-02-12 21:30:58 +00:00
|
|
|
const RELINK_DEVICE = 'network/RELINK_DEVICE';
|
2021-05-06 00:09:29 +00:00
|
|
|
const SET_CHALLENGE_STATUS = 'network/SET_CHALLENGE_STATUS';
|
2024-03-12 19:52:02 +00:00
|
|
|
const SET_OUTAGE = 'network/SET_OUTAGE';
|
2020-02-12 21:30:58 +00:00
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
export type SetNetworkStatusPayloadType = ReadonlyDeep<{
|
2020-02-12 21:30:58 +00:00
|
|
|
isOnline: boolean;
|
|
|
|
socketStatus: SocketStatus;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
type SetNetworkStatusAction = ReadonlyDeep<{
|
|
|
|
type: 'network/SET_NETWORK_STATUS';
|
|
|
|
payload: SetNetworkStatusPayloadType;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type RelinkDeviceActionType = ReadonlyDeep<{
|
2020-02-12 21:30:58 +00:00
|
|
|
type: 'network/RELINK_DEVICE';
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type SetChallengeStatusActionType = ReadonlyDeep<{
|
2021-05-06 00:09:29 +00:00
|
|
|
type: 'network/SET_CHALLENGE_STATUS';
|
|
|
|
payload: {
|
|
|
|
challengeStatus: NetworkStateType['challengeStatus'];
|
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2024-03-12 19:52:02 +00:00
|
|
|
type SetOutageActionType = ReadonlyDeep<{
|
|
|
|
type: 'network/SET_OUTAGE';
|
|
|
|
payload: {
|
|
|
|
isOutage: boolean;
|
|
|
|
};
|
|
|
|
}>;
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type NetworkActionType = ReadonlyDeep<
|
2024-03-18 21:48:00 +00:00
|
|
|
| SetNetworkStatusAction
|
2021-05-06 00:09:29 +00:00
|
|
|
| RelinkDeviceActionType
|
2023-01-13 20:07:26 +00:00
|
|
|
| SetChallengeStatusActionType
|
2024-03-12 19:52:02 +00:00
|
|
|
| SetOutageActionType
|
2023-01-13 20:07:26 +00:00
|
|
|
>;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
function setNetworkStatus(
|
|
|
|
payload: SetNetworkStatusPayloadType
|
|
|
|
): SetNetworkStatusAction {
|
2020-02-12 21:30:58 +00:00
|
|
|
return {
|
2024-03-18 21:48:00 +00:00
|
|
|
type: SET_NETWORK_STATUS,
|
2020-02-12 21:30:58 +00:00
|
|
|
payload,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function relinkDevice(): RelinkDeviceActionType {
|
|
|
|
trigger('setupAsNewDevice');
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: RELINK_DEVICE,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
function setChallengeStatus(
|
|
|
|
challengeStatus: NetworkStateType['challengeStatus']
|
|
|
|
): SetChallengeStatusActionType {
|
|
|
|
return {
|
|
|
|
type: SET_CHALLENGE_STATUS,
|
|
|
|
payload: { challengeStatus },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-12 19:52:02 +00:00
|
|
|
function setOutage(isOutage: boolean): SetOutageActionType {
|
|
|
|
return {
|
|
|
|
type: SET_OUTAGE,
|
|
|
|
payload: { isOutage },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
export const actions = {
|
2024-03-18 21:48:00 +00:00
|
|
|
setNetworkStatus,
|
2020-02-12 21:30:58 +00:00
|
|
|
relinkDevice,
|
2021-05-06 00:09:29 +00:00
|
|
|
setChallengeStatus,
|
2024-03-12 19:52:02 +00:00
|
|
|
setOutage,
|
2020-02-12 21:30:58 +00:00
|
|
|
};
|
|
|
|
|
2024-03-13 20:44:13 +00:00
|
|
|
export const useNetworkActions = (): BoundActionCreatorsMapObject<
|
|
|
|
typeof actions
|
|
|
|
> => useBoundActions(actions);
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
// Reducer
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
export function getEmptyState(): NetworkStateType {
|
2020-02-12 21:30:58 +00:00
|
|
|
return {
|
2024-03-18 21:48:00 +00:00
|
|
|
isOnline: true,
|
2024-03-12 19:52:02 +00:00
|
|
|
isOutage: false,
|
2021-06-09 22:28:54 +00:00
|
|
|
socketStatus: SocketStatus.OPEN,
|
2021-05-06 00:09:29 +00:00
|
|
|
challengeStatus: 'idle',
|
2020-02-12 21:30:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
2020-12-14 19:47:21 +00:00
|
|
|
state: Readonly<NetworkStateType> = getEmptyState(),
|
|
|
|
action: Readonly<NetworkActionType>
|
2020-02-12 21:30:58 +00:00
|
|
|
): NetworkStateType {
|
2024-03-18 21:48:00 +00:00
|
|
|
if (action.type === SET_NETWORK_STATUS) {
|
2020-02-12 21:30:58 +00:00
|
|
|
const { isOnline, socketStatus } = action.payload;
|
|
|
|
|
2020-12-16 01:57:34 +00:00
|
|
|
// This action is dispatched frequently. We avoid allocating a new object if nothing
|
|
|
|
// has changed to avoid an unnecessary re-render.
|
|
|
|
return assignWithNoUnnecessaryAllocation(state, {
|
2020-02-12 21:30:58 +00:00
|
|
|
isOnline,
|
|
|
|
socketStatus,
|
2020-12-16 01:57:34 +00:00
|
|
|
});
|
2020-02-12 21:30:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
if (action.type === SET_CHALLENGE_STATUS) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
challengeStatus: action.payload.challengeStatus,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-12 19:52:02 +00:00
|
|
|
if (action.type === SET_OUTAGE) {
|
|
|
|
const { isOutage } = action.payload;
|
|
|
|
|
|
|
|
// This action is dispatched frequently when offline.
|
|
|
|
// We avoid allocating a new object if nothing has changed to
|
|
|
|
// avoid an unnecessary re-render.
|
|
|
|
return assignWithNoUnnecessaryAllocation(state, {
|
|
|
|
isOutage,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
return state;
|
|
|
|
}
|