Skip to content

Commit 7ce8d09

Browse files
compulimCopilot
andauthored
Clean up graph code (#5657)
* Remove NODE_ENV branching * Rename from Graph2 to Graph * Update packages/api/src/hooks/useActivities.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a394eb2 commit 7ce8d09

File tree

14 files changed

+49
-45
lines changed

14 files changed

+49
-45
lines changed

packages/api/src/hooks/useActivities.ts

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,57 @@ declare const process: {
1010
};
1111
};
1212

13-
export default function useActivities(): readonly [readonly WebChatActivity[]] {
14-
const activitiesFromGraphState = useOrderedActivities();
13+
function useActivitiesForProduction(): readonly [readonly WebChatActivity[]] {
14+
return useOrderedActivities();
15+
}
16+
17+
function useActivitiesForDevelopment(): readonly [readonly WebChatActivity[]] {
18+
const activitiesFromGraphState = useActivitiesForProduction();
1519

1620
// Checks if store changed.
1721
const store = useStore();
1822
const prevStore = usePrevious(store);
1923

20-
// ASSERTION: Before we fully migrate to graph, make sure graph and Redux are the same.
21-
if (process.env.NODE_ENV !== 'production') {
22-
const [activitiesFromGraph] = activitiesFromGraphState;
24+
const [activitiesFromGraph] = activitiesFromGraphState;
25+
26+
const activitiesFromRedux = useSelector(({ activities }) => activities);
2327

24-
// Assert based on NODE_ENV.
25-
// eslint-disable-next-line react-hooks/rules-of-hooks
26-
const activitiesFromRedux = useSelector(({ activities }) => activities);
28+
// If store changed, skip one assertion turn.
29+
// This is because <GraphProvider> is using `useState()` for propagating changes.
30+
// It is always one render behind if `store` changed.
31+
if (prevStore === store) {
32+
if (activitiesFromGraph.length !== activitiesFromRedux.length) {
33+
throw new Error(
34+
`botframework-webchat-internal: Activities from graph and Redux are of different size (graph has ${activitiesFromGraph.length} activities, Redux has ${activitiesFromRedux.length} activities)`,
35+
{
36+
cause: {
37+
activitiesFromGraph,
38+
activitiesFromRedux
39+
}
40+
}
41+
);
42+
}
2743

28-
// If store changed, skip one assertion turn.
29-
// This is because <GraphProvider> is using `useState()` for propagating changes.
30-
// It is always one render behind if `store` changed.
31-
if (prevStore === store) {
32-
if (activitiesFromGraph.length !== activitiesFromRedux.length) {
44+
for (let index = 0; index < activitiesFromGraph.length; index++) {
45+
if (!Object.is(activitiesFromGraph.at(index), activitiesFromRedux.at(index))) {
3346
throw new Error(
34-
`botframework-webchat-internal: Activities from graph and Redux are of different size (graph has ${activitiesFromGraph.length} activities, Redux has ${activitiesFromRedux.length} activities)`,
47+
`botframework-webchat-internal: Activities from graph and Redux are different at index ${index}`,
3548
{
3649
cause: {
3750
activitiesFromGraph,
38-
activitiesFromRedux
51+
activitiesFromRedux,
52+
index
3953
}
4054
}
4155
);
4256
}
43-
44-
for (let index = 0; index < activitiesFromGraph.length; index++) {
45-
if (!Object.is(activitiesFromGraph.at(index), activitiesFromRedux.at(index))) {
46-
throw new Error(
47-
`botframework-webchat-internal: Activities from graph and Redux are of different at index ${index}`,
48-
{
49-
cause: {
50-
activitiesFromGraph,
51-
activitiesFromRedux,
52-
index
53-
}
54-
}
55-
);
56-
}
57-
}
5857
}
5958
}
6059

6160
return activitiesFromGraphState;
6261
}
62+
63+
const useActivities: () => readonly [readonly WebChatActivity[]] =
64+
process.env.NODE_ENV === 'production' ? useActivitiesForProduction : useActivitiesForDevelopment;
65+
66+
export default useActivities;

packages/core-graph/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export {
66
type GraphSubscriberRecord,
77
type ReadableGraph,
88
type WritableGraph
9-
} from './private/Graph2';
9+
} from './private/Graph';
1010
export { SlantNodeSchema, type SlantNode } from './private/schemas/colorNode';
1111
export {
1212
DirectLineActivityNodeSchema,

packages/core-graph/src/private/Graph2.act.spec.ts renamed to packages/core-graph/src/private/Graph.act.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from '@jest/globals';
22
import { scenario } from '@testduet/given-when-then';
33
import { assert, object } from 'valibot';
4-
import Graph from './Graph2';
4+
import Graph from './Graph';
55
import './schemas/private/expectExtendValibot';
66
import './schemas/private/expectIsFrozen';
77

packages/core-graph/src/private/Graph2.getState.spec.ts renamed to packages/core-graph/src/private/Graph.getState.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from '@jest/globals';
22
import { scenario } from '@testduet/given-when-then';
3-
import Graph from './Graph2';
3+
import Graph from './Graph';
44
import './schemas/private/expectExtendValibot';
55
import './schemas/private/expectIsFrozen';
66

packages/core-graph/src/private/Graph2.middleware.spec.ts renamed to packages/core-graph/src/private/Graph.middleware.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from '@jest/globals';
22
import { scenario } from '@testduet/given-when-then';
33
import { iteratorFilter, iteratorMap } from 'iter-fest';
44
import { fn } from 'jest-mock';
5-
import Graph from './Graph2';
5+
import Graph from './Graph';
66
import type { Identifier } from './schemas/Identifier';
77
import './schemas/private/expectExtendValibot';
88
import './schemas/private/expectIsFrozen';

packages/core-graph/src/private/Graph2.subscribe.spec.ts renamed to packages/core-graph/src/private/Graph.subscribe.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from '@jest/globals';
22
import { scenario } from '@testduet/given-when-then';
33
import { fn } from 'jest-mock';
44
import { assert, map, string, unknown } from 'valibot';
5-
import Graph, { type GraphState } from './Graph2';
5+
import Graph, { type GraphState } from './Graph';
66
import './schemas/private/expectExtendValibot';
77
import './schemas/private/expectIsFrozen';
88

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type WritableGraph<TInput extends GraphNode, TOutput extends GraphNode> = {
3333
const requestSchema = pipe(
3434
map(IdentifierSchema, object({ '@id': IdentifierSchema })),
3535
check(
36-
// TODO: Iterator.every is since iOS 18.4, we still need to use ponyfill for now.
36+
// TODO: [P4] Iterator.every is since iOS 18.4, we still need to use ponyfill until we drop support of iOS 18.4.
3737
value => iteratorEvery(value.entries(), ([key, node]) => key === node['@id']),
3838
'Key returned in Map must match `@id` in value'
3939
)
@@ -49,7 +49,7 @@ const middlewareValidator: GraphMiddleware<any, any> = () => next => request =>
4949
return Object.freeze(result);
5050
};
5151

52-
class Graph2<TInput extends GraphNode, TOutput extends GraphNode = TInput> implements ReadableGraph<TInput, TOutput> {
52+
class Graph<TInput extends GraphNode, TOutput extends GraphNode = TInput> implements ReadableGraph<TInput, TOutput> {
5353
#busy = false;
5454
#middleware: GraphMiddleware<TInput, TOutput>;
5555
#state: GraphState<TOutput> = Object.freeze(new Map());
@@ -139,7 +139,7 @@ class Graph2<TInput extends GraphNode, TOutput extends GraphNode = TInput> imple
139139
}
140140
}
141141

142-
export default Graph2;
142+
export default Graph;
143143
export {
144144
type GraphMiddleware,
145145
type GraphNode,

packages/core-graph/src/private/Graph2.upsert.spec.ts renamed to packages/core-graph/src/private/Graph.upsert.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from '@jest/globals';
22
import { scenario } from '@testduet/given-when-then';
3-
import Graph from './Graph2';
3+
import Graph from './Graph';
44
import './schemas/private/expectExtendValibot';
55
import './schemas/private/expectIsFrozen';
66
import type { Identifier } from './schemas/Identifier';

packages/core-graph/src/private/SlantGraph/SlantGraph.autoInversion.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from '@jest/globals';
22
import { scenario } from '@testduet/given-when-then';
33
import { fn } from 'jest-mock';
4-
import { GraphSubscriber } from '../Graph2';
4+
import { GraphSubscriber } from '../Graph';
55
import SlantGraph from './SlantGraph';
66

77
scenario('SlantGraph auto-inversion', bdd => {

packages/core-graph/src/private/SlantGraph/SlantGraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Graph from '../Graph2';
1+
import Graph from '../Graph';
22
import { type SlantNode } from '../schemas/colorNode';
33
import type { Identifier } from '../schemas/Identifier';
44
import assertSlantNode from './private/assertSlantNode';

0 commit comments

Comments
 (0)