Skip to content

Commit 31a121c

Browse files
committed
Update DailyNetwork, useNetwork, and prebuilt to use new networkState and networkStateReasons
1 parent 4333d72 commit 31a121c

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/DailyNetwork.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ import React, { useCallback, useEffect } from 'react';
55

66
import { useDaily } from './hooks/useDaily';
77
import { useDailyEvent } from './hooks/useDailyEvent';
8+
import { arraysDeepEqual } from './lib/customDeepEqual';
89
import { jotaiDebugLabel } from './lib/jotai-custom';
910

1011
export const topologyState = atom<DailyNetworkTopology | 'none'>('none');
1112
topologyState.debugLabel = jotaiDebugLabel('topology');
13+
export const networkState = atom<DailyNetworkStats['networkState']>('unknown');
14+
export const networkStateReasons = atom<
15+
DailyNetworkStats['networkStateReasons']
16+
>([]);
17+
// @deprecated
1218
export const networkQualityState = atom<DailyNetworkStats['quality']>(100);
1319
networkQualityState.debugLabel = jotaiDebugLabel('network-quality');
20+
// @deprecated
1421
export const networkThresholdState =
1522
atom<DailyNetworkStats['threshold']>('good');
1623
networkThresholdState.debugLabel = jotaiDebugLabel('network-threshold');
@@ -52,6 +59,22 @@ export const DailyNetwork: React.FC<React.PropsWithChildren<{}>> = ({
5259
'network-quality-change',
5360
useAtomCallback(
5461
useCallback((_get, set, ev) => {
62+
set(
63+
networkState,
64+
(prevNetworkState: DailyNetworkStats['networkState']) =>
65+
prevNetworkState !== ev.networkState
66+
? ev.networkState
67+
: prevNetworkState
68+
);
69+
set(
70+
networkStateReasons,
71+
(prevReasons: DailyNetworkStats['networkStateReasons']) => {
72+
const curReasons = ev.networkStateReasons ?? [];
73+
return !arraysDeepEqual(prevReasons, curReasons)
74+
? curReasons
75+
: prevReasons;
76+
}
77+
);
5578
set(networkQualityState, (prevQuality: DailyNetworkStats['quality']) =>
5679
prevQuality !== ev.quality ? ev.quality : prevQuality
5780
);
@@ -69,6 +92,8 @@ export const DailyNetwork: React.FC<React.PropsWithChildren<{}>> = ({
6992
useAtomCallback(
7093
useCallback((_get, set) => {
7194
set(topologyState, 'none');
95+
set(networkState, 'unknown');
96+
set(networkStateReasons, []);
7297
set(networkQualityState, 100);
7398
set(networkThresholdState, 'good');
7499
}, [])

src/hooks/useNetwork.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { useCallback, useDebugValue } from 'react';
44

55
import {
66
networkQualityState,
7+
networkState as netState,
8+
networkStateReasons as netReasons,
79
networkThresholdState,
810
topologyState,
911
} from '../DailyNetwork';
@@ -26,6 +28,8 @@ export const useNetwork = ({
2628
const daily = useDaily();
2729

2830
const topology = useAtomValue(topologyState);
31+
const networkState = useAtomValue(netState);
32+
const networkStateReasons = useAtomValue(netReasons);
2933
const quality = useAtomValue(networkQualityState);
3034
const threshold = useAtomValue(networkThresholdState);
3135

@@ -55,6 +59,8 @@ export const useNetwork = ({
5559

5660
const result = {
5761
getStats,
62+
networkState,
63+
networkStateReasons,
5864
quality,
5965
threshold,
6066
topology,

test/hooks/useNetwork.test.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,29 @@ const createWrapper =
5454
<DailyProvider callObject={callObject}>{children}</DailyProvider>;
5555

5656
describe('useNetwork', () => {
57-
it('returns getStats method, quality, threshold & topology with perfect defaults', () => {
57+
it('returns getStats method with perfect defaults', () => {
5858
const { result } = renderHook(() => useNetwork(), {
5959
wrapper: createWrapper(),
6060
});
6161
expect(result.current).toHaveProperty('getStats');
6262
expect(typeof result.current.getStats).toBe('function');
6363
expect(result.current).toHaveProperty('quality');
6464
expect(result.current.quality).toBe(100);
65+
expect(result.current).toHaveProperty('networkState');
66+
expect(result.current.networkState).toBe('unknown');
67+
expect(result.current).toHaveProperty('networkStateReasons');
68+
expect(result.current.networkStateReasons).toStrictEqual([]);
6569
expect(result.current).toHaveProperty('threshold');
6670
expect(result.current.threshold).toBe('good');
6771
expect(result.current).toHaveProperty('topology');
6872
expect(result.current.topology).toBe('none');
6973
});
7074
it('getStats calls getNetworkStats internally', async () => {
7175
const mockStats: DailyNetworkStats = {
76+
networkState: 'good',
77+
networkStateReasons: [],
7278
quality: 99,
79+
threshold: 'good',
7380
stats: {
7481
latest: {
7582
recvBitsPerSecond: 1000000,
@@ -102,7 +109,6 @@ describe('useNetwork', () => {
102109
worstAudioSendJitter: 0,
103110
averageNetworkRoundTripTime: 0.07171428571428572,
104111
},
105-
threshold: 'good',
106112
};
107113
const daily = Daily.createCallObject();
108114
(
@@ -184,14 +190,19 @@ describe('useNetwork', () => {
184190
const event: DailyEvent = 'network-quality-change';
185191
const payload: DailyEventObjectNetworkQualityEvent = mockEvent({
186192
action: 'network-quality-change',
193+
networkState: 'bad',
194+
networkStateReasons: ['sendPacketLoss'],
187195
quality: 80,
188196
threshold: 'low',
197+
stats: {},
189198
});
190199
act(() => {
191200
// @ts-ignore
192201
daily.emit(event, payload);
193202
});
194203
await waitFor(() => {
204+
expect(result.current.networkState).toBe('bad');
205+
expect(result.current.networkStateReasons).toEqual(['sendPacketLoss']);
195206
expect(result.current.quality).toBe(80);
196207
expect(result.current.threshold).toBe('low');
197208
expect(onNetworkQualityChange).toHaveBeenCalledWith(payload);

0 commit comments

Comments
 (0)