Blame view

src/global.tsx 3.14 KB
cc26d1fc   张志伟   🎉 重新构建项目,解决项目过大的问题
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
  import { Button, message, notification } from 'antd';
  
  import React from 'react';
  import { formatMessage } from 'umi';
  import defaultSettings from '../config/defaultSettings';
  
  const { pwa } = defaultSettings;
  // if pwa is true
  if (pwa) {
    // Notify user if offline now
    window.addEventListener('sw.offline', () => {
      message.warning(formatMessage({ id: 'app.pwa.offline' }));
    });
  
    // Pop up a prompt on the page asking the user if they want to use the latest version
    window.addEventListener('sw.updated', (event: Event) => {
      const e = event as CustomEvent;
      const reloadSW = async () => {
        // Check if there is sw whose state is waiting in ServiceWorkerRegistration
        // https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration
        const worker = e.detail && e.detail.waiting;
        if (!worker) {
          return true;
        }
        // Send skip-waiting event to waiting SW with MessageChannel
        await new Promise((resolve, reject) => {
          const channel = new MessageChannel();
          channel.port1.onmessage = msgEvent => {
            if (msgEvent.data.error) {
              reject(msgEvent.data.error);
            } else {
              resolve(msgEvent.data);
            }
          };
          worker.postMessage({ type: 'skip-waiting' }, [channel.port2]);
        });
        // Refresh current page to use the updated HTML and other assets after SW has skiped waiting
        window.location.reload(true);
        return true;
      };
      const key = `open${Date.now()}`;
      const btn = (
        <Button
          type="primary"
          onClick={() => {
            notification.close(key);
            reloadSW();
          }}
        >
          {formatMessage({ id: 'app.pwa.serviceworker.updated.ok' })}
        </Button>
      );
      notification.open({
        message: formatMessage({ id: 'app.pwa.serviceworker.updated' }),
        description: formatMessage({ id: 'app.pwa.serviceworker.updated.hint' }),
        btn,
        key,
        onClose: async () => {},
      });
    });
  } else if ('serviceWorker' in navigator) {
    // unregister service worker
    const { serviceWorker } = navigator;
    if (serviceWorker.getRegistrations) {
      serviceWorker.getRegistrations().then(sws => {
        sws.forEach(sw => {
          sw.unregister();
        });
      });
    }
    serviceWorker.getRegistration().then(sw => {
      if (sw) sw.unregister();
    });
  
    // remove all caches
    if (window.caches && window.caches.keys()) {
      caches.keys().then(keys => {
        keys.forEach(key => {
          caches.delete(key);
        });
      });
    }
  }
  
cc26d1fc   张志伟   🎉 重新构建项目,解决项目过大的问题
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  //@ts-ignore
  if (!window.Number.prototype._toFixed) {
    window.Number.prototype._toFixed = window.Number.prototype.toFixed;
  }
  window.Number.prototype.toFixed = function(m: number = 0): string {
    if (typeof this !== "number") {
      throw new Error("Invalid type");
    }
    let result: string = String(Math.round(Math.pow(10, m) * this) / Math.pow(10, m));
    if (result.indexOf(".") == -1) {
      if (m != 0) {
        result += ".";
        result += new Array(m + 1).join("0");
      }
    } else {
      let arr = result.split(".");
      if (arr[1].length < m) {
        arr[1] += new Array(m - arr[1].length + 1).join("0");
      }
      result = arr.join(".");
    }
    return result;
  };