src/vs/platform/userDataSync/common/userDataProfilesManifestMerge.ts

137 LOC · 135 covered · 2 uncovered · 47 ranges · 611 concepts · 21 introducers · 356 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- userDataProfilesManifestMerge.ts ×2
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { equals } from '../../../base/common/objects.js';
7 > import { IUserDataProfile, UseDefaultProfileFlags } from '../../userDataProfile/common/userDataProfile.js';
8 > import { ISyncUserDataProfile } from './userDataSync.js';
9 >
10 > interface IRelaxedMergeResult {
11 > local: { added: ISyncUserDataProfile[]; removed: IUserDataProfile[]; updated: ISyncUserDataProfile[] };
12 > remote: { added: IUserDataProfile[]; removed: ISyncUserDataProfile[]; updated: IUserDataProfile[] } | null;
13 > }
14 >
15 > export type IMergeResult = Required<IRelaxedMergeResult>;
16 >
17 > interface IUserDataProfileInfo {
18 > readonly id: string;
19 > readonly name: string;
20 > readonly icon?: string;
21 > readonly useDefaultFlags?: UseDefaultProfileFlags;
22 > }
23 >
24 > export function merge(local: IUserDataProfile[], remote: ISyncUserDataProfile[] | null, lastSync: ISyncUserDataProfile[] | null, ignored: string[]): IMergeResult {
25 > const localResult: { added: ISyncUserDataProfile[]; removed: IUserDataProfile[]; updated: ISyncUserDataProfile[] } = { added: [], removed: [], updated: [] }; userDataProfilesManifestMerge.ts ×3
26 > let remoteResult: { added: IUserDataProfile[]; removed: ISyncUserDataProfile[]; updated: IUserDataProfile[] } | null = { added: [], removed: [], updated: [] };
27 >
28 > if (!remote) {
29 > const added = local.filter(({ id }) => !ignored.includes(id)); userDataProfilesManifestMerge.ts ×3
30 > if (added.length) {
31 > remoteResult.added = added; userDataProfilesManifestMerge.ts ×1
33 > remoteResult = null; userDataProfilesManifestMerge.ts ×1
34 > }
36 > local: localResult,
37 > remote: remoteResult
38 > };
39 > }
41 > const localToRemote = compare(local, remote, ignored);
42 > if (localToRemote.added.length > 0 || localToRemote.removed.length > 0 || localToRemote.updated.length > 0) { userDataProfilesManifestMerge.ts ×3
44 > const baseToLocal = compare(lastSync, local, ignored);
45 > const baseToRemote = compare(lastSync, remote, ignored);
46 >
47 > // Remotely removed profiles
48 > for (const id of baseToRemote.removed) {
49 > const e = local.find(profile => profile.id === id); userDataProfilesManifestMerge.ts ×4
50 > if (e) {
51 > localResult.removed.push(e); userDataProfilesManifestMerge.ts ×2
52 > }
55 > // Remotely added profiles
56 > for (const id of baseToRemote.added) {
57 > const remoteProfile = remote.find(profile => profile.id === id)!; userDataProfilesManifestMerge.ts ×3
58 > // Got added in local
59 > if (baseToLocal.added.includes(id)) {
60 > // Is different from local to remote userDataProfilesManifestMerge.ts ×1
61 > if (localToRemote.updated.includes(id)) {
62 > // Remote wins always
63 > localResult.updated.push(remoteProfile);
64 > }
66 > localResult.added.push(remoteProfile); userDataProfilesManifestMerge.ts ×1
67 > }
70 > // Remotely updated profiles
71 > for (const id of baseToRemote.updated) {
72 > // Remote wins always userDataProfilesManifestMerge.ts ×1
73 > localResult.updated.push(remote.find(profile => profile.id === id)!);
74 > }
76 > // Locally added profiles
77 > for (const id of baseToLocal.added) {
78 > // Not there in remote userDataProfilesManifestMerge.ts ×2
79 > if (!baseToRemote.added.includes(id)) {
80 > remoteResult.added.push(local.find(profile => profile.id === id)!); userDataProfilesManifestMerge.ts ×1
81 > }
84 > // Locally updated profiles
85 > for (const id of baseToLocal.updated) {
86 > // If removed in remote userDataProfilesManifestMerge.ts ×2
87 > if (baseToRemote.removed.includes(id)) {
88 continue;
89 }
91 > // If not updated in remote
92 > if (!baseToRemote.updated.includes(id)) {
93 > remoteResult.updated.push(local.find(profile => profile.id === id)!);
94 > }
95 > }
97 > // Locally removed profiles
98 > for (const id of baseToLocal.removed) {
99 > const removedProfile = remote.find(profile => profile.id === id); userDataProfilesManifestMerge.ts ×4
100 > if (removedProfile) {
101 > remoteResult.removed.push(removedProfile); userDataProfilesManifestMerge.ts ×2
102 > }
106 > if (remoteResult.added.length === 0 && remoteResult.removed.length === 0 && remoteResult.updated.length === 0) { userDataProfilesManifestMerge.ts ×3
107 > remoteResult = null; userDataProfilesManifestMerge.ts ×1
108 > }
110 > return { local: localResult, remote: remoteResult };
111 > }
113 > function compare(from: IUserDataProfileInfo[] | null, to: IUserDataProfileInfo[], ignoredProfiles: string[]): { added: string[]; removed: string[]; updated: string[] } { userDataProfilesManifestMerge.ts ×5
114 > from = from ? from.filter(({ id }) => !ignoredProfiles.includes(id)) : [];
115 > to = to.filter(({ id }) => !ignoredProfiles.includes(id));
116 > const fromKeys = from.map(({ id }) => id);
117 > const toKeys = to.map(({ id }) => id);
118 > const added = toKeys.filter(key => !fromKeys.includes(key));
119 > const removed = fromKeys.filter(key => !toKeys.includes(key));
120 > const updated: string[] = [];
121 >
122 > for (const { id, name, icon, useDefaultFlags } of from) {
123 > if (removed.includes(id)) { userDataProfilesManifestMerge.ts ×4
125 > }
126 > const toProfile = to.find(p => p.id === id); userDataProfilesManifestMerge.ts ×4
127 > if (!toProfile
128 > || toProfile.name !== name
129 > || toProfile.icon !== icon userDataProfilesManifestMerge.ts ×1
130 > || !equals(toProfile.useDefaultFlags, useDefaultFlags)
132 > updated.push(id); userDataProfilesManifestMerge.ts ×1
133 > }
136 > return { added, removed, updated };
137 > }