UNPKG

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