@milaboratories/pl-middle-layer 2.9.1 → 2.10.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/dist/index.cjs +5 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/middle_layer/build_stamp.cjs +1 -1
- package/dist/middle_layer/build_stamp.js +1 -1
- package/dist/middle_layer/index.cjs +4 -0
- package/dist/middle_layer/index.d.ts +2 -1
- package/dist/middle_layer/index.js +2 -1
- package/dist/middle_layer/middle_layer.cjs +100 -31
- package/dist/middle_layer/middle_layer.cjs.map +1 -1
- package/dist/middle_layer/middle_layer.d.ts +54 -8
- package/dist/middle_layer/middle_layer.d.ts.map +1 -1
- package/dist/middle_layer/middle_layer.js +99 -30
- package/dist/middle_layer/middle_layer.js.map +1 -1
- package/dist/middle_layer/project.cjs +45 -38
- package/dist/middle_layer/project.cjs.map +1 -1
- package/dist/middle_layer/project.d.ts +18 -3
- package/dist/middle_layer/project.d.ts.map +1 -1
- package/dist/middle_layer/project.js +44 -36
- package/dist/middle_layer/project.js.map +1 -1
- package/dist/middle_layer/project_holds.cjs +266 -0
- package/dist/middle_layer/project_holds.cjs.map +1 -0
- package/dist/middle_layer/project_holds.d.ts +50 -0
- package/dist/middle_layer/project_holds.d.ts.map +1 -0
- package/dist/middle_layer/project_holds.js +262 -0
- package/dist/middle_layer/project_holds.js.map +1 -0
- package/dist/model/block_pack_spec.cjs.map +1 -1
- package/dist/model/block_pack_spec.d.ts +3 -3
- package/dist/model/block_pack_spec.d.ts.map +1 -1
- package/dist/model/block_pack_spec.js.map +1 -1
- package/dist/model/template_spec.d.ts +18 -0
- package/dist/model/template_spec.d.ts.map +1 -1
- package/dist/mutator/block-pack/block_pack.cjs +89 -2
- package/dist/mutator/block-pack/block_pack.cjs.map +1 -1
- package/dist/mutator/block-pack/block_pack.d.ts +16 -0
- package/dist/mutator/block-pack/block_pack.d.ts.map +1 -1
- package/dist/mutator/block-pack/block_pack.js +90 -4
- package/dist/mutator/block-pack/block_pack.js.map +1 -1
- package/dist/mutator/block-pack/required_capabilities.cjs +16 -0
- package/dist/mutator/block-pack/required_capabilities.cjs.map +1 -1
- package/dist/mutator/block-pack/required_capabilities.js +16 -1
- package/dist/mutator/block-pack/required_capabilities.js.map +1 -1
- package/dist/mutator/template/template_cache.cjs +8 -1
- package/dist/mutator/template/template_cache.cjs.map +1 -1
- package/dist/mutator/template/template_cache.d.ts +7 -3
- package/dist/mutator/template/template_cache.d.ts.map +1 -1
- package/dist/mutator/template/template_cache.js +8 -1
- package/dist/mutator/template/template_cache.js.map +1 -1
- package/package.json +6 -6
- package/src/middle_layer/index.ts +2 -0
- package/src/middle_layer/middle_layer.ts +121 -40
- package/src/middle_layer/project.ts +47 -42
- package/src/middle_layer/project_close_during_write.test.ts +142 -0
- package/src/middle_layer/project_handles.test.ts +168 -0
- package/src/middle_layer/project_holds.ts +359 -0
- package/src/middle_layer/project_slots.test.ts +331 -0
- package/src/model/block_pack_spec.ts +3 -2
- package/src/model/template_spec.ts +21 -0
- package/src/mutator/block-pack/block_pack.test.ts +10 -1
- package/src/mutator/block-pack/block_pack.ts +102 -2
- package/src/mutator/block-pack/block_pack_load.test.ts +52 -0
- package/src/mutator/block-pack/required_capabilities.ts +25 -0
- package/src/mutator/project-v3.test.ts +27 -16
- package/src/mutator/template/template_cache.ts +26 -3
- package/src/test/block_packs.ts +12 -0
- package/src/test/write_tx_hooks.ts +75 -0
|
@@ -6,7 +6,7 @@ import { outputRef } from "../model/args";
|
|
|
6
6
|
import { argsFingerprint } from "../model/args_derivation";
|
|
7
7
|
import { ProjectHelper } from "../model/project_helper";
|
|
8
8
|
import { blockArgsAuthorKey, projectFieldName } from "../model/project_model";
|
|
9
|
-
import {
|
|
9
|
+
import { prepareInstalled } from "../test/block_packs";
|
|
10
10
|
import { createProject, ProjectMutator } from "./project";
|
|
11
11
|
import type { AuthorMarker, BlockPackSpec } from "@milaboratories/pl-model-middle-layer";
|
|
12
12
|
import { createBlockStorage } from "@platforma-sdk/model";
|
|
@@ -31,6 +31,9 @@ test("v3 blocks: basic test with unified state", async () => {
|
|
|
31
31
|
const quickJs = await getQuickJS();
|
|
32
32
|
|
|
33
33
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
34
|
+
// Installed before any transaction opens: an open transaction cannot see it.
|
|
35
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
36
|
+
const installedBPSpecSumV3 = await prepareInstalled(pl, BPSpecSumV3);
|
|
34
37
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
35
38
|
const prjRef = await createProject(tx);
|
|
36
39
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -46,7 +49,7 @@ test("v3 blocks: basic test with unified state", async () => {
|
|
|
46
49
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
47
50
|
{
|
|
48
51
|
storageMode: "fromModel",
|
|
49
|
-
blockPack:
|
|
52
|
+
blockPack: installedBPSpecEnterV3,
|
|
50
53
|
},
|
|
51
54
|
);
|
|
52
55
|
// Set initial test data
|
|
@@ -79,7 +82,7 @@ test("v3 blocks: basic test with unified state", async () => {
|
|
|
79
82
|
{ id: "enter2", label: "Enter Numbers V3 #2", renderingMode: "Heavy" },
|
|
80
83
|
{
|
|
81
84
|
storageMode: "fromModel",
|
|
82
|
-
blockPack:
|
|
85
|
+
blockPack: installedBPSpecEnterV3,
|
|
83
86
|
},
|
|
84
87
|
);
|
|
85
88
|
mut.setStates([
|
|
@@ -104,7 +107,7 @@ test("v3 blocks: basic test with unified state", async () => {
|
|
|
104
107
|
{ id: "sum1", label: "Sum Numbers V3", renderingMode: "Heavy" },
|
|
105
108
|
{
|
|
106
109
|
storageMode: "fromModel",
|
|
107
|
-
blockPack:
|
|
110
|
+
blockPack: installedBPSpecSumV3,
|
|
108
111
|
},
|
|
109
112
|
);
|
|
110
113
|
// Set sources to reference upstream blocks
|
|
@@ -164,6 +167,7 @@ test("v3 blocks: prerunArgs skip test", async () => {
|
|
|
164
167
|
const quickJs = await getQuickJS();
|
|
165
168
|
|
|
166
169
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
170
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
167
171
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
168
172
|
const prjRef = await createProject(tx);
|
|
169
173
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -178,7 +182,7 @@ test("v3 blocks: prerunArgs skip test", async () => {
|
|
|
178
182
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
179
183
|
{
|
|
180
184
|
storageMode: "fromModel",
|
|
181
|
-
blockPack:
|
|
185
|
+
blockPack: installedBPSpecEnterV3,
|
|
182
186
|
},
|
|
183
187
|
);
|
|
184
188
|
// Set initial state: [3, 1, 2] - has one even number (2)
|
|
@@ -293,6 +297,7 @@ test("v3 blocks: migrateBlockPack preserves state and re-derives args and prerun
|
|
|
293
297
|
const quickJs = await getQuickJS();
|
|
294
298
|
|
|
295
299
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
300
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
296
301
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
297
302
|
const prjRef = await createProject(tx);
|
|
298
303
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -307,7 +312,7 @@ test("v3 blocks: migrateBlockPack preserves state and re-derives args and prerun
|
|
|
307
312
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
308
313
|
{
|
|
309
314
|
storageMode: "fromModel",
|
|
310
|
-
blockPack:
|
|
315
|
+
blockPack: installedBPSpecEnterV3,
|
|
311
316
|
},
|
|
312
317
|
);
|
|
313
318
|
mut.setStates([
|
|
@@ -337,7 +342,7 @@ test("v3 blocks: migrateBlockPack preserves state and re-derives args and prerun
|
|
|
337
342
|
// Call migrateBlockPack without newClearState (state-preserved path)
|
|
338
343
|
await pl.withWriteTx("MigrateBlockPack", async (tx) => {
|
|
339
344
|
const mut = await ProjectMutator.load(new ProjectHelper(quickJs), tx, prj);
|
|
340
|
-
mut.migrateBlockPack("enter1",
|
|
345
|
+
mut.migrateBlockPack("enter1", installedBPSpecEnterV3);
|
|
341
346
|
mut.save();
|
|
342
347
|
await tx.commit();
|
|
343
348
|
});
|
|
@@ -365,6 +370,7 @@ test("v3 blocks: migrateBlockPack with storage migration re-derives args and pre
|
|
|
365
370
|
const quickJs = await getQuickJS();
|
|
366
371
|
|
|
367
372
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
373
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
368
374
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
369
375
|
const prjRef = await createProject(tx);
|
|
370
376
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -379,7 +385,7 @@ test("v3 blocks: migrateBlockPack with storage migration re-derives args and pre
|
|
|
379
385
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
380
386
|
{
|
|
381
387
|
storageMode: "fromModel",
|
|
382
|
-
blockPack:
|
|
388
|
+
blockPack: installedBPSpecEnterV3,
|
|
383
389
|
},
|
|
384
390
|
);
|
|
385
391
|
mut.setStates([
|
|
@@ -405,7 +411,7 @@ test("v3 blocks: migrateBlockPack with storage migration re-derives args and pre
|
|
|
405
411
|
// Call migrateBlockPack (state-preserved) — triggers v1→v2→v3 migration
|
|
406
412
|
await pl.withWriteTx("MigrateBlockPack", async (tx) => {
|
|
407
413
|
const mut = await ProjectMutator.load(new ProjectHelper(quickJs), tx, prj);
|
|
408
|
-
mut.migrateBlockPack("enter1",
|
|
414
|
+
mut.migrateBlockPack("enter1", installedBPSpecEnterV3);
|
|
409
415
|
mut.save();
|
|
410
416
|
await tx.commit();
|
|
411
417
|
});
|
|
@@ -441,6 +447,7 @@ test("v3 blocks: resetToInitialStorage derives prerunArgs even when args() throw
|
|
|
441
447
|
const quickJs = await getQuickJS();
|
|
442
448
|
|
|
443
449
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
450
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
444
451
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
445
452
|
const prjRef = await createProject(tx);
|
|
446
453
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -455,7 +462,7 @@ test("v3 blocks: resetToInitialStorage derives prerunArgs even when args() throw
|
|
|
455
462
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
456
463
|
{
|
|
457
464
|
storageMode: "fromModel",
|
|
458
|
-
blockPack:
|
|
465
|
+
blockPack: installedBPSpecEnterV3,
|
|
459
466
|
},
|
|
460
467
|
);
|
|
461
468
|
mut.setStates([
|
|
@@ -512,6 +519,7 @@ test("v3 blocks: migrateBlockPack assigns author marker", async () => {
|
|
|
512
519
|
const quickJs = await getQuickJS();
|
|
513
520
|
|
|
514
521
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
522
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
515
523
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
516
524
|
const prjRef = await createProject(tx);
|
|
517
525
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -526,7 +534,7 @@ test("v3 blocks: migrateBlockPack assigns author marker", async () => {
|
|
|
526
534
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
527
535
|
{
|
|
528
536
|
storageMode: "fromModel",
|
|
529
|
-
blockPack:
|
|
537
|
+
blockPack: installedBPSpecEnterV3,
|
|
530
538
|
},
|
|
531
539
|
);
|
|
532
540
|
mut.setStates([
|
|
@@ -544,7 +552,7 @@ test("v3 blocks: migrateBlockPack assigns author marker", async () => {
|
|
|
544
552
|
const testAuthor: AuthorMarker = { authorId: "test-author-123", localVersion: 1 };
|
|
545
553
|
await pl.withWriteTx("MigrateWithAuthor", async (tx) => {
|
|
546
554
|
const mut = await ProjectMutator.load(new ProjectHelper(quickJs), tx, prj, testAuthor);
|
|
547
|
-
mut.migrateBlockPack("enter1",
|
|
555
|
+
mut.migrateBlockPack("enter1", installedBPSpecEnterV3);
|
|
548
556
|
mut.save();
|
|
549
557
|
await tx.commit();
|
|
550
558
|
});
|
|
@@ -568,6 +576,7 @@ test("v3 blocks: migrateBlockPack does not crash when args() returns undefined",
|
|
|
568
576
|
const quickJs = await getQuickJS();
|
|
569
577
|
|
|
570
578
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
579
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
571
580
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
572
581
|
const prjRef = await createProject(tx);
|
|
573
582
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -582,7 +591,7 @@ test("v3 blocks: migrateBlockPack does not crash when args() returns undefined",
|
|
|
582
591
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
583
592
|
{
|
|
584
593
|
storageMode: "fromModel",
|
|
585
|
-
blockPack:
|
|
594
|
+
blockPack: installedBPSpecEnterV3,
|
|
586
595
|
},
|
|
587
596
|
);
|
|
588
597
|
mut.setStates([
|
|
@@ -600,7 +609,7 @@ test("v3 blocks: migrateBlockPack does not crash when args() returns undefined",
|
|
|
600
609
|
// args() returns undefined (previously crashed inside createJsonFieldValue).
|
|
601
610
|
await pl.withWriteTx("MigrateBlockPack", async (tx) => {
|
|
602
611
|
const mut = await ProjectMutator.load(new ProjectHelper(quickJs), tx, prj);
|
|
603
|
-
mut.migrateBlockPack("enter1",
|
|
612
|
+
mut.migrateBlockPack("enter1", installedBPSpecEnterV3);
|
|
604
613
|
mut.save();
|
|
605
614
|
await tx.commit();
|
|
606
615
|
});
|
|
@@ -629,6 +638,7 @@ test("v3 blocks: every args derivation records the errors found in the args, non
|
|
|
629
638
|
const quickJs = await getQuickJS();
|
|
630
639
|
|
|
631
640
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
641
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
632
642
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
633
643
|
const prjRef = await createProject(tx);
|
|
634
644
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -640,7 +650,7 @@ test("v3 blocks: every args derivation records the errors found in the args, non
|
|
|
640
650
|
const mut = await ProjectMutator.load(new ProjectHelper(quickJs), tx, prj);
|
|
641
651
|
mut.addBlock(
|
|
642
652
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
643
|
-
{ storageMode: "fromModel", blockPack:
|
|
653
|
+
{ storageMode: "fromModel", blockPack: installedBPSpecEnterV3 },
|
|
644
654
|
);
|
|
645
655
|
mut.setStates([
|
|
646
656
|
{
|
|
@@ -668,6 +678,7 @@ test("v3 blocks: args without an args errors record of their own are kept and ru
|
|
|
668
678
|
const quickJs = await getQuickJS();
|
|
669
679
|
|
|
670
680
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
681
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
671
682
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
672
683
|
const prjRef = await createProject(tx);
|
|
673
684
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -679,7 +690,7 @@ test("v3 blocks: args without an args errors record of their own are kept and ru
|
|
|
679
690
|
const mut = await ProjectMutator.load(new ProjectHelper(quickJs), tx, prj);
|
|
680
691
|
mut.addBlock(
|
|
681
692
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
682
|
-
{ storageMode: "fromModel", blockPack:
|
|
693
|
+
{ storageMode: "fromModel", blockPack: installedBPSpecEnterV3 },
|
|
683
694
|
);
|
|
684
695
|
mut.setStates([
|
|
685
696
|
{
|
|
@@ -32,7 +32,9 @@ import type {
|
|
|
32
32
|
} from "@milaboratories/pl-model-backend";
|
|
33
33
|
import { notEmpty } from "@milaboratories/ts-helpers";
|
|
34
34
|
import type { BlockPackSpecPrepared } from "../../model";
|
|
35
|
-
import type { TemplateSpecPrepared } from "../../model/template_spec";
|
|
35
|
+
import type { InstallablePackTemplate, TemplateSpecPrepared } from "../../model/template_spec";
|
|
36
|
+
import type { BlockPackPreparer } from "../block-pack/block_pack";
|
|
37
|
+
import { throwIfMissingServerCapabilities } from "../block-pack/required_capabilities";
|
|
36
38
|
import { getDebugFlags } from "../../debug";
|
|
37
39
|
|
|
38
40
|
export const TemplateCacheType = resourceType("TemplateCache", "1");
|
|
@@ -776,6 +778,12 @@ export async function loadTemplateCached(
|
|
|
776
778
|
}
|
|
777
779
|
}
|
|
778
780
|
|
|
781
|
+
type CacheBlockPackOptions = {
|
|
782
|
+
cacheResourceId?: SignedResourceId;
|
|
783
|
+
/** Loads registry and local packs. */
|
|
784
|
+
preparer?: BlockPackPreparer;
|
|
785
|
+
};
|
|
786
|
+
|
|
779
787
|
/**
|
|
780
788
|
* Pre-materialize a block pack's template via cache.
|
|
781
789
|
* Returns a copy of the spec with the template replaced by a cached reference.
|
|
@@ -784,13 +792,28 @@ export async function loadTemplateCached(
|
|
|
784
792
|
export async function cacheBlockPackTemplate(
|
|
785
793
|
pl: PlClient,
|
|
786
794
|
spec: BlockPackSpecPrepared,
|
|
787
|
-
options?:
|
|
795
|
+
options?: CacheBlockPackOptions,
|
|
788
796
|
): Promise<BlockPackSpecPrepared> {
|
|
789
797
|
if (spec.template.type === "cached") return spec;
|
|
790
798
|
|
|
791
|
-
const resourceId =
|
|
799
|
+
const resourceId =
|
|
800
|
+
spec.template.type === "registry-pack" || spec.template.type === "local-pack"
|
|
801
|
+
? await installPackOnClient(pl, spec.template, options)
|
|
802
|
+
: await loadTemplateCached(pl, spec.template, options);
|
|
792
803
|
return {
|
|
793
804
|
...spec,
|
|
794
805
|
template: { type: "cached", resourceId },
|
|
795
806
|
};
|
|
796
807
|
}
|
|
808
|
+
|
|
809
|
+
async function installPackOnClient(
|
|
810
|
+
pl: PlClient,
|
|
811
|
+
template: InstallablePackTemplate,
|
|
812
|
+
options?: CacheBlockPackOptions,
|
|
813
|
+
): Promise<SignedResourceId> {
|
|
814
|
+
if (options?.preparer === undefined) throw new Error("BUG: a pack needs a preparer to load it");
|
|
815
|
+
const loaded = await options.preparer.loadPack(template);
|
|
816
|
+
// The manifest may predate the requiredCapabilities field: check what the pack declares.
|
|
817
|
+
throwIfMissingServerCapabilities(pl, loaded.requiredCapabilities);
|
|
818
|
+
return await loadTemplateCached(pl, loaded.template, options);
|
|
819
|
+
}
|
package/src/test/block_packs.ts
CHANGED
|
@@ -3,12 +3,24 @@ import { HmacSha256Signer } from "@milaboratories/ts-helpers";
|
|
|
3
3
|
import type { BlockPackSpec } from "@milaboratories/pl-model-middle-layer";
|
|
4
4
|
import { V2RegistryProvider } from "../block_registry/registry-v2-provider";
|
|
5
5
|
import { defaultHttpDispatcher } from "@milaboratories/pl-http";
|
|
6
|
+
import type { PlClient } from "@milaboratories/pl-client";
|
|
7
|
+
import type { BlockPackSpecPrepared } from "../model";
|
|
8
|
+
import { cacheBlockPackTemplate } from "../mutator/template/template_cache";
|
|
6
9
|
|
|
7
10
|
export const TestBPPreparer = new BlockPackPreparer(
|
|
8
11
|
new V2RegistryProvider(defaultHttpDispatcher()),
|
|
9
12
|
new HmacSha256Signer(HmacSha256Signer.generateSecret()),
|
|
10
13
|
);
|
|
11
14
|
|
|
15
|
+
/** Prepares a block pack and installs its template, as `Project.addBlock` does. */
|
|
16
|
+
export async function prepareInstalled(
|
|
17
|
+
pl: PlClient,
|
|
18
|
+
spec: BlockPackSpec,
|
|
19
|
+
): Promise<BlockPackSpecPrepared> {
|
|
20
|
+
const prepared = await TestBPPreparer.prepare(spec);
|
|
21
|
+
return await cacheBlockPackTemplate(pl, prepared, { preparer: TestBPPreparer });
|
|
22
|
+
}
|
|
23
|
+
|
|
12
24
|
export const BPSpecEnterV041NotPrepared: BlockPackSpec = {
|
|
13
25
|
type: "from-registry-v1",
|
|
14
26
|
registryUrl: "https://block.registry.platforma.bio/releases",
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { PlClient, PlTransaction } from "@milaboratories/pl-client";
|
|
2
|
+
|
|
3
|
+
type TxOptions = Parameters<PlClient["withWriteTx"]>[2];
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Wraps `pl.withWriteTx` so that `after` runs once a write transaction has returned, before its
|
|
7
|
+
* caller sees the result: for a project action, after the commit and before the action's own
|
|
8
|
+
* post-commit refresh. Returns the function that puts the original back.
|
|
9
|
+
*
|
|
10
|
+
* `after` gets the transaction name, e.g. `ProjectAction: addBlock`, and runs for every write
|
|
11
|
+
* transaction until restored; it is not run for the transactions it makes itself.
|
|
12
|
+
*/
|
|
13
|
+
export function afterWriteTx(pl: PlClient, after: (name: string) => Promise<void>): () => void {
|
|
14
|
+
const unwrapped = pl.withWriteTx;
|
|
15
|
+
const original = unwrapped.bind(pl);
|
|
16
|
+
let inHook = false;
|
|
17
|
+
pl.withWriteTx = async <T>(
|
|
18
|
+
name: string,
|
|
19
|
+
body: (tx: PlTransaction) => Promise<T>,
|
|
20
|
+
ops?: TxOptions,
|
|
21
|
+
): Promise<T> => {
|
|
22
|
+
const result = await original(name, body, ops);
|
|
23
|
+
if (!inHook) {
|
|
24
|
+
inHook = true;
|
|
25
|
+
try {
|
|
26
|
+
await after(name);
|
|
27
|
+
} finally {
|
|
28
|
+
inHook = false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
};
|
|
33
|
+
return () => {
|
|
34
|
+
pl.withWriteTx = unwrapped;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Runs `action` once, after the first write transaction named `name` returns. */
|
|
39
|
+
export function onceAfterWriteTx(
|
|
40
|
+
pl: PlClient,
|
|
41
|
+
name: string,
|
|
42
|
+
action: () => Promise<void>,
|
|
43
|
+
): { restore: () => void; fired: () => boolean } {
|
|
44
|
+
let fired = false;
|
|
45
|
+
const restore = afterWriteTx(pl, async (txName) => {
|
|
46
|
+
if (fired || txName !== name) return;
|
|
47
|
+
fired = true;
|
|
48
|
+
await action();
|
|
49
|
+
});
|
|
50
|
+
return { restore, fired: () => fired };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Counts the write transactions by name as they start, from now until restored. */
|
|
54
|
+
export function countWriteTx(pl: PlClient): {
|
|
55
|
+
count: (name: string) => number;
|
|
56
|
+
restore: () => void;
|
|
57
|
+
} {
|
|
58
|
+
const counts = new Map<string, number>();
|
|
59
|
+
const unwrapped = pl.withWriteTx;
|
|
60
|
+
const original = unwrapped.bind(pl);
|
|
61
|
+
pl.withWriteTx = async <T>(
|
|
62
|
+
name: string,
|
|
63
|
+
body: (tx: PlTransaction) => Promise<T>,
|
|
64
|
+
ops?: TxOptions,
|
|
65
|
+
): Promise<T> => {
|
|
66
|
+
counts.set(name, (counts.get(name) ?? 0) + 1);
|
|
67
|
+
return await original(name, body, ops);
|
|
68
|
+
};
|
|
69
|
+
return {
|
|
70
|
+
count: (name) => counts.get(name) ?? 0,
|
|
71
|
+
restore: () => {
|
|
72
|
+
pl.withWriteTx = unwrapped;
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|