Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Add portapi op waiting, improve changes handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Apr 28, 2020
1 parent f3e9d91 commit dfcbdc8
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions assets/js/portapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,28 @@ class Operation {
// single object
this.record = obj;
this.prepRecord(key, this.record);
this.changes.created++;
this.success = true;
this.loading = false;
} else {
// query: many objects
if (created) {
// set new
Vue.set(this.records, key, obj);
this.prepRecord(key, obj);
this.changes.created++;
} else {
// update existing
var existing = this.records[key];
if (existing) {
Object.assign(existing, obj);
this.prepRecord(key, existing);
this.changes.updated++;
} else {
// if not exists, set new
Vue.set(this.records, key, obj);
this.prepRecord(key, obj);
this.changes.created++;
}
}
}
Expand All @@ -81,6 +87,7 @@ class Operation {
}
Vue.delete(this.records, key);
}
this.changes.deleted++;
}
parseObject(key, data) {
if (data[0] == "J") {
Expand All @@ -98,8 +105,6 @@ class Operation {
for (const [key, value] of Object.entries(obj)) {
if (key.charAt(0) != key.charAt(0).toUpperCase()) {
delete obj[key]
} else if (value instanceof Object) {
this.cleanLowerCase(value)
}
}
}
Expand All @@ -111,6 +116,26 @@ class Operation {
// return
return sub
}
async wait() {
await this.newWaitPromise();
}
newWaitPromise() {
var that = this;
return new Promise((resolve, reject) => {
// create function to check if loading has finished
var checkIfFinishedLoading = null;
checkIfFinishedLoading = function() {

This comment has been minimized.

Copy link
@ppacher

ppacher Apr 30, 2020

Contributor

if you use an arrow function here there's no need to copy this into that :).
It's also not required to initialize checkIfFinishedLoading to null and always use let instead of var

let checkIfFinishedLoading = () => {
    // ... use `this` instead of `that`
}
if (that.loading) {
// check again later
setTimeout(checkIfFinishedLoading, 10);
return;
}
resolve();
}
// kick off
setTimeout(checkIfFinishedLoading, 10);
});
}
}

function install(vue, options) {
Expand Down Expand Up @@ -278,7 +303,6 @@ function install(vue, options) {
console.warn("received invalid message: " + msg);
break;
}
op.changes.created++;
op.updateRecord(splitted[2], splitted[3], true)
console.log(opID + ": ok " + splitted[2]);
break;
Expand All @@ -302,7 +326,6 @@ function install(vue, options) {
console.warn("received invalid message: " + msg);
break;
}
op.changes.updated++;
op.updateRecord(splitted[2], splitted[3], false)
console.log(opID + ": update " + splitted[2]);
break;
Expand All @@ -312,7 +335,6 @@ function install(vue, options) {
console.warn("received invalid message: " + msg);
break;
}
op.changes.created++;
op.updateRecord(splitted[2], splitted[3], true)
console.log(opID + ": new " + splitted[2]);
break;
Expand All @@ -322,7 +344,6 @@ function install(vue, options) {
console.warn("received invalid message: " + msg);
break;
}
op.changes.deleted++;
op.deleteRecord(splitted[2])
console.log(opID + ": delete " + splitted[2]);
break;
Expand Down

0 comments on commit dfcbdc8

Please sign in to comment.