diff options
Diffstat (limited to 'files/zh-cn/web/api/extendableevent/index.html')
| -rw-r--r-- | files/zh-cn/web/api/extendableevent/index.html | 197 |
1 files changed, 197 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 & 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> |
