Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #156 from statelyai/andarist/simulation-refactor
Browse files Browse the repository at this point in the history
Use upfront simulation mode
  • Loading branch information
Andarist authored Aug 9, 2021
2 parents edc8e9c + c413154 commit 8e0810c
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 248 deletions.
30 changes: 26 additions & 4 deletions cypress/integration/inspector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,35 @@ describe('Inspector', () => {
cy.inspectMachine(
createMachine({
id: 'my_inspected_machine',
initial: 'a',
states: {
a: {},
},
}),
);

cy.getCanvas().contains('my_inspected_machine');
});

it('should list events sent to an inspected machine', () => {
cy.visitInspector();

cy.inspectMachine(createMachine({})).then((service) => {
service.send({ type: 'MY_EVENT' });

cy.findByRole('tab', { name: 'Events' }).realClick();
cy.findByRole('tabpanel', { name: 'Events' }).contains('MY_EVENT');
});
});

it('should not come with the RESET button', () => {
cy.visitInspector();

cy.inspectMachine(
createMachine({
id: 'my_inspected_machine',
}),
);

// wait for the canvas being ready
cy.getCanvas().contains('my_inspected_machine');

cy.findByRole('button', { name: 'RESET' }).should('not.exist');
});
});
9 changes: 2 additions & 7 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import '@testing-library/cypress/add-commands';
import 'cypress-localstorage-commands';
import 'cypress-real-events/support';
import { inspect, Inspector } from '@xstate/inspect';
import {
createMachine,
interpret,
InterpreterFrom,
StateMachine,
} from 'xstate';
import { interpret, InterpreterFrom, StateMachine } from 'xstate';
import { state } from './state';
import { Mutation, Query } from '../../src/graphql/schemaTypes.generated';

Expand Down Expand Up @@ -72,7 +67,7 @@ const visitInspector = () => {
const inspector = inspect({
iframe: () =>
window.parent.document.querySelector<HTMLIFrameElement>('.aut-iframe'),
url: Cypress.config('baseUrl')!,
url: `${Cypress.config('baseUrl')!}?inspect`,
})!;
state('@@viz/inspector', inspector);
});
Expand Down
2 changes: 1 addition & 1 deletion src/ActorsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const ActorsPanel: React.FC = () => {
simActor.send({ type: 'SERVICE.FOCUS', sessionId });
}}
>
{serviceData?.machine.id ?? '(machine)'}
{serviceData!.machine.id ?? '(machine)'}
</Link>{' '}
({sessionId})
</Text>
Expand Down
1 change: 1 addition & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { useInterpretCanvas } from './useInterpretCanvas';

function App() {
const paletteService = useInterpret(paletteMachine);
// don't use `devTools: true` here as it would freeze your browser
const simService = useInterpret(simulationMachine);
const machine = useSelector(simService, (state) => {
return state.context.currentSessionId
Expand Down
22 changes: 13 additions & 9 deletions src/CanvasPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from './canvasMachine';
import { toDirectedGraph } from './directedGraph';
import { Graph } from './Graph';
import { useSimulation } from './SimulationContext';
import { useSimulation, useSimulationMode } from './SimulationContext';
import { theme } from './theme';
import { CanvasPanelHeader } from './CanvasPanelHeader';

Expand All @@ -45,6 +45,8 @@ export const CanvasPanel: React.FC = () => {
getShouldEnableZoomInButton,
);

const simulationMode = useSimulationMode();

return (
<ChakraProvider theme={theme}>
<Box display="grid" gridTemplateRows="3rem 1fr">
Expand Down Expand Up @@ -95,14 +97,16 @@ export const CanvasPanel: React.FC = () => {
variant="secondary"
/>
</ButtonGroup>
<Button
size="sm"
margin={2}
onClick={() => simService.send('MACHINES.RESET')}
variant="secondary"
>
RESET
</Button>
{simulationMode === 'visualizing' && (
<Button
size="sm"
margin={2}
onClick={() => simService.send('MACHINES.RESET')}
variant="secondary"
>
RESET
</Button>
)}
</HStack>
</Box>
</ChakraProvider>
Expand Down
2 changes: 1 addition & 1 deletion src/EventsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const EventsPanel: React.FC = () => {
const rawEvents = state.context!.events;
const nextEvents = useSelector(
sim,
(state) => selectMachine(state)?.state?.nextEvents,
(state) => selectMachine(state)?.state.nextEvents,
(a, b) => JSON.stringify(a) === JSON.stringify(b),
);

Expand Down
Loading

1 comment on commit 8e0810c

@vercel
Copy link

@vercel vercel bot commented on 8e0810c Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.