kalix-javascript-sdk icon indicating copy to clipboard operation
kalix-javascript-sdk copied to clipboard

Generated event-sources entity JS files pass redundant state parameter

Open mckeeh3 opened this issue 3 years ago • 1 comments

Here is an example:

entity.setInitial(entityId => ({}));

entity.setBehavior(state => ({
  commandHandlers: {
    AddItem(command, state, ctx) {
      return ctx.fail("The command handler for `AddItem` is not implemented, yet");
    },

Note that state is a parameter in setBehavior(state => ({ and each of the command handler functions are also called with state AddItem(command, state, ctx) {.

mckeeh3 avatar Sep 27 '21 17:09 mckeeh3

The state in the behaviour callback is for switching behaviour based on state, while the handler functions could be defined separately and will still want to receive the state as well. As the state machine approach is not expected to be used initially from the generated code, the codegen could instead leave out the state parameter there and generate:

entity.setBehavior(() => ({
  commandHandlers: {
    AddItem(command, state, ctx) {
      return ctx.fail("The command handler for `AddItem` is not implemented, yet");
    },
  ...

Also discussion on whether to keep behaviours, or make it more optional, in #334. Where there could be setCommandHandlers and setEventHandlers as well, to create default behaviour, if multi-behaviour / state machine approach is not being used.

pvlugter avatar May 25 '22 23:05 pvlugter