Skip to content

Commit 846dfea

Browse files
committed
feat: some random stuff
1 parent 13ad6fb commit 846dfea

File tree

6 files changed

+245
-117
lines changed

6 files changed

+245
-117
lines changed

packages/indexeddb/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A Josh provider",
55
"author": "Évelyne Lachance <[email protected]> (https://evie.codes/)",
66
"contributors": [
7-
"WilsontheWolf (https://wilson.antti.codes/)",
7+
"WilsontheWolf (https://shorty.systems/)",
88
"DanCodes <[email protected]> (https://dancodes.online/)"
99
],
1010
"license": "Apache-2.0",
@@ -36,6 +36,7 @@
3636
"@favware/cliff-jumper": "^2.0.1",
3737
"@favware/rollup-type-bundler": "^1.0.11",
3838
"@vitest/coverage-v8": "^0.32.0",
39+
"fake-indexeddb": "^4.0.1",
3940
"typedoc": "^0.24.8",
4041
"typedoc-json-parser": "^8.1.2",
4142
"vitest": "^0.32.0"

packages/indexeddb/src/lib/DbHandler.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
export default class DbHandler {
1+
export default class DbHandler<StoredValue = unknown> {
22
private idb: IDBFactory;
33
private db!: IDBDatabase;
44

55
public constructor() {
6-
if (!indexedDB) {
6+
if (!globalThis.indexedDB) {
77
throw new Error("Your browser doesn't support a stable version of IndexedDB. Josh is unable to run without one.");
88
}
99

10-
this.idb = indexedDB;
10+
this.idb = globalThis.indexedDB;
1111
}
1212

1313
public init() {
@@ -31,7 +31,7 @@ export default class DbHandler {
3131
});
3232
}
3333

34-
public async set(key: string, value: unknown) {
34+
public async set<Value = StoredValue>(key: string, value: Value) {
3535
const all = this.open();
3636
const doc = {
3737
key,
@@ -43,35 +43,34 @@ export default class DbHandler {
4343
await this.handleEvents(request);
4444
}
4545

46-
public async get(key: string) {
46+
public async get<Value = StoredValue>(key: string): Promise<Value | undefined> {
4747
const all = this.open();
4848
const request = all.get(key);
49-
const result = await this.handleEvents(request);
49+
const result = (await this.handleEvents(request)) as {
50+
value: Value | undefined; // Its shit like this why I don't like TS
51+
};
5052

51-
// @ts-ignore it exists f you TS
5253
return result?.value;
5354
}
5455

55-
public async getAll() {
56+
public async getAll<Value = StoredValue>(): Promise<{ [key: string]: Value }> {
5657
const all = this.open();
5758
const request = all.getAll();
58-
const docs = await this.handleEvents(request);
59-
const final = {};
59+
const docs = (await this.handleEvents(request)) as { key: string; value: Value }[];
60+
const final: { [key: string]: Value } = {}; // Why can't this be inferred from usage????
6061

61-
// @ts-ignore TS GO AWAY
6262
docs.forEach((x) => {
63-
// @ts-ignore TS GO AWAY
6463
final[x.key] = x.value;
6564
});
6665

6766
return final;
6867
}
6968

70-
public async getKeys() {
69+
public async getKeys(): Promise<string[]> {
7170
const all = this.open();
7271
const request = all.getAllKeys();
7372

74-
return this.handleEvents(request);
73+
return (await this.handleEvents(request)) as string[];
7574
}
7675

7776
public async count() {

0 commit comments

Comments
 (0)