Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

Commit

Permalink
Fix ESLint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mxstbr committed Mar 15, 2018
1 parent 69a9c01 commit c34663e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 84 deletions.
175 changes: 91 additions & 84 deletions public/push-sw.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,91 @@
/*
* Push notificiations ServiceWorker
*
* NOTE(@mxstbr): This only gets updated if you change the filename, not if you
* update the contents. Make sure to change the version number in the filename
* after updating the code!
*/

// A new notification is coming in, yay!
self.addEventListener('push', function(event) {
var notificationData = {};

try {
notificationData = event.data.json();
} catch (e) {
console.log('event.data.json() failed', e);
// 🚨 We either got no data or it's malformatted, ABORT ABORT ABORT
return;
}

// Check if the user is looking at Spectrum right now and don't show a notification
event.waitUntil(
clients
.matchAll({
type: 'window',
includeUncontrolled: true,
})
.then(windowClients => {
for (let i = 0; i < windowClients.length; i++) {
const windowClient = windowClients[i];
// The user is looking at Spectrum right now abort showing the notification!
// (except for if we're on localhost, i.e. in development)
if (
windowClient.focused &&
!(self.registration.scope.indexOf('http://localhost:3000') === 0)
) {
return;
}
}
return self.registration.showNotification(notificationData.title, {
vibrate: [200],
icon: '/img/apple-icon-144x144-precomposed.png',
badge: '/img/badge.png',
body: notificationData.body,
title: notificationData.title,
timestamp: notificationData.timestamp,
image: notificationData.image,
tag: notificationData.tag,
data: notificationData.data,
// If we don't set a tag and set renotify to true this'll throw an error
// renotify: notificationData.renotify || !!notificationData.tag,
});
})
);
});

// On notification click
self.addEventListener('notificationclick', function(event) {
event.notification.close();

const urlToOpen =
event.notification.data && event.notification.data.href
? new URL(event.notification.data.href, self.location.origin).href
: '/';

// see if the current is open and if it is focus it
event.waitUntil(
self.clients
.matchAll({
type: 'window',
includeUncontrolled: true,
})
.then(function(clientList) {
// If there is an open Spectrum.chat window navigate to the notification href
if (clientList.length > 0) {
return clientList[0]
.focus()
.then(client => client.navigate(urlToOpen));
}
// If there's no open Spectrum.chat window open a new one
return self.clients.openWindow(urlToOpen);
})
);
});
/*
* Push notificiations ServiceWorker
*
* NOTE(@mxstbr): This only gets updated if you change the filename, not if you
* update the contents. Make sure to change the version number in the filename
* after updating the code!
*/

// A new notification is coming in, yay!
// eslint-disable-next-line
self.addEventListener('push', function(event) {
var notificationData = {};

try {
notificationData = event.data.json();
} catch (e) {
console.log('event.data.json() failed', e);
// 🚨 We either got no data or it's malformatted, ABORT ABORT ABORT
return;
}

// Check if the user is looking at Spectrum right now and don't show a notification
event.waitUntil(
clients
.matchAll({
type: 'window',
includeUncontrolled: true,
})
.then(windowClients => {
for (let i = 0; i < windowClients.length; i++) {
const windowClient = windowClients[i];
// The user is looking at Spectrum right now abort showing the notification!
// (except for if we're on localhost, i.e. in development)
if (
windowClient.focused &&
// eslint-disable-next-line
!(self.registration.scope.indexOf('http://localhost:3000') === 0)
) {
return;
}
}
// eslint-disable-next-line
return self.registration.showNotification(notificationData.title, {
vibrate: [200],
icon: '/img/apple-icon-144x144-precomposed.png',
badge: '/img/badge.png',
body: notificationData.body,
title: notificationData.title,
timestamp: notificationData.timestamp,
image: notificationData.image,
tag: notificationData.tag,
data: notificationData.data,
// If we don't set a tag and set renotify to true this'll throw an error
// renotify: notificationData.renotify || !!notificationData.tag,
});
})
);
});

// On notification click
// eslint-disable-next-line
self.addEventListener('notificationclick', function(event) {
event.notification.close();

const urlToOpen =
event.notification.data && event.notification.data.href
? // eslint-disable-next-line
new URL(event.notification.data.href, self.location.origin).href
: '/';

// see if the current is open and if it is focus it
event.waitUntil(
// eslint-disable-next-line
self.clients
.matchAll({
type: 'window',
includeUncontrolled: true,
})
.then(function(clientList) {
// If there is an open Spectrum.chat window navigate to the notification href
if (clientList.length > 0) {
return clientList[0]
.focus()
.then(client => client.navigate(urlToOpen));
}
// If there's no open Spectrum.chat window open a new one
// eslint-disable-next-line
return self.clients.openWindow(urlToOpen);
})
);
});
1 change: 1 addition & 0 deletions public/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable */
// A simple, no-op service worker that takes immediate control.
// from: https://stackoverflow.com/a/38980776

Expand Down

0 comments on commit c34663e

Please sign in to comment.