Skip to content

Commit

Permalink
Explainer fixes.
Browse files Browse the repository at this point in the history
1) Fixes all instances of await being used in non-async functions.
2) Fixes unfinished sentences noticed along the way.

Fixes #14, Fixes #15
  • Loading branch information
pwnall committed Feb 5, 2020
1 parent f9c7e21 commit 2442ae1
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,13 @@ to session state changes, to clean up private cached data.
Cookies have also found a niche in storing user decisions to opt out of tracking
by ad networks, and receive less personalized ads.

Separetly, from a conceptual angle, a service worker is intended to be an
Separately, from a conceptual angle, a service worker is intended to be an
HTTP proxy for the pages under its scope. By this principle, service workers
must be able to read and modify the cookies accessible to pages under their
scopes.

### Reacting to session state changes

While the previous samples are mostly aimed at updating a
page's UI to reflect


The following code illustrates synchronous polling via `document.cookie`. The
code periodically induces jank, as `document.cookie` is a synchronous call
that blocks the main thread on disk I/O, if the cookie value isn't cached in
Expand Down Expand Up @@ -169,11 +165,11 @@ The following code snippet uses the Cookie Store API instead, and does not jank
the main thread.

```javascript
document.getElementById('opt-out-button').addEventListener('click', () => {
document.getElementById('opt-out-button').addEventListener('click', async () => {
await cookieStore.set({ name: 'opt_out', value: '1',
expires: new Date('Wed, 1 Jan 2025 00:00:00 GMT') });
});
document.getElementById('opt-in-button').addEventListener('click', () => {
document.getElementById('opt-in-button').addEventListener('click', async () => {
await cookieStore.delete({ name: 'opt_out' });
});
```
Expand Down Expand Up @@ -229,7 +225,9 @@ In other words, `get` and `getAll` take the same arguments, which can be

### Read the cookies for a specific URL

Cookies are URL-scoped, so pages
Cookies are URL-scoped, so fetches to different URLs may include different
cookies, even when the URLs have the same origin. The application can specify
the URL whose associated cookies will be read.

```javascript
await cookieStore.getAll({url: '/admin'});
Expand Down Expand Up @@ -380,19 +378,21 @@ Calls to `subscribe()` and `unsubscribe()` are idempotent.

```javascript
self.addEventListener('activate', (event) => {
// Snapshot current state of subscriptions.
const subscriptions = await self.registration.cookies.getSubscriptions();

// Clear any existing subscriptions.
for (const subscription of subscriptions)
await self.registration.cookies.unsubscribe(subscription);

await self.registration.cookies.subscribe([
{
name: 'session', // Get change events for session-related cookies.
matchType: 'starts-with', // Matches session_id, session-id, etc.
}
]);
event.waitUntil(async () =>
// Snapshot current state of subscriptions.
const subscriptions = await self.registration.cookies.getSubscriptions();

// Clear any existing subscriptions.
for (const subscription of subscriptions)
await self.registration.cookies.unsubscribe(subscription);

await self.registration.cookies.subscribe([
{
name: 'session', // Get change events for session-related cookies.
matchType: 'starts-with', // Matches session_id, session-id, etc.
}
]);
});
});
```
#### Alternative subscription model for service workers
Expand Down

0 comments on commit 2442ae1

Please sign in to comment.