From 19cda93450607b18ddbf437ba3407ba746ed7ce6 Mon Sep 17 00:00:00 2001 From: SunriseFox Date: Sat, 23 Nov 2019 12:59:29 +0800 Subject: [PATCH] fix: message loop on multi-tabs --- .../defaults/emptyDefinition.ts | 7 +++-- src/social-network/defaults/ui.ts | 7 +++-- src/utils/comparer.ts | 30 +++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 src/utils/comparer.ts diff --git a/src/social-network/defaults/emptyDefinition.ts b/src/social-network/defaults/emptyDefinition.ts index 542aa09e1d26..393c17915525 100644 --- a/src/social-network/defaults/emptyDefinition.ts +++ b/src/social-network/defaults/emptyDefinition.ts @@ -3,6 +3,7 @@ import { PersonIdentifier } from '../../database/type' import { SocialNetworkUIDefinition } from '../ui' import { nop, nopWithUnmount } from '../../utils/utils' import { Person, Group } from '../../database' +import { PersonArrayComparer, GroupArrayComparer } from '../../utils/comparer' /** * DO NOT use this in content script @@ -14,8 +15,8 @@ export const emptyDefinition: SocialNetworkUIDefinition = { shouldActivate() { return false }, - myIdentitiesRef: new ValueRef([] as Person[]), - groupsRef: new ValueRef([] as Group[]), + myIdentitiesRef: new ValueRef([] as Person[], PersonArrayComparer), + groupsRef: new ValueRef([] as Group[], GroupArrayComparer), lastRecognizedIdentity: new ValueRef({ identifier: PersonIdentifier.unknown }), currentIdentity: new ValueRef(null), init() { @@ -34,7 +35,7 @@ export const emptyDefinition: SocialNetworkUIDefinition = { return false }, posts: new Map(), - friendsRef: new ValueRef([] as Person[]), + friendsRef: new ValueRef([] as Person[], PersonArrayComparer), isDangerousNetwork: false, isValidUsername() { return true diff --git a/src/social-network/defaults/ui.ts b/src/social-network/defaults/ui.ts index d744e4463185..48e8aba242f8 100644 --- a/src/social-network/defaults/ui.ts +++ b/src/social-network/defaults/ui.ts @@ -6,11 +6,12 @@ import { PersonIdentifier } from '../../database/type' import { cloneDeep } from 'lodash-es' import { Person, Group } from '../../database' import { MaskbookDarkTheme, MaskbookLightTheme } from '../../utils/theme' +import { PersonArrayComparer, GroupArrayComparer } from '../../utils/comparer' const defaultDataSources: Required = cloneDeep({ - friendsRef: new ValueRef([] as Person[]), - myIdentitiesRef: new ValueRef([] as Person[]), - groupsRef: new ValueRef([] as Group[]), + friendsRef: new ValueRef([] as Person[], PersonArrayComparer), + myIdentitiesRef: new ValueRef([] as Person[], PersonArrayComparer), + groupsRef: new ValueRef([] as Group[], GroupArrayComparer), currentIdentity: new ValueRef(null), lastRecognizedIdentity: new ValueRef({ identifier: PersonIdentifier.unknown }), posts: new Map(), diff --git a/src/utils/comparer.ts b/src/utils/comparer.ts new file mode 100644 index 000000000000..032ae1fb2335 --- /dev/null +++ b/src/utils/comparer.ts @@ -0,0 +1,30 @@ +import { Group, Person } from '../database' + +export const PersonArrayComparer = (a: Person[], b: Person[]) => { + if (a.length !== b.length) return false + return a.every((person, index) => { + const target = b[index] + if (!person.identifier.equals(target.identifier)) return false + if (person.avatar !== target.avatar) return false + if (person.fingerprint !== target.fingerprint) return false + if (person.groups.length !== target.groups.length) return false + if (person.nickname !== target.nickname) return false + if (person.previousIdentifiers?.length !== target.previousIdentifiers?.length) return false + if (!person.groups.every((group, index) => target.groups[index].equals(group))) return false + return true + }) +} + +export function GroupArrayComparer(a: Group[], b: Group[]) { + if (a.length !== b.length) return false + return a.every((group, index) => { + const target = b[index] + if (!group.identifier.equals(target.identifier)) return false + if (group.avatar !== target.avatar) return false + if (group.banned !== target.banned) return false + if (group.groupName !== target.groupName) return false + if (group.members.length !== target.members.length) return false + if (!group.members.every((person, index) => target.members[index].equals(person))) return false + return true + }) +}