Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async dialog content => async create dialog #280

Merged
merged 2 commits into from
Jul 19, 2021
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
4 changes: 3 additions & 1 deletion src/code/dialogs/linkListDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const LinkListDialog = OperationChecklistDialog.extend({
return fields.slice(0, 2).concat(linkFields, fields.slice(3));
},

_displayDialog: function () {
_displayDialog: async function () {
const operation = getSelectedOperation();
loadFaked(operation);
const links = operation.getLinkListFromPortal(this.options.portal);
Expand All @@ -61,6 +61,8 @@ const LinkListDialog = OperationChecklistDialog.extend({
this.closeDialog();
};

await this.sortable.done;

this.createDialog({
title: wX("LINKS2", {
portalName: PortalUI.displayName(this.options.portal),
Expand Down
18 changes: 9 additions & 9 deletions src/code/dialogs/manageTeamDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const ManageTeamDialog = WDialog.extend({
window.map.off("wasabee:logout", this.closeDialog, this);
},

_setupTable: function () {
_setupTable: async function () {
const table = new Sortable();
table.fields = [
{
Expand Down Expand Up @@ -128,8 +128,7 @@ const ManageTeamDialog = WDialog.extend({
];
table.sortBy = 0;

// async populate
this._refreshTeam(table);
await this._refreshTeam(table);

return table;
},
Expand All @@ -147,19 +146,20 @@ const ManageTeamDialog = WDialog.extend({
}
},

update: function () {
const container = this._dialogContent(); // build the UI
update: async function () {
const container = await this._dialogContent(); // build the UI
// this is the correct way to change out a dialog's contents, audit the entire codebase making this change
this.setContent(container);
this.setTitle(wX("MANAGE_TEAM", { teamName: this.options.team.Name }));
},

_dialogContent: function () {
_dialogContent: async function () {
const container = L.DomUtil.create("div", "container");
const list = L.DomUtil.create("div", "list", container);

const table = this._setupTable();
const table = await this._setupTable();
list.appendChild(table.table);
await table.done;

const addlabel = L.DomUtil.create("label", null, container);
addlabel.textContent = wX("ADD_AGENT");
Expand Down Expand Up @@ -305,8 +305,8 @@ const ManageTeamDialog = WDialog.extend({
return container;
},

_displayDialog: function () {
const container = this._dialogContent();
_displayDialog: async function () {
const container = await this._dialogContent();
const buttons = {};
buttons[wX("CLOSE")] = () => {
this.closeDialog();
Expand Down
4 changes: 3 additions & 1 deletion src/code/dialogs/markerList.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MarkerList = OperationChecklistDialog.extend({
TYPE: "markerList",
},

_displayDialog: function () {
_displayDialog: async function () {
const operation = getSelectedOperation();
loadFaked(operation);
this.sortable = this.getListDialogContent(
Expand All @@ -27,6 +27,8 @@ const MarkerList = OperationChecklistDialog.extend({
this.closeDialog();
};

await this.sortable.done;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done is a getter, which isn't async. does this work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done returns _done that is either a promise or boolean
also, you can always await a constant expression

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd forgotten that _done is ever a promise.


this.createDialog({
title: wX("MARKER_LIST", { opName: operation.name }),
html: this.sortable.table,
Expand Down
32 changes: 21 additions & 11 deletions src/code/sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default class Sortable {

// class getters and setter's can't be async,
// this lets us build each row as a promise, then resolve them all together
const instantValues = [];
const promises = incoming.map(async (obj) => {
const row = L.DomUtil.create("tr");
const data = {
Expand Down Expand Up @@ -105,23 +106,32 @@ export default class Sortable {
cell.style.display = "none";
}
}
instantValues.push(data);
return data;
});

// resolve all rows at once
// XXX convert to allSettled and check for individual errors rather than failing hard if any row fails
// console.log(promises);
this._done = Promise.all(promises).then(
(values) => {
this._items = values;
this.sort();
return true;
},
(reject) => {
console.log("rejected", reject);
this._done = false;
}
);
if (instantValues.length === promises.length) {
// all promises are already fulfilled, dont use async Promise.all
this._items = instantValues;
this.sort();
this._done = true;
} else {
// always async
this._done = Promise.all(promises).then(
(values) => {
this._items = values;
this.sort();
return true;
},
(reject) => {
console.log("rejected", reject);
this._done = false;
}
);
}
}

get fields() {
Expand Down