Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```js
- export const getNotifications = async (title) => {
- let ASYNC_TIMEOUT = 10000 // 10s
- let didTimeout = false
- let notifications = []
- const workerDelayedPromise = () => {
- return new Promise(async (resolve, reject) => {
- // timeout in order to send reject if delayed
- const timeout = setTimeout(() => {
- didTimeout = true
- reject('Request notifications timed out')
- }, ASYNC_TIMEOUT)
- // clearTimeout and didTimeout to false
- const cleanTimeout = () => {
- clearTimeout(timeout)
- didTimeout = false
- }
- // get current serviceWorker
- let worker = await serviceWorker
- try {
- // get notifications was open in the navigator
- let response = await worker.getNotifications()
- // running only if timeout(ASYNC_TIMEOUT) still exists
- cleanTimeout()
- if (!didTimeout){
- resolve(response)
- }
- } catch (error) {
- reject(error)
- }
- })
- }
- try {
- notifications = await workerDelayedPromise()
- if (notifications.length <= 0) return []
- if (title){
- return notifications.filter(item => {
- if (item.title == title){
- return item
- }
- })
- }
- return notifications
- } catch (error) {
- console.error(error)
- return []
- }
- }
- ```
Add Comment
Please, Sign In to add comment