Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing a few time machine bugs #9697

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions pxteditor/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ namespace pxt.workspace {
}

// If no source changed, we can bail at this point
const diffed = diffScriptText(previousText, toWrite, currentTime, diff);
if (!diffed) {
if (scriptEquals(previousText, toWrite)) {
toWrite[pxt.HISTORY_FILE] = JSON.stringify(history);
return;
}
Expand Down Expand Up @@ -340,7 +339,11 @@ namespace pxt.workspace {
}
}
else {
history.entries.push(diffed);
const diffed = diffScriptText(previousText, toWrite, currentTime, diff);

if (diffed) {
history.entries.push(diffed);
}
}

// Finally, update the snapshots. These are failsafes in case something
Expand Down Expand Up @@ -379,4 +382,18 @@ namespace pxt.workspace {
text: pxt.workspace.createSnapshot(text)
};
}

function scriptEquals(a: ScriptText, b: ScriptText) {
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);

if (aKeys.length !== bKeys.length) return false;

for (const key of aKeys) {
if (bKeys.indexOf(key) === -1) return false;
if (a[key] !== b[key]) return false;
}

return true;
}
}
1 change: 1 addition & 0 deletions theme/timeMachine.less
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@

.time-machine-back-button {
flex-grow: 1;
flex-shrink: 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion webapp/src/dialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ export function isDontShowDownloadDialogFlagSet() {
}

export async function showTurnBackTimeDialogAsync(header: pxt.workspace.Header, reloadHeader: () => void) {
const text = await workspace.getTextAsync(header.id);
const text = await workspace.getTextAsync(header.id, true);
let history: pxt.workspace.HistoryFile;

if (text?.[pxt.HISTORY_FILE]) {
Expand Down
53 changes: 31 additions & 22 deletions webapp/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,24 +281,29 @@ export function initAsync() {
})
}

export function getTextAsync(id: string): Promise<ScriptText> {
return maybeSyncHeadersAsync()
.then(() => {
let e = lookup(id)
if (!e)
return Promise.resolve(null as ScriptText)
if (e.text)
return Promise.resolve(e.text)
return headerQ.enqueue(id, () => impl.getAsync(e.header)
.then(resp => {
if (!e.text) {
// otherwise we were beaten to it
e.text = fixupFileNames(resp.text);
}
e.version = resp.version;
return e.text
}))
})
export async function getTextAsync(id: string, getSavedText = false): Promise<ScriptText> {
await maybeSyncHeadersAsync();

const e = lookup(id);
if (!e) return null;

if (e.text && !getSavedText) {
return e.text;
}

return headerQ.enqueue(id, async () => {
const resp = await impl.getAsync(e.header);
const fixedText = fixupFileNames(resp.text);

if (getSavedText) {
return fixedText;
}
else if (!e.text) {
e.text = fixedText;
}
e.version = resp.version;
return e.text
});
}

export interface ScriptMeta {
Expand Down Expand Up @@ -578,14 +583,18 @@ export async function saveAsync(h: Header, text?: ScriptText, fromCloudSync?: bo
await fixupVersionAsync(e);
let ver: any;

const toWrite = text ? e.text : null;
let toWrite = text ? e.text : null;

if (pxt.appTarget.appTheme.timeMachine) {
try {
if (toWrite) {
const previous = await impl.getAsync(h);
const previous = await impl.getAsync(h);

if (previous) {
if (!toWrite && previous.header.pubVersions?.length !== h.pubVersions?.length) {
toWrite = { ...previous.text };
}

if (previous) {
if (toWrite) {
pxt.workspace.updateHistory(previous.text, toWrite, Date.now(), h.pubVersions || [], diffText, patchText);
}
}
Expand Down
Loading