- creating Alternative to SyncManager

- fix: refreshing with FF now it works!
This commit is contained in:
Paolo Arena
2019-02-08 17:10:25 +01:00
parent 5ee0d8e171
commit b65d0a2386
18 changed files with 340 additions and 195 deletions

View File

@@ -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
})

View File

@@ -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()