-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseActivities.ts
More file actions
66 lines (54 loc) · 2.14 KB
/
useActivities.ts
File metadata and controls
66 lines (54 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { useOrderedActivities } from '@msinternal/botframework-webchat-api-graph';
import type { WebChatActivity } from 'botframework-webchat-core';
import { useSelector, useStore } from './internal/WebChatReduxContext';
import usePrevious from './internal/usePrevious';
declare const process: {
env: {
NODE_ENV?: string | undefined;
};
};
function useActivitiesForProduction(): readonly [readonly WebChatActivity[]] {
return useOrderedActivities();
}
function useActivitiesForDevelopment(): readonly [readonly WebChatActivity[]] {
const activitiesFromGraphState = useActivitiesForProduction();
// Checks if store changed.
const store = useStore();
const prevStore = usePrevious(store);
const [activitiesFromGraph] = activitiesFromGraphState;
const activitiesFromRedux = useSelector(({ activities }) => activities);
// If store changed, skip one assertion turn.
// This is because <GraphProvider> is using `useState()` for propagating changes.
// It is always one render behind if `store` changed.
if (prevStore === store) {
if (activitiesFromGraph.length !== activitiesFromRedux.length) {
throw new Error(
`botframework-webchat-internal: Activities from graph and Redux are of different size (graph has ${activitiesFromGraph.length} activities, Redux has ${activitiesFromRedux.length} activities)`,
{
cause: {
activitiesFromGraph,
activitiesFromRedux
}
}
);
}
for (let index = 0; index < activitiesFromGraph.length; index++) {
if (!Object.is(activitiesFromGraph.at(index), activitiesFromRedux.at(index))) {
throw new Error(
`botframework-webchat-internal: Activities from graph and Redux are of different at index ${index}`,
{
cause: {
activitiesFromGraph,
activitiesFromRedux,
index
}
}
);
}
}
}
return activitiesFromGraphState;
}
const useActivities: () => readonly [readonly WebChatActivity[]] =
process.env.NODE_ENV === 'production' ? useActivitiesForProduction : useActivitiesForDevelopment;
export default useActivities;