aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/extendableevent
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/extendableevent
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/extendableevent')
-rw-r--r--files/zh-cn/web/api/extendableevent/index.html197
-rw-r--r--files/zh-cn/web/api/extendableevent/waituntil/index.html73
2 files changed, 270 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/extendableevent/index.html b/files/zh-cn/web/api/extendableevent/index.html
new file mode 100644
index 0000000000..4f1adf6b7f
--- /dev/null
+++ b/files/zh-cn/web/api/extendableevent/index.html
@@ -0,0 +1,197 @@
+---
+title: ExtendableEvent
+slug: Web/API/ExtendableEvent
+tags:
+ - API
+ - Experimental
+ - ExtendableEvent
+ - Interface
+ - NeedsTranslation
+ - Offline
+ - Reference
+ - Service Workers
+ - ServiceWorker
+ - TopicStub
+ - Workers
+translation_of: Web/API/ExtendableEvent
+---
+<div>{{APIRef("Service Workers API")}}{{SeeCompatTable}}</div>
+
+<div>作为 service worker 生命周期的一部分,<strong><code>ExtendableEvent</code></strong>接口延长了在全局范围上{{event("install")}}和{{event("activate")}}事件的生命周期。这样可以确保在升级数据库架构并删除过时的caches之前,不会调度任何函数事件(如{{domxref("FetchEvent")}})。</div>
+
+<div>如果在<code>ExtendableEvent</code>处理程序之外调用{{domxref("ExtendableEvent.waitUntil","waitUntil()")}},浏览器应该抛出一个<code>InvalidStateError</code>;还要注意,多个调用将堆积起来,结果promises 将添加到<a href="https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#dfn-extend-lifetime-promises">extend lifetime promises</a>.</div>
+
+<div> </div>
+
+<div class="note">
+<p><strong>提示</strong>: 上述段落中描述的行为在firefox 43中得到了修复(参见 {{bug(1180274)}} )。</p>
+</div>
+
+<p>此接口继承自{{domxref("Event")}}接口。</p>
+
+<p>{{InheritanceDiagram(700, 60, 20)}}</p>
+
+<div class="note">
+<p><strong>提示</strong>: 只有当全局范围是 {{domxref("ServiceWorkerGlobalScope")}} 时,此接口才可用。当它是一个 {{domxref("Window")}} 或其他类型 worker 的作用域时,它不可用。</p>
+</div>
+
+<h2 id="构造函数">构造函数</h2>
+
+<dl>
+ <dt>{{domxref("ExtendableEvent.ExtendableEvent()", "ExtendableEvent()")}}</dt>
+ <dd>创建新的<code>ExtendableEvent</code>对象。</dd>
+</dl>
+
+<h2 id="特性">特性</h2>
+
+<p>不实现任何特定属性,但从其父级事件继承属,<em>{{domxref("Event")}}</em>。</p>
+
+<h2 id="方法">方法</h2>
+
+<p><em>从他的父辈继承, </em><em>{{domxref("Event")}}</em>.</p>
+
+<dl>
+ <dt>{{domxref("ExtendableEvent.waitUntil", "ExtendableEvent.waitUntil()")}}</dt>
+ <dd>
+ <p>延长事件的生存期。它将在service worker 的 <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/install">install</a></code> 和 <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/activate">activate</a></code> 事件中被调用。</p>
+ </dd>
+</dl>
+
+<h2 id="实例">实例</h2>
+
+<p>代码片段来自<a href="https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/prefetch/service-worker.js">service worker prefetch sample</a> (查看 <a href="https://googlechrome.github.io/samples/service-worker/prefetch/">prefetch example live</a>.)。代码在{{domxref("ServiceWorkerGlobalScope.oninstall")}}中调用{{domxref("ExtendableEvent.waitUntil()")}},延迟将{{domxref("ServiceWorkerRegistration.installing")}} Worker视为已安装,直到传递的promise resolve(在所有资源都已被提取和缓存的情况,或者发生任何异常时的问题.)</p>
+
+<p>代码段还显示了对service worker使用的缓存进行版本控制的最佳实践。虽然在这个例子中只有一个缓存,但是相同的方法可以用于多个缓存。它将缓存的速记标识符映射到特定的、版本化的缓存名称。</p>
+
+<div class="note">
+<p><strong>提示</strong>:在chrome中,日志记录语句通过chrome://service worker internals访问的相关服务工作者的“inspect”接口可见。</p>
+</div>
+
+<pre class="brush: js">var CACHE_VERSION = 1;
+var CURRENT_CACHES = {
+ prefetch: 'prefetch-cache-v' + CACHE_VERSION
+};
+
+self.addEventListener('install', function(event) {
+ var urlsToPrefetch = [
+ './static/pre_fetched.txt',
+ './static/pre_fetched.html',
+ 'https://www.chromium.org/_/rsrc/1302286216006/config/customLogo.gif'
+ ];
+
+ console.log('Handling install event. Resources to pre-fetch:', urlsToPrefetch);
+
+ event.waitUntil(
+ caches.open(CURRENT_CACHES['prefetch']).then(function(cache) {
+ cache.addAll(urlsToPrefetch.map(function(urlToPrefetch) {
+ return new Request(urlToPrefetch, {mode: 'no-cors'});
+ })).then(function() {
+ console.log('All resources have been fetched and cached.');
+ });
+ }).catch(function(error) {
+ console.error('Pre-fetching failed:', error);
+ })
+ );
+});</pre>
+
+<div class="note"><strong>重点</strong>: 在获取资源时,如果有可能资源是由不支持 <a href="http://en.wikipedia.org/wiki/Cross-origin_resource_sharing">CORS</a> 的服务器提供的,那么使用 <code>{mode: 'no-cors'}</code> 非常重要。在本例中, <a href="http://www.chromium.org">www.chromium.org</a> 不支持CORS。</div>
+
+<h2 id="Specifications">Specifications</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('Service Workers', '#extendable-event', 'ExtendableEvent')}}</td>
+ <td>{{Spec2('Service Workers')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatChrome(40.0)}}</td>
+ <td>{{ CompatGeckoDesktop("44.0") }}<sup>[1]</sup></td>
+ <td>{{CompatNo}}</td>
+ <td>24</td>
+ <td>{{CompatNo}}</td>
+ </tr>
+ <tr>
+ <td>async <code>waitUntil()</code></td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{ CompatGeckoDesktop("53.0") }}<sup>[2]</sup></td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ <th>Chrome for Android</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatNo}}</td>
+ <td>{{ CompatGeckoMobile("44.0") }}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatChrome(40.0)}}</td>
+ </tr>
+ <tr>
+ <td>async <code>waitUntil()</code></td>
+ <td>{{CompatNo}}</td>
+ <td>{{ CompatGeckoMobile("53.0") }}<sup>[2]</sup></td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p>[1] Service workers (and <a href="/en-US/docs/Web/API/Push_API">Push</a>) have been disabled in the <a href="https://www.mozilla.org/en-US/firefox/organizations/">Firefox 45 &amp; 52 Extended Support Releases</a> (ESR.)</p>
+
+<p>[2] {{domxref("ExtendableEvent.waitUntil", "ExtendableEvent.waitUntil()")}} can now be called asynchronously (see {{bug(1263304)}}).</p>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a></li>
+ <li><a class="external external-icon" href="https://github.com/mdn/sw-test">Service workers basic code example</a></li>
+ <li><a class="external external-icon" href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker ready?</a></li>
+ <li>{{jsxref("Promise")}}</li>
+ <li><a href="https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers">Using web workers</a></li>
+</ul>
diff --git a/files/zh-cn/web/api/extendableevent/waituntil/index.html b/files/zh-cn/web/api/extendableevent/waituntil/index.html
new file mode 100644
index 0000000000..8e48586b67
--- /dev/null
+++ b/files/zh-cn/web/api/extendableevent/waituntil/index.html
@@ -0,0 +1,73 @@
+---
+title: ExtendableEvent.waitUntil()
+slug: Web/API/ExtendableEvent/waitUntil
+translation_of: Web/API/ExtendableEvent/waitUntil
+---
+<p>{{APIRef("Service Workers API")}}</p>
+
+<p><code><strong>ExtendableEvent.waitUntil()</strong></code> 方法告诉事件分发器该事件仍在进行。这个方法也可以用于检测进行的任务是否成功。在服务工作线程中,这个方法告诉浏览器事件一直进行,直至 promise 解决,浏览器不应该在事件中的异步操作完成之前终止服务工作线程。</p>
+
+<p>服务工作线程(<a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope">service workers</a>)中的 {{domxref("ServiceWorkerGlobalScope/install_event", "install")}} 事件使用 <code>waitUntil()</code> 来将服务工作线程保持在 {{domxref("ServiceWorkerRegistration.installing", "installing")}} 阶段。如果传入 <code>waitUntil()</code> 的 promise 被拒绝,则将此次安装视为失败,丢弃这个服务工作线程。这主要用于确保在服务工作线程安装以前,所有依赖的核心缓存都已经成功载入。</p>
+
+<p>服务工作线程(<a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope">service workers</a>)中的 {{domxref("ServiceWorkerGlobalScope/activate_event", "activate")}} 事件使用 <code>waitUntil()</code> 来延迟函数事件,如 <code>fetch</code> 和 <code>push</code>,直至传入 <code>waitUntil()</code> 的 promise 被解决。这让服务工作线程有时间更新数据库架构(database schema)和删除过时缓存({{domxref("Cache", "caches")}}),让其他事件能在一个完成更新的状态下进行。</p>
+
+<p> <code>waitUntil()</code> 方法最初必须在事件回调里调用,在此之后,方法可以被调用多次,直至所有传入的 promise 被解决。</p>
+
+<div class="note">
+<p><strong>注意</strong>: 上述段落描述的行为已经在Firefox 43中被修复 (参见 {{bug(1180274)}}.)</p>
+</div>
+
+<h2 id="语法">语法</h2>
+
+<pre class="notranslate"><em>extendableEvent</em>.waitUntil(<em>promise</em>);</pre>
+
+<h3 id="参数">参数</h3>
+
+<p>一个 {{jsxref("Promise")}}.</p>
+
+<h2 id="示例">示例</h2>
+
+<p>在服务工作线程的 <code>install</code> 事件中使用 <code>waitUntil()</code>。</p>
+
+<pre class="notranslate">addEventListener('install', event =&gt; {
+ const preCache = async () =&gt; {
+    const cache = await caches.open('static-v1');
+    return cache.addAll([
+      '/',
+  '/about/',
+  '/static/styles.css'
+    ]);
+  };
+ event.waitUntil(preCache());
+});</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('Service Workers', '#dom-extendableevent-waituntil', 'waitUntil()')}}</td>
+ <td>{{Spec2('Service Workers')}}</td>
+ <td>Initial definition</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<p>{{Compat("api.ExtendableEvent.waitUntil")}}</p>
+
+<h2 id="另参见">另参见</h2>
+
+<ul>
+ <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a></li>
+ <li><a class="external external-icon" href="https://github.com/mdn/sw-test">Service workers basic code example</a></li>
+ <li><a class="external external-icon" href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker ready?</a></li>
+ <li>{{jsxref("Promise")}}</li>
+ <li><a href="https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers">Using web workers</a></li>
+</ul>