You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

125 lines
2.7 KiB

  1. /*
  2. Copyright 2017 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import {DBWrapper} from 'workbox-core/_private/DBWrapper.mjs';
  14. import '../_version.mjs';
  15. const URL_KEY = 'url';
  16. const TIMESTAMP_KEY = 'timestamp';
  17. /**
  18. * Returns the timestamp model.
  19. *
  20. * @private
  21. */
  22. class CacheTimestampsModel {
  23. /**
  24. *
  25. * @param {string} cacheName
  26. *
  27. * @private
  28. */
  29. constructor(cacheName) {
  30. // TODO Check cacheName
  31. this._cacheName = cacheName;
  32. this._storeName = cacheName;
  33. this._db = new DBWrapper(this._cacheName, 2, {
  34. onupgradeneeded: (evt) => this._handleUpgrade(evt),
  35. });
  36. }
  37. /**
  38. * Should perform an upgrade of indexedDB.
  39. *
  40. * @param {Event} evt
  41. *
  42. * @private
  43. */
  44. _handleUpgrade(evt) {
  45. const db = evt.target.result;
  46. if (evt.oldVersion < 2) {
  47. // Remove old databases.
  48. if (db.objectStoreNames.contains('workbox-cache-expiration')) {
  49. db.deleteObjectStore('workbox-cache-expiration');
  50. }
  51. }
  52. db
  53. .createObjectStore(this._storeName, {keyPath: URL_KEY})
  54. .createIndex(TIMESTAMP_KEY, TIMESTAMP_KEY, {unique: false});
  55. }
  56. /**
  57. * @param {string} url
  58. * @param {number} timestamp
  59. *
  60. * @private
  61. */
  62. async setTimestamp(url, timestamp) {
  63. await this._db.put(this._storeName, {
  64. [URL_KEY]: new URL(url, location).href,
  65. [TIMESTAMP_KEY]: timestamp,
  66. });
  67. }
  68. /**
  69. * Get all of the timestamps in the indexedDB.
  70. *
  71. * @return {Array<Objects>}
  72. *
  73. * @private
  74. */
  75. async getAllTimestamps() {
  76. return await this._db.getAllMatching(this._storeName, {
  77. index: TIMESTAMP_KEY,
  78. });
  79. }
  80. /**
  81. * Returns the timestamp stored for a given URL.
  82. *
  83. * @param {string} url
  84. * @return {number}
  85. *
  86. * @private
  87. */
  88. async getTimestamp(url) {
  89. const timestampObject = await this._db.get(this._storeName, url);
  90. return timestampObject.timestamp;
  91. }
  92. /**
  93. * @param {string} url
  94. *
  95. * @private
  96. */
  97. async deleteUrl(url) {
  98. await this._db.delete(this._storeName, new URL(url, location).href);
  99. }
  100. /**
  101. * Removes the underlying IndexedDB object store entirely.
  102. */
  103. async delete() {
  104. await this._db.deleteDatabase();
  105. this._db = null;
  106. }
  107. }
  108. export default CacheTimestampsModel;