Source: lib/offline/indexeddb/db_operation.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.DBOperation');
  18. goog.require('shaka.util.PublicPromise');
  19. /**
  20. * A DBOperation wraps an IndexedDB transaction in a promise.
  21. */
  22. shaka.offline.indexeddb.DBOperation = class {
  23. /**
  24. * @param {IDBTransaction} transaction
  25. * @param {string} storeName
  26. */
  27. constructor(transaction, storeName) {
  28. /** @private {IDBTransaction} */
  29. this.transaction_ = transaction;
  30. /** @private {IDBObjectStore} */
  31. this.store_ = transaction.objectStore(storeName);
  32. /** @private {!shaka.util.PublicPromise} */
  33. this.promise_ = new shaka.util.PublicPromise();
  34. // Connect the transaction and the promise together.
  35. // |event.preventDefault()| is used on all non-successful callbacks to
  36. // prevent Firefox from surfacing the error on the main thread.
  37. transaction.onabort = (event) => {
  38. event.preventDefault();
  39. this.promise_.reject();
  40. };
  41. transaction.onerror = (event) => {
  42. event.preventDefault();
  43. this.promise_.reject();
  44. };
  45. transaction.oncomplete = (event) => {
  46. this.promise_.resolve();
  47. };
  48. }
  49. /**
  50. * @return {!Promise}
  51. */
  52. abort() {
  53. try {
  54. this.transaction_.abort();
  55. } catch (e) {
  56. // Ignore any exceptions that may be thrown as a result of aborting
  57. // the transaction.
  58. }
  59. // Wait for the promise to be rejected, but ignore the rejection error.
  60. return this.promise_.catch(() => {});
  61. }
  62. /**
  63. * Get the store that the operation can interact with. Requests can be made
  64. * on the store. All requests made on the store will complete successfully
  65. * before the operation's promise will resolve. If any request fails, the
  66. * operation's promise will be rejected.
  67. *
  68. * @return {IDBObjectStore}
  69. */
  70. store() { return this.store_; }
  71. /**
  72. * Get the promise that wraps the transaction. This promise will resolve when
  73. * all requests on the object store complete successfully and the transaction
  74. * completes. If any request fails or the operation is aborted, the promise
  75. * will be rejected.
  76. *
  77. * @return {!Promise}
  78. */
  79. promise() { return this.promise_; }
  80. };