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

Add FP goodies baked into alt #279

Merged
merged 1 commit into from
Jun 2, 2015
Merged

Add FP goodies baked into alt #279

merged 1 commit into from
Jun 2, 2015

Conversation

goatslacker
Copy link
Owner

Brings in a lot of the ideas of microflux into Alt.

This PR is full of new features:

  • preventDefault() is this new method available on the store that will prevent a change event from being emitted. This is a better alternative than returning false from each action handler as this is more explicit. This will also be needed for other methods below.
  • observe() is a new method that you define in your store. It was made mostly for when creating stores with POJOs and using alt instances. This method gets alt as its first and only parameter and you return an Object from it. The Object contains the same stuff that would go in bindListeners. These listeners are then bound.
  • otherwise() is a new method you define in your store. This is a concept borrowed from Haskell's guards. It is called for every single dispatch that isn't already being handled in your store. So say your store already has handlers for actions A and B and action C fires, this would hit the otherwise method. If action A were to be fired then only the handler would react.
  • reduce() is a new method you define in your store. This handles all dispatches and it is quite a special method since it doesn't work like the other ones. In reduce you return an object which then becomes the next state. This works well if you're looking to create simple stores of derived data or as some call them: "ephemeral stores".
  • output(), also a method you define in your store, shapes the data as its emitted in a change event. This is a method where you can transform the state (but not save) before it reaches the store listeners.

@goatslacker
Copy link
Owner Author

@jdlehman
Copy link
Collaborator

These are great! I am especially excited about reduce. I like how these are added in as features rather than requirements. Let's people use alt how they need to. Implementation looks solid from a first glance.

@goatslacker
Copy link
Owner Author

A couple of API questions:

Should I pass in what action was called into reduce? What should the function arguments be?

What about otherwise, what should the signature be?

And output as well?

@jdlehman
Copy link
Collaborator

I don't know if the action name is needed for reduce. Any state they want to manage in the store should be handled accordingly based on the data passed in. Some people might want it, but I think that might encourage large switch statements to handle logic rather than approaching it more functionally.

I think the function arguments you have for reduce (alt, state, payload) are fine.

otherwise can just be the same as any other action handler (which I believe is how you have it now).

for output I think you just need the current state, which is also what you have now (along with the alt instance).

tldr; i think everything one would need as function arguments for these functions are there in the current implementation.

goatslacker added a commit that referenced this pull request Jun 2, 2015
@goatslacker goatslacker merged commit a68c57d into master Jun 2, 2015
@goatslacker goatslacker deleted the microflux-ideas branch June 2, 2015 17:56
@jdlehman
Copy link
Collaborator

jdlehman commented Jun 3, 2015

We should add some docs for these 😄

@goatslacker
Copy link
Owner Author

on it!

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

Successfully merging this pull request may close these issues.

2 participants