Skip to content

Commit

Permalink
Code Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tohjustin committed Jan 21, 2017
1 parent f28fd73 commit f3a1842
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 32 deletions.
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-77107001-3', 'auto');
ga('send', 'pageview');
/* eslint-enable */
Expand Down
1 change: 1 addition & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export default {
_ringAlarm: function () {
let ring = () => {
if (this.allowMelody) {
this.$refs.audio.pause()
this.$refs.audio.currentTime = 0
this.$refs.audio.play()
}
Expand Down
2 changes: 0 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// import swURL from 'file?name=sw.js!babel!./sw' // eslint-disable-line no-unused-vars

// Register Service Worker
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
Expand Down
39 changes: 12 additions & 27 deletions src/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ const urlsToCache = [
'/sw.js',
'/index.html',
'/manifest.json',
'/static/alarm.mp3',
// '/static/alarm.mp3',
'/static/logo.png',
'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons',
'https://fonts.gstatic.com/s/materialicons/v19/2fcrYFNaTjcS6g4U3t-Y5UEw0lE80llgEseQY3FEmqw.woff2'
]

let generatePrecacheList = (assetJson, urlArray) => {
// Combine the list of files contained in our "assetJson" & "urlsToCache"
let _generatePrecacheList = (assetJson, urlArray) => {
return urlArray.concat(Object.keys(assetJson).map(function (val) { return assetJson[val] }))
}

Expand All @@ -25,18 +26,7 @@ self.addEventListener('install', event => {
fetch('asset-manifest.json')
.then(response => response.json())
.then(assets => {
cache.addAll(generatePrecacheList(assets, urlsToCache))
// let listOfItemsToCache = _.chain(assets).values().join(urlsToCache).uniq()
// cache.addAll(listOfItemsToCache)
// cache.addAll([
// '/',
// '/sw.js',
// '/index.html',
// '/manifest.json',
// '/static/alarm.mp3',
// '/static/logo.png',
// 'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons'
// ])
cache.addAll(_generatePrecacheList(assets, urlsToCache))
})
.catch(response => {
console.log('Fail to fetch asset-manifest.json!')
Expand All @@ -51,21 +41,16 @@ self.addEventListener('activate', event => {
})

self.addEventListener('fetch', event => {
// Don't intercept requests to google analytics
if (event.request.url.startsWith('https://www.google-analytics.com/')) {
return fetch(event.request)
} else {
// Return cached content if there's a cache hit
event.respondWith(
caches.open(CACHE_NAME)
.then(cache => cacheableRequestFailingToCacheStrategy({ event, cache }))
)
}
// Return cached content if there's a cache hit
event.respondWith(
caches.open(CACHE_NAME)
.then(cache => _cacheableRequestFailingToCacheStrategy({ event, cache }))
)
})

let cacheableRequestFailingToCacheStrategy = ({ event, cache }) => {
let _cacheableRequestFailingToCacheStrategy = ({ event, cache }) => {
return fetch(event.request)
.then(throwOnError) // do not cache errors
.then(_throwOnError) // do not cache errors
.then(response => {
cache.put(event.request, response.clone())
return response
Expand All @@ -74,7 +59,7 @@ let cacheableRequestFailingToCacheStrategy = ({ event, cache }) => {
}

// Prevent cache being polluted with failure responses
let throwOnError = response => {
let _throwOnError = response => {
if (response.status < 200 || response.status >= 300) {
throw new Error(response.statusText)
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/specs/App.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ describe('App', () => {
// vm._stopAlarm.restore()
const settings = [
{ allowMelody: false, allowVibration: false },
{ allowMelody: true, allowVibration: true },
{ allowMelody: false, allowVibration: true },
{ allowMelody: true, allowVibration: false },
{ allowMelody: true, allowVibration: true }
]
Expand Down Expand Up @@ -641,7 +641,7 @@ describe('App', () => {

vm._ringAlarm()
expect(spy).to.have.callCount(1)
expect(spy2).to.have.callCount(0)
expect(spy2).to.have.callCount(1)
expect(spy3).to.have.callCount(0)
expect(vm.alarmWorker).not.to.be.null
})
Expand Down

0 comments on commit f3a1842

Please sign in to comment.