Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions packages/api/src/hooks/useActivities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,57 @@ declare const process: {
};
};

export default function useActivities(): readonly [readonly WebChatActivity[]] {
const activitiesFromGraphState = useOrderedActivities();
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);

// ASSERTION: Before we fully migrate to graph, make sure graph and Redux are the same.
if (process.env.NODE_ENV !== 'production') {
const [activitiesFromGraph] = activitiesFromGraphState;
const [activitiesFromGraph] = activitiesFromGraphState;

const activitiesFromRedux = useSelector(({ activities }) => activities);

// Assert based on NODE_ENV.
// eslint-disable-next-line react-hooks/rules-of-hooks
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
}
}
);
}

// 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) {
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 size (graph has ${activitiesFromGraph.length} activities, Redux has ${activitiesFromRedux.length} activities)`,
`botframework-webchat-internal: Activities from graph and Redux are different at index ${index}`,
{
cause: {
activitiesFromGraph,
activitiesFromRedux
activitiesFromRedux,
index
}
}
);
}

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;
2 changes: 1 addition & 1 deletion packages/core-graph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export {
type GraphSubscriberRecord,
type ReadableGraph,
type WritableGraph
} from './private/Graph2';
} from './private/Graph';
export { SlantNodeSchema, type SlantNode } from './private/schemas/colorNode';
export {
DirectLineActivityNodeSchema,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from '@jest/globals';
import { scenario } from '@testduet/given-when-then';
import { assert, object } from 'valibot';
import Graph from './Graph2';
import Graph from './Graph';
import './schemas/private/expectExtendValibot';
import './schemas/private/expectIsFrozen';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from '@jest/globals';
import { scenario } from '@testduet/given-when-then';
import Graph from './Graph2';
import Graph from './Graph';
import './schemas/private/expectExtendValibot';
import './schemas/private/expectIsFrozen';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from '@jest/globals';
import { scenario } from '@testduet/given-when-then';
import { iteratorFilter, iteratorMap } from 'iter-fest';
import { fn } from 'jest-mock';
import Graph from './Graph2';
import Graph from './Graph';
import type { Identifier } from './schemas/Identifier';
import './schemas/private/expectExtendValibot';
import './schemas/private/expectIsFrozen';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from '@jest/globals';
import { scenario } from '@testduet/given-when-then';
import { fn } from 'jest-mock';
import { assert, map, string, unknown } from 'valibot';
import Graph, { type GraphState } from './Graph2';
import Graph, { type GraphState } from './Graph';
import './schemas/private/expectExtendValibot';
import './schemas/private/expectIsFrozen';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type WritableGraph<TInput extends GraphNode, TOutput extends GraphNode> = {
const requestSchema = pipe(
map(IdentifierSchema, object({ '@id': IdentifierSchema })),
check(
// TODO: Iterator.every is since iOS 18.4, we still need to use ponyfill for now.
// TODO: [P4] Iterator.every is since iOS 18.4, we still need to use ponyfill until we drop support of iOS 18.4.
value => iteratorEvery(value.entries(), ([key, node]) => key === node['@id']),
'Key returned in Map must match `@id` in value'
)
Expand All @@ -49,7 +49,7 @@ const middlewareValidator: GraphMiddleware<any, any> = () => next => request =>
return Object.freeze(result);
};

class Graph2<TInput extends GraphNode, TOutput extends GraphNode = TInput> implements ReadableGraph<TInput, TOutput> {
class Graph<TInput extends GraphNode, TOutput extends GraphNode = TInput> implements ReadableGraph<TInput, TOutput> {
#busy = false;
#middleware: GraphMiddleware<TInput, TOutput>;
#state: GraphState<TOutput> = Object.freeze(new Map());
Expand Down Expand Up @@ -139,7 +139,7 @@ class Graph2<TInput extends GraphNode, TOutput extends GraphNode = TInput> imple
}
}

export default Graph2;
export default Graph;
export {
type GraphMiddleware,
type GraphNode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from '@jest/globals';
import { scenario } from '@testduet/given-when-then';
import Graph from './Graph2';
import Graph from './Graph';
import './schemas/private/expectExtendValibot';
import './schemas/private/expectIsFrozen';
import type { Identifier } from './schemas/Identifier';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from '@jest/globals';
import { scenario } from '@testduet/given-when-then';
import { fn } from 'jest-mock';
import { GraphSubscriber } from '../Graph2';
import { GraphSubscriber } from '../Graph';
import SlantGraph from './SlantGraph';

scenario('SlantGraph auto-inversion', bdd => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-graph/src/private/SlantGraph/SlantGraph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Graph from '../Graph2';
import Graph from '../Graph';
import { type SlantNode } from '../schemas/colorNode';
import type { Identifier } from '../schemas/Identifier';
import assertSlantNode from './private/assertSlantNode';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert, type BaseSchema } from 'valibot';
import { type GraphMiddleware } from '../../Graph2';
import { type GraphMiddleware } from '../../Graph';
import { SlantNodeSchema, type SlantNode } from '../../schemas/colorNode';
import { DirectLineActivityNodeSchema } from '../../schemas/DirectLineActivityNode';
import isOfType from '../../schemas/isOfType';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-expect-error No @types/core-js-pure.
import difference from 'core-js-pure/features/set/difference';
import { type GraphMiddleware } from '../../Graph2';
import { type GraphMiddleware } from '../../Graph';
import { type SlantNode } from '../../schemas/colorNode';
import type { Identifier } from '../../schemas/Identifier';
import { type NodeReference } from '../../schemas/NodeReference';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type GraphMiddleware } from '../../Graph2';
import { type GraphMiddleware } from '../../Graph';
import colorNode, { type SlantNode } from '../../schemas/colorNode';
import flattenNodeObject from '../../schemas/flattenNodeObject';
import type { Identifier } from '../../schemas/Identifier';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type GraphMiddleware } from '../../Graph2';
import { type GraphMiddleware } from '../../Graph';
import { type SlantNode } from '../../schemas/colorNode';
import type { Identifier } from '../../schemas/Identifier';
import type { AnyNode } from '../SlantGraph';
Expand Down
Loading