-
Notifications
You must be signed in to change notification settings - Fork 13
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
[WIP] Avoid forcing useless renders #1569
Draft
paultranvan
wants to merge
3
commits into
master
Choose a base branch
from
fix-store-selector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,6 +119,7 @@ | |
* @property {import("./types").AppMetadata} [appMetadata] - Metadata about the application that will be used in ensureCozyMetadata | ||
* @property {import("./types").ClientCapabilities} [capabilities] - Capabilities sent by the stack | ||
* @property {boolean} [store] - If set to false, the client will not instantiate a Redux store automatically. Use this if you want to merge cozy-client's store with your own redux store. See [here](https://docs.cozy.io/en/cozy-client/react-integration/#1b-use-your-own-redux-store) for more information. | ||
* @property {boolean} [forceHydratation] - If set to true, all documents will be hydrated w.r.t. the provided schema's relationships, even if the relationship does not exist on the doc. | ||
*/ | ||
|
||
/** | ||
|
@@ -1105,7 +1106,7 @@ | |
* Save the document or array of documents into the persisted storage (if any) | ||
* | ||
* @private | ||
* @param {CozyClientDocument | Array<CozyClientDocument>} data - Document or array of documents to be saved | ||
Check warning on line 1109 in packages/cozy-client/src/CozyClient.js GitHub Actions / Build and publish
|
||
* @returns {Promise<void>} | ||
*/ | ||
async persistVirtualDocuments(definition, data) { | ||
|
@@ -1193,7 +1194,7 @@ | |
if (queryDef instanceof QueryDefinition) { | ||
definitions.push(queryDef) | ||
} else { | ||
documents.push(queryDef) | ||
documents.push(doc) | ||
} | ||
} catch { | ||
// eslint-disable-next-line | ||
|
@@ -1309,9 +1310,11 @@ | |
|
||
hydrateRelationships(document, schemaRelationships) { | ||
const methods = this.getRelationshipStoreAccessors() | ||
return mapValues(schemaRelationships, (assoc, name) => | ||
createAssociation(document, assoc, methods) | ||
) | ||
return mapValues(schemaRelationships, (assoc, name) => { | ||
if (this.options?.forceHydratation || document.relationships?.[assoc]) { | ||
return createAssociation(document, assoc, methods) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
|
@@ -1421,13 +1424,29 @@ | |
return queryResults | ||
} | ||
|
||
const data = | ||
const hydratedData = | ||
hydrated && doctype | ||
? this.hydrateDocuments(doctype, queryResults.data) | ||
: queryResults.data | ||
|
||
const relationships = this.schema.getDoctypeSchema(doctype)?.relationships | ||
const relationshipNames = relationships | ||
? Object.keys(relationships) | ||
: null | ||
|
||
// The `data` array contains the hydrated data with the relationships, if any. | ||
// The `storeData` array contains the documents from the store: this is useful to preserve | ||
// referential equality, to be later evaluated to determine whether or not the | ||
// documents had changed. | ||
return { | ||
...queryResults, | ||
data: isSingleDocQuery && singleDocData ? data[0] : data | ||
data: | ||
isSingleDocQuery && singleDocData ? hydratedData[0] : hydratedData, | ||
storeData: | ||
isSingleDocQuery && singleDocData | ||
? queryResults.data[0] | ||
: queryResults.data, | ||
relationshipNames | ||
} | ||
} catch (e) { | ||
logger.warn( | ||
|
@@ -1852,8 +1871,8 @@ | |
* @template {string} T | ||
* | ||
* @param {string} slug - the cozy-app's slug containing the setting (can be 'instance' for global settings) | ||
* @param {T[]} keys - The names of the settings to retrieve | ||
* @returns {Promise<Record<T, any>>} - The value of the requested setting | ||
*/ | ||
async getSettings(slug, keys) { | ||
return getSettings(this, slug, keys) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* Equality check | ||
* | ||
* Note we do not make a shallow equality check on documents, as it is less efficient and should | ||
* not be necessary: the queryResult.data is built by extracting documents from the state, thus | ||
* preserving references. | ||
* | ||
* @param {import("../types").QueryStateResult} queryResA - A query result to compare | ||
* @param {import("../types").QueryStateResult} queryResB - A query result to compare | ||
* @returns | ||
*/ | ||
export const equalityCheckForQuery = (queryResA, queryResB) => { | ||
//console.log('Call equality check : ', queryResA, queryResB) | ||
if (queryResA === queryResB) { | ||
// Referential equality | ||
return true | ||
} | ||
|
||
if ( | ||
typeof queryResA !== 'object' || | ||
queryResA === null || | ||
typeof queryResB !== 'object' || | ||
queryResB === null | ||
) { | ||
// queryResA or queryResB is not an object or null | ||
return false | ||
} | ||
|
||
if (queryResA.id !== queryResB.id) { | ||
return false | ||
} | ||
if (queryResA.fetchStatus !== queryResB.fetchStatus) { | ||
return false | ||
} | ||
|
||
const docsA = queryResA.storeData | ||
const docsB = queryResB.storeData | ||
if (!docsA || !docsB) { | ||
// No data to check | ||
return false | ||
} | ||
if (!Array.isArray(docsA) && !Array.isArray(docsB) && docsA !== docsB) { | ||
// Only one doc | ||
return false | ||
} | ||
|
||
if ( | ||
Array.isArray(docsA) && | ||
Array.isArray(docsB) && | ||
!arraysHaveSameLength(docsA, docsB) | ||
) { | ||
// A document was added or removed | ||
return false | ||
} | ||
|
||
if (Array.isArray(docsA) && Array.isArray(docsB)) { | ||
for (let i = 0; i < docsA.length; i++) { | ||
if (docsA[i] !== docsB[i]) { | ||
// References should be the same for non-updated documents | ||
return false | ||
} | ||
} | ||
} | ||
|
||
if (queryResA.relationshipNames) { | ||
// In case of relationships, we cannot check referential equality, because we | ||
// "hydrate" the data by creating a new instance of the related relationship class. | ||
// Thus, we check the document revision instead. | ||
const hydratedDataA = queryResA.data | ||
const hydratedDataB = queryResB.data | ||
if (!Array.isArray(hydratedDataA) && !Array.isArray(hydratedDataB)) { | ||
// One doc with changed relationship | ||
return revsAreEqual(hydratedDataA, hydratedDataB) | ||
} | ||
if (!arraysHaveSameLength(hydratedDataA, hydratedDataB)) { | ||
// A relationship have been added or removed | ||
return false | ||
} | ||
if (Array.isArray(hydratedDataA) && Array.isArray(hydratedDataB)) { | ||
for (let i = 0; i < hydratedDataA.length; i++) { | ||
for (const name of queryResA.relationshipNames) { | ||
// Check hydrated relationship | ||
const includedA = hydratedDataA[i][name] | ||
const includedB = hydratedDataB[i][name] | ||
if (includedA && includedB) { | ||
if (!revsAreEqual(includedA, includedB)) { | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
|
||
const revsAreEqual = (docA, docB) => { | ||
return docA?._rev === docB?._rev | ||
} | ||
|
||
const arraysHaveSameLength = (arrayA, arrayB) => { | ||
return ( | ||
Array.isArray(arrayA) && | ||
Array.isArray(arrayB) && | ||
arrayA.length === arrayB.length | ||
) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this part of the fix? I don't understand the impact compared to the previous implementation