- creating Alternative to SyncManager
- fix: refreshing with FF now it works!
This commit is contained in:
@@ -10,6 +10,7 @@ import Paths from '@paths'
|
||||
import { rescodes } from '@src/store/Modules/rescodes'
|
||||
|
||||
import { UserStore } from '@modules'
|
||||
import globalroutines from './../../globalroutines/index'
|
||||
|
||||
|
||||
// const algoliaApi = new AlgoliaSearch()
|
||||
@@ -52,8 +53,11 @@ export namespace ApiTool {
|
||||
sendRequest(url, lang, mytok, method, mydata)
|
||||
.then(resreceived => {
|
||||
ricevuto = true
|
||||
let res = resreceived
|
||||
console.log('SendReq RES=', res)
|
||||
let res = resreceived.clone()
|
||||
if (process.env.DEV) {
|
||||
console.log('SendReq RES [', res.status, ']', res)
|
||||
}
|
||||
|
||||
let x_auth_token = ''
|
||||
if (res.status === 200) {
|
||||
try {
|
||||
@@ -105,6 +109,75 @@ export namespace ApiTool {
|
||||
})
|
||||
}
|
||||
|
||||
export async function syncAlternative(mystrparam) {
|
||||
console.log('[ALTERNATIVE Background syncing', mystrparam)
|
||||
|
||||
let multiparams = mystrparam.split('|')
|
||||
if (multiparams) {
|
||||
if (multiparams.length > 3) {
|
||||
let cmd = multiparams[0]
|
||||
let table = multiparams[1]
|
||||
let method = multiparams[2]
|
||||
let token = multiparams[3]
|
||||
// let lang = multiparams[3]
|
||||
|
||||
if (cmd === 'sync-todos') {
|
||||
console.log('[Alternative] Syncing', cmd, table, method)
|
||||
|
||||
const headers = new Headers()
|
||||
headers.append('content-Type', 'application/json')
|
||||
headers.append('Accept', 'application/json')
|
||||
headers.append('x-auth', token)
|
||||
|
||||
console.log('A1) INIZIO.............................................................')
|
||||
|
||||
await globalroutines(null, 'readall', table, null)
|
||||
.then(function (alldata) {
|
||||
const myrecs = [...alldata]
|
||||
console.log('----------------------- LEGGO QUALCOSA ')
|
||||
if (myrecs) {
|
||||
for (let rec of myrecs) {
|
||||
// console.log('syncing', table, '', rec.descr)
|
||||
let link = process.env.MONGODB_HOST + '/todos'
|
||||
|
||||
if (method !== 'POST')
|
||||
link += '/' + rec._id
|
||||
|
||||
console.log(' [Alternative] ++++++++++++++++++ SYNCING !!!! ', rec.descr, table, 'FETCH: ', method, link, 'data:')
|
||||
|
||||
// Insert/Delete/Update table to the server
|
||||
fetch(link, {
|
||||
method: method,
|
||||
headers: headers,
|
||||
mode: 'cors', // 'no-cors',
|
||||
body: JSON.stringify(rec)
|
||||
})
|
||||
.then(function (resData) {
|
||||
// console.log('Result CALL ', method, ' OK? =', resData.ok);
|
||||
|
||||
// Anyway Delete this, otherwise in some cases will return error, but it's not a problem.
|
||||
// for example if I change a record and then I deleted ...
|
||||
// if (resData.ok) {
|
||||
// deleteItemFromData(table, rec._id);
|
||||
globalroutines(null, 'delete', table, null, rec._id)
|
||||
|
||||
console.log('DELETE: ', mystrparam)
|
||||
// deleteItemFromData('swmsg', mystrparam)
|
||||
globalroutines(null, 'delete', 'swmsg', null, mystrparam)
|
||||
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.log(' [Alternative] !!!!!!!!!!!!!!! Error while sending data', err)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
console.log(' [Alternative] A2) ?????????????????????????? ESCO DAL LOOP !!!!!!!!! err=')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
export default ApiTool
|
||||
|
||||
@@ -3,8 +3,9 @@ import { storeBuilder } from './Store/Store'
|
||||
|
||||
import Api from '@api'
|
||||
import { rescodes } from './rescodes'
|
||||
import { Todos, UserStore } from '@store'
|
||||
import { GlobalStore, Todos, UserStore } from '@store'
|
||||
import globalroutines from './../../globalroutines/index'
|
||||
import { Mutation } from "vuex-module-decorators"
|
||||
|
||||
|
||||
const state: ITodosState = {
|
||||
@@ -40,6 +41,8 @@ namespace Mutations {
|
||||
|
||||
function setTodos_changed(state: ITodosState) {
|
||||
state.todos_changed++
|
||||
mutations.setTestpao('Cambiato : ' + String(state.todos_changed))
|
||||
console.log('******************************* state.todos_changed', state.todos_changed)
|
||||
}
|
||||
|
||||
export const mutations = {
|
||||
@@ -67,14 +70,13 @@ namespace Actions {
|
||||
|
||||
// If something in the call of Service Worker went wrong (Network or Server Down), then retry !
|
||||
async function sendSwMsgIfAvailable() {
|
||||
|
||||
console.log(' -------- sendSwMsgIfAvailable')
|
||||
|
||||
let something = false
|
||||
|
||||
let count = await checkPendingMsg(null)
|
||||
if (count > 0) {
|
||||
if (('serviceWorker' in navigator && 'SyncManager' in window)) {
|
||||
if ('serviceWorker' in navigator) {
|
||||
console.log(' -------- sendSwMsgIfAvailable')
|
||||
|
||||
let count = await checkPendingMsg(null)
|
||||
if (count > 0) {
|
||||
return navigator.serviceWorker.ready
|
||||
.then(function (sw) {
|
||||
|
||||
@@ -90,14 +92,21 @@ namespace Actions {
|
||||
something = true
|
||||
for (let rec of arr_recmsg) {
|
||||
console.log(' .... sw.sync.register ( ', rec._id)
|
||||
sw.sync.register(rec._id)
|
||||
if ('SyncManager' in window) {
|
||||
sw.sync.register(rec._id)
|
||||
} else {
|
||||
// #Todo ++ Alternative to SyncManager
|
||||
Api.syncAlternative(rec._id)
|
||||
}
|
||||
}
|
||||
return something
|
||||
}
|
||||
return something
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return something
|
||||
}
|
||||
|
||||
@@ -170,12 +179,12 @@ namespace Actions {
|
||||
state.networkDataReceived = false
|
||||
|
||||
let ris = await Api.SendReq(call, UserStore.state.lang, token, 'GET', null)
|
||||
.then(({resData, body}) => {
|
||||
.then(({ resData, body }) => {
|
||||
state.networkDataReceived = true
|
||||
|
||||
// console.log('******* UPDATE TODOS.STATE.TODOS !:', resData.todos)
|
||||
state.todos = [...body.todos]
|
||||
Todos.state.todos_changed++
|
||||
Todos.mutations.setTodos_changed()
|
||||
|
||||
console.log('state.todos', state.todos, 'checkPending', checkPending)
|
||||
|
||||
@@ -222,7 +231,7 @@ namespace Actions {
|
||||
async function testfunc() {
|
||||
while (true) {
|
||||
consolelogpao('testfunc')
|
||||
Todos.state.todos_changed++
|
||||
Todos.mutations.setTodos_changed()
|
||||
// console.log('Todos.state.todos_changed:', Todos.state.todos_changed)
|
||||
await aspettansec(5000)
|
||||
}
|
||||
@@ -241,6 +250,7 @@ namespace Actions {
|
||||
console.log('ITEM', newItem)
|
||||
if (method === 'POST') {
|
||||
state.todos.push(newItem)
|
||||
Todos.mutations.setTodos_changed()
|
||||
// } else if (method === 'PATCH') {
|
||||
// state.todos.map(item => {
|
||||
// if (item._id === newItem._id) {
|
||||
@@ -255,22 +265,20 @@ namespace Actions {
|
||||
|
||||
async function dbInsertSaveTodo(context, itemtodo: ITodo, method) {
|
||||
console.log('dbInsertSaveTodo', itemtodo, method)
|
||||
let call = process.env.MONGODB_HOST + '/todos/' + itemtodo._id
|
||||
let call = process.env.MONGODB_HOST + '/todos'
|
||||
|
||||
if (method !== 'POST')
|
||||
call += '/' + itemtodo._id
|
||||
|
||||
const token = UserStore.state.idToken
|
||||
|
||||
let res = await Api.SendReq(call, UserStore.state.lang, token, method, itemtodo)
|
||||
.then(({res, body}) => {
|
||||
console.log('RESDATA =', body)
|
||||
if (body.newItem) {
|
||||
const newId = body.newItem._id
|
||||
|
||||
// if (method === 'PATCH') {
|
||||
// newItem = newItem.todo
|
||||
// }
|
||||
.then(({ res, newItem }) => {
|
||||
console.log('dbInsertSaveTodo RIS =', newItem)
|
||||
if (newItem) {
|
||||
|
||||
// Update ID on local
|
||||
UpdateNewIdFromDB(itemtodo, body.newItem, method)
|
||||
UpdateNewIdFromDB(itemtodo, newItem, method)
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
@@ -288,10 +296,14 @@ namespace Actions {
|
||||
const token = UserStore.state.idToken
|
||||
|
||||
let res = await Api.SendReq(call, UserStore.state.lang, token, 'DELETE', item)
|
||||
.then(function ({res, body}) {
|
||||
.then(function ({ res, itemris }) {
|
||||
|
||||
// Delete Item in to Array
|
||||
state.todos.splice(state.todos.indexOf(item), 1)
|
||||
if (res.status === 200) {
|
||||
// Delete Item in to Array
|
||||
state.todos.splice(state.todos.indexOf(item), 1)
|
||||
|
||||
Todos.mutations.setTodos_changed()
|
||||
}
|
||||
|
||||
return rescodes.OK
|
||||
})
|
||||
|
||||
@@ -24,10 +24,10 @@ const state: IUserState = {
|
||||
repeatPassword: '',
|
||||
idToken: '',
|
||||
tokens: [],
|
||||
verifiedEmail: false,
|
||||
verified_email: false,
|
||||
categorySel: 'personal',
|
||||
servercode: 0,
|
||||
x_auth_token: '',
|
||||
x_auth_token: ''
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ namespace Getters {
|
||||
},
|
||||
get getServerCode() {
|
||||
return getServerCode()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ namespace Mutations {
|
||||
state.userId = data.userId
|
||||
state.username = data.username
|
||||
state.idToken = data.idToken
|
||||
state.verifiedEmail = data.verifiedEmail
|
||||
state.verified_email = data.verified_email
|
||||
state.category = data.categorySel
|
||||
// @ts-ignore
|
||||
state.tokens = [
|
||||
@@ -130,7 +130,7 @@ namespace Mutations {
|
||||
state.username = ''
|
||||
state.tokens = []
|
||||
state.idToken = ''
|
||||
state.verifiedEmail = false
|
||||
state.verified_email = false
|
||||
state.categorySel = 'personal'
|
||||
}
|
||||
|
||||
@@ -299,19 +299,13 @@ namespace Actions {
|
||||
let x_auth_token: string = ''
|
||||
|
||||
return Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
||||
.then(({ res, body }) => {
|
||||
.then(({ res, newuser }) => {
|
||||
myres = res
|
||||
if (process.env.DEV) {
|
||||
console.log('RISULTATO ')
|
||||
console.log('STATUS ' + myres.status + ' ' + (myres.statusText))
|
||||
console.log('BODY:')
|
||||
console.log(body)
|
||||
}
|
||||
|
||||
Mutations.mutations.setServerCode(myres.status)
|
||||
|
||||
if (myres.status === 200) {
|
||||
let userId = body.userId
|
||||
let userId = newuser.userId
|
||||
let username = authData.username
|
||||
if (process.env.DEV) {
|
||||
console.log('USERNAME = ' + username)
|
||||
@@ -322,7 +316,7 @@ namespace Actions {
|
||||
userId: userId,
|
||||
username: username,
|
||||
idToken: x_auth_token,
|
||||
verifiedEmail: false
|
||||
verified_email: false
|
||||
})
|
||||
|
||||
const now = new Date()
|
||||
@@ -338,16 +332,8 @@ namespace Actions {
|
||||
// dispatch('setLogoutTimer', myres.data.expiresIn);
|
||||
|
||||
return rescodes.OK
|
||||
} else if (myres.status === 404) {
|
||||
if (process.env.DEV) {
|
||||
console.log('CODE = ' + body.code)
|
||||
}
|
||||
return body.code
|
||||
} else {
|
||||
if (process.env.DEV) {
|
||||
console.log('CODE = ' + body.code)
|
||||
}
|
||||
return body.code
|
||||
return rescodes.ERR_GENERICO
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
@@ -380,14 +366,7 @@ namespace Actions {
|
||||
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
||||
.then(({ res, body }) => {
|
||||
myres = res
|
||||
if (process.env.DEV) {
|
||||
console.log('RISULTATO ')
|
||||
console.log('STATUS ' + res.status + ' ' + (res.statusText))
|
||||
console.log('BODY:')
|
||||
console.log(body)
|
||||
}
|
||||
|
||||
if (body.code === serv_constants.RIS_CODE_LOGIN_ERR) {
|
||||
if (res.code === serv_constants.RIS_CODE_LOGIN_ERR) {
|
||||
Mutations.mutations.setServerCode(body.code)
|
||||
return body.code
|
||||
}
|
||||
@@ -395,9 +374,10 @@ namespace Actions {
|
||||
Mutations.mutations.setServerCode(myres.status)
|
||||
|
||||
if (myres.status === 200) {
|
||||
let userId = body.userId
|
||||
let myuser: IUserState = body.usertosend
|
||||
let userId = myuser.userId
|
||||
let username = authData.username
|
||||
let verifiedEmail = body.verified_email === 'true' || body.verified_email === true
|
||||
let verifiedEmail = myuser.verified_email === true
|
||||
if (process.env.DEV) {
|
||||
console.log('USERNAME = ' + username)
|
||||
console.log('IDUSER= ' + userId)
|
||||
@@ -405,7 +385,7 @@ namespace Actions {
|
||||
userId: userId,
|
||||
username: username,
|
||||
idToken: state.x_auth_token,
|
||||
verifiedEmail: verifiedEmail
|
||||
verified_email: verifiedEmail
|
||||
})
|
||||
}
|
||||
|
||||
@@ -520,7 +500,7 @@ namespace Actions {
|
||||
userId: userId,
|
||||
username: username,
|
||||
idToken: token,
|
||||
verifiedEmail: verifiedEmail
|
||||
verified_email: verifiedEmail
|
||||
})
|
||||
|
||||
setGlobal()
|
||||
|
||||
Reference in New Issue
Block a user