Skip to content
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
52 changes: 36 additions & 16 deletions src/extensions/default/Git/src/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,32 +399,52 @@ define(function (require, exports) {
}
}

let lastExecutionTime = 0;
let isCommandExecuting = false;
const FOCUS_SWITCH_DEDUPE_TIME = 5000;
let scheduledRefresh = null;
const REFRESH_DEDUPE_TIME = 3000;

function refreshOnFocusChange() {
// to sync external git changes after switching to app.
if (gitEnabled) {
const now = Date.now();
const isGitPanelVisible = Panel.getPanel().is(":visible");

if (isCommandExecuting) {
// if we haven't already scheduled a refresh, queue one
if (!scheduledRefresh) {
scheduledRefresh = setTimeout(() => {
scheduledRefresh = null;
refreshOnFocusChange();
}, REFRESH_DEDUPE_TIME);
}
return;
}

if (now - lastExecutionTime > FOCUS_SWITCH_DEDUPE_TIME) {
isCommandExecuting = true;
lastExecutionTime = Date.now();
Git.hasStatusChanged().then((hasChanged) => {
if(!hasChanged){
return;
}

CommandManager.execute(Constants.CMD_GIT_REFRESH).fail((err) => {
console.error("error refreshing on focus switch", err);
});
}).finally(()=>{
isCommandExecuting = true;

// if the git panel is visible, its very likely user is working with git (maybe external)
// so when Phoenix gains focus, we do a complete git refresh to show latest status
if(isGitPanelVisible) {
CommandManager.execute(Constants.CMD_GIT_REFRESH).fail((err) => {
console.error("error refreshing on focus switch", err);
}).always(() => {
isCommandExecuting = false;
// if a refresh got queued while we were executing, run it immediately now
if (scheduledRefresh) {
clearTimeout(scheduledRefresh);
scheduledRefresh = null;
refreshOnFocusChange();
}
});
} else {
// if panel not visible, we just refresh the git branch (shown in sidebar)
Branch.refresh();
isCommandExecuting = false;

// run if something got queued
if (scheduledRefresh) {
clearTimeout(scheduledRefresh);
scheduledRefresh = null;
refreshOnFocusChange();
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/extensions/default/Git/src/git/GitCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,8 @@ define(function (require, exports) {
});
}

// this function right now is not being used anywhere,
// but leaving it here (we might need it in the future)
function hasStatusChanged() {
const prevStatus = lastGitStatusResults;
return status().then(function (currentStatus) {
Expand Down
Loading