You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-07-31 15:24:23 +03:00
utils: Fix bug in deepCompare which would incorrectly return objects with disjoint keys as equal (#2586)
* utils: Fix bug in deepCompare which would incorrectly return objects with disjoint keys as equal * Fix bugs in sync test This test wrongly asserted that `initialSyncLimit` would be used to make a filter It is used only for the initial sync inline filter, and not in POST /filter Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
@ -427,7 +427,7 @@ describe("MatrixClient", function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
await client.startClient();
|
await client.startClient({ filter });
|
||||||
await syncPromise;
|
await syncPromise;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -152,6 +152,9 @@ describe("utils", function() {
|
|||||||
assert.isTrue(utils.deepCompare({ a: 1, b: 2 }, { a: 1, b: 2 }));
|
assert.isTrue(utils.deepCompare({ a: 1, b: 2 }, { a: 1, b: 2 }));
|
||||||
assert.isTrue(utils.deepCompare({ a: 1, b: 2 }, { b: 2, a: 1 }));
|
assert.isTrue(utils.deepCompare({ a: 1, b: 2 }, { b: 2, a: 1 }));
|
||||||
assert.isFalse(utils.deepCompare({ a: 1, b: 2 }, { a: 1, b: 3 }));
|
assert.isFalse(utils.deepCompare({ a: 1, b: 2 }, { a: 1, b: 3 }));
|
||||||
|
assert.isFalse(utils.deepCompare({ a: 1, b: 2 }, { a: 1 }));
|
||||||
|
assert.isFalse(utils.deepCompare({ a: 1 }, { a: 1, b: 2 }));
|
||||||
|
assert.isFalse(utils.deepCompare({ a: 1 }, { b: 1 }));
|
||||||
|
|
||||||
assert.isTrue(utils.deepCompare({
|
assert.isTrue(utils.deepCompare({
|
||||||
1: { name: "mhc", age: 28 },
|
1: { name: "mhc", age: 28 },
|
||||||
|
@ -102,7 +102,7 @@ const MSC2716_ROOM_VERSIONS = [
|
|||||||
function getFilterName(userId: string, suffix?: string): string {
|
function getFilterName(userId: string, suffix?: string): string {
|
||||||
// scope this on the user ID because people may login on many accounts
|
// scope this on the user ID because people may login on many accounts
|
||||||
// and they all need to be stored!
|
// and they all need to be stored!
|
||||||
return `FILTER_SYNC_${userId}` + suffix ? "_" + suffix : "";
|
return `FILTER_SYNC_${userId}` + (suffix ? "_" + suffix : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
function debuglog(...params) {
|
function debuglog(...params) {
|
||||||
|
17
src/utils.ts
17
src/utils.ts
@ -238,33 +238,24 @@ export function deepCompare(x: any, y: any): boolean {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// disable jshint "The body of a for in should be wrapped in an if
|
|
||||||
// statement"
|
|
||||||
/* jshint -W089 */
|
|
||||||
|
|
||||||
// check that all of y's direct keys are in x
|
// check that all of y's direct keys are in x
|
||||||
let p;
|
for (const p in y) {
|
||||||
for (p in y) {
|
|
||||||
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
|
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// finally, compare each of x's keys with y
|
// finally, compare each of x's keys with y
|
||||||
for (p in y) { // eslint-disable-line guard-for-in
|
for (const p in x) {
|
||||||
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
|
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p) || !deepCompare(x[p], y[p])) {
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!deepCompare(x[p], y[p])) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* jshint +W089 */
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dev note: This returns a tuple, but jsdoc doesn't like that. https://github.com/jsdoc/jsdoc/issues/1703
|
// Dev note: This returns an array of tuples, but jsdoc doesn't like that. https://github.com/jsdoc/jsdoc/issues/1703
|
||||||
/**
|
/**
|
||||||
* Creates an array of object properties/values (entries) then
|
* Creates an array of object properties/values (entries) then
|
||||||
* sorts the result by key, recursively. The input object must
|
* sorts the result by key, recursively. The input object must
|
||||||
|
Reference in New Issue
Block a user