This repository has been archived by the owner on Jan 15, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
313 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// import Constants from './constants/constants'; | ||
import Utils from './helpers/utils'; | ||
import idbKeyval from '../third_party/libs/idb-keyval'; | ||
|
||
class BackgroundFetchHelper { | ||
|
||
constructor () { | ||
this._onBackgroundFetched = this._onBackgroundFetched.bind(this); | ||
this._onBackgroundFetchFailed = this._onBackgroundFetchFailed.bind(this); | ||
|
||
self.addEventListener('backgroundfetchfail', | ||
this._onBackgroundFetchFailed); | ||
|
||
self.addEventListener('backgroundfetched', | ||
this._onBackgroundFetched); | ||
|
||
this._fetchCallbacks = null; | ||
} | ||
|
||
fetch (tag, assets, callbacks) { | ||
// Store the assets that are being background fetched because there may be | ||
// a teardown in the SW and the responses will need to be hooked back | ||
// up when the responses come in. | ||
idbKeyval.set(tag, assets).then(_ => { | ||
const requests = assets.map(asset => { | ||
const {url, options} = asset.responseInfo; | ||
if (options.headers) { | ||
options.headers = new Headers(options.headers); | ||
} | ||
|
||
return new Request(url, options); | ||
}); | ||
|
||
// TODO: Update these to be based on the downloaded show. | ||
const options = { | ||
title: 'Downloading show for offline.' | ||
}; | ||
this._fetchCallbacks = callbacks; | ||
registration.backgroundFetch.fetch(tag, requests, options); | ||
}); | ||
} | ||
|
||
_broadcastToAllClients () { | ||
console.log('Broadcast to all.'); | ||
} | ||
|
||
_onBackgroundFetched (evt) { | ||
if (!this._fetchCallbacks) { | ||
this._broadcastToAllClients(); | ||
return; | ||
} | ||
|
||
const tag = evt.tag; | ||
idbKeyval.get(tag).then(assets => { | ||
if (!assets) { | ||
console.error('Unknown background fetch.'); | ||
return; | ||
} | ||
|
||
return caches.open(tag).then(cache => { | ||
const fetches = evt.fetches; | ||
return Promise.all(assets.map(asset => { | ||
const fetch = fetches.find(r => { | ||
return (r.response.url === asset.responseInfo.url || | ||
r.response.url.endsWith(asset.responseInfo.url)); | ||
}); | ||
|
||
if (!fetch) { | ||
console.log( | ||
`Unable to find response for ${asset.responseInfo.url}`); | ||
return; | ||
} | ||
|
||
const response = fetch.response; | ||
if (!asset.chunk) { | ||
return cache.put(asset.request, response); | ||
} | ||
|
||
return Utils.cacheInChunks(cache, response); | ||
})); | ||
}).then(_ => { | ||
this._fetchCallbacks.onBackgroundFetched.call(); | ||
this._teardown(evt.tag); | ||
}); | ||
}); | ||
} | ||
|
||
_onBackgroundFetchFailed (evt) { | ||
this._fetchCallbacks.onBackgroundFetchFailed.call(); | ||
this._teardown(evt.tag); | ||
} | ||
|
||
_teardown (tag) { | ||
this._fetchCallbacks = null; | ||
return idbKeyval.delete(tag); | ||
} | ||
} | ||
|
||
self.BackgroundFetchHelper = new BackgroundFetchHelper(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.