cleaned some code Promise...

This commit is contained in:
Paolo Arena
2019-02-14 20:08:22 +01:00
parent bfa8a48211
commit 1f830b3e98
4 changed files with 83 additions and 85 deletions

View File

@@ -104,8 +104,9 @@ export default class Todo extends Vue {
// console.log('reload_fromServer_changed!', value, oldValue) // console.log('reload_fromServer_changed!', value, oldValue)
// if (value) { // if (value) {
Todos.actions.dbLoadTodo(false) Todos.actions.dbLoadTodo(false)
.then(() => {
Todos.actions.updateArrayInMemory() return Todos.actions.updateArrayInMemory()
})
} }
// } // }
} }
@@ -127,7 +128,7 @@ export default class Todo extends Vue {
let mythis = this let mythis = this
mythis.tmpstrTodos = '' mythis.tmpstrTodos = ''
globalroutines(null, 'readall', 'todos', null) return globalroutines(null, 'readall', 'todos', null)
.then(function (alldata) { .then(function (alldata) {
const myrecs = [...alldata] const myrecs = [...alldata]
@@ -145,7 +146,7 @@ export default class Todo extends Vue {
let mythis = this let mythis = this
mythis.tmpstrTodos = '' mythis.tmpstrTodos = ''
globalroutines(null, 'write', 'todos', this.todos_arr[0]) return globalroutines(null, 'write', 'todos', this.todos_arr[0])
.then(function (alldata) { .then(function (alldata) {
mythis.getArrTodos() mythis.getArrTodos()
}) })
@@ -190,7 +191,7 @@ export default class Todo extends Vue {
this.itemDragStart = null this.itemDragStart = null
} }
async updateLinkedList(init: boolean, arr: ITodo[] = this.todos_arr) { updateLinkedList(init: boolean, arr: ITodo[] = this.todos_arr) {
// console.log('updateLinkedList', this.todos_arr) // console.log('updateLinkedList', this.todos_arr)
@@ -201,7 +202,7 @@ export default class Todo extends Vue {
idprev = arr[0].id_prev idprev = arr[0].id_prev
idnext = arr[0].id_next idnext = arr[0].id_next
} }
await arr.forEach((elem, index) => { arr.forEach((elem, index) => {
if (index === 0) { if (index === 0) {
idprev = rescodes.LIST_START idprev = rescodes.LIST_START
} else { } else {
@@ -318,33 +319,36 @@ export default class Todo extends Vue {
} }
} }
await this.updateLinkedList(false) this.updateLinkedList(false)
// Updated only elements modified // Updated only elements modified
await this.updateModifyRecords(true) await this.updateModifyRecords(true)
this.updatetable(false, 'onEnd') await this.updatetable(false, 'onEnd')
} }
async updateModifyRecords(refresh: boolean = false) { async updateModifyRecords(refresh: boolean = false) {
let update = false let update = false
await this.todos_arr.forEach((elem: ITodo) => { await this.todos_arr.forEach((elem: ITodo) => {
if (elem.modified) { if (elem.modified) {
console.log('calling MODIFY 3') console.log('calling MODIFY 3')
this.modify(elem, false) return this.modify(elem, false)
update = true .then(() => {
elem.modified = false update = true
elem.modified = false
})
} }
}) })
if (update) if (update)
await this.updatetable(refresh, 'updateModifyRecords') return await this.updatetable(refresh, 'updateModifyRecords')
} }
created() { async created() {
this.load() await this.load()
} }
setarrPriority() { setarrPriority() {
@@ -373,15 +377,13 @@ export default class Todo extends Vue {
} }
// console.log('Priority:' + this.listPriorityLabel) // console.log('Priority:' + this.listPriorityLabel)
this.setarrPriority() this.setarrPriority()
this.clearArr()
await this.updatetable(false, 'load') await this.updatetable(false, 'load')
this.loadDone = true
this.checkUpdate_everytime() this.checkUpdate_everytime()
this.loadDone = true
} }
// Call to check if need to refresh // Call to check if need to refresh
@@ -393,7 +395,7 @@ export default class Todo extends Vue {
initcat() { initcat() {
let tomorrow = new Date(); let tomorrow = new Date()
tomorrow.setDate(tomorrow.getDate() + 1) tomorrow.setDate(tomorrow.getDate() + 1)
@@ -448,6 +450,7 @@ export default class Todo extends Vue {
objtodo.descr = this.todo objtodo.descr = this.todo
objtodo.category = this.getCategory() objtodo.category = this.getCategory()
const lastelem: ITodo = this.getLastList() const lastelem: ITodo = this.getLastList()
objtodo.id_prev = (lastelem !== null) ? lastelem._id : rescodes.LIST_START objtodo.id_prev = (lastelem !== null) ? lastelem._id : rescodes.LIST_START
objtodo.id_next = rescodes.LIST_END objtodo.id_next = rescodes.LIST_END
@@ -467,10 +470,10 @@ export default class Todo extends Vue {
// console.log('calling MODIFY 4', lastelem) // console.log('calling MODIFY 4', lastelem)
} }
const rismod = await this.modify(lastelem, false) await this.modify(lastelem, false)
this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS, 'POST', objtodo, true) await this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS, 'POST', objtodo, true)
this.updatetable(false, 'insertTodo') await this.updatetable(false, 'insertTodo')
// console.log('ESCO.........') // console.log('ESCO.........')
@@ -496,49 +499,44 @@ export default class Todo extends Vue {
const mythis = this const mythis = this
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
await navigator.serviceWorker.ready const ready = await navigator.serviceWorker.ready
.then(function (sw) { .then(function (sw) {
// _id: new Date().toISOString(), // _id: new Date().toISOString(),
// console.log('---------------------- navigator.serviceWorker.ready') // console.log('---------------------- navigator.serviceWorker.ready')
// mythis.sendMessageToSW(item, method) return globalroutines(mythis, 'write', table, item, id)
globalroutines(mythis, 'write', table, item, id)
.then(function (id) { .then(function (id) {
// console.log('id', id) // console.log('id', id)
const sep = '|'
}) let multiparams = cmdSw + sep + table + sep + method + sep + UserStore.state.x_auth_token + sep + UserStore.state.lang
const sep = '|' let mymsgkey = {
_id: multiparams,
let multiparams = cmdSw + sep + table + sep + method + sep + UserStore.state.x_auth_token + sep + UserStore.state.lang value: multiparams
let mymsgkey = { }
_id: multiparams, return globalroutines(mythis, 'write', 'swmsg', mymsgkey, multiparams)
value: multiparams .then(ris => {
} // if ('SyncManager' in window) {
globalroutines(mythis, 'write', 'swmsg', mymsgkey, multiparams) // console.log(' SENDING... sw.sync.register', multiparams)
.then(ris => { // return sw.sync.register(multiparams)
// if ('SyncManager' in window) { // } else {
// console.log(' SENDING... sw.sync.register', multiparams) // #Todo ++ Alternative 2 to SyncManager
// return sw.sync.register(multiparams) return Api.syncAlternative(multiparams)
// } else { // }
// #Todo ++ Alternative 2 to SyncManager })
Api.syncAlternative(multiparams) .then(function () {
// } let data = { message: msg, position: 'bottom', timeout: 3000 }
}) mythis.$q.notify(data)
.then(function () { })
.catch(function (err) {
let snackbarContainer = document.querySelector('#confirmation-toast') console.error('Errore in globalroutines', table, err)
let data = { message: msg } })
// snackbarContainer.MaterialSnackbar.showSnackbar(data)
})
.catch(function (err) {
console.error('Errore in globalroutines', table, err)
}) })
}) })
if (update) { if (update) {
// // Update the array in memory, from todos table from IndexedDb // // Update the array in memory, from todos table from IndexedDb
Todos.actions.updateArrayInMemory() await Todos.actions.updateArrayInMemory()
.then((ris) => { .then((ris) => {
return ris return ris
}) })
@@ -547,11 +545,11 @@ export default class Todo extends Vue {
} else { } else {
if (cmd === rescodes.DB.CMD_SYNC_NEW_TODOS) { if (cmd === rescodes.DB.CMD_SYNC_NEW_TODOS) {
if (method === 'POST') if (method === 'POST')
Todos.actions.dbInsertTodo(item) await Todos.actions.dbInsertTodo(item)
else if (method === 'PATCH') else if (method === 'PATCH')
Todos.actions.dbSaveTodo(item) await Todos.actions.dbSaveTodo(item)
} else if (cmd === rescodes.DB.CMD_DELETE_TODOS) } else if (cmd === rescodes.DB.CMD_DELETE_TODOS)
Todos.actions.dbDeleteTodo(item) await Todos.actions.dbDeleteTodo(item)
} }
} }
@@ -560,8 +558,8 @@ export default class Todo extends Vue {
} }
deleteItemToSyncAndDb(table: String, item: ITodo, id, update: boolean) { async deleteItemToSyncAndDb(table: String, item: ITodo, id, update: boolean) {
return this.cmdToSyncAndDb(rescodes.DB.CMD_DELETE_TODOS, table, 'DELETE', item, id, '', update) return await this.cmdToSyncAndDb(rescodes.DB.CMD_DELETE_TODOS, table, 'DELETE', item, id, '', update)
} }
/* /*
@@ -603,17 +601,17 @@ export default class Todo extends Vue {
myobjprev.id_next = myobjtrov.id_next myobjprev.id_next = myobjtrov.id_next
myobjprev.modified = true myobjprev.modified = true
console.log('calling MODIFY 2') console.log('calling MODIFY 2')
this.modify(myobjprev, false) await this.modify(myobjprev, false)
} }
if (myobjnext !== null) { if (myobjnext !== null) {
myobjnext.id_prev = myobjtrov.id_prev myobjnext.id_prev = myobjtrov.id_prev
myobjnext.modified = true myobjnext.modified = true
console.log('calling MODIFY 1') console.log('calling MODIFY 1')
this.modify(myobjnext, false) await this.modify(myobjnext, false)
} }
this.deleteItemToSyncAndDb(rescodes.DB.TABLE_DELETE_TODOS, myobjtrov, id, true) await this.deleteItemToSyncAndDb(rescodes.DB.TABLE_DELETE_TODOS, myobjtrov, id, true)
const mythis = this const mythis = this
// Delete item // Delete item
@@ -815,7 +813,7 @@ export default class Todo extends Vue {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
resolve() resolve()
}) })
await globalroutines(this, 'read', 'todos', null, myobj._id) return await globalroutines(this, 'read', 'todos', null, myobj._id)
.then(miorec => { .then(miorec => {
if (miorec === undefined) { if (miorec === undefined) {
console.log('~~~~~~~~~~~~~~~~~~~~ !!!!!!!!!!!!!!!!!! Record not Found !!!!!! id=', myobj._id) console.log('~~~~~~~~~~~~~~~~~~~~ !!!!!!!!!!!!!!!!!! Record not Found !!!!!! id=', myobj._id)
@@ -837,15 +835,15 @@ export default class Todo extends Vue {
// this.logelem('modify', miorec) // this.logelem('modify', miorec)
globalroutines(this, 'write', 'todos', miorec) return globalroutines(this, 'write', 'todos', miorec)
.then(ris => { .then(ris => {
this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS_PATCH, 'PATCH', miorec, update) return this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS_PATCH, 'PATCH', miorec, update)
.then(() => { .then(() => {
// console.log('SET MODIFIED FALSE') // console.log('SET MODIFIED FALSE')
if (update) if (update)
this.updatetable(false, 'modify') return this.updatetable(false, 'modify')
}) })
}) })

View File

@@ -37,16 +37,16 @@
<!--{{ tmpstrTodos }}--> <!--{{ tmpstrTodos }}-->
<!--<div class="flex-item btn-item">--> <div class="flex-item btn-item">
<!--<q-btn class="mybtn" round color="" icon="lock" @click="getArrTodos">Get Todo</q-btn>--> <q-btn class="mybtn" round color="" icon="lock" @click="getArrTodos">Get Todo</q-btn>
<!--<q-btn class="mybtn" round color="" icon="person" @click="setArrTodos">Set Todo</q-btn>--> <q-btn class="mybtn" round color="" icon="person" @click="setArrTodos">Set Todo</q-btn>
<!--<q-btn class="mybtn" round color="" icon="list" @click="reload_fromServer++">Reload</q-btn>--> <q-btn class="mybtn" round color="" icon="list" @click="reload_fromServer++">Reload</q-btn>
<!--</div>--> </div>
<!--<q-input v-model="testPao" float-label="testPao"/>--> <!--<q-input v-model="testPao" float-label="testPao"/>-->
<!--<q-input v-model="todos_changed" float-label="todos_changed"/>--> <q-input v-model="todos_changed" float-label="todos_changed"/>
<!--<q-input v-model="reload_fromServer" float-label="reload_fromServer"/>--> <q-input v-model="reload_fromServer" float-label="reload_fromServer"/>
<!--<div class="flex-item btn-item">--> <!--<div class="flex-item btn-item">-->
<!--&lt;!&ndash;<q-btn class="mybtn" round color="" icon="lock" @click="clicktest()"></q-btn>&ndash;&gt;--> <!--&lt;!&ndash;<q-btn class="mybtn" round color="" icon="lock" @click="clicktest()"></q-btn>&ndash;&gt;-->

View File

@@ -90,17 +90,23 @@ namespace Actions {
// console.log('---------------------- 2) navigator (2) .serviceWorker.ready') // console.log('---------------------- 2) navigator (2) .serviceWorker.ready')
let promiseChain = Promise.resolve()
something = true something = true
for (let rec of arr_recmsg) { for (let rec of arr_recmsg) {
// console.log(' .... sw.sync.register ( ', rec._id) // console.log(' .... sw.sync.register ( ', rec._id)
// if ('SyncManager' in window) { // if ('SyncManager' in window) {
// sw.sync.register(rec._id) // sw.sync.register(rec._id)
// } else { // } else {
// #Todo ++ Alternative to SyncManager
Api.syncAlternative(rec._id) // #Alternative to SyncManager
promiseChain = promiseChain.then(() => {
return Api.syncAlternative(rec._id)
})
// } // }
} }
return something return promiseChain
} }
}) })
@@ -108,7 +114,9 @@ namespace Actions {
} }
} }
return something return new Promise(function (resolve, reject) {
resolve(something)
})
} }
async function waitAndcheckPendingMsg(context) { async function waitAndcheckPendingMsg(context) {
@@ -155,19 +163,11 @@ namespace Actions {
} }
} }
} catch (e) { } catch (e) {
} }
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
/*
globalroutines(null, 'readall', 'swmsg')
.then(function (arr_recmsg) {
if (arr_recmsg.length > 0) {
*/
// Check if there is something // Check if there is something
globalroutines(null, 'count', 'swmsg') return globalroutines(null, 'count', 'swmsg')
.then(function (count) { .then(function (count) {
if (count > 0) { if (count > 0) {
console.log('count = ', count) console.log('count = ', count)

View File

@@ -371,7 +371,7 @@ namespace Actions {
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
sub = await navigator.serviceWorker.ready sub = await navigator.serviceWorker.ready
.then(function (swreg) { .then(function (swreg) {
const sub = swreg.pushManager.getSubscription() let sub = swreg.pushManager.getSubscription()
return sub return sub
}) })
.catch(e => { .catch(e => {