@lunarislab/state-sync 1.1.11 → 1.1.13
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/classes/Client.d.ts +4 -1
- package/classes/Client.js +18 -0
- package/classes/Saver.js +2 -1
- package/classes/Watcher.js +1 -0
- package/package.json +1 -1
package/classes/Client.d.ts
CHANGED
@@ -4,7 +4,7 @@ import { EventSignature } from "abitype/dist/types/human-readable/types/signatur
|
|
4
4
|
import { Syncer } from "./Syncer";
|
5
5
|
import { ISyncerConfig } from "../types/Syncer";
|
6
6
|
import { SimpleEmitter } from "./SimpleEmitter";
|
7
|
-
import { LevelDBConstructor } from "../types/Level";
|
7
|
+
import { IAbstractLevel, IDatabaseItem, LevelDBConstructor } from "../types/Level";
|
8
8
|
/**
|
9
9
|
* The StateSync client class.
|
10
10
|
*/
|
@@ -13,6 +13,9 @@ export declare class StateSync extends SimpleEmitter<IStateSyncEvents> {
|
|
13
13
|
wallet: WalletClient;
|
14
14
|
level: LevelDBConstructor;
|
15
15
|
levelPath: string;
|
16
|
+
protected dbInstances: Map<string, IAbstractLevel<string, IDatabaseItem>>;
|
16
17
|
constructor(config: IClientConfig);
|
18
|
+
createDB(id: string): IAbstractLevel<string, IDatabaseItem>;
|
19
|
+
getDB(id: string): IAbstractLevel<string, IDatabaseItem>;
|
17
20
|
createSyncer<signature extends EventSignature>(config: ISyncerConfig<signature>): Promise<Syncer<signature>>;
|
18
21
|
}
|
package/classes/Client.js
CHANGED
@@ -13,13 +13,31 @@ class StateSync extends SimpleEmitter_1.SimpleEmitter {
|
|
13
13
|
this.wallet = config.wallet;
|
14
14
|
this.level = config.level;
|
15
15
|
this.levelPath = config.levelPath || "";
|
16
|
+
this.dbInstances = new Map();
|
16
17
|
this.public.watchBlocks({
|
17
18
|
onBlock: (block) => {
|
18
19
|
this.emit("newBlock", { block });
|
19
20
|
}
|
20
21
|
});
|
21
22
|
}
|
23
|
+
;
|
24
|
+
createDB(id) {
|
25
|
+
if (this.dbInstances.has(id))
|
26
|
+
throw Error("Database already exists");
|
27
|
+
const fullPath = `${this.levelPath}/${id}`.replace('//', "/");
|
28
|
+
const db = new this.level(fullPath, { valueEncoding: "json" });
|
29
|
+
this.dbInstances.set(id, db);
|
30
|
+
return db;
|
31
|
+
}
|
32
|
+
getDB(id) {
|
33
|
+
const db = this.dbInstances.get(id);
|
34
|
+
if (!db)
|
35
|
+
throw Error("Database not found");
|
36
|
+
return db;
|
37
|
+
}
|
22
38
|
async createSyncer(config) {
|
39
|
+
if (!this.dbInstances.has(config.dbID))
|
40
|
+
this.createDB(config.dbID);
|
23
41
|
return new Syncer_1.Syncer(this, config);
|
24
42
|
}
|
25
43
|
;
|
package/classes/Saver.js
CHANGED
@@ -8,7 +8,7 @@ class Saver {
|
|
8
8
|
this.dbID = config.dbID;
|
9
9
|
this.onLog = config.onLog;
|
10
10
|
this.getItemID = config.getItemID ? config.getItemID : (log) => log.address;
|
11
|
-
this.db =
|
11
|
+
this.db = this.client.getDB(config.dbID);
|
12
12
|
}
|
13
13
|
;
|
14
14
|
// save some env variables in the db
|
@@ -24,6 +24,7 @@ class Saver {
|
|
24
24
|
}
|
25
25
|
;
|
26
26
|
async init() {
|
27
|
+
await this.db.open();
|
27
28
|
const env = await this.getEnv();
|
28
29
|
this.lastBlockNum = env?.lastBlockNum ? BigInt(env.lastBlockNum) : 0n;
|
29
30
|
await this._updateEnv();
|
package/classes/Watcher.js
CHANGED