Comments (14)
@timojaask That is incorrect. Middleware occurs around reducers.
public func persistanceMiddleware() -> Middleware<PersistenceMiddlewareState> {
return { dispatch, getState in
return { next in
return { action in
// Any code here is before the reducers
next(action) // This executes the reducers
// Any code here is after the reducers
}
}
}
}
from reswift.
@timojaask Middleware can act after the store changes. Here is an example:
public func persistanceMiddleware() -> Middleware<PersistenceMiddlewareState> {
return { dispatch, getState in
return { next in
return { action in
next(action) // This executes the next middleware, and the reducers at the end of the middleware chain.
// Any call to `getState` here or below is after the reducers have worked.
getState()?.convertToData().writeToFile(myFile)
}
}
}
}
from reswift.
Sorry, I realized my comment was incorrect, so I removed it. I guess you posted your response around the same time. Thanks for providing the solution! I'll test it out.
from reswift.
Yes, may I suggest that we stick with the same middleware pattern from Redux. Then it should be pretty easy to port over a robust optimistic update solution such as redux-optimist.
from reswift.
BTW, great work on this library. We've just decided to use this on our next project and I'm looking forward to contributing.
from reswift.
@aexmachina thanks a lot for your response! The goal is to have a middleware API that resembles the one of redux. The folks over at https://github.com/reduxkit/reduxkit are actually close to feature parity with the original Redux API, and we are currently considering merging efforts.
If we end up with separate projects though, I definitely want to bring a redux-like middleware implementation to Swift Flow soon!
from reswift.
FWIW, your implementation is much nicer than ReduxKit 😄 I think Swift Flow manages to "go with the grain" of Swift's type system, rather than getting caught up in generics.
from reswift.
Will tackle store enhancing and middleware separately. Middleware is being implemented here: #9
from reswift.
How were you envisioning the usage of a store enhancer?
let newStore = SomeStoreEnhancer(store: existingStore)
store.enhance(enhancer: SomeStoreEnhancer)
let store = SomeStoreEnhancer(MainStore)(reducer: reducer, appState: TestStringAppState())
The redux store enhancers take a createStore function, enhance it, and return a new wrapped createStore function. The createStore equivalent here would most likely be MainStore#init. For the store enhancer to have direct access to the dispatch and state, it might be best for store enhancer to just be a store function.
I guess one test would be to implement applyMiddleware as a store enhancer.
from reswift.
@agentk I originally opened this ticket for the implementation of middleware, which has been closed in #9. Middleware was very high priority - I'm not entirely sure about store enhancers yet.
This is actually one of the areas where I'm wondering if there's an implementation more natural to Swift. From the original documentation of redux:
A store enhancer is a higher-order function that composes a store creator to return a new, enhanced store creator. This is similar to middleware in that it allows you to alter the store interface in a composable way.
It seems like in Swift this would practically mean a one-to-one mapping between a specific store enhancer and a specific store type, since unlike in JS you cannot modify the interface of the store without introducing a new type. If my assumption is correct, it also means that store enhancers couldn't be composed.
If both assumptions are correct I think it would be better to have individual types for the different stores one can create.
But you can see I still have a little bit of research to do 😄
from reswift.
So far it doesn't seem we have a need for actual store enhancers, therefore I'm closing this issue for now.
from reswift.
Yeah, I think middleware is the crucial requirement here.
from reswift.
@Ben-G I am trying to implement state persister, for which I feel that store enhancer would be the best option.
Middleware cannot be used because it's called before store changes.
Another option is to make persister a StoreSubscriber
and subscribe to store changes, but you can't use StoreSubscriber
as a function parameter because it's a protocol with associated types, which makes it very clumsy and I can't figure out how to write tests for that.
So it seems like there's a need for store enhancer. I know you're not working on this project anymore, but since you have already looked at the enhancer, I wanted to ask you if you could share any findings. Were there any issues or gotchas with implementing an enhancer?
EDIT: I guess there really is no way to avoid using StoreSubscriber
no matter what approach I use. So I guess I'll just try to work with it and see what I can come up with.
from reswift.
I didn't tried it to use getState()
after next(action)
, I have to think about it.
from reswift.
Related Issues (20)
- Concurrency - protected reads HOT 2
- How to subscribe/unsubscribe store in a UIView like in UITableViewCell, CollectionViewCell etc ? HOT 1
- What's a good way to cast results of a subscription in SwiftUI to a desired output type? HOT 3
- Managing ReSwift app state inside FileDocument HOT 1
- Remove Travis from all ReSwift repos? / How to set up GH Actions
- ReSwift 7 Roadmap HOT 20
- Docs: Update outdated and vulnerable jQuery version HOT 3
- Array as a State HOT 1
- Clarification on Single Store object for an application. HOT 1
- Change visibility of Store from 'open' to 'public final'? HOT 2
- SIGSEGV on state.didSet HOT 4
- Using Inject with ReSwift HOT 1
- Testing: Waiting for store.dispatch() to complete HOT 2
- Huge CPU gap between ReSwift Store vs just SwiftUI ObservableObject HOT 1
- Crash: sometimes will crash at this point, what can i do to find the root cause HOT 11
- Crash when dispatching actions from inside StoreSubscriber.newState() HOT 2
- How to use a state on recursive screens? HOT 4
- Filter on updates not working HOT 3
- When do you upgrade ReSwiftRouter ? Now,ReSwiftRouter just depends ReSwift 5.0.0, but lasted ReSwift is 6.1.1 HOT 2
- Privacy Manifest HOT 2
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 reswift.