Comments (7)
Interesting case!
I'll try to dive into it more deeper later today, but at least in the setSP function you should be able to do:
mobservable.transaction(funtion() {
x.stock = newStock;
x.price = newPrice;
});
that might save some updates.
from mobx.
Wonderful. It is working (only had to update the setSP function as you described ).
Since you find this use case interesting, maybe you could tell us whether this approach is good. Maybe there are better ways to "sync objects" using mobservable?
I'm also thinking of syncing objects across VMs and across machines (over the wire)
window.setSP = function(x, newStock, newPrice){
mobservable.transaction(function() {
x.stock = newStock;
x.price = newPrice;
});
}
from mobx.
Now that I think of this, this is exactly one of the situations a had on my todo list to forbid :-P.
The problem is that the side effects are not pure; they are updating the state their own execution depends on. In this case it works, but in more complex situations it can easily become hard to reason about and error prone; for examples, if the code would contain x.price = newPrice + 1
, it would probably start to loop. Also it is harder to optimize.
A clean way to solve this would have a central stock and price state, and just derive the others from it:
centralState = mobservable.makeReactive({
stock: 0,
price: 5
});
wareHouse = mobservable.makeReactive({
stock: function() {
return centralState.stock
},
price: function() {
return centralState.price
},
setStock(newStock) {
centralState.stock = newStock;
},
setPrice(newPrice) {
centralState.price = newPrice;
}
});
That would completely avoid the need for the side effects. The set functions can probably be turned into ES5 setter for more convenient invocation.
But it depends on your use case whether you want to have such a central state. Would the above solution suffice for you? If you are creating a distributed system, the original solution makes sense and will work (and keep working), because it turns the problem in an asynchronous problem. But again, beware of unending loops in such case. But within a single process, data should either be derived data, or mutable state, but not both at the same time. Just to ease the mental model of your app.
from mobx.
Thanks.
I thought of the 'loop' problem and I'm fine with it (it could happen if the syncing between objects does some rounding so that the "declared equivalence" between the objects will never be 100% met).
Is there any other potential problem in the above approach?
I thought of having a 'star topology' but it'd be better not to have to sick to any particular topology. Furthermore, to the best of my understanding the code you provided above still need something like 'mobservable.transaction' to avoid triggering changes more then the "absolute minimum number of times". Also, wareHouse is aware of the centralState (i.e. tight coupling).
In the best of worlds. I'd have objects that are totally unaware of each other and all syncing happens with the help of an "external broker" in the minimum number of steps 🎱.
from mobx.
Yeah I can definitely see your use-case. I think the safest way to protect against looping is to update a version number upon change or distribution, and check whether your already up-to date before applying changes.
I'll close the issue for now, but don't hesitate to comment if new issues arise.
from mobx.
One last comment if you will. Should I conclude that the approach I've chosen is good to go?
PS: Indeed I will have to use versioning to avoid extra steps or even infinite loops.
from mobx.
Yes, this a valid approach for a distributed system :) For just local
system I think it is recommendable to stay with 'one truth'.
On Fri, Aug 28, 2015 at 12:00 AM, mehdi-cit [email protected]
wrote:
One last comment if you will. Should I conclude that the approach I've
chosen is good to go?
PS: Indeed I will have to use versioning to avoid extra steps or even
infinite loops.—
Reply to this email directly or view it on GitHub
#22 (comment)
.
from mobx.
Related Issues (20)
- Flow JS Types not longer exported in package build? (Upgrade MobX 4 -> 6) HOT 1
- observable is not working when placed inside a function HOT 3
- @action @observable not work in @observer Class Component HOT 4
- Uncaught ReferenceError: process is not defined HOT 1
- mobx-react @inject does not work with new decorators HOT 1
- Using makeAutoObservable before variable initialization doesnt trigger state changes HOT 1
- @action field uses addInitializer from decorator context but that does not exist in the (new) spec HOT 2
- TS complains about @override
- `StubArray` workaround is incompatible with terser's `unsafe_arrows` option HOT 4
- mobx-react-lite component wrapped in `observer` renders twice in latest Next.js HOT 3
- Peer dependency issue for [email protected] HOT 4
- Migration from Mobx 4 > 6 HOT 6
- `trackDerivedFunction` pre-allocates memory for new deps array too aggressively HOT 1
- Invalid/extra warning in observable.box(array)
- Recommended pattern `MyComp = observer(function MyComp() {...})` leads to subtle bug
- Storing a Lit template in a mobx store does not work HOT 3
- Cannot update past `6.10.0` HOT 1
- Supporting of new Set methods in observable Sets HOT 3
- [feature] Investigate running MobX on top of signals standard / polyfill HOT 1
- [eslint-plugin-mobx] ESLint v9 Support
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 mobx.