Repository navigation
[Fix #269] use cached data #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1998ca0
09b9494
0f5e115
98a1672
916b9b0
35becc3
4b7f267
6943d36
790def1
f71294e
9358b41
3ee9294
fcc49ac
853390e
0c72be1
294aa31
d2a5390
5b00715
8a3bc95
bf48eb3
fbc16fc
48570e8
68c3d04
937c3f7
cb505e3
46f73e1
a2e20d0
617ff64
5ffdefa
2c45edb
d479c12
020ccb2
0756a27
7904d58
c7ac445
127f56c
9a4135f
9a41243
cdb9e82
1caf3f9
ad4758d
e531270
69fac60
ff82a94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -37,13 +37,25 @@ export default function (mapOnyxToState) { | |||
| // disconnected. It is a key value store with the format {[mapping.key]: connectionID}. | ||||
| this.activeConnectionIDs = {}; | ||||
|
|
||||
| const cachedState = _.reduce(mapOnyxToState, (resultObj, mapping, propertyName) => { | ||||
| const key = Str.result(mapping.key, props); | ||||
| const value = Onyx.tryGetCachedValue(key, mapping); | ||||
|
|
||||
| if (value !== undefined) { | ||||
| // eslint-disable-next-line no-param-reassign | ||||
| resultObj[propertyName] = value; | ||||
| } | ||||
|
|
||||
| return resultObj; | ||||
| }, {}); | ||||
|
|
||||
| // If we have all the data we need, then we can render the component immediately | ||||
| cachedState.loading = _.size(cachedState) < requiredKeysForInit.length; | ||||
|
|
||||
| // Object holding the temporary initial state for the component while we load the various Onyx keys | ||||
| this.tempState = {}; | ||||
| this.tempState = cachedState; | ||||
|
Comment on lines
55
to
+56
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure why do we set the tempState to the same cachedState as the normal state. I might be missing something but could you help me understand this? Should we update the comment above?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The intention here is that If we did not get all the values from the cache then the values are held in the Though the question did make me wonder about something... if react-native-onyx/lib/withOnyx.js Line 117 in 80c6f00
Seems like that would be undesirable in the case where you did not initialize all the values in the constructor.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the write up. @hannojg what do you think of the concern Marc raised above? |
||||
|
|
||||
| this.state = { | ||||
| // If there are no required keys for init then we can render the wrapped component immediately | ||||
| loading: requiredKeysForInit.length > 0, | ||||
| }; | ||||
| this.state = cachedState; | ||||
| } | ||||
|
|
||||
| componentDidMount() { | ||||
|
|
@@ -60,7 +72,6 @@ export default function (mapOnyxToState) { | |||
| _.each(mapOnyxToState, (mapping, propertyName) => { | ||||
| const previousKey = Str.result(mapping.key, prevProps); | ||||
| const newKey = Str.result(mapping.key, this.props); | ||||
|
|
||||
| if (previousKey !== newKey) { | ||||
| Onyx.disconnect(this.activeConnectionIDs[previousKey], previousKey); | ||||
| delete this.activeConnectionIDs[previousKey]; | ||||
|
|
@@ -88,6 +99,16 @@ export default function (mapOnyxToState) { | |||
| * @param {*} val | ||||
| */ | ||||
| setWithOnyxState(statePropertyName, val) { | ||||
| // We might have loaded the values for the onyx keys/mappings already from the cache. | ||||
| // In case we were able to load all the values upfront, the loading state will be false. | ||||
| // However, Onyx.js will always call setWithOnyxState, as it doesn't know that this implementation | ||||
| // already loaded the values from cache. Thus we have to check whether the value has changed | ||||
| // before we set the state to prevent unnecessary renders. | ||||
| const prevValue = this.state[statePropertyName]; | ||||
| if (!this.state.loading && prevValue === val) { | ||||
| return; | ||||
| } | ||||
|
|
||||
| if (!this.state.loading) { | ||||
| this.setState({[statePropertyName]: val}); | ||||
| return; | ||||
|
|
@@ -100,7 +121,14 @@ export default function (mapOnyxToState) { | |||
| return; | ||||
| } | ||||
|
|
||||
| this.setState({...this.tempState, loading: false}); | ||||
| const stateUpdate = {...this.tempState, loading: false}; | ||||
|
|
||||
| // The state is set here manually, instead of using setState because setState is an async operation, meaning it might execute on the next tick, | ||||
| // or at a later point in the microtask queue. That can lead to a race condition where | ||||
| // setWithOnyxState is called before the state is actually set. This results in unreliable behavior when checking the loading state and has been mainly observed on fabric. | ||||
| this.state = stateUpdate; | ||||
|
|
||||
| this.setState(stateUpdate); // Trigger a render | ||||
| delete this.tempState; | ||||
| } | ||||
|
|
||||
|
|
||||
Uh oh!
There was an error while loading. Please reload this page.