StrategyHandler.d.ts
7.72 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { HandlerCallbackOptions, WorkboxPlugin, WorkboxPluginCallbackParam } from 'workbox-core/types.js';
import { Strategy } from './Strategy.js';
import './_version.js';
/**
* A class created every time a Strategy instance instance calls
* {@link workbox-strategies.Strategy~handle} or
* {@link workbox-strategies.Strategy~handleAll} that wraps all fetch and
* cache actions around plugin callbacks and keeps track of when the strategy
* is "done" (i.e. all added `event.waitUntil()` promises have resolved).
*
* @memberof workbox-strategies
*/
declare class StrategyHandler {
request: Request;
url?: URL;
event: ExtendableEvent;
params?: any;
private _cacheKeys;
private readonly _strategy;
private readonly _extendLifetimePromises;
private readonly _handlerDeferred;
private readonly _plugins;
private readonly _pluginStateMap;
/**
* Creates a new instance associated with the passed strategy and event
* that's handling the request.
*
* The constructor also initializes the state that will be passed to each of
* the plugins handling this request.
*
* @param {workbox-strategies.Strategy} strategy
* @param {Object} options
* @param {Request|string} options.request A request to run this strategy for.
* @param {ExtendableEvent} options.event The event associated with the
* request.
* @param {URL} [options.url]
* @param {*} [options.params] The return value from the
* {@link workbox-routing~matchCallback} (if applicable).
*/
constructor(strategy: Strategy, options: HandlerCallbackOptions);
/**
* Fetches a given request (and invokes any applicable plugin callback
* methods) using the `fetchOptions` (for non-navigation requests) and
* `plugins` defined on the `Strategy` object.
*
* The following plugin lifecycle methods are invoked when using this method:
* - `requestWillFetch()`
* - `fetchDidSucceed()`
* - `fetchDidFail()`
*
* @param {Request|string} input The URL or request to fetch.
* @return {Promise<Response>}
*/
fetch(input: RequestInfo): Promise<Response>;
/**
* Calls `this.fetch()` and (in the background) runs `this.cachePut()` on
* the response generated by `this.fetch()`.
*
* The call to `this.cachePut()` automatically invokes `this.waitUntil()`,
* so you do not have to manually call `waitUntil()` on the event.
*
* @param {Request|string} input The request or URL to fetch and cache.
* @return {Promise<Response>}
*/
fetchAndCachePut(input: RequestInfo): Promise<Response>;
/**
* Matches a request from the cache (and invokes any applicable plugin
* callback methods) using the `cacheName`, `matchOptions`, and `plugins`
* defined on the strategy object.
*
* The following plugin lifecycle methods are invoked when using this method:
* - cacheKeyWillByUsed()
* - cachedResponseWillByUsed()
*
* @param {Request|string} key The Request or URL to use as the cache key.
* @return {Promise<Response|undefined>} A matching response, if found.
*/
cacheMatch(key: RequestInfo): Promise<Response | undefined>;
/**
* Puts a request/response pair in the cache (and invokes any applicable
* plugin callback methods) using the `cacheName` and `plugins` defined on
* the strategy object.
*
* The following plugin lifecycle methods are invoked when using this method:
* - cacheKeyWillByUsed()
* - cacheWillUpdate()
* - cacheDidUpdate()
*
* @param {Request|string} key The request or URL to use as the cache key.
* @param {Response} response The response to cache.
* @return {Promise<boolean>} `false` if a cacheWillUpdate caused the response
* not be cached, and `true` otherwise.
*/
cachePut(key: RequestInfo, response: Response): Promise<boolean>;
/**
* Checks the list of plugins for the `cacheKeyWillBeUsed` callback, and
* executes any of those callbacks found in sequence. The final `Request`
* object returned by the last plugin is treated as the cache key for cache
* reads and/or writes. If no `cacheKeyWillBeUsed` plugin callbacks have
* been registered, the passed request is returned unmodified
*
* @param {Request} request
* @param {string} mode
* @return {Promise<Request>}
*/
getCacheKey(request: Request, mode: 'read' | 'write'): Promise<Request>;
/**
* Returns true if the strategy has at least one plugin with the given
* callback.
*
* @param {string} name The name of the callback to check for.
* @return {boolean}
*/
hasCallback<C extends keyof WorkboxPlugin>(name: C): boolean;
/**
* Runs all plugin callbacks matching the given name, in order, passing the
* given param object (merged ith the current plugin state) as the only
* argument.
*
* Note: since this method runs all plugins, it's not suitable for cases
* where the return value of a callback needs to be applied prior to calling
* the next callback. See
* {@link workbox-strategies.StrategyHandler#iterateCallbacks}
* below for how to handle that case.
*
* @param {string} name The name of the callback to run within each plugin.
* @param {Object} param The object to pass as the first (and only) param
* when executing each callback. This object will be merged with the
* current plugin state prior to callback execution.
*/
runCallbacks<C extends keyof NonNullable<WorkboxPlugin>>(name: C, param: Omit<WorkboxPluginCallbackParam[C], 'state'>): Promise<void>;
/**
* Accepts a callback and returns an iterable of matching plugin callbacks,
* where each callback is wrapped with the current handler state (i.e. when
* you call each callback, whatever object parameter you pass it will
* be merged with the plugin's current state).
*
* @param {string} name The name fo the callback to run
* @return {Array<Function>}
*/
iterateCallbacks<C extends keyof WorkboxPlugin>(name: C): Generator<NonNullable<WorkboxPlugin[C]>>;
/**
* Adds a promise to the
* [extend lifetime promises]{@link https://w3c.github.io/ServiceWorker/#extendableevent-extend-lifetime-promises}
* of the event event associated with the request being handled (usually a
* `FetchEvent`).
*
* Note: you can await
* {@link workbox-strategies.StrategyHandler~doneWaiting}
* to know when all added promises have settled.
*
* @param {Promise} promise A promise to add to the extend lifetime promises
* of the event that triggered the request.
*/
waitUntil<T>(promise: Promise<T>): Promise<T>;
/**
* Returns a promise that resolves once all promises passed to
* {@link workbox-strategies.StrategyHandler~waitUntil}
* have settled.
*
* Note: any work done after `doneWaiting()` settles should be manually
* passed to an event's `waitUntil()` method (not this handler's
* `waitUntil()` method), otherwise the service worker thread my be killed
* prior to your work completing.
*/
doneWaiting(): Promise<void>;
/**
* Stops running the strategy and immediately resolves any pending
* `waitUntil()` promises.
*/
destroy(): void;
/**
* This method will call cacheWillUpdate on the available plugins (or use
* status === 200) to determine if the Response is safe and valid to cache.
*
* @param {Request} options.request
* @param {Response} options.response
* @return {Promise<Response|undefined>}
*
* @private
*/
_ensureResponseSafeToCache(response: Response): Promise<Response | undefined>;
}
export { StrategyHandler };