Skip to content

Commit

Permalink
Fix task queue management
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed May 19, 2020
1 parent ccbfb70 commit 8d471f4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 56 deletions.
36 changes: 10 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async function activate(
align: 'left',
item: statusWidget
});
gitExtension.eventLogger.connect(onEvent);
gitExtension.logger.connect(onEvent);

return gitExtension;

Expand All @@ -140,9 +140,15 @@ async function activate(
* @param event - event name
*/
function onEvent(model: IGitExtension, event: string) {
if (event === 'refresh') {
statusWidget.status = 'refreshing...';
let status;
if (event === 'git:idle') {
status = 'idle';
} else if (event === 'git:refresh') {
status = 'refreshing...';
} else {
status = 'working...';
}
statusWidget.status = status;
}
}

Expand All @@ -163,30 +169,8 @@ class StatusWidget extends Widget {
* Sets the current status.
*/
set status(text: string) {
if (!this._lock) {
this.node.textContent = 'Git: ' + text;
this.lock();
}
}

/**
* Locks the status widget.
*/
lock(): void {
this._lock = new Promise(resolve => this.release());
}

/**
* Releases the status widget "lock".
*/
release(): void {
this._lock = null;
this.node.textContent = 'Git: ' + text;
}

/**
* Promise for "locking" the widget (i.e., blocking the widget from displaying new status changes).
*/
private _lock: Promise<void> | null = null;
}

/**
Expand Down
79 changes: 49 additions & 30 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,34 @@ export class GitExtension implements IGitExtension {
* @returns promise which resolves upon adding files to the repository staging area
*/
async add(...filename: string[]): Promise<Response> {
await this.ready;
const path = this.pathRepository;
let response;

await this.ready;
if (path === null) {
return Promise.resolve(
new Response(
JSON.stringify({
code: -1,
message: 'Not in a Git repository.'
})
)
);
response = {
code: -1,
message: 'Not in a Git repository.'
};
return Promise.resolve(new Response(JSON.stringify(response)));
}
const tid = this._addTask('git:add:files');
const response = await httpGitRequest('/git/add', 'POST', {
add_all: !filename,
filename: filename || '',
top_repo_path: path
});
try {
response = await httpGitRequest('/git/add', 'POST', {
add_all: !filename,
filename: filename || '',
top_repo_path: path
});
} catch (err) {
this._removeTask(tid);
throw new ServerConnection.NetworkError(err);
}
this._removeTask(tid);

if (response.status !== 200) {
const data = await response.json();
throw new ServerConnection.ResponseError(response, data.message);
}
this.refreshStatus();
return Promise.resolve(response);
}
Expand All @@ -124,10 +131,10 @@ export class GitExtension implements IGitExtension {
* @returns promise which resolves upon adding files to the repository staging area
*/
async addAllUnstaged(): Promise<Response> {
await this.ready;
const path = this.pathRepository;
let response;

await this.ready;
if (path === null) {
response = {
code: -1,
Expand Down Expand Up @@ -160,9 +167,10 @@ export class GitExtension implements IGitExtension {
* @returns promise which resolves upon adding files to the repository staging area
*/
async addAllUntracked(): Promise<Response> {
await this.ready;
const path = this.pathRepository;
let response;

await this.ready;
if (path === null) {
response = {
code: -1,
Expand Down Expand Up @@ -716,10 +724,10 @@ export class GitExtension implements IGitExtension {
});
} catch (err) {
this._removeTask(tid);
console.error(err);

// TODO we should notify the user
this._setStatus([]);
console.error(err);
return;
}
this._removeTask(tid);
Expand Down Expand Up @@ -1099,8 +1107,8 @@ export class GitExtension implements IGitExtension {
/**
* A signal emitted whenever a model event occurs.
*/
get eventLogger(): ISignal<IGitExtension, string> {
return this._eventLogger;
get logger(): ISignal<IGitExtension, string> {
return this._logger;
}

/**
Expand Down Expand Up @@ -1197,6 +1205,11 @@ export class GitExtension implements IGitExtension {
task: task
});

// If this task is the only task, broadcast the task...
if (this._taskList.length === 1) {
this._logger.emit(task);
}
// Return the task identifier to allow consumers to remove the task once completed:
return id;
}

Expand All @@ -1206,21 +1219,27 @@ export class GitExtension implements IGitExtension {
* @param id - task identifier
*/
private _removeTask(task: number): void {
let node = this._taskList.first();
let node = this._taskList.firstNode;

// Check the first node...
if (node.value && node.value.id === task) {
if (node && node.value.id === task) {
this._taskList.removeNode(node);
return;
}
// Walk the task list looking for a task with the provided identifier...
while (node.next) {
node = node.next;
if (node.value && node.value.id === task) {
this._taskList.removeNode(node);
return;
} else {
// Walk the task list looking for a task with the provided identifier...
while (node.next) {
node = node.next;
if (node.value && node.value.id === task) {
this._taskList.removeNode(node);
break;
}
}
}
// Check for pending tasks and broadcast the oldest pending task...
if (this._taskList.length === 0) {
this._logger.emit('git:idle');
} else {
this._logger.emit(this._taskList.first.task);
}
}

/**
Expand Down Expand Up @@ -1256,7 +1275,7 @@ export class GitExtension implements IGitExtension {
IChangedArgs<string | null>
>(this);
private _statusChanged = new Signal<IGitExtension, Git.IStatusFile[]>(this);
private _eventLogger = new Signal<IGitExtension, string>(this);
private _logger = new Signal<IGitExtension, string>(this);
}

export class BranchMarker implements Git.IBranchMarker {
Expand Down

0 comments on commit 8d471f4

Please sign in to comment.