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

[Question] Using the returned objects in place of the function calls #177

Closed
sunny-mittal opened this issue Jun 1, 2022 · 2 comments
Closed

Comments

@sunny-mittal
Copy link

sunny-mittal commented Jun 1, 2022

Hello,
I'm trying out robot after having used xstate fairly extensively and the best way for me to learn a lib is to just play around with various functions in node to get a feel for what they do. When trying out reduce, I see it just returns the anonymous (or named) function you provide it mapped to the fn key of an object, so:

const fn = () => {}
reduce(fn).fn === fn // true

So I figured I could probably (I'd never do this) just use that object shape in place of the reduce call, something like:

loading: invoke(
  invocable,
  transition(
    'done',
    'success',
    { fn: (ctx, evt) => ({...ctx, title: evt.data }) }
  )
)

but this doesn't work. I'm curious what I'm missing here. Looking forward to an answer :)

@sunny-mittal sunny-mittal changed the title Just a simple question [Question] Using the returned objects in place of the function calls Jun 1, 2022
@sunny-mittal
Copy link
Author

After looking at the source code, I see that it's a bit more magical and clever than I could deduce from simple node output. I see that reduceType is used as a prototype for all reducer objects so they can be distinguished from actions/guards that have the same shape. I've never thought to do that but it's pretty dang smart. Closing now.

@matthewp
Copy link
Owner

matthewp commented Jun 1, 2022

@sunny-mittal The prototype system in Robot is intended to be extended. So creating your own type that extends reduceType, etc is encourage (albeit advanced, not something most people will do).

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

No branches or pull requests

2 participants