Skip to content

Commit

Permalink
Fix IndexedDB types
Browse files Browse the repository at this point in the history
The built-in externs for IndexedDB in Closure Compiler are missing
some type info.  This overrides them to add what's missing.

This also tweaks type information in a few places in lib/offline/ to
match.

This was caught by a compiler upgrade.

Issue #2528

Change-Id: I97096656f53b426067219e2d4e3aa16f32b87188
  • Loading branch information
joeyparrish committed Apr 30, 2020
1 parent f15f371 commit 3ac75b0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
24 changes: 24 additions & 0 deletions externs/idb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/** @license
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Externs for IndexedDB to override the ones provided by the
* Closure Compiler.
*
* TODO: contribute fixes to the Closure Compiler externs
*
* @externs
*/

/**
* @constructor
* @extends {IDBRequest}
* @suppress {duplicate}
* The upstream extern doesn't have the correct type for Result.
*/
var IDBOpenDBRequest = function() {};

/** @type {!IDBDatabase} */
IDBOpenDBRequest.prototype.result;
8 changes: 4 additions & 4 deletions lib/offline/indexeddb/base_storage_cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ shaka.offline.indexeddb.BaseStorageCell = class {
/** @type {!Map.<number, shaka.extern.ManifestDB>} */
const values = new Map();

await op.forEachEntry(async (/** number */ key, /** ? */ value) => {
await op.forEachEntry(async (key, value) => {
const manifest = await this.convertManifest(value);
values.set(key, manifest);
values.set(/** @type {number} */(key), manifest);
});

await op.promise();
Expand Down Expand Up @@ -158,12 +158,12 @@ shaka.offline.indexeddb.BaseStorageCell = class {
const keys = [];

// Write each segment out. When each request completes, the key will
// be in |event.target.result| as can be seen in
// be in |request.result| as can be seen in
// https://w3c.github.io/IndexedDB/#key-generator-construct.
for (const value of values) {
const request = store.add(value);
request.onsuccess = (event) => {
const key = event.target.result;
const key = /** @type {number} */(request.result);
keys.push(key);
};
}
Expand Down
12 changes: 6 additions & 6 deletions lib/offline/indexeddb/db_operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ shaka.offline.indexeddb.DBOperation = class {
* Calls the given callback for each entry in the database. The callback
* must be synchronous, but this operation happens asynchronously.
*
* @param {function(number, T, !IDBCursorWithValue=):(Promise|undefined)}
* @param {function(!IDBKeyType, T, !IDBCursorWithValue=):(Promise|undefined)}
* callback
* @return {!Promise}
* @template T
Expand All @@ -72,16 +72,16 @@ shaka.offline.indexeddb.DBOperation = class {
const req = this.store_.openCursor();
req.onerror = reject;
req.onsuccess = async (event) => {
// When we reach the end of the data that the cursor is iterating
// over, |event.target.result| will be null to signal the end of the
// iteration.
// When we reach the end of the data that the cursor is iterating over,
// |req.result| will be null to signal the end of the iteration.
// https://developer.mozilla.org/en-US/docs/Web/API/IDBCursor/continue
const cursor = event.target.result;
if (!cursor) {
if (req.result == null) {
resolve();
return;
}

/** @type {!IDBCursorWithValue} */
const cursor = /** @type {!IDBCursorWithValue} */(req.result);
await callback(cursor.key, cursor.value, cursor);
cursor.continue();
};
Expand Down
4 changes: 2 additions & 2 deletions lib/offline/indexeddb/storage_mechanism.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ shaka.offline.indexeddb.StorageMechanism = class {
const p = new shaka.util.PublicPromise();
const open = window.indexedDB.open(name, version);
open.onsuccess = (event) => {
const db = event.target.result;
const db = open.result;
this.db_ = db;
this.v1_ = shaka.offline.indexeddb.StorageMechanism.createV1_(db);
this.v2_ = shaka.offline.indexeddb.StorageMechanism.createV2_(db);
Expand All @@ -69,7 +69,7 @@ shaka.offline.indexeddb.StorageMechanism = class {
};
open.onupgradeneeded = (event) => {
// Add object stores for the latest version only.
this.createStores_(event.target.result);
this.createStores_(open.result);
};
open.onerror = (event) => {
p.reject(new shaka.util.Error(
Expand Down

0 comments on commit 3ac75b0

Please sign in to comment.