File: sw.js

package info (click to toggle)
thunderbird 1%3A140.4.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,609,432 kB
  • sloc: cpp: 7,672,442; javascript: 5,901,613; ansic: 3,898,954; python: 1,413,343; xml: 653,997; asm: 462,286; java: 180,927; sh: 113,489; makefile: 20,460; perl: 14,288; objc: 13,059; yacc: 4,583; pascal: 3,352; lex: 1,720; ruby: 1,222; exp: 762; sql: 715; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (64 lines) | stat: -rw-r--r-- 2,272 bytes parent folder | download | duplicates (18)
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

importScripts('sw-helpers.js');

async function getFetchResult(record) {
  const response = await record.responseReady.catch(() => null);
  if (!response) return null;

  return {
    url: response.url,
    status: response.status,
    text: await response.text().catch(() => 'error'),
  };
}

function handleBackgroundFetchEvent(event) {
  let matchFunction = null;

  switch (event.registration.id) {
    case 'matchexistingrequest':
      matchFunction = event.registration.match.bind(
          event.registration, '/background-fetch/resources/feature-name.txt');
      break;
    case 'matchexistingrequesttwice':
      matchFunction = (async () => {
        const match1 = await event.registration.match('/background-fetch/resources/feature-name.txt');
        const match2 = await event.registration.match('/background-fetch/resources/feature-name.txt');
        return [match1, match2];
    }).bind(event.registration);
      break;
    case 'matchmissingrequest':
      matchFunction = event.registration.match.bind(
          event.registration, '/background-fetch/resources/missing.txt');
      break;
    default:
      matchFunction = event.registration.matchAll.bind(event.registration);
      break;
  }

  event.waitUntil(
    matchFunction()
      // Format `match(All)?` function results.
      .then(records => {
        if (!records) return [];  // Nothing was matched.
        if (!records.map) return [records];  // One entry was returned.
        return records;  // Already in a list.
      })
      // Extract responses.
      .then(records =>
        Promise.all(records.map(record => getFetchResult(record))))
      // Clone registration and send message.
      .then(results => {
        const registrationCopy = cloneRegistration(event.registration);
        sendMessageToDocument(
          { type: event.type, eventRegistration: registrationCopy, results })
      })
      .catch(error => {
        sendMessageToDocument(
          { type: event.type, eventRegistration:{}, results:[], error:true })
      }));
}

self.addEventListener('backgroundfetchsuccess', handleBackgroundFetchEvent);
self.addEventListener('backgroundfetchfail', handleBackgroundFetchEvent);
self.addEventListener('backgroundfetchabort', handleBackgroundFetchEvent);