Skip to content

Commit c3a7c5d

Browse files
committed
Fix reactive graph backend updates
1 parent 9cb33fd commit c3a7c5d

11 files changed

Lines changed: 1185 additions & 1090 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
2+
import { initializeTrrack, Registry } from '@trrack/core';
3+
import { useEffect, useMemo, useState } from 'react';
4+
5+
function setup() {
6+
const registry = Registry.create<'count'>();
7+
const add = registry.register(
8+
'add',
9+
(state: { count: number }, amount: number) => {
10+
state.count += amount;
11+
},
12+
{
13+
eventType: 'count',
14+
}
15+
);
16+
17+
const trrack = initializeTrrack({
18+
registry,
19+
initialState: { count: 0 },
20+
});
21+
22+
return { trrack, add };
23+
}
24+
25+
function BackendConsumer() {
26+
const { trrack, add } = useMemo(() => setup(), []);
27+
const [backend, setBackend] = useState(() => trrack.graph.backend);
28+
29+
useEffect(() => {
30+
return trrack.currentChange(() => {
31+
setBackend(trrack.graph.backend);
32+
});
33+
}, [trrack]);
34+
35+
return (
36+
<div>
37+
<button
38+
onClick={() => {
39+
void trrack.apply('Add', add(1));
40+
}}
41+
>
42+
Add
43+
</button>
44+
<div data-testid="node-count">{Object.keys(backend.nodes).length}</div>
45+
<div data-testid="current-id">{backend.current}</div>
46+
</div>
47+
);
48+
}
49+
50+
describe('React backend subscriptions', () => {
51+
it('rerenders when currentChange listeners set graph.backend into React state', async () => {
52+
render(<BackendConsumer />);
53+
54+
const initialCurrentId = screen.getByTestId('current-id').textContent;
55+
expect(screen.getByTestId('node-count').textContent).toBe('1');
56+
57+
fireEvent.click(screen.getByRole('button', { name: 'Add' }));
58+
59+
await waitFor(() => {
60+
expect(screen.getByTestId('node-count').textContent).toBe('2');
61+
});
62+
63+
expect(screen.getByTestId('current-id').textContent).not.toBe(
64+
initialCurrentId
65+
);
66+
});
67+
});

packages/core/dist/graph/graph-slice.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type GraphActionCreators<S, E extends string> = {
2222
};
2323
export type ProvenanceGraphActions<S, E extends string> = GraphActionCreators<S, E>;
2424
export type ProvenanceGraphAction<S, E extends string> = ReturnType<GraphActionCreators<S, E>['addMetadata']> | ReturnType<GraphActionCreators<S, E>['addArtifact']> | ReturnType<GraphActionCreators<S, E>['changeCurrent']> | ReturnType<GraphActionCreators<S, E>['addNode']> | ReturnType<GraphActionCreators<S, E>['load']>;
25+
export declare function cloneGraph<State, Event extends string>(graph: ProvenanceGraph<State, Event>): ProvenanceGraph<State, Event>;
2526
export declare function graphSliceCreator<State, Event extends string>(initialState: State, args?: {
2627
artifact?: unknown;
2728
metadata?: Record<string, unknown>;

packages/core/dist/index.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)