main
js 248 lines 6.35 KB
Raw
1 // flow-typed signature: f6bda44505d6258bae702a65ee2878f2
2 // flow-typed version: 840509ea9d/serviceworkers/flow_>=v0.261.x
3
4 type FrameType = 'auxiliary' | 'top-level' | 'nested' | 'none';
5 type VisibilityState = 'hidden' | 'visible' | 'prerender' | 'unloaded';
6
7 declare class WindowClient extends Client {
8 visibilityState: VisibilityState;
9 focused: boolean;
10 focus(): Promise<WindowClient>;
11 navigate(url: string): Promise<WindowClient>;
12 }
13
14 declare class Client {
15 id: string;
16 reserved: boolean;
17 url: string;
18 frameType: FrameType;
19 postMessage(message: any, transfer?: Iterator<any> | Array<any>): void;
20 }
21
22 declare class ExtendableEvent extends Event {
23 waitUntil(f: Promise<mixed>): void;
24 }
25
26 type NotificationEvent$Init = {
27 ...Event$Init,
28 notification: Notification,
29 action?: string,
30 ...
31 };
32
33 declare class NotificationEvent extends ExtendableEvent {
34 constructor(type: string, eventInitDict?: NotificationEvent$Init): void;
35 +notification: Notification;
36 +action: string;
37 }
38
39 type ForeignFetchOptions = {
40 scopes: Iterator<string>,
41 origins: Iterator<string>,
42 ...
43 };
44
45 declare class InstallEvent extends ExtendableEvent {
46 registerForeignFetch(options: ForeignFetchOptions): void;
47 }
48
49 declare class FetchEvent extends ExtendableEvent {
50 request: Request;
51 clientId: string;
52 isReload: boolean;
53 respondWith(response: Response | Promise<Response>): void;
54 preloadResponse: Promise<?Response>;
55 }
56
57 type ClientType = 'window' | 'worker' | 'sharedworker' | 'all';
58 type ClientQueryOptions = {
59 includeUncontrolled?: boolean,
60 includeReserved?: boolean,
61 type?: ClientType,
62 ...
63 };
64
65 declare class Clients {
66 get(id: string): Promise<?Client>;
67 matchAll(options?: ClientQueryOptions): Promise<Array<Client>>;
68 openWindow(url: string): Promise<?WindowClient>;
69 claim(): Promise<void>;
70 }
71
72 type ServiceWorkerState =
73 | 'installing'
74 | 'installed'
75 | 'activating'
76 | 'activated'
77 | 'redundant';
78
79 declare class ServiceWorker extends EventTarget {
80 scriptURL: string;
81 state: ServiceWorkerState;
82
83 postMessage(message: any, transfer?: Iterator<any>): void;
84
85 onstatechange?: EventHandler;
86 }
87
88 declare class NavigationPreloadState {
89 enabled: boolean;
90 headerValue: string;
91 }
92
93 declare class NavigationPreloadManager {
94 enable: Promise<void>;
95 disable: Promise<void>;
96 setHeaderValue(value: string): Promise<void>;
97 getState: Promise<NavigationPreloadState>;
98 }
99
100 type PushSubscriptionOptions = {
101 userVisibleOnly?: boolean,
102 applicationServerKey?: string | ArrayBuffer | $ArrayBufferView,
103 ...
104 };
105
106 declare class PushSubscriptionJSON {
107 endpoint: string;
108 expirationTime: number | null;
109 keys: {[string]: string, ...};
110 }
111
112 declare class PushSubscription {
113 +endpoint: string;
114 +expirationTime: number | null;
115 +options: PushSubscriptionOptions;
116 getKey(name: string): ArrayBuffer | null;
117 toJSON(): PushSubscriptionJSON;
118 unsubscribe(): Promise<boolean>;
119 }
120
121 declare class PushManager {
122 +supportedContentEncodings: Array<string>;
123 subscribe(options?: PushSubscriptionOptions): Promise<PushSubscription>;
124 getSubscription(): Promise<PushSubscription | null>;
125 permissionState(
126 options?: PushSubscriptionOptions
127 ): Promise<'granted' | 'denied' | 'prompt'>;
128 }
129
130 type ServiceWorkerUpdateViaCache = 'imports' | 'all' | 'none';
131
132 type GetNotificationOptions = {
133 tag?: string,
134 ...
135 };
136
137 declare class ServiceWorkerRegistration extends EventTarget {
138 +installing: ?ServiceWorker;
139 +waiting: ?ServiceWorker;
140 +active: ?ServiceWorker;
141 +navigationPreload: NavigationPreloadManager;
142 +scope: string;
143 +updateViaCache: ServiceWorkerUpdateViaCache;
144 +pushManager: PushManager;
145
146 getNotifications?: (
147 filter?: GetNotificationOptions
148 ) => Promise<$ReadOnlyArray<Notification>>;
149 showNotification?: (
150 title: string,
151 options?: NotificationOptions
152 ) => Promise<void>;
153 update(): Promise<void>;
154 unregister(): Promise<boolean>;
155
156 onupdatefound?: EventHandler;
157 }
158
159 type WorkerType = 'classic' | 'module';
160
161 type RegistrationOptions = {
162 scope?: string,
163 type?: WorkerType,
164 updateViaCache?: ServiceWorkerUpdateViaCache,
165 ...
166 };
167
168 declare class ServiceWorkerContainer extends EventTarget {
169 +controller: ?ServiceWorker;
170 +ready: Promise<ServiceWorkerRegistration>;
171
172 getRegistration(
173 clientURL?: string
174 ): Promise<ServiceWorkerRegistration | void>;
175 getRegistrations(): Promise<Iterator<ServiceWorkerRegistration>>;
176 register(
177 scriptURL: string | TrustedScriptURL,
178 options?: RegistrationOptions
179 ): Promise<ServiceWorkerRegistration>;
180 startMessages(): void;
181
182 oncontrollerchange?: EventHandler;
183 onmessage?: EventHandler;
184 onmessageerror?: EventHandler;
185 }
186
187 /**
188 * This feature has been removed from the Web standards.
189 */
190 declare class ServiceWorkerMessageEvent extends Event {
191 data: any;
192 lastEventId: string;
193 origin: string;
194 ports: Array<MessagePort>;
195 source: ?(ServiceWorker | MessagePort);
196 }
197
198 declare class ExtendableMessageEvent extends ExtendableEvent {
199 data: any;
200 lastEventId: string;
201 origin: string;
202 ports: Array<MessagePort>;
203 source: ?(ServiceWorker | MessagePort);
204 }
205
206 type CacheQueryOptions = {
207 ignoreSearch?: boolean,
208 ignoreMethod?: boolean,
209 ignoreVary?: boolean,
210 cacheName?: string,
211 ...
212 };
213
214 declare class Cache {
215 match(request: RequestInfo, options?: CacheQueryOptions): Promise<Response>;
216 matchAll(
217 request: RequestInfo,
218 options?: CacheQueryOptions
219 ): Promise<Array<Response>>;
220 add(request: RequestInfo): Promise<void>;
221 addAll(requests: Array<RequestInfo>): Promise<void>;
222 put(request: RequestInfo, response: Response): Promise<void>;
223 delete(request: RequestInfo, options?: CacheQueryOptions): Promise<boolean>;
224 keys(
225 request?: RequestInfo,
226 options?: CacheQueryOptions
227 ): Promise<Array<Request>>;
228 }
229
230 declare class CacheStorage {
231 match(request: RequestInfo, options?: CacheQueryOptions): Promise<Response>;
232 has(cacheName: string): Promise<true>;
233 open(cacheName: string): Promise<Cache>;
234 delete(cacheName: string): Promise<boolean>;
235 keys(): Promise<Array<string>>;
236 }
237
238 // Service worker global scope
239 // https://www.w3.org/TR/service-workers/#service-worker-global-scope
240 declare var clients: Clients;
241 declare var caches: CacheStorage;
242 declare var registration: ServiceWorkerRegistration;
243 declare function skipWaiting(): Promise<void>;
244 declare var onactivate: ?EventHandler;
245 declare var oninstall: ?EventHandler;
246 declare var onfetch: ?EventHandler;
247 declare var onforeignfetch: ?EventHandler;
248 declare var onmessage: ?EventHandler;