Notification with Service Workers now is working!

When a Notification arrives, it save into the IndexDb,
then in Vue.js call a polling to check in the db if there is a different record count.
If is different then call a get to update the notification.
This commit is contained in:
paoloar77
2022-08-07 02:01:20 +02:00
parent 66ee007e92
commit ce20daed6d
27 changed files with 411 additions and 232 deletions

View File

@@ -9,12 +9,12 @@ import { useGlobalStore } from '@store/globalStore'
import globalroutines from '../../globalroutines/index'
import { useProjectStore } from '@store/Projects'
import { useTodoStore } from '@store/Todos'
import { useNotifStore } from '@store/NotifStore'
export function getLinkByTableName(nametable: string) {
if (nametable === 'todos') {
return 'todos'
}
if (nametable === 'projects') {
} else if (nametable === 'projects') {
return 'projects'
}
return ''
@@ -34,13 +34,13 @@ export const DB = {
}
export function allTables() {
/* const myarr = OtherTables
for (const tab of costanti.MainTables) {
for (const method of costanti.allMethod) {
const myarr = OtherTables
for (const tab of MainTables) {
for (const method of allMethod) {
myarr.push(method + tab)
}
} */
return costanti.OtherTables
}
return myarr
}
async function updatefromIndexedDbToState(nametab: string) {
@@ -52,7 +52,7 @@ async function updatefromIndexedDbToState(nametab: string) {
}
async function checkPendingMsg() {
// console.log('checkPendingMsg')
console.log('checkPendingMsg')
const globalStore = useGlobalStore()
const config = await globalroutines('read', 'config', null, '1')
@@ -82,8 +82,26 @@ async function checkPendingMsg() {
}).catch(() => reject()))
}
async function checkPendingNotif() {
const notifStore = useNotifStore()
const howmanybefore = notifStore.countNotif
// eslint-disable-next-line @typescript-eslint/no-misused-promises
return new Promise((resolve, reject) => globalroutines('count', 'notifications')
.then((count) => {
if (count !== howmanybefore) {
notifStore.setCountNotifs(count)
return resolve(true)
}
return resolve(false)
}).catch(() => reject()))
}
function useServiceWorker() {
return false // return 'serviceWorker' in navigator
return 'serviceWorker' in navigator
// return false //
}
// If something in the call of Service Worker went wrong (Network or Server Down), then retry !
@@ -99,7 +117,7 @@ async function sendSwMsgIfAvailable() {
.then(() => globalroutines('readall', 'swmsg')
.then((arr_recmsg) => {
if (arr_recmsg.length > 0) {
// console.log('---------------------- 2) navigator (2) .serviceWorker.ready')
console.log('---------------------- 2) navigator (2) .serviceWorker.ready')
let promiseChain = Promise.resolve()
for (const rec of arr_recmsg) {
@@ -130,6 +148,14 @@ export async function waitAndRefreshData() {
return await todos.dbLoad({ checkPending: false })
}
export function waitAndRefreshNotif() {
const notifStore = useNotifStore()
notifStore.updateNotification = true
}
export async function waitAndcheckPendingMsg() {
// await aspettansec(1000)
const globalStore = useGlobalStore()
@@ -157,6 +183,21 @@ export async function waitAndcheckPendingMsg() {
})
}
export async function waitAndcheckPendingNotif() {
// await aspettansec(1000)
const globalStore = useGlobalStore()
return checkPendingNotif()
.then((ris) => {
if (ris) {
if (globalStore.isOnline()) { // If is Offline, then check
return waitAndRefreshNotif()
}
}
})
}
async function dbInsertSave(call: string, item: any, method: string) {
let ret = true
const userStore = useUserStore()