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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
content / test / data / background_sync / service_worker.js [blame]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// The "onsync" event currently understands commands (passed as
// registration tags) coming from the test. Any other tag is
// passed through to the document unchanged.
//
// "delay" - Delays finishing the sync event with event.waitUntil.
// Send a postMessage of "completeDelayedRegistration" to finish the
// event.
'use strict';
let resolveCallback = null;
let rejectCallback = null;
let periodicSyncEventCount = 0;
this.onmessage = (event) => {
switch(event.data.action) {
case 'completeDelayedSyncEvent': {
if (resolveCallback === null) {
sendMessageToClients('sync', 'error - resolveCallback is null');
return;
}
periodicSyncEventCount++;
resolveCallback();
sendMessageToClients('sync', 'ok - delay completed');
return;
}
case 'rejectDelayedSyncEvent': {
if (rejectCallback === null) {
sendMessageToClients('sync', 'error - rejectCallback is null');
return;
}
rejectCallback();
sendMessageToClients('sync', 'ok - delay rejected');
}
case 'registerOneShotSync': {
const tag = event.data.tag;
registration.sync.register(tag)
.then(() => {
sendMessageToClients('register', 'ok - ' + tag + ' registered in SW');
})
.catch(sendSyncErrorToClients);
return;
}
case 'registerPeriodicSync': {
const tag = event.data.tag;
const minInterval = event.data.minInterval;
registration.periodicSync.register(tag, { minInterval: minInterval })
.then(() =>
sendMessageToClients('register', 'ok - ' + tag + ' registered in SW'))
.catch(sendSyncErrorToClients);
return;
}
case 'unregister': {
const tag = event.data.tag;
registration.periodicSync.unregister(tag)
.then(() =>
sendMessageToClients(
'unregister', 'ok - ' + tag + ' unregistered in SW'))
.catch(sendSyncErrorToClients);
return;
}
case 'hasOneShotSyncTag': {
const tag = event.data.tag;
registration.sync.getTags()
.then((tags) => {
if (tags.indexOf(tag) >= 0) {
sendMessageToClients('register', 'ok - ' + tag + ' found');
} else {
sendMessageToClients('register', 'error - ' + tag + ' not found');
return;
}
})
.catch(sendSyncErrorToClients);
return;
}
case 'hasPeriodicSyncTag': {
const tag = event.data.tag;
registration.periodicSync.getTags()
.then((tags) => {
if (tags.indexOf(tag) >= 0) {
sendMessageToClients('register', 'ok - ' + tag + ' found in SW');
} else {
sendMessageToClients('register', 'error - ' + tag + ' not found');
return;
}
})
.catch(sendSyncErrorToClients);
return;
}
case 'getOneShotSyncTags': {
registration.sync.getTags()
.then((tags) => {
sendMessageToClients('register', 'ok - ' + tags.toString());
})
.catch(sendSyncErrorToClients);
return;
}
case 'getPeriodicSyncTags': {
registration.periodicSync.getTags()
.then((tags) => {
sendMessageToClients('register', 'ok - ' + tags.toString());
})
.catch(sendSyncErrorToClients);
return;
}
case 'getPeriodicSyncEventCount':
sendMessageToClients('gotEventCount', periodicSyncEventCount.toString());
return;
}
}
function handleSync(event, isPeriodic) {
const expectedEventType = isPeriodic ? 'PeriodicSyncEvent' : 'SyncEvent';
const eventProperties = [
// Extract name from toString result: "[object <Class>]"
Object.prototype.toString.call(event).match(/\s([a-zA-Z]+)/)[1],
(typeof event.waitUntil)
];
if (eventProperties[0] !== expectedEventType) {
return sendMessageToClients('sync', 'error - wrong event type');
}
if (eventProperties[1] != 'function') {
return sendMessageToClients('sync', 'error - wrong wait until type');
}
if (event.tag === undefined) {
return sendMessageToClients('sync', 'error - registration missing tag');
}
const tag = event.tag;
if (tag === 'delay') {
const syncPromise = new Promise((resolve, reject) => {
resolveCallback = resolve;
rejectCallback = reject;
});
return syncPromise;
}
if (isPeriodic)
periodicSyncEventCount++;
return sendMessageToClients('sync', tag + ' fired');
}
self.addEventListener('sync', event => {
event.waitUntil(handleSync(event, /* isPeriodic= */ false));
});
self.addEventListener('periodicsync', event => {
event.waitUntil(handleSync(event, /* isPeriodic= */ true));
});
function sendMessageToClients(type, data) {
return clients.matchAll({ includeUncontrolled: true }).then((clients) => {
clients.forEach((client) => {
client.postMessage({type, data});
});
}, (error) => {
console.log(error);
});
}
function sendSyncErrorToClients(error) {
sendMessageToClients('sync', error.name + ' - ' + error.message);
}