Skip to content

Commit

Permalink
Add pxtarget.json options for tuning time machine intervals (#9786)
Browse files Browse the repository at this point in the history
* Add pxtarget.json options for tuning time machine intervals

* Update history.ts
  • Loading branch information
riknoll authored Jan 3, 2024
1 parent 6ae01af commit cb89b11
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions localtypings/pxtarget.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ declare namespace pxt {
timeMachine?: boolean; // Save/restore old versions of a project experiment
blocklySoundVolume?: number; // A number between 0 and 1 that sets the volume for blockly sounds (e.g. connect, disconnect, click)
timeMachineQueryParams?: string[]; // An array of query params to pass to timemachine iframe embed
timeMachineDiffInterval?: number; // An interval in milliseconds at which to take diffs to store in project history. Defaults to 5 minutes
timeMachineSnapshotInterval?: number; // An interval in milliseconds at which to take full project snapshots in project history. Defaults to 15 minutes
}

interface DownloadDialogTheme {
Expand Down
30 changes: 23 additions & 7 deletions pxteditor/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ namespace pxt.workspace {
maxTime?: number;
}

// 5 minutes
const DIFF_HISTORY_INTERVAL = 1000 * 60 * 5;
// 5 minutes. This is overridden in pxtarget.json
const DEFAULT_DIFF_HISTORY_INTERVAL = 1000 * 60 * 5;

// 15 minutes
const SNAPSHOT_HISTORY_INTERVAL = 1000 * 60 * 15;
// 15 minutes. This is overridden in pxtarget.json
const DEFAULT_SNAPSHOT_HISTORY_INTERVAL = 1000 * 60 * 15;

const ONE_DAY = 1000 * 60 * 60 * 24;

Expand Down Expand Up @@ -324,7 +324,7 @@ namespace pxt.workspace {
const topTime = history.entries[history.entries.length - 1].timestamp;
const prevTime = history.entries[history.entries.length - 2].timestamp;

if (currentTime - topTime < DIFF_HISTORY_INTERVAL && topTime - prevTime < DIFF_HISTORY_INTERVAL) {
if (currentTime - topTime < diffInterval() && topTime - prevTime < diffInterval()) {
shouldCombine = true;
}
}
Expand Down Expand Up @@ -352,7 +352,7 @@ namespace pxt.workspace {
if (history.snapshots.length == 0) {
history.snapshots.push(takeSnapshot(previousText, currentTime - 1));
}
else if (currentTime - history.snapshots[history.snapshots.length - 1].timestamp >= SNAPSHOT_HISTORY_INTERVAL) {
else if (currentTime - history.snapshots[history.snapshots.length - 1].timestamp >= snapshotInterval()) {
history.snapshots.push(takeSnapshot(previousText, currentTime));

const trimmed: pxt.workspace.SnapshotEntry[] = [];
Expand Down Expand Up @@ -441,4 +441,20 @@ namespace pxt.workspace {

return true;
}
}

function diffInterval() {
if (pxt.appTarget?.appTheme?.timeMachineDiffInterval != undefined) {
return pxt.appTarget.appTheme.timeMachineDiffInterval;
}

return DEFAULT_DIFF_HISTORY_INTERVAL;
}

function snapshotInterval() {
if (pxt.appTarget?.appTheme?.timeMachineSnapshotInterval != undefined) {
return pxt.appTarget.appTheme.timeMachineSnapshotInterval;
}

return DEFAULT_SNAPSHOT_HISTORY_INTERVAL;
}
}

0 comments on commit cb89b11

Please sign in to comment.