Skip to content

Commit 29c05c7

Browse files
authored
Store workspaceState with associated editSession (microsoft#186193)
Store workspaceState with associated editSession
1 parent df4d9ce commit 29c05c7

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

src/vs/workbench/contrib/editSessions/browser/editSessions.contribution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ export class EditSessionsContribution extends Disposable implements IWorkbenchCo
743743
return undefined;
744744
}
745745

746-
const data: EditSession = { folders, version: 2 };
746+
const data: EditSession = { folders, version: 2, workspaceStateId: this.editSessionsStorageService.lastWrittenResources.get('workspaceState')?.ref };
747747

748748
try {
749749
this.logService.info(`Storing edit session...`);

src/vs/workbench/contrib/editSessions/browser/editSessionsStorageService.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
5858
return this._didSignOut.event;
5959
}
6060

61+
private _lastWrittenResources = new Map<SyncResource, { ref: string; content: string }>();
62+
get lastWrittenResources() {
63+
return this._lastWrittenResources;
64+
}
65+
66+
private _lastReadResources = new Map<SyncResource, { ref: string; content: string }>();
67+
get lastReadResources() {
68+
return this._lastReadResources;
69+
}
70+
6171
storeClient: EditSessionsStoreClient | undefined; // TODO@joyceerhl lifecycle hack
6272

6373
constructor(
@@ -89,9 +99,9 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
8999
}
90100

91101
/**
92-
*
93-
* @param editSession An object representing edit session state to be restored.
94-
* @returns The ref of the stored edit session state.
102+
* @param resource: The resource to retrieve content for.
103+
* @param content An object representing resource state to be restored.
104+
* @returns The ref of the stored state.
95105
*/
96106
async write(resource: SyncResource, content: string | EditSession): Promise<string> {
97107
await this.initialize(false);
@@ -103,14 +113,20 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
103113
content.machine = await this.getOrCreateCurrentMachineId();
104114
}
105115

106-
return this.storeClient!.writeResource(resource, typeof content === 'string' ? content : JSON.stringify(content), null, undefined, createSyncHeaders(generateUuid()));
116+
content = typeof content === 'string' ? content : JSON.stringify(content);
117+
const ref = await this.storeClient!.writeResource(resource, content, null, undefined, createSyncHeaders(generateUuid()));
118+
119+
this._lastWrittenResources.set(resource, { ref, content });
120+
121+
return ref;
107122
}
108123

109124
/**
125+
* @param resource: The resource to retrieve content for.
110126
* @param ref: A specific content ref to retrieve content for, if it exists.
111127
* If undefined, this method will return the latest saved edit session, if any.
112128
*
113-
* @returns An object representing the requested or latest edit session state, if any.
129+
* @returns An object representing the requested or latest state, if any.
114130
*/
115131
async read(resource: SyncResource, ref: string | undefined): Promise<{ ref: string; content: string } | undefined> {
116132
await this.initialize(false);
@@ -133,7 +149,11 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
133149
}
134150

135151
// TODO@joyceerhl Validate session data, check schema version
136-
return (content !== undefined && content !== null && ref !== undefined) ? { ref, content } : undefined;
152+
if (content !== undefined && content !== null && ref !== undefined) {
153+
this._lastReadResources.set(resource, { ref, content });
154+
return { ref, content };
155+
}
156+
return undefined;
137157
}
138158

139159
async delete(resource: SyncResource, ref: string | null) {

src/vs/workbench/contrib/editSessions/common/editSessions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export interface IEditSessionsStorageService {
3535

3636
storeClient: EditSessionsStoreClient | undefined;
3737

38+
lastReadResources: Map<SyncResource, { ref: string; content: string }>;
39+
lastWrittenResources: Map<SyncResource, { ref: string; content: string }>;
40+
3841
initialize(silent?: boolean): Promise<boolean>;
3942
read(resource: SyncResource, ref: string | undefined): Promise<{ ref: string; content: string } | undefined>;
4043
write(resource: SyncResource, content: string | EditSession): Promise<string>;
@@ -82,6 +85,7 @@ export const EditSessionSchemaVersion = 3;
8285

8386
export interface EditSession {
8487
version: number;
88+
workspaceStateId?: string;
8589
machine?: string;
8690
folders: Folder[];
8791
}

src/vs/workbench/contrib/editSessions/common/workspaceStateSync.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity'
1717
import { IUserDataProfile } from 'vs/platform/userDataProfile/common/userDataProfile';
1818
import { AbstractSynchroniser, IAcceptResult, IMergeResult, IResourcePreview, ISyncResourcePreview } from 'vs/platform/userDataSync/common/abstractSynchronizer';
1919
import { IRemoteUserData, IResourceRefHandle, IUserDataSyncBackupStoreService, IUserDataSyncConfiguration, IUserDataSyncEnablementService, IUserDataSyncLogService, IUserDataSyncStoreService, IUserDataSynchroniser, IWorkspaceState, SyncResource } from 'vs/platform/userDataSync/common/userDataSync';
20-
import { IEditSessionsStorageService } from 'vs/workbench/contrib/editSessions/common/editSessions';
20+
import { EditSession, IEditSessionsStorageService } from 'vs/workbench/contrib/editSessions/common/editSessions';
2121
import { IWorkspaceIdentityService } from 'vs/workbench/services/workspaces/common/workspaceIdentityService';
2222

2323

@@ -99,11 +99,14 @@ export class WorkspaceStateSynchroniser extends AbstractSynchroniser implements
9999
});
100100

101101
const content: IWorkspaceState = { folders, storage: contributedData, version: this.version };
102-
this.editSessionsStorageService.write('workspaceState', stringify(content));
102+
await this.editSessionsStorageService.write('workspaceState', stringify(content));
103103
}
104104

105105
override async apply(): Promise<ISyncResourcePreview | null> {
106-
const resource = await this.editSessionsStorageService.read('workspaceState', undefined);
106+
const payload = this.editSessionsStorageService.lastReadResources.get('editSessions')?.content;
107+
const workspaceStateId = payload ? (JSON.parse(payload) as EditSession).workspaceStateId : undefined;
108+
109+
const resource = await this.editSessionsStorageService.read('workspaceState', workspaceStateId);
107110
if (!resource) {
108111
return null;
109112
}
@@ -143,6 +146,8 @@ export class WorkspaceStateSynchroniser extends AbstractSynchroniser implements
143146
}
144147
this.storageService.storeAll(storageEntries, true);
145148
}
149+
150+
this.editSessionsStorageService.delete('workspaceState', resource.ref);
146151
return null;
147152
}
148153

0 commit comments

Comments
 (0)