Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(annotations): Handle annotations staged change event #1247

Merged
merged 7 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions src/lib/AnnotationControlsFSM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export const stateModeMap = {
export default class AnnotationControlsFSM {
private currentState = AnnotationState.NONE;

public getState = (): AnnotationState => this.currentState;

public setState = (state: AnnotationState): void => {
mxiao6 marked this conversation as resolved.
Show resolved Hide resolved
this.currentState = state;
};

public transition = (input: AnnotationInput, mode: AnnotationMode = AnnotationMode.NONE): AnnotationMode => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Having mode default to none seems ripe for bugs to be introduced. Would it work if it was just optionally undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The state in FSM cannot be undefined, so if mode is undefined and state transition depends on mode, the state has to be none which is the same as default mode to none.

if (input === AnnotationInput.CLICK) {
this.currentState = mode === stateModeMap[this.currentState] ? AnnotationState.NONE : modeStateMap[mode];
Expand Down
90 changes: 90 additions & 0 deletions src/lib/__tests__/AnnotationControlsFSM-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import AnnotationControlsFSM, { AnnotationInput, AnnotationState } from '../AnnotationControlsFSM';
import { AnnotationMode } from '../AnnotationControls';

let annotationControlsFSM;

describe('lib/AnnotationControlsFSM', () => {
beforeEach(() => {
annotationControlsFSM = new AnnotationControlsFSM();
});

[
{
state: AnnotationState.NONE,
mxiao6 marked this conversation as resolved.
Show resolved Hide resolved
input: AnnotationInput.CLICK,
mode: AnnotationMode.HIGHLIGHT,
nextState: AnnotationState.HIGHLIGHT,
Copy link
Contributor

Choose a reason for hiding this comment

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

Does nextState and output ever differ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nextState could be highlight_temp and output is highlight

output: AnnotationMode.HIGHLIGHT,
},
{
state: AnnotationState.NONE,
input: AnnotationInput.CLICK,
mode: AnnotationMode.REGION,
nextState: AnnotationState.REGION,
output: AnnotationMode.REGION,
},
{
state: AnnotationState.NONE,
input: AnnotationInput.CREATE,
mode: AnnotationMode.HIGHLIGHT,
nextState: AnnotationState.HIGHLIGHT_TEMP,
output: AnnotationMode.HIGHLIGHT,
},
{
state: AnnotationState.NONE,
input: AnnotationInput.CREATE,
mode: AnnotationMode.REGION,
nextState: AnnotationState.REGION_TEMP,
output: AnnotationMode.REGION,
},
{
state: AnnotationState.HIGHLIGHT,
input: AnnotationInput.CANCEL,
mode: AnnotationMode.HIGHLIGHT,
nextState: AnnotationState.HIGHLIGHT,
output: AnnotationMode.HIGHLIGHT,
},
{
state: AnnotationState.HIGHLIGHT,
input: AnnotationInput.SUCCESS,
mode: undefined,
nextState: AnnotationState.HIGHLIGHT,
output: AnnotationMode.HIGHLIGHT,
},
{
state: AnnotationState.HIGHLIGHT_TEMP,
input: AnnotationInput.CLICK,
mode: AnnotationMode.HIGHLIGHT,
nextState: AnnotationState.NONE,
output: AnnotationMode.NONE,
},
{
state: AnnotationState.HIGHLIGHT_TEMP,
input: AnnotationInput.CLICK,
mode: AnnotationMode.REGION,
nextState: AnnotationState.REGION,
output: AnnotationMode.REGION,
},
{
state: AnnotationState.HIGHLIGHT_TEMP,
input: AnnotationInput.CANCEL,
mode: AnnotationMode.HIGHLIGHT,
nextState: AnnotationState.NONE,
output: AnnotationMode.NONE,
},
{
state: AnnotationState.HIGHLIGHT_TEMP,
input: AnnotationInput.SUCCESS,
mode: undefined,
nextState: AnnotationState.NONE,
output: AnnotationMode.NONE,
},
].forEach(({ state, input, mode, nextState, output }) => {
it(`should go to state ${nextState} and output ${output}`, () => {
annotationControlsFSM.setState(state);

expect(annotationControlsFSM.transition(input, mode)).to.equal(output);
expect(annotationControlsFSM.getState()).to.equal(nextState);
});
});
});