Skip to content

Commit

Permalink
fix: not processing queue items in error cases
Browse files Browse the repository at this point in the history
If processEditPendingQueue failed with some error any pending items would not be processed
(at that time, later they would be but that was not intended).
Changed logging as well (removed the huge logs). Still keeping some logs
as warnings (even though they are not...).
  • Loading branch information
mbehr1 committed Jan 1, 2021
1 parent 9178e80 commit ae995d8
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/extension/fbaEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ export class FBAEditorProvider implements vscode.CustomTextEditorProvider, vscod
case 'update':
try {
FBAEditorProvider.updateTextDocument(docData, document, { fishbone: e.data, title: e.title, attributes: e.attributes });
/*
// lets do two quick changes: was used to test race condition from issue #7
setTimeout(() =>
FBAEditorProvider.updateTextDocument(docData, document, { fishbone: e.data, title: e.title, attributes: e.attributes }), 1);
*/

// lets do two quick changes: was used to test race condition from issue #7
// setTimeout(() =>
// FBAEditorProvider.updateTextDocument(docData, document, { fishbone: e.data, title: e.title, attributes: e.attributes }), 1);

} catch (e) {
console.error(`Fishbone: Could not update document. Changes are lost. Please consider closing and reopening the doc. Error= ${e.name}:${e.message}.`);
vscode.window.showErrorMessage(`Fishbone: Could not update document. Changes are lost. Please consider closing and reopening the doc. Error= ${e.name}:${e.message}.`);
Expand Down Expand Up @@ -392,15 +392,15 @@ export class FBAEditorProvider implements vscode.CustomTextEditorProvider, vscod
* To do so it calls itself recursively.
* @param docData document specific data
*/
static async processEditsPendingQueue(docData: DocData) {
static async processEditsPendingQueue(docData: DocData): Promise<void> {
const editToProcess = docData.editsPending[0];
const document = editToProcess.document;
const docObj = editToProcess.toChangeObj;

console.log(`processEditsPendingQueue called with json.keys=${Object.keys(docObj)}`);
Object.keys(docObj).forEach(key => {
/*Object.keys(docObj).forEach(key => {
console.log(` ${key}=${JSON.stringify(docObj[key])}`);
});
});*/

const edit = new vscode.WorkspaceEdit();

Expand Down Expand Up @@ -553,9 +553,23 @@ export class FBAEditorProvider implements vscode.CustomTextEditorProvider, vscod
const yamlStr = yaml.safeDump(yamlObj);

if (yamlStr === prevDocText) {
console.warn(`FBAEditorProvider.processEditsPendingQueue text unchanged! Could skip replace.`);
console.warn(`FBAEditorProvider.processEditsPendingQueue text unchanged! Skipping replace.`);
// need to remove this one from the queue
docData.editsPending.shift();
// if there is another one in the queue: apply that one
if (docData.editsPending.length > 0) {
FBAEditorProvider.processEditsPendingQueue(docData);
}
return;
}

// we could try to determine a "patch set". We could use e.g. the "google/diff-match-patch" lib but
// for our use case and with yaml as the file format and the range reqs (line, col)
// we can do a simpler approach comparing common lines at begin and end
// todo: benefit would be smaller edits. so most likely less memory usage.
// cost is more cpu to determine it. Mainly splitting the text into lines?
// or determining later which line/col is in range.

edit.replace(
document.uri,
new vscode.Range(0, 0, document.lineCount, 0),
Expand All @@ -566,17 +580,20 @@ export class FBAEditorProvider implements vscode.CustomTextEditorProvider, vscod
docData.editsPending.shift();
console.error(`storing as YAML failed. Error=${e.name}:${e.message}`);
vscode.window.showErrorMessage(`Fishbone: Could not update document. Changes are lost. Please consider closing and reopening the doc. Storing as YAML failed. Error=${e.name}:${e.message}`);
return false;
// if there is another one in the queue: apply that one
if (docData.editsPending.length > 0) {
FBAEditorProvider.processEditsPendingQueue(docData);
}
return;
}
console.warn(`FBAEditorProvider.processEditsPendingQueue will apply edit with size=${edit.size}, editsPending.length=${docData.editsPending.length} version=${document.version}`);

//console.log(`FBAEditorProvider.processEditsPendingQueue will apply edit with size=${edit.size}, editsPending.length=${docData.editsPending.length} version=${document.version}`);
// if we call applyEdit while the prev. one is not done yet, the 2nd one will be neg. fulfilled. issue #7
vscode.workspace.applyEdit(edit).then((fulfilled) => {
// remove the one from queue:
const fulFilledEdit = docData.editsPending.shift();

if (fulfilled) {
console.warn(`FBAEditorProvider.processEditsPendingQueue fulfilled (${fulFilledEdit?.docVersion}) editsPending.length=${docData.editsPending.length}`);
console.log(`FBAEditorProvider.processEditsPendingQueue fulfilled (${fulFilledEdit?.docVersion}) editsPending.length=${docData.editsPending.length}`);
} else {
// todo we could reapply here? (but avoid endless retrying...)
console.error(`processEditsPendingQueue fulfilled=${fulfilled}`);
Expand All @@ -585,9 +602,8 @@ export class FBAEditorProvider implements vscode.CustomTextEditorProvider, vscod
// if there is another one in the queue: apply that one
if (docData.editsPending.length > 0) {
FBAEditorProvider.processEditsPendingQueue(docData);
}
}
});
return true;
}

static getFBDataFromText(text: string, updateFn: undefined | ((yamlObj: any) => void)) {
Expand Down

0 comments on commit ae995d8

Please sign in to comment.