Xstate: Add ability to pass params to service declared as property

Created on 10 Jul 2019  路  10Comments  路  Source: davidkpiano/xstate

Feature request?

Description:

This exchange in the chat best summarizes the request:

Q: Is there any way to pass hardcoded values to an invocation when the service is declared in a services property?

A: Currently no, ... Should be just like actions and guards

(Feature) Potential implementation:

  • What the API would look like

// future enhancement

services: {
  fetchUser: (context, event, { service }) => {
    // service.params === { role, username, password }, etc.
  }
}
  • If this is a breaking change

Should not be a breaking change

enhancement

Most helpful comment

Yes it will.

All 10 comments

Could you explain your use case?

I believe it can be handled easily with functions, no?

function sendRequest({ method = "get", path, body }) {
  // ...
}

function fetchUser() {
  let role = "foo"
  let username = "bar"
  let password = "baz"

  return sendRequest({ path: "/user", body: { role, username, password } })
}

services: {
  fetchUser: (context, event) => fetchUser()
}

I also don't see a value of this at the moment as this introduces new special API to cover a use case which can be easily covered by language itself.

I see the utility of this now - this should come in the form of something like:

invoke: {
  src: {
    type: 'customerAPI',
    method: 'GET',
    params: { user: 42 }
  }
}

Where the implementation details for customerAPI are provided.

So would this just fill arguments of the given customerAPI?

So would this just fill arguments of the given customerAPI?

No, that's up to the implementation. (Contrived example below)

services: {
  customerAPI: (context, event, { service }) => {
    return fetch(API.getCustomer + toQueryParams(service.params), {
      method: service.method
    });
  }
}

Will the third argument be a "stateService" object that not only has the extra service data like you demonstrated but also the current state?
I once had to dump the state (value) into an event just to use it inside a service. This wasn't pretty at all, accessing the state directly within a service would be really helpful.

Yes, just like the third "meta" argument for actions and guards, it will have the current state.

Also, please don't rely on state if you can avoid it. It's not an optimal pattern - you should already know which state you're in given the machine structure.

Also, please don't rely on state if you can avoid it. It's not an optimal pattern - you should already know which state you're in given the machine structure.

I agree. In my use case, I modeled some radio buttons as state nodes and needed to send their value to the server. Haven't ran into any other scenario where I needed state inside a service but it's useful to know that it'll be possible.

@davidkpiano will this feature be available in V5

Yes it will.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kurtmilam picture kurtmilam  路  3Comments

carloslfu picture carloslfu  路  3Comments

dakom picture dakom  路  3Comments

mattiamanzati picture mattiamanzati  路  3Comments

doup picture doup  路  3Comments