Source: lib/offline/indexeddb/db_connection.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.offline.indexeddb.DBConnection');
  18. goog.require('shaka.offline.indexeddb.DBOperation');
  19. goog.require('shaka.util.ArrayUtils');
  20. /**
  21. * DBConnection is used to manage an IndexedDB connection. It can create new
  22. * operations. If the connection is killed (via |destroy|) all pending
  23. * operations will be cancelled.
  24. */
  25. shaka.offline.indexeddb.DBConnection = class {
  26. /**
  27. * @param {IDBDatabase} connection A connection to an IndexedDB instance.
  28. */
  29. constructor(connection) {
  30. /** @private {IDBDatabase} */
  31. this.connection_ = connection;
  32. /** @private {!Array.<shaka.offline.indexeddb.DBOperation>} */
  33. this.pending_ = [];
  34. }
  35. /**
  36. * @return {!Promise}
  37. */
  38. destroy() {
  39. return Promise.all(this.pending_.map((op) => {
  40. return op.abort();
  41. }));
  42. }
  43. /**
  44. * @param {string} store The name of the store that the operation should
  45. * occur on.
  46. * @return {!shaka.offline.indexeddb.DBOperation}
  47. */
  48. startReadOnlyOperation(store) {
  49. return this.startOperation_(store, 'readonly');
  50. }
  51. /**
  52. * @param {string} store The name of the store that the operation should
  53. * occur on.
  54. * @return {!shaka.offline.indexeddb.DBOperation}
  55. */
  56. startReadWriteOperation(store) {
  57. return this.startOperation_(store, 'readwrite');
  58. }
  59. /**
  60. * @param {string} store The name of the store that the operation should
  61. * occur on.
  62. * @param {string} type The type of operation being performed on the store.
  63. * This determines what commands may be performed. This
  64. * can either be "readonly" or "readwrite".
  65. * @return {!shaka.offline.indexeddb.DBOperation}
  66. * @private
  67. */
  68. startOperation_(store, type) {
  69. let transaction = this.connection_.transaction([store], type);
  70. let operation = new shaka.offline.indexeddb.DBOperation(transaction, store);
  71. this.pending_.push(operation);
  72. // Once the operation is done (regardless of outcome) stop tracking it.
  73. operation.promise().then(
  74. () => this.stopTracking_(operation),
  75. () => this.stopTracking_(operation)
  76. );
  77. return operation;
  78. }
  79. /**
  80. * @param {!shaka.offline.indexeddb.DBOperation} operation
  81. * @private
  82. */
  83. stopTracking_(operation) {
  84. shaka.util.ArrayUtils.remove(this.pending_, operation);
  85. }
  86. };