aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/extendableevent/index.html
blob: 078b495540002071d52db77977a9b4decd0cf4d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
---
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>

{{Compat("api.ExtendableEvent")}}

<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>