From 8cd2c019b8efedfca2aa7c0ba0f57bba47228d35 Mon Sep 17 00:00:00 2001 From: loicraux Date: Tue, 1 Apr 2025 16:01:24 +0200 Subject: [PATCH] feat(Offline): Allow no timeout when opening IndexedDB database (#8372) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is the followup to the previous PR #8366 , to allow to disable the timeout, as per @avelad [suggestion](https://github.com/shaka-project/shaka-player/pull/8366#issuecomment-2766660785) to add this in another PR... --------- Co-authored-by: Loïc Raux Co-authored-by: Álvaro Velad Galván Co-authored-by: Wojciech Tyczyński --- docs/tutorials/offline.md | 24 +++++++++++++++++++--- lib/offline/indexeddb/storage_mechanism.js | 14 +++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/docs/tutorials/offline.md b/docs/tutorials/offline.md index 3bef5d8da..885b4ec0d 100644 --- a/docs/tutorials/offline.md +++ b/docs/tutorials/offline.md @@ -685,11 +685,29 @@ setting or set it explicitly with: usePersistentLicense: true ``` -## Configure opening IndexedDB timeout +## Configure the timeout for opening IndexedDB-based storage -This configuration must be done before doing any other operation: +There is a default timeout of 5 seconds for opening IndexedDB-based storage. +This is useful to ensure that your application does not block indefinitely +while waiting for the IndexedDB database to open. + +If you want to change the value of this timeout, you can do so by setting +accordingly the value of the `shaka.offline.indexeddb.StorageMechanismOpenTimeout` +variable. For example, to set the timeout to 10 seconds, you can do the following: ```js // In seconds -shaka.offline.indexeddb.StorageMechanismOpenTimeout = 5; +shaka.offline.indexeddb.StorageMechanismOpenTimeout = 10; ``` + +You can also disable this timeout and consequently wait indefinitely for the +IndexedDB database to open successfully or fail to open. To do this, set the +`shaka.offline.indexeddb.StorageMechanismOpenTimeout` variable to `false`: + +```js +shaka.offline.indexeddb.StorageMechanismOpenTimeout = false; +``` + +Note that this configuration must be done before doing any other offline +storage related operation (download and store content, list content, +remove content, playback of content). diff --git a/lib/offline/indexeddb/storage_mechanism.js b/lib/offline/indexeddb/storage_mechanism.js index 88d897527..fa1c50662 100644 --- a/lib/offline/indexeddb/storage_mechanism.js +++ b/lib/offline/indexeddb/storage_mechanism.js @@ -67,7 +67,10 @@ shaka.offline.indexeddb.StorageMechanism = class { shaka.util.Error.Category.STORAGE, shaka.util.Error.Code.INDEXED_DB_INIT_TIMED_OUT)); }); - timeOutTimer.tickAfter(shaka.offline.indexeddb.StorageMechanismOpenTimeout); + const openTimeout = shaka.offline.indexeddb.StorageMechanismOpenTimeout; + if (typeof openTimeout === 'number' && openTimeout > 0) { + timeOutTimer.tickAfter(openTimeout); + } const open = window.indexedDB.open(name, version); open.onsuccess = (event) => { @@ -75,6 +78,7 @@ shaka.offline.indexeddb.StorageMechanism = class { // Too late, we have already given up on opening the storage mechanism. return; } + timeOutTimer.stop(); const db = open.result; this.db_ = db; this.v1_ = shaka.offline.indexeddb.StorageMechanism.createV1_(db); @@ -86,7 +90,6 @@ shaka.offline.indexeddb.StorageMechanism = class { this.v5_ = shaka.offline.indexeddb.StorageMechanism.createV5_(db); this.sessions_ = shaka.offline.indexeddb.StorageMechanism.createEmeSessionCell_(db); - timeOutTimer.stop(); p.resolve(); }; open.onupgradeneeded = (event) => { @@ -98,12 +101,12 @@ shaka.offline.indexeddb.StorageMechanism = class { // Too late, we have already given up on opening the storage mechanism. return; } + timeOutTimer.stop(); p.reject(new shaka.util.Error( shaka.util.Error.Severity.CRITICAL, shaka.util.Error.Category.STORAGE, shaka.util.Error.Code.INDEXED_DB_ERROR, open.error)); - timeOutTimer.stop(); // Firefox will raise an error on the main thread unless we stop it here. event.preventDefault(); @@ -383,7 +386,10 @@ shaka.offline.indexeddb.StorageMechanism.V5_MANIFEST_STORE = 'manifest-v5'; shaka.offline.indexeddb.StorageMechanism.SESSION_ID_STORE = 'session-ids'; /** - * @type {number} + * Timeout in seconds for opening the IndexedDB database, + * or false to disable the timeout and wait indefinitely + * for the database to open successfully or fail. + * @type {number|boolean} * @export */ shaka.offline.indexeddb.StorageMechanismOpenTimeout = 5;