mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 19:07:08 +00:00
[workbox-window]: add types (#35522)
This commit is contained in:
parent
3159ea0560
commit
bf5b5072f8
115
types/workbox-window/Workbox.d.ts
vendored
Normal file
115
types/workbox-window/Workbox.d.ts
vendored
Normal file
@ -0,0 +1,115 @@
|
||||
import { EventTargetShim } from './utils/EventTargetShim';
|
||||
import { WorkboxEvent } from './utils/WorkboxEvent';
|
||||
|
||||
interface WorkboxMessageEvent extends WorkboxEvent {
|
||||
/**
|
||||
* The `data` property from the original `message` event.
|
||||
*/
|
||||
readonly data: any;
|
||||
}
|
||||
|
||||
interface WorkboxExtendableEvent extends WorkboxEvent {
|
||||
/**
|
||||
* The service worker instance.
|
||||
*/
|
||||
readonly sw: ServiceWorker;
|
||||
}
|
||||
|
||||
interface WorkboxUpdatableEvent extends WorkboxExtendableEvent {
|
||||
/**
|
||||
* True if a service worker was already
|
||||
* controlling when this `Workbox` instance called `register()`.
|
||||
*/
|
||||
readonly isUpdate?: boolean;
|
||||
}
|
||||
|
||||
interface WorkboxWaitingEvent extends WorkboxUpdatableEvent {
|
||||
/**
|
||||
* True if a service worker with
|
||||
* a matching `scriptURL` was already waiting when this `Workbox`
|
||||
* instance called `register()`.
|
||||
*/
|
||||
readonly wasWaitingBeforeRegister?: boolean;
|
||||
}
|
||||
|
||||
interface WokerboxEventMap {
|
||||
message: WorkboxMessageEvent;
|
||||
installed: WorkboxEvent;
|
||||
waiting: WorkboxWaitingEvent;
|
||||
controlling: WorkboxEvent;
|
||||
activated: WorkboxEvent;
|
||||
redundant: WorkboxEvent;
|
||||
externalinstalled: WorkboxExtendableEvent;
|
||||
externalwaiting: WorkboxExtendableEvent;
|
||||
externalactivated: WorkboxExtendableEvent;
|
||||
}
|
||||
|
||||
declare class Workbox extends EventTargetShim {
|
||||
/**
|
||||
* Creates a new Workbox instance with a script URL and service worker
|
||||
* options. The script URL and options are the same as those used when
|
||||
* calling `navigator.serviceWorker.register(scriptURL, options)`. See:
|
||||
* https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register
|
||||
*/
|
||||
constructor(scriptURL: string, registerOptions?: RegistrationOptions);
|
||||
|
||||
/**
|
||||
* Registers a service worker for this instances script URL and service
|
||||
* worker options. By default this method delays registration until after
|
||||
* the window has loaded.
|
||||
*/
|
||||
register(): Promise<ServiceWorkerRegistration>;
|
||||
|
||||
/**
|
||||
* Resolves to the service worker registered by this instance as soon as it
|
||||
* is active. If a service worker was already controlling at registration
|
||||
* time then it will resolve to that if the script URLs (and optionally
|
||||
* script versions) match, otherwise it will wait until an update is found
|
||||
* and activates.
|
||||
*/
|
||||
readonly active: Promise<ServiceWorker>;
|
||||
|
||||
/**
|
||||
* Resolves to the service worker registered by this instance as soon as it
|
||||
* is controlling the page. If a service worker was already controlling at
|
||||
* registration time then it will resolve to that if the script URLs (and
|
||||
* optionally script versions) match, otherwise it will wait until an update
|
||||
* is found and starts controlling the page.
|
||||
* Note: the first time a service worker is installed it will active but
|
||||
* not start controlling the page unless `clients.claim()` is called in the
|
||||
* service worker.
|
||||
*/
|
||||
readonly controlling: Promise<ServiceWorker>;
|
||||
|
||||
/**
|
||||
* Resolves with a reference to a service worker that matches the script URL
|
||||
* of this instance, as soon as it's available.
|
||||
*
|
||||
* If, at registration time, there's already an active or waiting service
|
||||
* worker with a matching script URL, it will be used (with the waiting
|
||||
* service worker taking precedence over the active service worker if both
|
||||
* match, since the waiting service worker would have been registered more
|
||||
* recently).
|
||||
* If there's no matching active or waiting service worker at registration
|
||||
* time then the promise will not resolve until an update is found and starts
|
||||
* installing, at which point the installing service worker is used.
|
||||
*/
|
||||
getSW(): Promise<ServiceWorker>;
|
||||
|
||||
/**
|
||||
* Sends the passed data object to the service worker registered by this
|
||||
* instance (via [`getSW()`]{@link module:workbox-window.Workbox#getSW}) and resolves
|
||||
* with a response (if any).
|
||||
*
|
||||
* A response can be set in a message handler in the service worker by
|
||||
* calling `event.ports[0].postMessage(...)`, which will resolve the promise
|
||||
* returned by `messageSW()`. If no response is set, the promise will never
|
||||
* resolve.
|
||||
*/
|
||||
messageSW(): Promise<object>;
|
||||
|
||||
addEventListener<K extends keyof WokerboxEventMap>(type: K, listener: (this: Workbox, ev: WokerboxEventMap[K]) => void): void;
|
||||
removeEventListener<K extends keyof WokerboxEventMap>(type: K, listener: (this: Workbox, ev: WokerboxEventMap[K]) => void): void;
|
||||
}
|
||||
|
||||
export { Workbox };
|
||||
8
types/workbox-window/index.d.ts
vendored
Normal file
8
types/workbox-window/index.d.ts
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
// Type definitions for workbox-window 4.3
|
||||
// Project: https://github.com/GoogleChrome/workbox/tree/master/packages/workbox-window, https://github.com/GoogleChrome/workbox
|
||||
// Definitions by: Yamagishi Kazutoshi <https://github.com/ykzts>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 3.0
|
||||
|
||||
export { Workbox } from './Workbox';
|
||||
export { messageSW } from './messageSW';
|
||||
12
types/workbox-window/messageSW.d.ts
vendored
Normal file
12
types/workbox-window/messageSW.d.ts
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Sends a data object to a service worker via `postMessage` and resolves with
|
||||
* a response (if any).
|
||||
*
|
||||
* A response can be set in a message handler in the service worker by
|
||||
* calling `event.ports[0].postMessage(...)`, which will resolve the promise
|
||||
* returned by `messageSW()`. If no response is set, the promise will not
|
||||
* resolve.
|
||||
*/
|
||||
declare function messageSW(sw: ServiceWorker, data: object): Promise<undefined | object>;
|
||||
|
||||
export { messageSW };
|
||||
28
types/workbox-window/tsconfig.json
Normal file
28
types/workbox-window/tsconfig.json
Normal file
@ -0,0 +1,28 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6",
|
||||
"webworker"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"Workbox.d.ts",
|
||||
"messageSW.d.ts",
|
||||
"utils/EventTargetShim.d.ts",
|
||||
"utils/WorkboxEvent.d.ts",
|
||||
"workbox-window-tests.ts"
|
||||
]
|
||||
}
|
||||
3
types/workbox-window/tslint.json
Normal file
3
types/workbox-window/tslint.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json"
|
||||
}
|
||||
18
types/workbox-window/utils/EventTargetShim.d.ts
vendored
Normal file
18
types/workbox-window/utils/EventTargetShim.d.ts
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
import { WorkboxEvent } from './WorkboxEvent';
|
||||
|
||||
interface EventListenerShim {
|
||||
(evt: WorkboxEvent): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* A minimal `EventTarget` shim.
|
||||
* This is necessary because not all browsers support constructable
|
||||
* `EventTarget`, so using a real `EventTarget` will error.
|
||||
*/
|
||||
declare class EventTargetShim {
|
||||
addEventListener(type: string, listener: EventListenerShim): void;
|
||||
removeEventListener(type: string, listener: EventListenerShim): void;
|
||||
dispatchEvent(evt: WorkboxEvent): void;
|
||||
}
|
||||
|
||||
export { EventTargetShim };
|
||||
25
types/workbox-window/utils/WorkboxEvent.d.ts
vendored
Normal file
25
types/workbox-window/utils/WorkboxEvent.d.ts
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
import { Workbox } from '../Workbox';
|
||||
|
||||
/**
|
||||
* A minimal `Event` subclass shim.
|
||||
* This doesn't *actually* subclass `Event` because not all browsers support
|
||||
* constructable `EventTarget`, and using a real `Event` will error.
|
||||
*/
|
||||
interface WorkboxEvent {
|
||||
/**
|
||||
* The original event.
|
||||
*/
|
||||
readonly originalEvent: Event;
|
||||
|
||||
/**
|
||||
* The event type.
|
||||
*/
|
||||
readonly type: string;
|
||||
|
||||
/**
|
||||
* The `Workbox` instance.
|
||||
*/
|
||||
readonly target: Workbox;
|
||||
}
|
||||
|
||||
export { WorkboxEvent };
|
||||
16
types/workbox-window/workbox-window-tests.ts
Normal file
16
types/workbox-window/workbox-window-tests.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { Workbox } from 'workbox-window';
|
||||
|
||||
{
|
||||
const wb = new Workbox('/sw.js');
|
||||
|
||||
wb.addEventListener('message', (event) => {
|
||||
event.target; // $ExpectType Workbox
|
||||
event.data; // $ExpectType any
|
||||
});
|
||||
|
||||
wb.addEventListener('waiting', (event) => {
|
||||
event.isUpdate; // $ExpectType boolean | undefined
|
||||
});
|
||||
|
||||
wb.register(); // $ExpectType Promise<ServiceWorkerRegistration>
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user