begin integration of improved indexeddb code
refactored SlopDB class to contain a CheckCache object, as there was a circular dependency previously
This commit is contained in:
@@ -62,7 +62,7 @@ describe("SlopDB", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("caches a checked url", async () => {
|
it("caches a checked url", async () => {
|
||||||
const cache = slopdb.get_check_cache()
|
const cache = slopdb.check_cache
|
||||||
expect(cache).toBeInstanceOf(CheckCache)
|
expect(cache).toBeInstanceOf(CheckCache)
|
||||||
|
|
||||||
const slop_url = new URL("https://sloppy-slop.com/sloparticle")
|
const slop_url = new URL("https://sloppy-slop.com/sloparticle")
|
||||||
@@ -81,7 +81,7 @@ describe("SlopDB", () => {
|
|||||||
it("updates a cached url's timestamp when it is accessed", async () => {
|
it("updates a cached url's timestamp when it is accessed", async () => {
|
||||||
|
|
||||||
|
|
||||||
const cache = slopdb.get_check_cache()
|
const cache = slopdb.check_cache
|
||||||
|
|
||||||
const slop_url = new URL("https://sloppy-slop.com/sloparticle")
|
const slop_url = new URL("https://sloppy-slop.com/sloparticle")
|
||||||
await cache.store(slop_url.toString())
|
await cache.store(slop_url.toString())
|
||||||
@@ -94,7 +94,7 @@ describe("SlopDB", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("evicts the least recently accessed URL when an item is added to a full cache", async () => {
|
it("evicts the least recently accessed URL when an item is added to a full cache", async () => {
|
||||||
const cache = slopdb.get_check_cache()
|
const cache = slopdb.check_cache
|
||||||
cache.cache_capacity = 2
|
cache.cache_capacity = 2
|
||||||
|
|
||||||
const slop_url = new URL("https://sloppy-slop.com/sloparticle")
|
const slop_url = new URL("https://sloppy-slop.com/sloparticle")
|
||||||
|
|||||||
@@ -35,12 +35,12 @@ export class IDBCursorValueIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class CheckCache {
|
export class CheckCache {
|
||||||
slopdb: SlopDB
|
slopdb: IDBPDatabase
|
||||||
cache_capacity: number
|
cache_capacity: number
|
||||||
size: number
|
size: number
|
||||||
static cache_objectstore_name = "checkcache"
|
static cache_objectstore_name = "checkcache"
|
||||||
|
|
||||||
constructor(slopdb: SlopDB, max_entries: number) {
|
constructor(slopdb: IDBPDatabase, max_entries: number) {
|
||||||
this.slopdb = slopdb
|
this.slopdb = slopdb
|
||||||
this.cache_capacity = max_entries
|
this.cache_capacity = max_entries
|
||||||
this.size = 0
|
this.size = 0
|
||||||
@@ -54,7 +54,7 @@ export class CheckCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async store(url: string) {
|
async store(url: string) {
|
||||||
const cache_store = this.slopdb.db.transaction(CheckCache.cache_objectstore_name, "readwrite").objectStore(CheckCache.cache_objectstore_name)
|
const cache_store = this.slopdb.transaction(CheckCache.cache_objectstore_name, "readwrite").objectStore(CheckCache.cache_objectstore_name)
|
||||||
const cache_item = this.cache_item_factory(url)
|
const cache_item = this.cache_item_factory(url)
|
||||||
cache_store.add(cache_item)
|
cache_store.add(cache_item)
|
||||||
this.size++
|
this.size++
|
||||||
@@ -65,7 +65,7 @@ export class CheckCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async get(url: string) {
|
async get(url: string) {
|
||||||
const cache_store = this.slopdb.db.transaction(CheckCache.cache_objectstore_name, "readwrite").objectStore(CheckCache.cache_objectstore_name)
|
const cache_store = this.slopdb.transaction(CheckCache.cache_objectstore_name, "readwrite").objectStore(CheckCache.cache_objectstore_name)
|
||||||
const cache_item = await cache_store.get(url)
|
const cache_item = await cache_store.get(url)
|
||||||
if(cache_item) {
|
if(cache_item) {
|
||||||
cache_item.check_timestamp = Date.now()
|
cache_item.check_timestamp = Date.now()
|
||||||
@@ -75,7 +75,7 @@ export class CheckCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async evict_lru() {
|
async evict_lru() {
|
||||||
const timestamp_index = this.slopdb.db.transaction(CheckCache.cache_objectstore_name, "readwrite").store.index("timestamp")
|
const timestamp_index = this.slopdb.transaction(CheckCache.cache_objectstore_name, "readwrite").store.index("timestamp")
|
||||||
const timestamp_cursor = await timestamp_index.openCursor()
|
const timestamp_cursor = await timestamp_index.openCursor()
|
||||||
await timestamp_cursor.delete()
|
await timestamp_cursor.delete()
|
||||||
this.size--
|
this.size--
|
||||||
@@ -84,9 +84,12 @@ export class CheckCache {
|
|||||||
|
|
||||||
export class SlopDB {
|
export class SlopDB {
|
||||||
version: number
|
version: number
|
||||||
open_promise: Promise<IDBPDatabase>
|
open_promise: Promise<any>
|
||||||
db: IDBPDatabase
|
db: IDBPDatabase
|
||||||
|
|
||||||
|
// slop_store: SlopStore
|
||||||
|
check_cache: CheckCache
|
||||||
|
|
||||||
static apply_db_upgrade(db: IDBPDatabase, idb_version: number) {
|
static apply_db_upgrade(db: IDBPDatabase, idb_version: number) {
|
||||||
switch (idb_version) {
|
switch (idb_version) {
|
||||||
case 1:
|
case 1:
|
||||||
@@ -100,6 +103,7 @@ export class SlopDB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
constructor(idb_version: number) {
|
constructor(idb_version: number) {
|
||||||
|
// create or update/upgrade the indexeddb
|
||||||
this.version = idb_version
|
this.version = idb_version
|
||||||
this.open_promise = openDB("SlopDB", idb_version, {
|
this.open_promise = openDB("SlopDB", idb_version, {
|
||||||
upgrade(db, oldVersion, newVersion, transaction, event) {
|
upgrade(db, oldVersion, newVersion, transaction, event) {
|
||||||
@@ -113,15 +117,15 @@ export class SlopDB {
|
|||||||
blocking(curVer, bloVer, event) {
|
blocking(curVer, bloVer, event) {
|
||||||
console.error("IDB Open blocking " + curVer + " blocked ver " + bloVer)
|
console.error("IDB Open blocking " + curVer + " blocked ver " + bloVer)
|
||||||
}
|
}
|
||||||
|
}).then((db) => {
|
||||||
|
this.db = db
|
||||||
|
// create a CheckCache object to handle the check_cache object store
|
||||||
|
this.check_cache = new CheckCache(this.db, 512)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async db_opened() {
|
async db_opened() {
|
||||||
this.db = await this.open_promise
|
await this.open_promise
|
||||||
}
|
|
||||||
|
|
||||||
get_check_cache() {
|
|
||||||
return new CheckCache(this, 256)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,39 +1,15 @@
|
|||||||
import { API_URL } from "./common.js"
|
import { API_URL } from "./common.js"
|
||||||
|
import { SlopDB } from "./indexed-db.js"
|
||||||
|
|
||||||
let access_token: string
|
let access_token: string
|
||||||
|
let db: SlopDB
|
||||||
|
|
||||||
function setup_storage_db() {
|
async function setup_storage_db() {
|
||||||
/* create indexeddb object store to retain objects in the form of
|
db = new SlopDB(2)
|
||||||
* {"domain": "domain.tld",
|
await db.db_opened()
|
||||||
* "paths": [
|
|
||||||
* "page/1",
|
|
||||||
* "page/2"
|
|
||||||
* ]
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
let db: IDBDatabase
|
|
||||||
const db_request = window.indexedDB.open("SlopDB", 1)
|
|
||||||
|
|
||||||
db_request.onerror = (event) => {
|
|
||||||
// handle error
|
|
||||||
console.log(event)
|
|
||||||
}
|
|
||||||
|
|
||||||
db_request.onsuccess = (event) => {
|
|
||||||
// create objectstore
|
|
||||||
console.log(event)
|
|
||||||
//@ts-ignore
|
|
||||||
db = event.target.result
|
|
||||||
}
|
|
||||||
|
|
||||||
db_request.onupgradeneeded = (event) => {
|
|
||||||
console.log(event)
|
|
||||||
//@ts-ignore
|
|
||||||
db = event.target.result
|
|
||||||
const slop_store = db.createObjectStore("slop", { keyPath: "domain" })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function on_install_handler() {
|
async function on_install_handler() {
|
||||||
setup_storage_db()
|
setup_storage_db()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user