Skip to content

Commit

Permalink
Fixing a few time machine bugs (#9697)
Browse files Browse the repository at this point in the history
* don't create an extra diff when updating history

* ensure shares show up in history immediately

* prevent text bleeding on small screens
  • Loading branch information
riknoll authored Sep 26, 2023
1 parent 9d013fe commit fc52471
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
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

0 comments on commit fc52471

Please sign in to comment.