From 480de10ff3c3869b4af6d3a03f6992bf954f0da5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Tue, 16 Apr 2019 09:43:19 +0200 Subject: [PATCH] [FIX] store: properly compare values on store updates Before this commit, we tried to use a hash function for non observable values, but this was not correct, since some changes could be done that would compensate each other. With this commit, we properly compare each non observable value. --- src/store.ts | 54 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/store.ts b/src/store.ts index b1beb59c..738bbe3b 100644 --- a/src/store.ts +++ b/src/store.ts @@ -273,17 +273,22 @@ export function connect(mapStateToProps, options: any = {}) { if (!hashFunction) { let deep = "deep" in options ? options.deep : true; let defaultRevFunction = deep ? deepRevNumber : revNumber; - hashFunction = function({ storeProps, currentStoreProps }) { + hashFunction = function({ storeProps }, options) { + const { currentStoreProps } = options; if ("__owl__" in storeProps) { return defaultRevFunction(storeProps); } let hash = 0; - let currentProps = currentStoreProps; for (let key in storeProps) { const val = storeProps[key]; - hash += - defaultRevFunction(storeProps[key]) || - (val !== currentProps[key] ? 1 : 0); + const hashVal = defaultRevFunction(val); + if (hashVal === 0) { + if (val !== currentStoreProps[key]) { + options.didChange = true; + } + } else { + hash += hashVal; + } } return hash; }; @@ -299,26 +304,35 @@ export function connect(mapStateToProps, options: any = {}) { super(parent, mergedProps); this.__owl__.ownProps = ownProps; this.__owl__.currentStoreProps = storeProps; - this.__owl__.storeHash = hashFunction({ - state: env.store.state, - storeProps: storeProps, - currentStoreProps: storeProps, - revNumber, - deepRevNumber - }); + this.__owl__.storeHash = hashFunction( + { + state: env.store.state, + storeProps: storeProps, + revNumber, + deepRevNumber + }, + { + currentStoreProps: storeProps + } + ); } mounted() { this.env.store.on("update", this, () => { const ownProps = this.__owl__.ownProps; const storeProps = mapStateToProps(this.env.store.state, ownProps); - let didChange = false; - const storeHash = hashFunction({ - state: this.env.store.state, - storeProps: storeProps, - currentStoreProps: this.__owl__.currentStoreProps, - revNumber, - deepRevNumber - }); + const options: any = { + currentStoreProps: this.__owl__.currentStoreProps + }; + const storeHash = hashFunction( + { + state: this.env.store.state, + storeProps: storeProps, + revNumber, + deepRevNumber + }, + options + ); + let didChange = options.didChange; if (storeHash !== this.__owl__.storeHash) { didChange = true; this.__owl__.storeHash = storeHash;