Skip to content

Commit

Permalink
#93960 Ability to compare globalState with current state
Browse files Browse the repository at this point in the history
  • Loading branch information
sandy081 committed May 11, 2020
1 parent 5deb015 commit 71bb111
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/vs/platform/userDataSync/common/extensionsSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface ILastSyncUserData extends IRemoteUserData {

export class ExtensionsSynchroniser extends AbstractSynchroniser implements IUserDataSynchroniser {

private static readonly EXTENSIONS_DATA_URI = URI.from({ scheme: USER_DATA_SYNC_SCHEME, authority: 'extensions', path: `/data.json` });
private static readonly EXTENSIONS_DATA_URI = URI.from({ scheme: USER_DATA_SYNC_SCHEME, authority: 'extensions', path: `/current.json` });
protected readonly version: number = 2;
protected isEnabled(): boolean { return super.isEnabled() && this.extensionGalleryService.isEnabled(); }

Expand Down
27 changes: 22 additions & 5 deletions src/vs/platform/userDataSync/common/globalStateSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { SyncStatus, IUserDataSyncStoreService, IUserDataSyncLogService, IGlobalState, SyncResource, IUserDataSynchroniser, IUserDataSyncEnablementService, IUserDataSyncBackupStoreService, ISyncResourceHandle, IStorageValue, ISyncPreviewResult } from 'vs/platform/userDataSync/common/userDataSync';
import { SyncStatus, IUserDataSyncStoreService, IUserDataSyncLogService, IGlobalState, SyncResource, IUserDataSynchroniser, IUserDataSyncEnablementService, IUserDataSyncBackupStoreService, ISyncResourceHandle, IStorageValue, ISyncPreviewResult, USER_DATA_SYNC_SCHEME } from 'vs/platform/userDataSync/common/userDataSync';
import { VSBuffer } from 'vs/base/common/buffer';
import { Event } from 'vs/base/common/event';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { dirname, joinPath, basename } from 'vs/base/common/resources';
import { dirname, joinPath, basename, isEqual } from 'vs/base/common/resources';
import { IFileService } from 'vs/platform/files/common/files';
import { IStringDictionary } from 'vs/base/common/collections';
import { edit } from 'vs/platform/userDataSync/common/content';
Expand Down Expand Up @@ -41,6 +41,7 @@ interface ILastSyncUserData extends IRemoteUserData {

export class GlobalStateSynchroniser extends AbstractSynchroniser implements IUserDataSynchroniser {

private static readonly GLOBAL_STATE_DATA_URI = URI.from({ scheme: USER_DATA_SYNC_SCHEME, authority: 'globalState', path: `/current.json` });
protected readonly version: number = 1;

constructor(
Expand Down Expand Up @@ -139,28 +140,44 @@ export class GlobalStateSynchroniser extends AbstractSynchroniser implements IUs
async stop(): Promise<void> { }

async getAssociatedResources({ uri }: ISyncResourceHandle): Promise<{ resource: URI, comparableResource?: URI }[]> {
return [{ resource: joinPath(uri, 'globalState.json') }];
return [{ resource: joinPath(uri, 'globalState.json'), comparableResource: GlobalStateSynchroniser.GLOBAL_STATE_DATA_URI }];
}

async resolveContent(uri: URI): Promise<string | null> {
if (isEqual(uri, GlobalStateSynchroniser.GLOBAL_STATE_DATA_URI)) {
const localGlobalState = await this.getLocalGlobalState();
return this.format(localGlobalState);
}

let content = await super.resolveContent(uri);
if (content) {
return content;
}

content = await super.resolveContent(dirname(uri));
if (content) {
const syncData = this.parseSyncData(content);
if (syncData) {
switch (basename(uri)) {
case 'globalState.json':
const edits = format(syncData.content, undefined, {});
return applyEdits(syncData.content, edits);
return this.format(JSON.parse(syncData.content));
}
}
}

return null;
}

private format(globalState: IGlobalState): string {
const storageKeys = Object.keys(globalState.storage).sort();
const storage: IStringDictionary<IStorageValue> = {};
storageKeys.forEach(key => storage[key] = globalState.storage[key]);
globalState.storage = storage;
const content = JSON.stringify(globalState);
const edits = format(content, undefined, {});
return applyEdits(content, edits);
}

async acceptConflict(conflict: URI, content: string): Promise<void> {
throw new Error(`${this.syncResourceLogLabel}: Conflicts should not occur`);
}
Expand Down

0 comments on commit 71bb111

Please sign in to comment.