Comments (2)
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 modifieddispatch
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.
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)
- PascalCase compatibility layer HOT 2
- Move Heartbeat changed listening to Roact HOT 3
- loggerMiddleware should throw if given invalid arguments HOT 4
- Expunge remaining references to bin scripts
- Use arguments to debug.traceback to improve `NoYield` output HOT 1
- Middlewares are applied in last-in-first-out order HOT 2
- Deprecate 'changed' and introduce new store update signal HOT 2
- Add Rodux.createAction HOT 4
- Don't fire update if the top-level state doesn't change after a reducer HOT 2
- Lift restriction on subscribers yielding
- If the initialState argument of createReducer is nil, then its actionHandlers are never called. HOT 2
- Add the ability to access state from other scripts HOT 2
- Rename root folder from lib to src
- Update docs to fix reducer page HOT 1
- Add a Signal.Destroy method HOT 1
- Store.spec.lua has an undestructed store
- Don't pretty-print tables in loggerMiddleware.lua
- Expose store `ACTION_LOG_LENGTH` as a user-settable param, and add a `:clearLog` method to the store
- Update version and make new release or tag HOT 3
- Continue execution after error is thrown while flushing store updates HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rodux.