Giter Site home page Giter Site logo

Introduce middleware about rodux HOT 2 CLOSED

LPGhatguy avatar LPGhatguy commented on June 6, 2024 2
Introduce middleware

from rodux.

Comments (2)

AmaranthineCodices avatar AmaranthineCodices commented on June 6, 2024

There are several possible ways to do this, I think.

  • Add middlewares via a method of Store, e.g. Store:addMiddleware(...).
  • Specify middlewares when creating the store as another argument, e.g. Store.new(reducer, initialState, middlewares)
  • "Replace" the store's dispatch method. This is how Redux implements middleware (it actually creates a new store with the modified dispatch method, but that's an implementation detail, I think).

Of these options, I'm most in favor of specifying middlewares at store creation. Adding them via a method seems like a potential rats' nest of errors, and replacing the store's dispatch method is...obtuse, to put it lightly. Even with Redux's very nicely written documentation, it took me a good hour or so to untangle how Redux implements middleware (...across all of 20 lines of code, for that matter). That doesn't bode well for future maintenance or contributions to the functionality, if it were replicated in Rodux.

The middleware functions themselves should have the following signature:

middleware(action, store) -> action|nil

Middlewares will be invoked for each action; they may choose to return a new action, return the existing one, or consume the action by returning nil. They are free to dispatch new actions as they see fit. This can cause re-entrancy, and care must be taken to avoid it.

from rodux.

AmaranthineCodices avatar AmaranthineCodices commented on June 6, 2024

I have a prototype middleware implementation available here.

I kind of diverged from my above remarks a bit. The store's dispatch method is overridden; middlewares generate new dispatch methods. They are specified in the constructor, not as a weird store enhancer like Redux.

The API now:

- function Store.new(reducer, initialState)
+ function Store.new(reducer, initialState, middlewares)

Middlewares are applied left-to-right order - the rightmost middleware is invoked first.

Middlewares look like this:

local function loggerMiddleware(next)
    return function(store, action)
        print("action dispatched:", action.type)
        next(action)
        print("new state:")
        printTable(store:getState())
    end
end

Using this looks like:

local store = Store.new(reducer, initialState, { loggerMiddleware })
store:dispatch({
    type = "someAction",
    ...
})
-- prints "action dispatched: someAction"
-- then the new state

Thoughts?

from rodux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.