@peerbit/program 2.3.2 → 2.4.1
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 +51 -11
- package/lib/esm/handler.js.map +1 -1
- package/package.json +6 -6
- package/src/handler.ts +67 -16
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
|
}
|
|
@@ -78,19 +85,32 @@ export class Handler {
|
|
|
78
85
|
throw error;
|
|
79
86
|
}
|
|
80
87
|
}
|
|
81
|
-
else
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return program;
|
|
88
|
+
else {
|
|
89
|
+
if (options.parent == program) {
|
|
90
|
+
throw new Error("Parent program can not be equal to the program");
|
|
85
91
|
}
|
|
86
|
-
|
|
87
|
-
const existing =
|
|
88
|
-
if (existing) {
|
|
89
|
-
return
|
|
92
|
+
if (!program.closed) {
|
|
93
|
+
const existing = this.items.get(program.address);
|
|
94
|
+
if (existing === program) {
|
|
95
|
+
return program;
|
|
96
|
+
}
|
|
97
|
+
else if (existing) {
|
|
98
|
+
// we got existing, but it is not the same instance
|
|
99
|
+
const existing = await this.checkProcessExisting(program.address, program, options?.existing);
|
|
100
|
+
if (existing) {
|
|
101
|
+
return existing;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
// assume new instance was not added to monitored items, just add it
|
|
106
|
+
// and return it as we would opened it normally
|
|
107
|
+
this.items.set(program.address, program);
|
|
108
|
+
return program;
|
|
90
109
|
}
|
|
91
110
|
}
|
|
92
111
|
}
|
|
93
112
|
logger.debug(`Open database '${program.constructor.name}`);
|
|
113
|
+
// TODO prevent resave if already saved
|
|
94
114
|
const address = await program.save(this.properties.client.services.blocks);
|
|
95
115
|
const existing = await this.checkProcessExisting(address, program, options?.existing);
|
|
96
116
|
if (existing) {
|
|
@@ -127,7 +147,27 @@ export class Handler {
|
|
|
127
147
|
if (options?.parent) {
|
|
128
148
|
return fn();
|
|
129
149
|
}
|
|
130
|
-
|
|
150
|
+
let address;
|
|
151
|
+
if (typeof storeOrAddress === "string") {
|
|
152
|
+
address = storeOrAddress;
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
if (storeOrAddress.closed) {
|
|
156
|
+
address = await storeOrAddress.save(this.properties.client.services.blocks);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
address = storeOrAddress.address;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (address) {
|
|
163
|
+
let queue = this._openQueue.get(address);
|
|
164
|
+
if (!queue) {
|
|
165
|
+
queue = new PQueue({ concurrency: 1 });
|
|
166
|
+
this._openQueue.set(address, queue);
|
|
167
|
+
}
|
|
168
|
+
return queue.add(fn); // TODO p-queue seem to return void type ;
|
|
169
|
+
}
|
|
170
|
+
return fn(); // No address lookup,
|
|
131
171
|
}
|
|
132
172
|
}
|
|
133
173
|
//# 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,CAAiB;IACd,UAAU,
|
|
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,CAAiB;IACd,UAAU,CAAsB;IAExC,YACU,UAQR;QARQ,eAAU,GAAV,UAAU,CAQlB;QAED,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,EAAE,CAAC;QAC5B,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,EAAE,CAAC;IACxB,CAAC;IAED,KAAK,CAAC,IAAI;QACT,MAAM,OAAO,CA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AAO,CAAC,IAAI,CAAC,CAAC;YACjC,MAAM,OAAO,CAAC,SAAS,EAAE,CAAC;YAC1B,OAAO,OAAY,CAAC;QACrB,CAAC,CAAC;QAEF,gEAAgE;QAChE,2CAA2C;QAC3C,IAAI,OAAO,EAAE,MAAM,EAAE;YACpB,OAAO,EAAE,EAAE,CAAC;SACZ;QAED,IAAI,OAAe,CAAC;QACpB,IAAI,OAAO,cAAc,KAAK,QAAQ,EAAE;YACvC,OAAO,GAAG,cAAc,CAAC;SACzB;aAAM;YACN,IAAI,cAAc,CAAC,MAAM,EAAE;gBAC1B,OAAO,GAAG,MAAM,cAAc,CAAC,IAAI,CAClC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CACtC,CAAC;aACF;iBAAM;gBACN,OAAO,GAAG,cAAc,CAAC,OAAO,CAAC;aACjC;SACD;QAED,IAAI,OAAO,EAAE;YACZ,IAAI,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YACzC,IAAI,CAAC,KAAK,EAAE;gBACX,KAAK,GAAG,IAAI,MAAM,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,CAAC,CAAC;gBACvC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;aACpC;YACD,OAAO,KAAK,CAAC,GAAG,CAAC,EAAE,CAAa,CAAC,CAAC,0CAA0C;SAC5E;QACD,OAAO,EAAE,EAAE,CAAC,CAAC,qBAAqB;IACnC,CAAC;CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/program",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
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.
|
|
32
|
+
"@dao-xyz/borsh": "^5.1.8",
|
|
33
|
+
"@peerbit/blocks-interface": "^1.1.3",
|
|
34
|
+
"@peerbit/crypto": "1.0.10",
|
|
35
35
|
"@peerbit/lazy-level": "^1.2.1",
|
|
36
|
-
"@peerbit/pubsub-interface": "^1.1.
|
|
36
|
+
"@peerbit/pubsub-interface": "^1.1.5"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "1a57e60b02b5ea1c0c743871a92404a0115ed683"
|
|
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
|
);
|
|
@@ -175,27 +188,42 @@ export class Handler<T extends Manageable<any>> {
|
|
|
175
188
|
);
|
|
176
189
|
throw error;
|
|
177
190
|
}
|
|
178
|
-
} else
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
} else if (existing) {
|
|
183
|
-
const existing = await this.checkProcessExisting(
|
|
184
|
-
program.address,
|
|
185
|
-
program,
|
|
186
|
-
options?.existing
|
|
187
|
-
);
|
|
191
|
+
} else {
|
|
192
|
+
if (options.parent == program) {
|
|
193
|
+
throw new Error("Parent program can not be equal to the program");
|
|
194
|
+
}
|
|
188
195
|
|
|
189
|
-
|
|
190
|
-
|
|
196
|
+
if (!program.closed) {
|
|
197
|
+
const existing = this.items.get(program.address);
|
|
198
|
+
if (existing === program) {
|
|
199
|
+
return program;
|
|
200
|
+
} else if (existing) {
|
|
201
|
+
// we got existing, but it is not the same instance
|
|
202
|
+
const existing = await this.checkProcessExisting(
|
|
203
|
+
program.address,
|
|
204
|
+
program,
|
|
205
|
+
options?.existing
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
if (existing) {
|
|
209
|
+
return existing as S;
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
// assume new instance was not added to monitored items, just add it
|
|
213
|
+
// and return it as we would opened it normally
|
|
214
|
+
this.items.set(program.address, program);
|
|
215
|
+
return program;
|
|
191
216
|
}
|
|
192
217
|
}
|
|
193
218
|
}
|
|
194
219
|
|
|
195
220
|
logger.debug(`Open database '${program.constructor.name}`);
|
|
221
|
+
|
|
222
|
+
// TODO prevent resave if already saved
|
|
196
223
|
const address = await program.save(
|
|
197
224
|
this.properties.client.services.blocks
|
|
198
225
|
);
|
|
226
|
+
|
|
199
227
|
const existing = await this.checkProcessExisting(
|
|
200
228
|
address,
|
|
201
229
|
program,
|
|
@@ -204,6 +232,7 @@ export class Handler<T extends Manageable<any>> {
|
|
|
204
232
|
if (existing) {
|
|
205
233
|
return existing as S;
|
|
206
234
|
}
|
|
235
|
+
|
|
207
236
|
await program.beforeOpen(this.properties.client, {
|
|
208
237
|
onBeforeOpen: async (p) => {
|
|
209
238
|
if (
|
|
@@ -239,6 +268,28 @@ export class Handler<T extends Manageable<any>> {
|
|
|
239
268
|
if (options?.parent) {
|
|
240
269
|
return fn();
|
|
241
270
|
}
|
|
242
|
-
|
|
271
|
+
|
|
272
|
+
let address: string;
|
|
273
|
+
if (typeof storeOrAddress === "string") {
|
|
274
|
+
address = storeOrAddress;
|
|
275
|
+
} else {
|
|
276
|
+
if (storeOrAddress.closed) {
|
|
277
|
+
address = await storeOrAddress.save(
|
|
278
|
+
this.properties.client.services.blocks
|
|
279
|
+
);
|
|
280
|
+
} else {
|
|
281
|
+
address = storeOrAddress.address;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (address) {
|
|
286
|
+
let queue = this._openQueue.get(address);
|
|
287
|
+
if (!queue) {
|
|
288
|
+
queue = new PQueue({ concurrency: 1 });
|
|
289
|
+
this._openQueue.set(address, queue);
|
|
290
|
+
}
|
|
291
|
+
return queue.add(fn) as any as S; // TODO p-queue seem to return void type ;
|
|
292
|
+
}
|
|
293
|
+
return fn(); // No address lookup,
|
|
243
294
|
}
|
|
244
295
|
}
|