implemented SlopStore and tested
This commit is contained in:
@@ -153,7 +153,26 @@ describe("SlopStore", () => {
|
||||
db.close()
|
||||
})
|
||||
|
||||
it("passes", () => {
|
||||
expect(true).toBeTrue()
|
||||
it("stores slop", async () => {
|
||||
const stored = await slopdb.slop_store.store("its-slop.slop", "some/slop")
|
||||
expect(stored).toBe("its-slop.slop")
|
||||
|
||||
const retrieved = await slopdb.slop_store.get(new URL("http://its-slop.slop/some/slop"))
|
||||
const expected_set = new Set()
|
||||
expected_set.add("some/slop")
|
||||
expect(retrieved).toEqual({domain: "its-slop.slop", paths: expected_set})
|
||||
})
|
||||
|
||||
it("stores new slop paths to existing domains", async () => {
|
||||
const stored = await slopdb.slop_store.store("its-slop.slop", "some/slop")
|
||||
const new_path = await slopdb.slop_store.store("its-slop.slop", "some/other/slop")
|
||||
expect(new_path).toBe("its-slop.slop")
|
||||
|
||||
const expected_set = new Set()
|
||||
expected_set.add("some/slop")
|
||||
expected_set.add("some/other/slop")
|
||||
|
||||
const retrieved = await slopdb.slop_store.get(new URL("http://its-slop.slop/some/slop"))
|
||||
expect(retrieved).toEqual({domain: "its-slop.slop", paths: expected_set})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -56,12 +56,24 @@ export class SlopStore {
|
||||
this.slopdb = slopdb
|
||||
}
|
||||
|
||||
async store(domain: string, path: string) {
|
||||
|
||||
async store(domain: string, path: string): Promise<IDBValidKey> {
|
||||
const slop_store = this.slopdb.transaction(SlopStore.slop_objectstore_name, "readwrite").objectStore(SlopStore.slop_objectstore_name)
|
||||
const existing_domain = await slop_store.get(domain)
|
||||
if(existing_domain) {
|
||||
// append this path to the existing domain object
|
||||
existing_domain.paths.add(path)
|
||||
return slop_store.put(existing_domain)
|
||||
} else {
|
||||
// create a domain object containing this path
|
||||
const paths_set = new Set()
|
||||
paths_set.add(path)
|
||||
return slop_store.add({domain: domain, paths: paths_set})
|
||||
}
|
||||
}
|
||||
|
||||
async get(url: string) {
|
||||
|
||||
async get(url: URL) {
|
||||
const slop_store = this.slopdb.transaction(SlopStore.slop_objectstore_name, "readonly").objectStore(SlopStore.slop_objectstore_name)
|
||||
return await slop_store.get(url.hostname)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user