aboutsummaryrefslogtreecommitdiff
path: root/node_modules/workbox-broadcast-cache-update/BroadcastCacheUpdate.mjs
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/workbox-broadcast-cache-update/BroadcastCacheUpdate.mjs
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
Diffstat (limited to 'node_modules/workbox-broadcast-cache-update/BroadcastCacheUpdate.mjs')
-rw-r--r--node_modules/workbox-broadcast-cache-update/BroadcastCacheUpdate.mjs99
1 files changed, 99 insertions, 0 deletions
diff --git a/node_modules/workbox-broadcast-cache-update/BroadcastCacheUpdate.mjs b/node_modules/workbox-broadcast-cache-update/BroadcastCacheUpdate.mjs
new file mode 100644
index 00000000..650af241
--- /dev/null
+++ b/node_modules/workbox-broadcast-cache-update/BroadcastCacheUpdate.mjs
@@ -0,0 +1,99 @@
+/*
+ Copyright 2016 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.
+*/
+
+import {WorkboxError} from 'workbox-core/_private/WorkboxError.mjs';
+import {responsesAreSame} from './utils/responsesAreSame.mjs';
+import {broadcastUpdate} from './broadcastUpdate.mjs';
+import './_version.mjs';
+
+/**
+ * Uses the [Broadcast Channel API]{@link https://developers.google.com/web/updates/2016/09/broadcastchannel}
+ * to notify interested parties when a cached response has been updated.
+ *
+ * For efficiency's sake, the underlying response bodies are not compared;
+ * only specific response headers are checked.
+ *
+ * @memberof workbox.broadcastUpdate
+ */
+class BroadcastCacheUpdate {
+ /**
+ * Construct a BroadcastCacheUpdate instance with a specific `channelName` to
+ * broadcast messages on
+ *
+ * @param {string} channelName The name that will be used when creating
+ * the `BroadcastChannel`.
+ * @param {Object} options
+ * @param {Array<string>}
+ * [options.headersToCheck=['content-length', 'etag', 'last-modified']] A
+ * list of headers that will be used to determine whether the responses
+ * differ.
+ * @param {string} [options.source='workbox-broadcast-cache-update'] An
+ * attribution value that indicates where the update originated.
+ */
+ constructor(channelName, {headersToCheck, source} = {}) {
+ if (process.env.NODE_ENV !== 'production') {
+ if (typeof channelName !== 'string' || channelName.length === 0) {
+ throw new WorkboxError('channel-name-required');
+ }
+ }
+
+ this._channelName = channelName;
+ this._headersToCheck = headersToCheck || [
+ 'content-length',
+ 'etag',
+ 'last-modified',
+ ];
+ this._source = source || 'workbox-broadcast-cache-update';
+
+ // TODO assert typeof headersToCheck instanceof Array
+ }
+
+ /**
+ * @return {BroadcastChannel|undefined} The BroadcastChannel instance used for
+ * broadcasting updates, or undefined if the browser doesn't support the
+ * Broadcast Channel API.
+ *
+ * @private
+ */
+ _getChannel() {
+ if (('BroadcastChannel' in self) && !this._channel) {
+ this._channel = new BroadcastChannel(this._channelName);
+ }
+ return this._channel;
+ }
+
+ /**
+ * Compare two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response)
+ * and send a message via the
+ * {@link https://developers.google.com/web/updates/2016/09/broadcastchannel|Broadcast Channel API}
+ * if they differ.
+ *
+ * Neither of the Responses can be {@link http://stackoverflow.com/questions/39109789|opaque}.
+ *
+ * @param {Response} firstResponse First responses to compare.
+ * @param {Response} secondResponse Second responses to compare.
+ * @param {string} url The URL of the updated request.
+ * @param {string} cacheName Name of the cache the responses belong to.
+ * This is included in the message posted on the broadcast channel.
+ */
+ notifyIfUpdated(firstResponse, secondResponse, url, cacheName) {
+ if (!responsesAreSame(
+ firstResponse, secondResponse, this._headersToCheck)) {
+ broadcastUpdate(this._getChannel(), cacheName, url, this._source);
+ }
+ }
+}
+
+export {BroadcastCacheUpdate};