Skip to content

Commit 8cd2c01

Browse files
loicrauxLoïc Rauxaveladtykus160
authored
feat(Offline): Allow no timeout when opening IndexedDB database (#8372)
This is the followup to the previous PR #8366 , to allow to disable the timeout, as per @avelad [suggestion](#8366 (comment)) to add this in another PR... --------- Co-authored-by: Loïc Raux <[email protected]> Co-authored-by: Álvaro Velad Galván <[email protected]> Co-authored-by: Wojciech Tyczyński <[email protected]>
1 parent 60c6b19 commit 8cd2c01

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

docs/tutorials/offline.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,29 @@ setting or set it explicitly with:
685685
usePersistentLicense: true
686686
```
687687

688-
## Configure opening IndexedDB timeout
688+
## Configure the timeout for opening IndexedDB-based storage
689689

690-
This configuration must be done before doing any other operation:
690+
There is a default timeout of 5 seconds for opening IndexedDB-based storage.
691+
This is useful to ensure that your application does not block indefinitely
692+
while waiting for the IndexedDB database to open.
693+
694+
If you want to change the value of this timeout, you can do so by setting
695+
accordingly the value of the `shaka.offline.indexeddb.StorageMechanismOpenTimeout`
696+
variable. For example, to set the timeout to 10 seconds, you can do the following:
691697

692698
```js
693699
// In seconds
694-
shaka.offline.indexeddb.StorageMechanismOpenTimeout = 5;
700+
shaka.offline.indexeddb.StorageMechanismOpenTimeout = 10;
701+
```
702+
703+
You can also disable this timeout and consequently wait indefinitely for the
704+
IndexedDB database to open successfully or fail to open. To do this, set the
705+
`shaka.offline.indexeddb.StorageMechanismOpenTimeout` variable to `false`:
706+
707+
```js
708+
shaka.offline.indexeddb.StorageMechanismOpenTimeout = false;
695709
```
710+
711+
Note that this configuration must be done before doing any other offline
712+
storage related operation (download and store content, list content,
713+
remove content, playback of content).

lib/offline/indexeddb/storage_mechanism.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,18 @@ shaka.offline.indexeddb.StorageMechanism = class {
6767
shaka.util.Error.Category.STORAGE,
6868
shaka.util.Error.Code.INDEXED_DB_INIT_TIMED_OUT));
6969
});
70-
timeOutTimer.tickAfter(shaka.offline.indexeddb.StorageMechanismOpenTimeout);
70+
const openTimeout = shaka.offline.indexeddb.StorageMechanismOpenTimeout;
71+
if (typeof openTimeout === 'number' && openTimeout > 0) {
72+
timeOutTimer.tickAfter(openTimeout);
73+
}
7174

7275
const open = window.indexedDB.open(name, version);
7376
open.onsuccess = (event) => {
7477
if (timedOut) {
7578
// Too late, we have already given up on opening the storage mechanism.
7679
return;
7780
}
81+
timeOutTimer.stop();
7882
const db = open.result;
7983
this.db_ = db;
8084
this.v1_ = shaka.offline.indexeddb.StorageMechanism.createV1_(db);
@@ -86,7 +90,6 @@ shaka.offline.indexeddb.StorageMechanism = class {
8690
this.v5_ = shaka.offline.indexeddb.StorageMechanism.createV5_(db);
8791
this.sessions_ =
8892
shaka.offline.indexeddb.StorageMechanism.createEmeSessionCell_(db);
89-
timeOutTimer.stop();
9093
p.resolve();
9194
};
9295
open.onupgradeneeded = (event) => {
@@ -98,12 +101,12 @@ shaka.offline.indexeddb.StorageMechanism = class {
98101
// Too late, we have already given up on opening the storage mechanism.
99102
return;
100103
}
104+
timeOutTimer.stop();
101105
p.reject(new shaka.util.Error(
102106
shaka.util.Error.Severity.CRITICAL,
103107
shaka.util.Error.Category.STORAGE,
104108
shaka.util.Error.Code.INDEXED_DB_ERROR,
105109
open.error));
106-
timeOutTimer.stop();
107110

108111
// Firefox will raise an error on the main thread unless we stop it here.
109112
event.preventDefault();
@@ -383,7 +386,10 @@ shaka.offline.indexeddb.StorageMechanism.V5_MANIFEST_STORE = 'manifest-v5';
383386
shaka.offline.indexeddb.StorageMechanism.SESSION_ID_STORE = 'session-ids';
384387

385388
/**
386-
* @type {number}
389+
* Timeout in seconds for opening the IndexedDB database,
390+
* or <code>false</code> to disable the timeout and wait indefinitely
391+
* for the database to open successfully or fail.
392+
* @type {number|boolean}
387393
* @export
388394
*/
389395
shaka.offline.indexeddb.StorageMechanismOpenTimeout = 5;

0 commit comments

Comments
 (0)