Comments (10)
For whatever it is worth, when sync: true
is passed to the derive the issue seems to not get triggered.
from valtio.
Hmmm, it sounds like a bug. Probably a tough one.
Looks like there are two derived states with circular structure.
Hope someone can dig into it.
from valtio.
Still trying to wrap my head around what's happening, but just going to share what I got:
After initialization of the store the pendingCount
in the sourceObjectEntry
for byId.b
ends up as -4.
store.byId.a = createItem('a', store);
store.byId.b = createItem('b', store);
await time();
console.log(
sourceObjectMap.get(store.byId.b).pendingCount
); // -4
What seems to happen is that at the time of the store.byId.a
assignment store.byId.b
of course does not yet exist, so markPending
only gets executed for the already existing subscriptions. However once the unmarkPending
gets executed for the store.byId.a
assignment on the next tick, the store.byId.b
assignment already happened, thus unmarkPending
gets executed a bunch of extra times without a corresponding markPending
call ever happening.
Execution order as far as I understand it is:
store.byId.a
assignment- markPending
store.byId.a
only store.byId.b
assignment- unmarkPending BOTH
store.byId.a
andstore.byId.b
Thus waiting a tick between store.byId.a =
and store.byId.b =
causes the issue not to trigger.
store.byId.a = createItem('a', store);
await time();
store.byId.b = createItem('b', store);
A solution that might work is to make a shallow copy of the subscriptions before the subscription.promise = Promise.resolve().then
, but I am far from sure whether that wouldn't have other unintended consequences. Definitely need to check out the entire library and run the unit tests against it at the very least, but would love some feedback whether that does or doesn't sound reasonable.
from valtio.
Plus, I haven't yet mentally modelled out why commenting out the isSelected
derived property made the isHovered
derived property work, so that's something I will have to look into as well~
from valtio.
@DavidMulder0 Hey, have you figured this out? I am experiencing the same issue and its pretty hard to wrap the head around the source code, would be helpful if you can give me a headstart
from valtio.
I have the same problem as well! After a VERY cursory look, I'm not sure what the point of running without {sync: true}
is. It seems like the difference is just:
if (notifyInSync) {
unmarkPending(sourceObject)
} else {
subscription.p = Promise.resolve().then(() => {
delete subscription.p // promise
unmarkPending(sourceObject)
})
}
What's the objective of {sync: false}
?
from valtio.
With sync
, batching is disabled, which causes too many updates in some cases, possibly dropping performance.
from valtio.
Alright, that's reasonable. Just throwing {sync: true}
into my code actually made a bunch of things crash. The workaround I'm using for now is
const state = derive({
derived: (get) => {
...
return {
property1: ...,
property2: ...,
}
}
I was also attempting derive state from 2 separate proxy objects. For that, I've just created a third proxy object, which I update with watch
, which doesn't seem to have the same problems.
from valtio.
btw, here's an RFC related to this issue: #792
from valtio.
Transferred to: valtiojs/derive-valtio#2
from valtio.
Related Issues (20)
- `use-sync-external-store` should be marked as a `peerDep` HOT 7
- synchronous partial state update Interface HOT 1
- Consuming a valtio proxy from another package results in "TypeError: proxyState is not iterable" HOT 1
- Cache key could refer previous value HOT 18
- About Rerendering ,Is it a bug or my usage error,Request help HOT 1
- There is a defect in the ref in proxyWithHistory HOT 1
- Typescript & devtools HOT 1
- Vanilla `subscribe` does not trigger when a computed getter is updated HOT 2
- Branded primitive types are incompatible with Snapshot<T> HOT 8
- Snapshots can be mutated by adding or removing properties HOT 2
- bug when filtering data
- replace object not trigger render HOT 3
- defineProperty trap makes property non-CEW in React Native (Hermes engine) HOT 10
- proxyState is not iterable
- tsc fails when using functions in valitio/utils
- use class throw error
- [Warning] Please install/enable Redux devtools extension
- Support option to disable render optimization in useSnapshot HOT 1
- Performance Degradation in proxySet Instances with Increased Keys. HOT 1
- Why my code doesn't work, just getter/setter, cause ui cannot listen the state change
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 valtio.