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

Are action creators legacy? #68

Closed
ShMcK opened this issue Oct 25, 2017 · 5 comments
Closed

Are action creators legacy? #68

ShMcK opened this issue Oct 25, 2017 · 5 comments
Assignees
Labels

Comments

@ShMcK
Copy link
Member

ShMcK commented Oct 25, 2017

Dispatch is basically made of a bunch of action creators organized by model.

See createDispatcher

The current design potentially offers the following benefits

  • potential autocomplete for dispatches (not really working)
  • immediate referencing of the action creator

Idea 1: Hacky JavaScript ?

But if all action creators are all essentially the same code, is there a way to create action creators on the fly?

dispatch.count.addBy(5)
// someone how captures "count" as model
// someone captures "addOne" as the action
// somehow captures the param "5" as payload

Although, I don't think the above would work dynamically,

Idea 2: Dispatch as a Function

it's possible to reference dispatches in a way that would work. Some examples:

dispatch('count', 'addBy', 5)
dispatch('count/addBy', 5)

This would call dispatch['count']['addBy'](5). Boom.

Or some variation of above.

The only benefit really being that you don't have to create action creators on startup, and hold them in memory through the life of the app. Note that this is what most standard redux apps do anyway, it just strikes me as wasteful.

I'm not sure how you would structure this if you were adding extra properties to the action, like "meta". But we don't currently handle any situations like that anyway.

dispatch('count/addBy', 5, { meta: something: true })

Summary

Consider this more of a thought experiment than a proposal.

@blairbodnar
Copy link
Member

Definitely some cool ideas here! @ShMcK

Perhaps we could provide the flexibility for both. By default, create the action creators on start up, but also have options to change that behavior.

Maybe two options like this:

  1. A top level option that tells rematch to create all action creators on the fly.
init({ lazyActionCreators: true })
  1. A model option that tells rematch to only create action creators on the fly for that specific model.
model({
  lazyActionCreators: true,
  name: 'count',
  ...
})

@ShMcK
Copy link
Member Author

ShMcK commented Oct 26, 2017

Not a bad idea. But the slippery slope of configuration might be better avoided.

I chatted with Scott today about this. I'll summarize the key points

The ideal dispatch API should:

  • autocomplete
  • have a simple API
  • throw an error or warning if you dispatch an invalid action

Probably the current API has the most hope and getting all three.

@blairbodnar
Copy link
Member

Yup yup, agreed. And depending on your IDE, The autocomplete kinda works anyways.

@ShMcK
Copy link
Member Author

ShMcK commented Oct 26, 2017

You and your amazing WebStorm.

Anything else you'd like in an action?

@blairbodnar
Copy link
Member

haha, yup, WebStorm makes me happy!

Can't think of much else for now. Idea # 2 you mentioned above would be cool though!

So far, my day to day use with mirror is pretty nice, so I'm happy so far with what we have!

@ShMcK ShMcK closed this as completed Nov 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants