Skip to content
This repository has been archived by the owner on Sep 9, 2019. It is now read-only.

Support Middleware #1

Open
blainekasten opened this issue Apr 20, 2016 · 3 comments
Open

Support Middleware #1

blainekasten opened this issue Apr 20, 2016 · 3 comments

Comments

@blainekasten
Copy link
Owner

Talking with @developerdizzle. We talked about middlewares and the general ideas. Here are some thoughts as a take away.

Middleware might be needed

Legimiate cases for middleware are:

  • history (undo/redo)
  • logging
  • Devtools

I think there are 2 ways we could approach this. actionExtender's or generic middleware.

This is how I would expect them to be implemented:

actionExtender
// happens at some top level
const history = [];
actionExtender('undoable', function(prevState, proposedState, componentName, actionName) {
  // do something with the previous or proposed state
  history.push(prevState);
});
function foo({actions}) {
  return <input onClick={actions.handleClick.undoable} /> // undoable being the key here
}

wrap(foo, { actions: {
  handleClick(state, next) {
    // does something
    next({foo: 'bar'});
  }
}});

Pro's here are actions can opt into using a middleware. This technically isn't middleware but it might support all the use-cases we need.

middleware

This is true middleware, every action called also calls the middleware's.

// some top level file
middleware(function logger(prevState, proposedState, componentName, actionName) {
  fetch('api/v2/log', {prevState, proposedState, componentName, actionName});
});

Other notes

The implementation of this should probably live on Provider. Something like this:

render(
  <Provider middlewares={[logger, devTools]}>
    ...
  </Provider>
)
@developerdizzle
Copy link

I think I like the actionExtender method better, specifically for undo/redo. I can see a case where you wouldn't want undo/redo to apply to every action, and I feel like it would be harder to code a middleware to selectively undo certain actions. Logging and devtools are a little more passive, so middleware makes more sense to me for those.

@blainekasten
Copy link
Owner Author

the middleware part is how redux-undo works. So I don't think we necessarily need actionExtender to pull it off.

I also just want to ultimately land on 1 api. Too many API options are generally more confusing than helpful.

@blainekasten
Copy link
Owner Author

I've taken an initial stab at this in this branch MiddlewareAttempt1. Can't say I love it. It's hard to think of what is actually beneficial with these things in coflux. Redux did this to act in between reducers and updaters.

not sure we have an equivalent here.. needs more thought

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

No branches or pull requests

2 participants