Skip to content

Commit

Permalink
Update Entity Etag in server side changes chosen scenario (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyaginidhi committed Aug 9, 2023
1 parent be18db4 commit 2d75d4d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/web/client/context/fileData.ts
Expand Up @@ -10,6 +10,7 @@ export interface IFileData extends IFileInfo {
entityFileExtensionType: string;
attributePath: IAttributePath;
hasDirtyChanges: boolean;
hasDiffViewTriggered: boolean;
encodeAsBase64: boolean | undefined;
mimeType: string | undefined;
isContentLoaded?: boolean;
Expand All @@ -23,6 +24,7 @@ export class FileData implements IFileData {
private _entityFileExtensionType: string;
private _attributePath: IAttributePath;
private _hasDirtyChanges!: boolean;
private _hasDiffViewTriggered!: boolean;
private _encodeAsBase64: boolean | undefined;
private _mimeType: string | undefined;
private _isContentLoaded: boolean | undefined;
Expand Down Expand Up @@ -55,7 +57,9 @@ export class FileData implements IFileData {
public get hasDirtyChanges(): boolean {
return this._hasDirtyChanges;
}

public get hasDiffViewTriggered(): boolean {
return this._hasDiffViewTriggered;
}
public get isContentLoaded(): boolean | undefined {
return this._isContentLoaded;
}
Expand All @@ -67,6 +71,9 @@ export class FileData implements IFileData {
public set setEntityEtag(value: string) {
this._entityEtag = value;
}
public set setHasDiffViewTriggered(value: boolean) {
this._hasDiffViewTriggered = value;
}

constructor(
entityId: string,
Expand All @@ -88,6 +95,7 @@ export class FileData implements IFileData {
this._encodeAsBase64 = encodeAsBase64;
this._mimeType = mimeType;
this._hasDirtyChanges = false;
this._hasDiffViewTriggered = false;
this._isContentLoaded = isContentLoaded;
}
}
10 changes: 10 additions & 0 deletions src/web/client/context/fileDataMap.ts
Expand Up @@ -50,6 +50,16 @@ export class FileDataMap {
throw Error("File does not exist in the map"); // TODO - Revisit errors and dialog experience here
}

public updateDiffViewTriggered(fileFsPath: string, diffViewTriggerValue: boolean) {
const existingEntity = this.fileMap.get(fileFsPath);

if (existingEntity) {
existingEntity.setHasDiffViewTriggered = diffViewTriggerValue;
return;
}
throw Error("File does not exist in the map"); // TODO - Revisit errors and dialog experience here
}

public updateEtagValue(fileFsPath: string, etag: string) {
const existingEntity = this.fileMap.get(fileFsPath);

Expand Down
22 changes: 21 additions & 1 deletion src/web/client/dal/fileSystemProvider.ts
Expand Up @@ -18,13 +18,17 @@ import { telemetryEventNames } from "../telemetry/constants";
import { getFolderSubUris } from "../utilities/folderHelperUtility";
import { EtagHandlerService } from "../services/etagHandlerService";
import {
fileHasDiffViewTriggered,
fileHasDirtyChanges,
getEntityEtag,
getFileEntityEtag,
getFileEntityId,
getFileEntityName,
getFileName,
updateDiffViewTriggered,
updateEntityColumnContent,
updateFileDirtyChanges,
updateFileEntityEtag,
} from "../utilities/fileAndEntityUtil";
import { isVersionControlEnabled } from "../utilities/commonUtil";
import { IFileInfo } from "../common/interfaces";
Expand Down Expand Up @@ -91,6 +95,8 @@ export class PortalsFS implements vscode.FileSystemProvider {
latestContent.length > 0 &&
getFileEntityEtag(uri.fsPath) !== entityEtagValue
) {
updateDiffViewTriggered(uri.fsPath, true);
updateFileEntityEtag(uri.fsPath, entityEtagValue);
await this.updateMtime(uri, latestContent);
WebExtensionContext.telemetry.sendInfoTelemetry(
telemetryEventNames.WEB_EXTENSION_DIFF_VIEW_TRIGGERED
Expand Down Expand Up @@ -135,7 +141,20 @@ export class PortalsFS implements vscode.FileSystemProvider {
data = await this._lookup(uri, true);
}

return data instanceof File ? data.data : new Uint8Array();
const fileContent = data instanceof File ? data.data : new Uint8Array();
if (fileHasDirtyChanges(uri.fsPath)) {
if (fileHasDiffViewTriggered(uri.fsPath)) {
updateDiffViewTriggered(uri.fsPath, false);
} else {
const fileData = WebExtensionContext.fileDataMap.getFileMap.get(uri.fsPath);
if (fileData?.entityId && fileData.attributePath) {
updateEntityColumnContent(fileData?.entityId, fileData?.attributePath, Buffer.from(fileContent).toString());
updateFileDirtyChanges(uri.fsPath, false);
}
}
}

return fileContent;
}

async writeFile(
Expand Down Expand Up @@ -476,6 +495,7 @@ export class PortalsFS implements vscode.FileSystemProvider {

// Update fileDataMap with the latest changes
updateFileDirtyChanges(uri.fsPath, false);
updateDiffViewTriggered(uri.fsPath, false);

// Update the etag of the file after saving
await EtagHandlerService.updateFileEtag(uri.fsPath);
Expand Down
12 changes: 3 additions & 9 deletions src/web/client/services/etagHandlerService.ts
Expand Up @@ -12,6 +12,7 @@ import { PortalsFS } from "../dal/fileSystemProvider";
import { telemetryEventNames } from "../telemetry/constants";
import { GetFileContent } from "../utilities/commonUtil";
import {
getFileEntityEtag,
getFileEntityId,
getFileEntityName,
updateEntityEtag,
Expand All @@ -30,10 +31,7 @@ export class EtagHandlerService {

const requestSentAtTime = new Date().getTime();

const entityEtag =
WebExtensionContext.fileDataMap.getFileMap.get(
fileFsPath
)?.entityEtag;
const entityEtag = getFileEntityEtag(fileFsPath);

const dataverseOrgUrl = WebExtensionContext.urlParametersMap.get(
queryParameters.ORG_URL
Expand Down Expand Up @@ -88,13 +86,9 @@ export class EtagHandlerService {
await portalFs.readFile(vscode.Uri.parse(fileFsPath))
);
const latestContent = GetFileContent(result, attributePath, entityName, entityId);
updateEntityEtag(entityId, result[ODATA_ETAG]);

if (currentContent !== latestContent) {
updateEntityEtag(
entityId,
result[ODATA_ETAG]
);

WebExtensionContext.telemetry.sendInfoTelemetry(
telemetryEventNames.WEB_EXTENSION_ENTITY_CONTENT_CHANGED
);
Expand Down
15 changes: 15 additions & 0 deletions src/web/client/utilities/fileAndEntityUtil.ts
Expand Up @@ -13,6 +13,11 @@ export function fileHasDirtyChanges(fileFsPath: string) {
?.hasDirtyChanges as boolean;
}

export function fileHasDiffViewTriggered(fileFsPath: string) {
return WebExtensionContext.fileDataMap.getFileMap.get(fileFsPath)
?.hasDiffViewTriggered as boolean;
}

export function getFileEntityId(fileFsPath: string) {
return WebExtensionContext.fileDataMap.getFileMap.get(fileFsPath)
?.entityId as string ?? WebExtensionContext.getVscodeWorkspaceState(fileFsPath)?.entityId as string;
Expand Down Expand Up @@ -42,6 +47,16 @@ export function updateFileDirtyChanges(
);
}

export function updateDiffViewTriggered(
fileFsPath: string,
hasDiffViewTriggered: boolean
) {
WebExtensionContext.fileDataMap.updateDiffViewTriggered(
fileFsPath,
hasDiffViewTriggered
);
}

export function doesFileExist(fileFsPath: string) {
return WebExtensionContext.fileDataMap.getFileMap.has(vscode.Uri.parse(fileFsPath).fsPath);
}
Expand Down

0 comments on commit 2d75d4d

Please sign in to comment.