@pluv/addon-indexeddb 5.0.1 → 5.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +121 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +88 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +9 -9
- package/CHANGELOG.md +0 -1170
- package/eslint.config.mjs +0 -4
- package/src/IndexedDBStorage.ts +0 -132
- package/src/addonIndexedDB.ts +0 -34
- package/src/index.ts +0 -1
- package/tsconfig.json +0 -5
- package/tsdown.config.ts +0 -14
package/eslint.config.mjs
DELETED
package/src/IndexedDBStorage.ts
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { AbstractStorageStore } from "@pluv/client";
|
|
2
|
-
import type { IDBPDatabase } from "idb";
|
|
3
|
-
import { openDB } from "idb";
|
|
4
|
-
|
|
5
|
-
const STORAGE_STORE_FLATTEN_THRESHOLD = 500;
|
|
6
|
-
const UPGRADES_KEY = "__PLUV_UPDATES";
|
|
7
|
-
|
|
8
|
-
type IndexDBSchema = {
|
|
9
|
-
[UPGRADES_KEY]: {
|
|
10
|
-
key: number;
|
|
11
|
-
value: string;
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
type IndexedDB = IDBPDatabase<IndexDBSchema>;
|
|
16
|
-
|
|
17
|
-
export class IndexedDBStorage extends AbstractStorageStore {
|
|
18
|
-
public room: string;
|
|
19
|
-
|
|
20
|
-
private _db: IndexedDB | null = null;
|
|
21
|
-
private _dbRef: number = 0;
|
|
22
|
-
private _dbSize: number = 0;
|
|
23
|
-
|
|
24
|
-
constructor(room: string) {
|
|
25
|
-
super(room);
|
|
26
|
-
|
|
27
|
-
this.room = room;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public async addUpdate(update: string): Promise<void> {
|
|
31
|
-
const db = this._db;
|
|
32
|
-
|
|
33
|
-
if (!db) return;
|
|
34
|
-
|
|
35
|
-
await db.add(UPGRADES_KEY, update);
|
|
36
|
-
|
|
37
|
-
this._dbSize += 1;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public destroy(): Promise<void> {
|
|
41
|
-
if (!this._db) return Promise.resolve();
|
|
42
|
-
|
|
43
|
-
this._db.close();
|
|
44
|
-
this._db = null;
|
|
45
|
-
this._dbRef = 0;
|
|
46
|
-
this._dbSize = 0;
|
|
47
|
-
|
|
48
|
-
return Promise.resolve();
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public async flatten(encodedState: string): Promise<void> {
|
|
52
|
-
const db = this._db;
|
|
53
|
-
|
|
54
|
-
if (!db) return;
|
|
55
|
-
|
|
56
|
-
await db.add(UPGRADES_KEY, encodedState);
|
|
57
|
-
await db.delete(UPGRADES_KEY, this._getUpperBound(this._dbRef, true));
|
|
58
|
-
|
|
59
|
-
this._dbSize = await this._getSize();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public async getShouldFlatten(): Promise<boolean> {
|
|
63
|
-
const dbSize = await this._getSize();
|
|
64
|
-
|
|
65
|
-
return dbSize >= STORAGE_STORE_FLATTEN_THRESHOLD;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
public async getUpdates(start: number = 0): Promise<readonly string[]> {
|
|
69
|
-
const db = this._db;
|
|
70
|
-
|
|
71
|
-
if (!db) return [];
|
|
72
|
-
|
|
73
|
-
const updates = await db.getAll(UPGRADES_KEY, this._getLowerBound(start));
|
|
74
|
-
|
|
75
|
-
const lastKey = await this._getLastKey();
|
|
76
|
-
|
|
77
|
-
this._dbRef = lastKey + 1;
|
|
78
|
-
|
|
79
|
-
return updates;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public async initialize(): Promise<void> {
|
|
83
|
-
const newDB = await openDB<IndexDBSchema>(this.room, undefined, {
|
|
84
|
-
upgrade: (db) => {
|
|
85
|
-
db.createObjectStore(UPGRADES_KEY, { autoIncrement: true });
|
|
86
|
-
},
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
newDB.addEventListener("versionchange", () => {
|
|
90
|
-
newDB.close();
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
if (typeof window !== "undefined") {
|
|
94
|
-
window.addEventListener("unload", () => {
|
|
95
|
-
newDB.close();
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
this._db = newDB;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
private async _getLastKey(): Promise<number> {
|
|
103
|
-
const db = this._db;
|
|
104
|
-
|
|
105
|
-
if (!db) throw new Error("IndexedDB not initialized");
|
|
106
|
-
|
|
107
|
-
const store = db.transaction(UPGRADES_KEY).objectStore(UPGRADES_KEY);
|
|
108
|
-
const cursor = await store.openCursor(null, "prev");
|
|
109
|
-
|
|
110
|
-
return cursor?.key ?? -1;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
private _getLowerBound(lower: number, open: boolean = false): IDBKeyRange {
|
|
114
|
-
return IDBKeyRange.lowerBound(lower, open);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private async _getSize(): Promise<number> {
|
|
118
|
-
if (this._dbSize) return this._dbSize;
|
|
119
|
-
|
|
120
|
-
const db = this._db;
|
|
121
|
-
|
|
122
|
-
if (!db) throw new Error("IndexedDB not initialized");
|
|
123
|
-
|
|
124
|
-
const store = db.transaction(UPGRADES_KEY).objectStore(UPGRADES_KEY);
|
|
125
|
-
|
|
126
|
-
return store.count();
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
private _getUpperBound(upper: number, open: boolean = false): IDBKeyRange {
|
|
130
|
-
return IDBKeyRange.upperBound(upper, open);
|
|
131
|
-
}
|
|
132
|
-
}
|
package/src/addonIndexedDB.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { PluvRoom, PluvRoomAddon } from "@pluv/client";
|
|
2
|
-
import type { AbstractCrdtDocFactory, CrdtType } from "@pluv/crdt";
|
|
3
|
-
import type { IOLike, JsonObject } from "@pluv/types";
|
|
4
|
-
import { IndexedDBStorage } from "./IndexedDBStorage";
|
|
5
|
-
|
|
6
|
-
export interface AddonIndexedDBConfig<
|
|
7
|
-
TIO extends IOLike<any, any, any>,
|
|
8
|
-
TMetadata extends JsonObject,
|
|
9
|
-
TPresence extends Record<string, any>,
|
|
10
|
-
TCrdt extends AbstractCrdtDocFactory<any, any>,
|
|
11
|
-
> {
|
|
12
|
-
enabled?: boolean | ((room: PluvRoom<TIO, TMetadata, TPresence, TCrdt>) => boolean);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const addonIndexedDB = <
|
|
16
|
-
TIO extends IOLike<any, any, any>,
|
|
17
|
-
TMetadata extends JsonObject,
|
|
18
|
-
TPresence extends Record<string, any>,
|
|
19
|
-
TCrdt extends AbstractCrdtDocFactory<any, any>,
|
|
20
|
-
>(
|
|
21
|
-
config?: AddonIndexedDBConfig<TIO, TMetadata, TPresence, TCrdt>,
|
|
22
|
-
): PluvRoomAddon<TIO, TMetadata, TPresence, TCrdt> => {
|
|
23
|
-
const { enabled = true } = config ?? {};
|
|
24
|
-
|
|
25
|
-
return ({ room }) => {
|
|
26
|
-
const _enabled = typeof enabled === "boolean" ? enabled : enabled(room);
|
|
27
|
-
|
|
28
|
-
if (!_enabled) return {};
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
storage: new IndexedDBStorage(room.id),
|
|
32
|
-
};
|
|
33
|
-
};
|
|
34
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { addonIndexedDB } from "./addonIndexedDB";
|
package/tsconfig.json
DELETED
package/tsdown.config.ts
DELETED