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

142 LOC · 133 covered · 9 uncovered · 52 ranges · 620 concepts · 29 introducers · 373 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 > /*--------------------------------------------------------------------------------------------- globalStateMerge.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 { IStringDictionary } from '../../../base/common/collections.js';
7 > import * as objects from '../../../base/common/objects.js';
8 > import { ILogService } from '../../log/common/log.js';
9 > import { IStorageValue, SYNC_SERVICE_URL_TYPE } from './userDataSync.js';
10 >
11 > export interface IMergeResult {
12 > local: { added: IStringDictionary<IStorageValue>; removed: string[]; updated: IStringDictionary<IStorageValue> };
13 > remote: { added: string[]; removed: string[]; updated: string[]; all: IStringDictionary<IStorageValue> | null };
14 > }
15 >
16 > export function merge(localStorage: IStringDictionary<IStorageValue>, remoteStorage: IStringDictionary<IStorageValue> | null, baseStorage: IStringDictionary<IStorageValue> | null, storageKeys: { machine: ReadonlyArray<string>; unregistered: ReadonlyArray<string> }, logService: ILogService): IMergeResult {
17 > if (!remoteStorage) { globalStateMerge.ts ×4
18 > return { remote: { added: Object.keys(localStorage), removed: [], updated: [], all: Object.keys(localStorage).length > 0 ? localStorage : null }, local: { added: {}, removed: [], updated: {} } }; globalStateSync.ts ×15
19 > }
21 > const localToRemote = compare(localStorage, remoteStorage);
22 > if (localToRemote.added.size === 0 && localToRemote.removed.size === 0 && localToRemote.updated.size === 0) { globalStateMerge.ts ×4
23 > // No changes found between local and remote. globalStateMerge.ts ×1
24 > return { remote: { added: [], removed: [], updated: [], all: null }, local: { added: {}, removed: [], updated: {} } };
25 > }
27 > const baseToRemote = baseStorage ? compare(baseStorage, remoteStorage) : { added: Object.keys(remoteStorage).reduce((r, k) => { r.add(k); return r; }, new Set<string>()), removed: new Set<string>(), updated: new Set<string>() }; globalStateMerge.ts ×4
28 > const baseToLocal = baseStorage ? compare(baseStorage, localStorage) : { added: Object.keys(localStorage).reduce((r, k) => { r.add(k); return r; }, new Set<string>()), removed: new Set<string>(), updated: new Set<string>() };
29 >
30 > const local: { added: IStringDictionary<IStorageValue>; removed: string[]; updated: IStringDictionary<IStorageValue> } = { added: {}, removed: [], updated: {} };
31 > const remote: IStringDictionary<IStorageValue> = objects.deepClone(remoteStorage);
32 >
33 > const isFirstTimeSync = !baseStorage;
34 >
35 > // Added in local
36 > for (const key of baseToLocal.added.values()) {
37 > // If syncing for first time remote value gets precedence always, globalStateMerge.ts ×1
38 > // except for sync service type key - local value takes precedence for this key
39 > if (key !== SYNC_SERVICE_URL_TYPE && isFirstTimeSync && baseToRemote.added.has(key)) {
40 > continue; globalStateMerge.ts ×1
41 > }
43 > remote[key] = localStorage[key];
44 > }
46 > // Updated in local
47 > for (const key of baseToLocal.updated.values()) {
48 > remote[key] = localStorage[key]; globalStateMerge.ts ×1
49 > }
51 > // Removed in local
52 > for (const key of baseToLocal.removed.values()) {
53 > // Do not remove from remote if key is not registered. globalStateMerge.ts ×1
54 > if (storageKeys.unregistered.includes(key)) {
55 > continue; globalStateMerge.ts ×1
56 > }
57 > delete remote[key]; globalStateMerge.ts ×1
58 > }
60 > // Added in remote
61 > for (const key of baseToRemote.added.values()) {
62 > const remoteValue = remoteStorage[key]; globalStateMerge.ts ×5
63 > if (storageKeys.machine.includes(key)) {
64 > logService.info(`GlobalState: Skipped adding ${key} in local storage because it is declared as machine scoped.`); globalStateMerge.ts ×1
65 > continue;
66 > }
67 > // Skip if the value is also added in local from the time it is last synced globalStateMerge.ts ×5
68 > if (baseStorage && baseToLocal.added.has(key)) {
69 continue;
70 }
71 > const localValue = localStorage[key]; globalStateMerge.ts ×5
72 > if (localValue && localValue.value === remoteValue.value) {
73 > continue; globalStateMerge.ts ×1
74 > }
76 > // Local sync service type value takes precedence if syncing for first time
77 > if (key === SYNC_SERVICE_URL_TYPE && isFirstTimeSync && baseToLocal.added.has(key)) { globalStateMerge.ts ×5
78 continue;
79 }
81 > if (localValue) {
82 > local.updated[key] = remoteValue; globalStateMerge.ts ×1
83 > } else { globalStateMerge.ts ×3
84 > local.added[key] = remoteValue; globalStateMerge.ts ×1
85 > }
88 > // Updated in Remote
89 > for (const key of baseToRemote.updated.values()) {
90 > const remoteValue = remoteStorage[key]; globalStateMerge.ts ×3
91 > if (storageKeys.machine.includes(key)) {
92 > logService.info(`GlobalState: Skipped updating ${key} in local storage because it is declared as machine scoped.`); globalStateMerge.ts ×1
93 > continue;
94 > }
95 > // Skip if the value is also updated or removed in local globalStateMerge.ts ×1
96 > if (baseToLocal.updated.has(key) || baseToLocal.removed.has(key)) { globalStateMerge.ts ×3
97 > continue; globalStateMerge.ts ×1
98 > }
99 > const localValue = localStorage[key]; globalStateMerge.ts ×2
100 > if (localValue && localValue.value === remoteValue.value) { globalStateMerge.ts ×3
101 continue;
102 }
103 > local.updated[key] = remoteValue; globalStateMerge.ts ×2
104 > }
106 > // Removed in remote
107 > for (const key of baseToRemote.removed.values()) {
108 > if (storageKeys.machine.includes(key)) { globalStateMerge.ts ×2
109 logService.trace(`GlobalState: Skipped removing ${key} in local storage because it is declared as machine scoped.`);
110 continue;
111 }
112 > // Skip if the value is also updated or removed in local globalStateMerge.ts ×2
113 > if (baseToLocal.updated.has(key) || baseToLocal.removed.has(key)) {
114 > continue; globalStateMerge.ts ×1
115 > }
116 > local.removed.push(key); globalStateMerge.ts ×1
117 > }
119 > const result = compare(remoteStorage, remote);
120 > return { local, remote: { added: [...result.added], updated: [...result.updated], removed: [...result.removed], all: result.added.size === 0 && result.removed.size === 0 && result.updated.size === 0 ? null : remote } }; globalStateMerge.ts ×4
121 > }
123 > function compare(from: IStringDictionary<any>, to: IStringDictionary<any>): { added: Set<string>; removed: Set<string>; updated: Set<string> } { globalStateMerge.ts ×4
124 > const fromKeys = Object.keys(from);
125 > const toKeys = Object.keys(to);
126 > const added = toKeys.filter(key => !fromKeys.includes(key)).reduce((r, key) => { r.add(key); return r; }, new Set<string>());
127 > const removed = fromKeys.filter(key => !toKeys.includes(key)).reduce((r, key) => { r.add(key); return r; }, new Set<string>());
128 > const updated: Set<string> = new Set<string>();
129 >
130 > for (const key of fromKeys) {
131 > if (removed.has(key)) {
132 > continue; globalStateMerge.ts ×1
133 > }
134 > const value1 = from[key]; globalStateMerge.ts ×4
135 > const value2 = to[key];
136 > if (!objects.equals(value1, value2)) {
137 > updated.add(key); globalStateMerge.ts ×1
138 > }
140 >
141 > return { added, removed, updated };
142 > }