@peerbit/program 2.3.1 → 2.4.0
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/lib/esm/handler.js +31 -3
- package/lib/esm/handler.js.map +1 -1
- package/package.json +7 -7
- package/src/handler.ts +42 -4
package/lib/esm/handler.js
CHANGED
|
@@ -7,12 +7,15 @@ export class Handler {
|
|
|
7
7
|
_openQueue;
|
|
8
8
|
constructor(properties) {
|
|
9
9
|
this.properties = properties;
|
|
10
|
-
this._openQueue = new
|
|
10
|
+
this._openQueue = new Map();
|
|
11
11
|
this.items = new Map();
|
|
12
12
|
}
|
|
13
13
|
async stop() {
|
|
14
|
+
await Promise.all([...this._openQueue.values()].map((x) => {
|
|
15
|
+
x.clear();
|
|
16
|
+
return x.onIdle();
|
|
17
|
+
}));
|
|
14
18
|
this._openQueue.clear();
|
|
15
|
-
await this._openQueue.onIdle();
|
|
16
19
|
// Close all open databases
|
|
17
20
|
await Promise.all([...this.items.values()].map((program) => program.close()));
|
|
18
21
|
// Remove all databases from the state
|
|
@@ -20,6 +23,7 @@ export class Handler {
|
|
|
20
23
|
}
|
|
21
24
|
_onProgamClose(program) {
|
|
22
25
|
this.items.delete(program.address.toString());
|
|
26
|
+
// TODO remove item from this._openQueue?
|
|
23
27
|
}
|
|
24
28
|
async _onProgramOpen(program, mergeSrategy) {
|
|
25
29
|
const programAddress = program.address?.toString();
|
|
@@ -69,6 +73,9 @@ export class Handler {
|
|
|
69
73
|
else {
|
|
70
74
|
program = (await this.properties.load(storeOrAddress, this.properties.client.services.blocks, options)); // TODO fix typings
|
|
71
75
|
if (!this.properties.shouldMonitor(program)) {
|
|
76
|
+
if (!program) {
|
|
77
|
+
throw new Error("Failed to resolve program with address: " + storeOrAddress);
|
|
78
|
+
}
|
|
72
79
|
throw new Error(`Failed to open program because program is of type ${program?.constructor.name} `);
|
|
73
80
|
}
|
|
74
81
|
}
|
|
@@ -91,6 +98,7 @@ export class Handler {
|
|
|
91
98
|
}
|
|
92
99
|
}
|
|
93
100
|
logger.debug(`Open database '${program.constructor.name}`);
|
|
101
|
+
// TODO prevent resave if already saved
|
|
94
102
|
const address = await program.save(this.properties.client.services.blocks);
|
|
95
103
|
const existing = await this.checkProcessExisting(address, program, options?.existing);
|
|
96
104
|
if (existing) {
|
|
@@ -127,7 +135,27 @@ export class Handler {
|
|
|
127
135
|
if (options?.parent) {
|
|
128
136
|
return fn();
|
|
129
137
|
}
|
|
130
|
-
|
|
138
|
+
let address;
|
|
139
|
+
if (typeof storeOrAddress === "string") {
|
|
140
|
+
address = storeOrAddress;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
if (storeOrAddress.closed) {
|
|
144
|
+
address = await storeOrAddress.save(this.properties.client.services.blocks);
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
address = storeOrAddress.address;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (address) {
|
|
151
|
+
let queue = this._openQueue.get(address);
|
|
152
|
+
if (!queue) {
|
|
153
|
+
queue = new PQueue({ concurrency: 1 });
|
|
154
|
+
this._openQueue.set(address, queue);
|
|
155
|
+
}
|
|
156
|
+
return queue.add(fn); // TODO p-queue seem to return void type ;
|
|
157
|
+
}
|
|
158
|
+
return fn(); // No address lookup,
|
|
131
159
|
}
|
|
132
160
|
}
|
|
133
161
|
//# sourceMappingURL=handler.js.map
|
package/lib/esm/handler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/handler.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,SAAS,CAAC;AAE7B,OAAO,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAErD,MAAM,CAAC,MAAM,MAAM,GAAG,QAAQ,CAAC,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC,CAAC;AAyD9D,MAAM,OAAO,OAAO;IAKT;IAJV,KAAK,CAA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;CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/program",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "Program interface",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -29,11 +29,11 @@
|
|
|
29
29
|
"author": "dao.xyz",
|
|
30
30
|
"license": "MIT",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@dao-xyz/borsh": "^5.1.
|
|
33
|
-
"@peerbit/blocks-interface": "^1.1.
|
|
34
|
-
"@peerbit/crypto": "1.0.
|
|
35
|
-
"@peerbit/lazy-level": "^1.2.
|
|
36
|
-
"@peerbit/pubsub-interface": "^1.1.
|
|
32
|
+
"@dao-xyz/borsh": "^5.1.8",
|
|
33
|
+
"@peerbit/blocks-interface": "^1.1.3",
|
|
34
|
+
"@peerbit/crypto": "1.0.10",
|
|
35
|
+
"@peerbit/lazy-level": "^1.2.1",
|
|
36
|
+
"@peerbit/pubsub-interface": "^1.1.5"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "851f1a4fdff94efd0f9331a43d6f8652e27e6d26"
|
|
39
39
|
}
|
package/src/handler.ts
CHANGED
|
@@ -62,7 +62,7 @@ export type ProgramInitializationOptions<Args, T extends Manageable<Args>> = {
|
|
|
62
62
|
|
|
63
63
|
export class Handler<T extends Manageable<any>> {
|
|
64
64
|
items: Map<string, T>;
|
|
65
|
-
private _openQueue: PQueue
|
|
65
|
+
private _openQueue: Map<string, PQueue>;
|
|
66
66
|
|
|
67
67
|
constructor(
|
|
68
68
|
readonly properties: {
|
|
@@ -75,13 +75,18 @@ export class Handler<T extends Manageable<any>> {
|
|
|
75
75
|
shouldMonitor: (thing: any) => boolean;
|
|
76
76
|
}
|
|
77
77
|
) {
|
|
78
|
-
this._openQueue = new
|
|
78
|
+
this._openQueue = new Map();
|
|
79
79
|
this.items = new Map();
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
async stop() {
|
|
83
|
+
await Promise.all(
|
|
84
|
+
[...this._openQueue.values()].map((x) => {
|
|
85
|
+
x.clear();
|
|
86
|
+
return x.onIdle();
|
|
87
|
+
})
|
|
88
|
+
);
|
|
83
89
|
this._openQueue.clear();
|
|
84
|
-
await this._openQueue.onIdle();
|
|
85
90
|
|
|
86
91
|
// Close all open databases
|
|
87
92
|
await Promise.all(
|
|
@@ -94,6 +99,8 @@ export class Handler<T extends Manageable<any>> {
|
|
|
94
99
|
|
|
95
100
|
private _onProgamClose(program: Manageable<any>) {
|
|
96
101
|
this.items.delete(program.address!.toString());
|
|
102
|
+
|
|
103
|
+
// TODO remove item from this._openQueue?
|
|
97
104
|
}
|
|
98
105
|
|
|
99
106
|
private async _onProgramOpen(
|
|
@@ -163,7 +170,13 @@ export class Handler<T extends Manageable<any>> {
|
|
|
163
170
|
this.properties.client.services.blocks,
|
|
164
171
|
options
|
|
165
172
|
)) as S; // TODO fix typings
|
|
173
|
+
|
|
166
174
|
if (!this.properties.shouldMonitor(program)) {
|
|
175
|
+
if (!program) {
|
|
176
|
+
throw new Error(
|
|
177
|
+
"Failed to resolve program with address: " + storeOrAddress
|
|
178
|
+
);
|
|
179
|
+
}
|
|
167
180
|
throw new Error(
|
|
168
181
|
`Failed to open program because program is of type ${program?.constructor.name} `
|
|
169
182
|
);
|
|
@@ -193,9 +206,12 @@ export class Handler<T extends Manageable<any>> {
|
|
|
193
206
|
}
|
|
194
207
|
|
|
195
208
|
logger.debug(`Open database '${program.constructor.name}`);
|
|
209
|
+
|
|
210
|
+
// TODO prevent resave if already saved
|
|
196
211
|
const address = await program.save(
|
|
197
212
|
this.properties.client.services.blocks
|
|
198
213
|
);
|
|
214
|
+
|
|
199
215
|
const existing = await this.checkProcessExisting(
|
|
200
216
|
address,
|
|
201
217
|
program,
|
|
@@ -239,6 +255,28 @@ export class Handler<T extends Manageable<any>> {
|
|
|
239
255
|
if (options?.parent) {
|
|
240
256
|
return fn();
|
|
241
257
|
}
|
|
242
|
-
|
|
258
|
+
|
|
259
|
+
let address: string;
|
|
260
|
+
if (typeof storeOrAddress === "string") {
|
|
261
|
+
address = storeOrAddress;
|
|
262
|
+
} else {
|
|
263
|
+
if (storeOrAddress.closed) {
|
|
264
|
+
address = await storeOrAddress.save(
|
|
265
|
+
this.properties.client.services.blocks
|
|
266
|
+
);
|
|
267
|
+
} else {
|
|
268
|
+
address = storeOrAddress.address;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (address) {
|
|
273
|
+
let queue = this._openQueue.get(address);
|
|
274
|
+
if (!queue) {
|
|
275
|
+
queue = new PQueue({ concurrency: 1 });
|
|
276
|
+
this._openQueue.set(address, queue);
|
|
277
|
+
}
|
|
278
|
+
return queue.add(fn) as any as S; // TODO p-queue seem to return void type ;
|
|
279
|
+
}
|
|
280
|
+
return fn(); // No address lookup,
|
|
243
281
|
}
|
|
244
282
|
}
|