theia-ide / sprotty

A next-gen web-based graphics framework
Apache License 2.0
138 stars 23 forks source link

Define actions as interfaces #200

Open spoenemann opened 6 years ago

spoenemann commented 6 years ago

Just a thought:

Actions are meant to be plain JSON data structures that can be transferred between client and server. Currently they are implemented with classes, and their properties are all passed via constructor.

export class MoveAction implements Action {
    kind = MoveCommand.KIND

    constructor(public readonly moves: ElementMove[],
                public readonly animate: boolean = true) {
    }
}

const action = new MoveAction([{ elementId: 'foo', toPosition: { x: 10, y: 20 } }], true)

An alternative would be to define actions as interfaces.

export interface MoveAction extends Action {
    kind: 'move'
    moves: ElementMove[]
    animate: boolean
}

const action = <MoveAction>{
    kind: 'move',
    moves: [{ elementId: 'foo', toPosition: { x: 10, y: 20 } }],
    animate: true
}

This would prevent using the action classes as runtime values, which can lead to mistakes such as using instanceof on an action.