UNPKG

7.35 kBTypeScriptView Raw
1import './_version.js';
2interface OnSyncCallbackOptions {
3 queue: Queue;
4}
5interface OnSyncCallback {
6 (options: OnSyncCallbackOptions): void | Promise<void>;
7}
8export interface QueueOptions {
9 forceSyncFallback?: boolean;
10 maxRetentionTime?: number;
11 onSync?: OnSyncCallback;
12}
13interface QueueEntry {
14 request: Request;
15 timestamp?: number;
16 metadata?: object;
17}
18/**
19 * A class to manage storing failed requests in IndexedDB and retrying them
20 * later. All parts of the storing and replaying process are observable via
21 * callbacks.
22 *
23 * @memberof workbox-background-sync
24 */
25declare class Queue {
26 private readonly _name;
27 private readonly _onSync;
28 private readonly _maxRetentionTime;
29 private readonly _queueStore;
30 private readonly _forceSyncFallback;
31 private _syncInProgress;
32 private _requestsAddedDuringSync;
33 /**
34 * Creates an instance of Queue with the given options
35 *
36 * @param {string} name The unique name for this queue. This name must be
37 * unique as it's used to register sync events and store requests
38 * in IndexedDB specific to this instance. An error will be thrown if
39 * a duplicate name is detected.
40 * @param {Object} [options]
41 * @param {Function} [options.onSync] A function that gets invoked whenever
42 * the 'sync' event fires. The function is invoked with an object
43 * containing the `queue` property (referencing this instance), and you
44 * can use the callback to customize the replay behavior of the queue.
45 * When not set the `replayRequests()` method is called.
46 * Note: if the replay fails after a sync event, make sure you throw an
47 * error, so the browser knows to retry the sync event later.
48 * @param {number} [options.maxRetentionTime=7 days] The amount of time (in
49 * minutes) a request may be retried. After this amount of time has
50 * passed, the request will be deleted from the queue.
51 * @param {boolean} [options.forceSyncFallback=false] If `true`, instead
52 * of attempting to use background sync events, always attempt to replay
53 * queued request at service worker startup. Most folks will not need
54 * this, unless you explicitly target a runtime like Electron that
55 * exposes the interfaces for background sync, but does not have a working
56 * implementation.
57 */
58 constructor(name: string, { forceSyncFallback, onSync, maxRetentionTime }?: QueueOptions);
59 /**
60 * @return {string}
61 */
62 get name(): string;
63 /**
64 * Stores the passed request in IndexedDB (with its timestamp and any
65 * metadata) at the end of the queue.
66 *
67 * @param {QueueEntry} entry
68 * @param {Request} entry.request The request to store in the queue.
69 * @param {Object} [entry.metadata] Any metadata you want associated with the
70 * stored request. When requests are replayed you'll have access to this
71 * metadata object in case you need to modify the request beforehand.
72 * @param {number} [entry.timestamp] The timestamp (Epoch time in
73 * milliseconds) when the request was first added to the queue. This is
74 * used along with `maxRetentionTime` to remove outdated requests. In
75 * general you don't need to set this value, as it's automatically set
76 * for you (defaulting to `Date.now()`), but you can update it if you
77 * don't want particular requests to expire.
78 */
79 pushRequest(entry: QueueEntry): Promise<void>;
80 /**
81 * Stores the passed request in IndexedDB (with its timestamp and any
82 * metadata) at the beginning of the queue.
83 *
84 * @param {QueueEntry} entry
85 * @param {Request} entry.request The request to store in the queue.
86 * @param {Object} [entry.metadata] Any metadata you want associated with the
87 * stored request. When requests are replayed you'll have access to this
88 * metadata object in case you need to modify the request beforehand.
89 * @param {number} [entry.timestamp] The timestamp (Epoch time in
90 * milliseconds) when the request was first added to the queue. This is
91 * used along with `maxRetentionTime` to remove outdated requests. In
92 * general you don't need to set this value, as it's automatically set
93 * for you (defaulting to `Date.now()`), but you can update it if you
94 * don't want particular requests to expire.
95 */
96 unshiftRequest(entry: QueueEntry): Promise<void>;
97 /**
98 * Removes and returns the last request in the queue (along with its
99 * timestamp and any metadata). The returned object takes the form:
100 * `{request, timestamp, metadata}`.
101 *
102 * @return {Promise<QueueEntry | undefined>}
103 */
104 popRequest(): Promise<QueueEntry | undefined>;
105 /**
106 * Removes and returns the first request in the queue (along with its
107 * timestamp and any metadata). The returned object takes the form:
108 * `{request, timestamp, metadata}`.
109 *
110 * @return {Promise<QueueEntry | undefined>}
111 */
112 shiftRequest(): Promise<QueueEntry | undefined>;
113 /**
114 * Returns all the entries that have not expired (per `maxRetentionTime`).
115 * Any expired entries are removed from the queue.
116 *
117 * @return {Promise<Array<QueueEntry>>}
118 */
119 getAll(): Promise<Array<QueueEntry>>;
120 /**
121 * Returns the number of entries present in the queue.
122 * Note that expired entries (per `maxRetentionTime`) are also included in this count.
123 *
124 * @return {Promise<number>}
125 */
126 size(): Promise<number>;
127 /**
128 * Adds the entry to the QueueStore and registers for a sync event.
129 *
130 * @param {Object} entry
131 * @param {Request} entry.request
132 * @param {Object} [entry.metadata]
133 * @param {number} [entry.timestamp=Date.now()]
134 * @param {string} operation ('push' or 'unshift')
135 * @private
136 */
137 _addRequest({ request, metadata, timestamp }: QueueEntry, operation: 'push' | 'unshift'): Promise<void>;
138 /**
139 * Removes and returns the first or last (depending on `operation`) entry
140 * from the QueueStore that's not older than the `maxRetentionTime`.
141 *
142 * @param {string} operation ('pop' or 'shift')
143 * @return {Object|undefined}
144 * @private
145 */
146 _removeRequest(operation: 'pop' | 'shift'): Promise<QueueEntry | undefined>;
147 /**
148 * Loops through each request in the queue and attempts to re-fetch it.
149 * If any request fails to re-fetch, it's put back in the same position in
150 * the queue (which registers a retry for the next sync event).
151 */
152 replayRequests(): Promise<void>;
153 /**
154 * Registers a sync event with a tag unique to this instance.
155 */
156 registerSync(): Promise<void>;
157 /**
158 * In sync-supporting browsers, this adds a listener for the sync event.
159 * In non-sync-supporting browsers, or if _forceSyncFallback is true, this
160 * will retry the queue on service worker startup.
161 *
162 * @private
163 */
164 private _addSyncListener;
165 /**
166 * Returns the set of queue names. This is primarily used to reset the list
167 * of queue names in tests.
168 *
169 * @return {Set<string>}
170 *
171 * @private
172 */
173 static get _queueNames(): Set<string>;
174}
175export { Queue };