mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
Add type definitions for service-worker-mock (#36791)
* Add type definitions for service-worker-mock * Fix linting issues * Process feedback
This commit is contained in:
parent
dc8a044062
commit
dd813f10a9
56
types/service-worker-mock/index.d.ts
vendored
Normal file
56
types/service-worker-mock/index.d.ts
vendored
Normal file
@ -0,0 +1,56 @@
|
||||
// Type definitions for service-worker-mock 2.0
|
||||
// Project: https://github.com/pinterest/service-workers/tree/master/packages/service-worker-mock#readme
|
||||
// Definitions by: Remco Haszing <https://github.com/remcohaszing>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.8
|
||||
|
||||
export = makeServiceWorkerEnv;
|
||||
declare function makeServiceWorkerEnv(): WorkerGlobalScope;
|
||||
|
||||
declare namespace makeServiceWorkerEnv {
|
||||
interface Caches {
|
||||
[key: string]: Cache;
|
||||
}
|
||||
|
||||
type Listeners = Map<keyof ServiceWorkerGlobalScopeEventMap, EventListener>;
|
||||
|
||||
interface Snapshot {
|
||||
/**
|
||||
* A key/value map of current cache contents.
|
||||
*/
|
||||
caches: Caches;
|
||||
|
||||
/**
|
||||
* A list of active clients.
|
||||
*/
|
||||
clients: Client[];
|
||||
|
||||
/**
|
||||
* A list of active notifications.
|
||||
*/
|
||||
notifications: Notification[];
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
/**
|
||||
* A key/value map of active listeners (`install`/`activate`/`fetch`/etc).
|
||||
*/
|
||||
const listeners: makeServiceWorkerEnv.Listeners;
|
||||
|
||||
/**
|
||||
* Used to trigger active listeners.
|
||||
*/
|
||||
function trigger(type: keyof ServiceWorkerGlobalScopeEventMap): Promise<void>;
|
||||
|
||||
/**
|
||||
* Used to generate a snapshot of the service worker internals.
|
||||
*/
|
||||
function snapshot(): makeServiceWorkerEnv.Snapshot;
|
||||
|
||||
interface WorkerGlobalScope {
|
||||
listeners: typeof listeners;
|
||||
trigger: typeof trigger;
|
||||
snapshot: typeof snapshot;
|
||||
}
|
||||
}
|
||||
18
types/service-worker-mock/service-worker-mock-tests.ts
Normal file
18
types/service-worker-mock/service-worker-mock-tests.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import makeServiceWorkerEnv = require('service-worker-mock');
|
||||
|
||||
const mock = makeServiceWorkerEnv();
|
||||
|
||||
Object.assign(self, mock);
|
||||
|
||||
trigger('install');
|
||||
mock.trigger('install');
|
||||
trigger('fetch');
|
||||
mock.trigger('fetch');
|
||||
|
||||
const emitFetch = listeners.get('fetch') as EventListener;
|
||||
emitFetch(new Event('fetch'));
|
||||
|
||||
mock.caches.open('OLD_CACHE');
|
||||
console.log(snapshot().caches.OLD_CACHE);
|
||||
console.log(self.snapshot().clients[0].id);
|
||||
console.log(mock.snapshot().notifications[0].body);
|
||||
24
types/service-worker-mock/tsconfig.json
Normal file
24
types/service-worker-mock/tsconfig.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6",
|
||||
"webworker"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictFunctionTypes": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"service-worker-mock-tests.ts"
|
||||
]
|
||||
}
|
||||
1
types/service-worker-mock/tslint.json
Normal file
1
types/service-worker-mock/tslint.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Loading…
Reference in New Issue
Block a user