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

[Feature] Enforce/describe State Meta Data with TS #809

Closed
semopz opened this issue Nov 20, 2019 · 6 comments · Fixed by #4863
Closed

[Feature] Enforce/describe State Meta Data with TS #809

semopz opened this issue Nov 20, 2019 · 6 comments · Fixed by #4863

Comments

@semopz
Copy link

semopz commented Nov 20, 2019

Bug or feature request?

Feature

Description:

Currently metadata is of any/unknown type but in the case of actors, I need to be able to lock down what kind of metadata each event can or should have. I'm not sure if this is wading too much into context (extended state) territory but if states can be configured to only accept certain events, they should also be able to emit only a certain type of metadata.

(Feature) Potential implementation:

This is what the API might look like

type Locales = 'en-US' | 'en-GB' | 'fr-CA';
enum EventIntendedFor {
  NoOneOrSilent,
  User,
  Console,
  EventLog,
  System,
}

type Translations = { [code in Locales]: string };
type EventInfo = {
  for: EventIntendedFor,
  message: Translations
};

interface MachineMeta {
  // Tells the app what the machine state is in the local language (if the app wants to display that info)
  strings: {
    label: Translations;
    desc: Translations;
  },
  info?: EventInfo;
}

interface Color extends MachineMeta {
  rgb: number;
  // Different beeps and chirps or instructions
  audio?: number[];
};


interface LightStateSchema {
  states: {
    green: {
      meta: Color
    };
    yellow: {
      meta: Color
    };
    red: {
      meta: Color
      states: {
        walk: {
          meta?: MachineMeta | any
        };
        wait: {
          meta?: MachineMeta | any
        };
        stop: {
          meta?: MachineMeta | any
        };
      };
    };
  };
}

It shouldn't be a breaking change as omitting the meta definition should just imply meta can be any.

Leaving the flexibility in makes the definition a bit verbose (maybe I don't care what the nested states have as metadata but if I decide to put it in, I'd still like some intellisense).

Would be nice if you could define a meta inside the states definition so that all nested states inherit the meta definition. But would that also apply to the nested states within (might break builtin states like onDone)?

Link to reproduction or proof-of-concept:

Sorry, not sure how to implement this

@semopz semopz changed the title [Feature] Enforce/describe metadata with TS [Feature] Enforce/describe State Meta Data with TS Nov 20, 2019
@jamiehaywood
Copy link

any movement on this?

@Allaoua9
Copy link

It would be nice to have this 👍 !

@nstadigs
Copy link

nstadigs commented Oct 7, 2022

I have a use case where the UI elements and the states of my physical devices in my automated home is driven by the metadata of the states of an xstate machine. So I use metadata extensively. Being able to enforce the type of the meta property would really improve my workflow!

@davidkpiano
Copy link
Member

I have a use case where the UI elements and the states of my physical devices in my automated home is driven by the metadata of the states of an xstate machine. So I use metadata extensively. Being able to enforce the type of the meta property would really improve my workflow!

Thanks for sharing the use-case! I have some ideas around typing this and other properties that I will share soon in https://github.com/statelyai/rfcs

@Andarist
Copy link
Member

@nstadigs do u need for all states to have the same type of metadata or would you like for it to be per-state information?

@nstadigs
Copy link

@Andarist having all states share the type of metadata would suit my use case best :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants