|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information. |
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 |
|
6 | | -import { SyncStatus, IUserDataSyncStoreService, IUserDataSyncLogService, IGlobalState, SyncResource, IUserDataSynchroniser, IUserDataSyncEnablementService, IUserDataSyncBackupStoreService, ISyncResourceHandle, IStorageValue, ISyncPreviewResult } from 'vs/platform/userDataSync/common/userDataSync'; |
| 6 | +import { SyncStatus, IUserDataSyncStoreService, IUserDataSyncLogService, IGlobalState, SyncResource, IUserDataSynchroniser, IUserDataSyncEnablementService, IUserDataSyncBackupStoreService, ISyncResourceHandle, IStorageValue, ISyncPreviewResult, USER_DATA_SYNC_SCHEME } from 'vs/platform/userDataSync/common/userDataSync'; |
7 | 7 | import { VSBuffer } from 'vs/base/common/buffer'; |
8 | 8 | import { Event } from 'vs/base/common/event'; |
9 | 9 | import { IEnvironmentService } from 'vs/platform/environment/common/environment'; |
10 | | -import { dirname, joinPath, basename } from 'vs/base/common/resources'; |
| 10 | +import { dirname, joinPath, basename, isEqual } from 'vs/base/common/resources'; |
11 | 11 | import { IFileService } from 'vs/platform/files/common/files'; |
12 | 12 | import { IStringDictionary } from 'vs/base/common/collections'; |
13 | 13 | import { edit } from 'vs/platform/userDataSync/common/content'; |
@@ -41,6 +41,7 @@ interface ILastSyncUserData extends IRemoteUserData { |
41 | 41 |
|
42 | 42 | export class GlobalStateSynchroniser extends AbstractSynchroniser implements IUserDataSynchroniser { |
43 | 43 |
|
| 44 | + private static readonly GLOBAL_STATE_DATA_URI = URI.from({ scheme: USER_DATA_SYNC_SCHEME, authority: 'globalState', path: `/current.json` }); |
44 | 45 | protected readonly version: number = 1; |
45 | 46 |
|
46 | 47 | constructor( |
@@ -139,28 +140,44 @@ export class GlobalStateSynchroniser extends AbstractSynchroniser implements IUs |
139 | 140 | async stop(): Promise<void> { } |
140 | 141 |
|
141 | 142 | async getAssociatedResources({ uri }: ISyncResourceHandle): Promise<{ resource: URI, comparableResource?: URI }[]> { |
142 | | - return [{ resource: joinPath(uri, 'globalState.json') }]; |
| 143 | + return [{ resource: joinPath(uri, 'globalState.json'), comparableResource: GlobalStateSynchroniser.GLOBAL_STATE_DATA_URI }]; |
143 | 144 | } |
144 | 145 |
|
145 | 146 | async resolveContent(uri: URI): Promise<string | null> { |
| 147 | + if (isEqual(uri, GlobalStateSynchroniser.GLOBAL_STATE_DATA_URI)) { |
| 148 | + const localGlobalState = await this.getLocalGlobalState(); |
| 149 | + return this.format(localGlobalState); |
| 150 | + } |
| 151 | + |
146 | 152 | let content = await super.resolveContent(uri); |
147 | 153 | if (content) { |
148 | 154 | return content; |
149 | 155 | } |
| 156 | + |
150 | 157 | content = await super.resolveContent(dirname(uri)); |
151 | 158 | if (content) { |
152 | 159 | const syncData = this.parseSyncData(content); |
153 | 160 | if (syncData) { |
154 | 161 | switch (basename(uri)) { |
155 | 162 | case 'globalState.json': |
156 | | - const edits = format(syncData.content, undefined, {}); |
157 | | - return applyEdits(syncData.content, edits); |
| 163 | + return this.format(JSON.parse(syncData.content)); |
158 | 164 | } |
159 | 165 | } |
160 | 166 | } |
| 167 | + |
161 | 168 | return null; |
162 | 169 | } |
163 | 170 |
|
| 171 | + private format(globalState: IGlobalState): string { |
| 172 | + const storageKeys = Object.keys(globalState.storage).sort(); |
| 173 | + const storage: IStringDictionary<IStorageValue> = {}; |
| 174 | + storageKeys.forEach(key => storage[key] = globalState.storage[key]); |
| 175 | + globalState.storage = storage; |
| 176 | + const content = JSON.stringify(globalState); |
| 177 | + const edits = format(content, undefined, {}); |
| 178 | + return applyEdits(content, edits); |
| 179 | + } |
| 180 | + |
164 | 181 | async acceptConflict(conflict: URI, content: string): Promise<void> { |
165 | 182 | throw new Error(`${this.syncResourceLogLabel}: Conflicts should not occur`); |
166 | 183 | } |
|
0 commit comments