@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
|
@@ -11,6 +11,7 @@ import { AvailableShare, OutgoingShare } from "./sharing_list.js";
|
|
|
11
11
|
import "../model/index.js";
|
|
12
12
|
import { BlockPackPreparer } from "../mutator/block-pack/block_pack.js";
|
|
13
13
|
import { Project } from "./project.js";
|
|
14
|
+
import { ProjectHandle, ProjectHolder } from "./project_holds.js";
|
|
14
15
|
import { MiddleLayerOps, MiddleLayerOpsConstructor } from "./ops.js";
|
|
15
16
|
import { BlockUpdateWatcher } from "../block_registry/watcher.js";
|
|
16
17
|
import { MiddleLayerDriverKit } from "./driver_kit.js";
|
|
@@ -407,8 +408,19 @@ export declare class MiddleLayer {
|
|
|
407
408
|
* Uses LRU cache with TX-scan fallback. */
|
|
408
409
|
private resolveTemplateId;
|
|
409
410
|
/** Permanently deletes project from the project list, this will result in
|
|
410
|
-
* destruction of all attached objects, like files, analysis results etc.
|
|
411
|
-
|
|
411
|
+
* destruction of all attached objects, like files, analysis results etc.
|
|
412
|
+
*
|
|
413
|
+
* A loaded project is taken from every holder: each {@link ProjectHandle} ends with `deleted`
|
|
414
|
+
* and the project is destroyed, with no snapshot. Holds do not prevent the delete unless
|
|
415
|
+
* `refuseIfHeldBy` is given. Holds taken during the delete end with it too.
|
|
416
|
+
*
|
|
417
|
+
* @param options.refuseIfHeldBy refuse the delete when it returns `true` for the label of any
|
|
418
|
+
* holder of the project.
|
|
419
|
+
* @throws {@link ProjectInUseError} when refused; the project is left as it was
|
|
420
|
+
*/
|
|
421
|
+
deleteProject(id: ProjectId$1, options?: {
|
|
422
|
+
refuseIfHeldBy?: (holder: ProjectHolder) => boolean;
|
|
423
|
+
}): Promise<void>;
|
|
412
424
|
/**
|
|
413
425
|
* Duplicates an existing project and adds the copy to this user's project list, beside the
|
|
414
426
|
* project it was copied from.
|
|
@@ -562,8 +574,11 @@ export declare class MiddleLayer {
|
|
|
562
574
|
/** Scans the donor's outbox and deletes expired envelopes (backend auto-revokes their grants).
|
|
563
575
|
* Envelopes with `expiresAt: null` (share-with-everybody) are skipped. */
|
|
564
576
|
private runEnvelopeCleanup;
|
|
565
|
-
|
|
566
|
-
|
|
577
|
+
/** The loaded projects and who holds them. */
|
|
578
|
+
private readonly projectSlots;
|
|
579
|
+
/** The holds taken by {@link openProject}, one per project, released by {@link closeProject}. */
|
|
580
|
+
private readonly legacyHolds;
|
|
581
|
+
/** Snapshot writes started by a project close and not yet finished. Held only so
|
|
567
582
|
* {@link close} can give them a bounded chance to land. */
|
|
568
583
|
private readonly pendingSnapshotWrites;
|
|
569
584
|
private trackSnapshotWrite;
|
|
@@ -574,13 +589,44 @@ export declare class MiddleLayer {
|
|
|
574
589
|
* does not routinely lose it. Bounded, because a wedged filesystem must not hang the quit,
|
|
575
590
|
* and losing the write costs one cold open rather than any correctness. */
|
|
576
591
|
private drainSnapshotWrites;
|
|
577
|
-
/**
|
|
592
|
+
/**
|
|
593
|
+
* Takes a hold on a project, loading it when nobody holds it yet. The project stays loaded until
|
|
594
|
+
* every hold is released, and is taken from all holders when it is deleted or the middle layer
|
|
595
|
+
* closes; the handle's `signal` says when and why.
|
|
596
|
+
*
|
|
597
|
+
* Concurrent acquires share one load. An acquire while the project is being destroyed waits for
|
|
598
|
+
* the destroy and loads the project anew, so there is never more than one {@link Project} per id.
|
|
599
|
+
*
|
|
600
|
+
* @param holder label for logs and {@link projectHolders}, e.g. `desktop` or `mcp:<instance>`
|
|
601
|
+
* @throws when the project is not in the project list, its load fails, or the middle layer is
|
|
602
|
+
* closed
|
|
603
|
+
*/
|
|
604
|
+
acquireProject(id: ProjectId$1, holder: ProjectHolder): Promise<ProjectHandle>;
|
|
605
|
+
/** Labels of the live holds on a project, one per hold; empty when nobody holds it. */
|
|
606
|
+
projectHolders(id: ProjectId$1): ProjectHolder[];
|
|
607
|
+
/**
|
|
608
|
+
* Opens a project, and starts corresponding project maintenance loop: takes a hold labelled
|
|
609
|
+
* `legacy`, which {@link closeProject} releases. Opening a project this API already holds does
|
|
610
|
+
* nothing.
|
|
611
|
+
*
|
|
612
|
+
* @deprecated use {@link acquireProject}, and release the handle
|
|
613
|
+
*/
|
|
578
614
|
openProject(id: ProjectId$1): Promise<void>;
|
|
579
|
-
/**
|
|
615
|
+
/**
|
|
616
|
+
* Releases the hold {@link openProject} took. The project is destroyed only when nobody else
|
|
617
|
+
* holds it. Closing a project this API does not hold does nothing.
|
|
618
|
+
*
|
|
619
|
+
* @deprecated use {@link ProjectHandle.release}
|
|
620
|
+
*/
|
|
580
621
|
closeProject(id: ProjectId$1): Promise<void>;
|
|
581
|
-
/**
|
|
622
|
+
/**
|
|
623
|
+
* Returns a project access object for a loaded project, whoever holds it.
|
|
624
|
+
*
|
|
625
|
+
* A caller holding no {@link ProjectHandle} must not keep the result across awaits: the project
|
|
626
|
+
* can be destroyed meanwhile.
|
|
627
|
+
*/
|
|
582
628
|
getOpenedProject(id: ProjectId$1): Project;
|
|
583
|
-
/** Returns true if project
|
|
629
|
+
/** Returns true if the project is currently loaded, whoever holds it. */
|
|
584
630
|
isProjectOpened(id: ProjectId$1): boolean;
|
|
585
631
|
/** Counters for the persisted project tree mirrors, or undefined when they are switched off.
|
|
586
632
|
* Reads and hits are what show whether a reopen was actually warm, and the miss breakdown
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middle_layer.d.ts","names":["ProjectListEntry","ProjectId","TemplateId"],"sources":["../../src/middle_layer/middle_layer.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"middle_layer.d.ts","names":["ProjectListEntry","ProjectId","TemplateId"],"sources":["../../src/middle_layer/middle_layer.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAwLiB;EACf,WAAW;WACF,IAAI;WACJ,qBAAqB;WACrB,QAAQ;WACR,sBAAsB;WACtB,gBAAgB;WAChB,qBAAqB;WACrB,QAAQ;WACR,KAAK;WACL,YAAY;WACZ,wBAAwB;WACxB,oBAAoB;WACpB,SAAS;WACT,WAAW;WACX,iBAAiB;WACjB,eAAe;;;WAGf,gBAAgB;;;;;;;;;;;;;;qBAed;mBAIQ;WACD,WAAW;WACX,QAAQ;mBACP;mBACA;mBACA;mBACA;mBACA;mBACA;mBACA;mBACA;mBACA;mBACA;mBACA;mBACA;WACD,uBAAuB;;WAEvB,aAAa,wBAAwBA;;WAErC,cAAc,wBAAwB;;;WAGtC,SAAS,wBAAwB;;EAE1C,gBAAgB,WAAW;;;EAG3B,iBAAiB,WAAW;;;;mBAIlB;WAlCH,IAAI;UAEb;;;;;MA0CI,kBAAkB;;;;;;;MA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EAmBL,4BAAwB;;;SAMvB;;MAKH,wBAAwB;;SAKf,KAClB,IAAI,UACJ,iBACA,MAAM,4BACL,QAAQ"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { throwIfMissingServerCapabilities } from "../mutator/block-pack/required_capabilities.js";
|
|
1
2
|
import { V2RegistryProvider } from "../block_registry/registry-v2-provider.js";
|
|
2
3
|
import "../block_registry/index.js";
|
|
3
4
|
import { InstanceStatsWatch } from "./instance_stats.js";
|
|
@@ -15,9 +16,10 @@ import { getDebugFlags } from "../debug/index.js";
|
|
|
15
16
|
import { createProject, duplicateProject, withProject, withProjectAuthored } from "../mutator/project.js";
|
|
16
17
|
import { applyTemplateEntries } from "../mutator/template_construct.js";
|
|
17
18
|
import { cacheBlockPackTemplate } from "../mutator/template/template_cache.js";
|
|
18
|
-
import { Project, throwIfMissingServerCapabilities } from "./project.js";
|
|
19
19
|
import { buildFolderShareEnvelope, buildShareEnvelope, buildTemplateShareEnvelope, clearShareHidden, copyEnvelopeProjectsIntoList, writeShareHidden } from "../mutator/sharing.js";
|
|
20
20
|
import { createAvailableSharesComputable, createAvailableSharesTree, createLiveEnvelopesComputable, createOutgoingShares, createSharingStateTree } from "./sharing_list.js";
|
|
21
|
+
import { Project } from "./project.js";
|
|
22
|
+
import { ProjectSlots, asProjectHolder } from "./project_holds.js";
|
|
21
23
|
import { DefaultMiddleLayerOpsPaths, DefaultMiddleLayerOpsSettings } from "./ops.js";
|
|
22
24
|
import { BlockUpdateWatcher } from "../block_registry/watcher.js";
|
|
23
25
|
import { initDriverKit } from "./driver_kit.js";
|
|
@@ -39,6 +41,8 @@ import { getQuickJS } from "quickjs-emscripten";
|
|
|
39
41
|
* enough for a ten-megabyte encode and write on ordinary storage, short enough that a wedged
|
|
40
42
|
* filesystem does not hold the quit open. */
|
|
41
43
|
const SNAPSHOT_DRAIN_TIMEOUT_MS = 5e3;
|
|
44
|
+
/** The holder of the holds {@link MiddleLayer.openProject} takes. */
|
|
45
|
+
const LEGACY_HOLDER = asProjectHolder("legacy");
|
|
42
46
|
/**
|
|
43
47
|
* Main access object to work with pl from UI.
|
|
44
48
|
*
|
|
@@ -321,6 +325,7 @@ var MiddleLayer = class MiddleLayer {
|
|
|
321
325
|
for (const id of outcome.removal.projects) this.projectIdCache.delete(id);
|
|
322
326
|
for (const id of outcome.removal.templates) this.templateIdCache.delete(id);
|
|
323
327
|
await Promise.all([
|
|
328
|
+
...outcome.removal.projects.map((id) => this.projectSlots.evict(id, "deleted")),
|
|
324
329
|
this.foldersTree.refreshState(),
|
|
325
330
|
this.projectListTree.refreshState(),
|
|
326
331
|
this.templateListTree.refreshState()
|
|
@@ -550,7 +555,7 @@ var MiddleLayer = class MiddleLayer {
|
|
|
550
555
|
}
|
|
551
556
|
this.env.runtimeCapabilities.throwIfIncompatible(blockCfg.featureFlags);
|
|
552
557
|
throwIfMissingServerCapabilities(this.pl, preparedBp.requiredCapabilities);
|
|
553
|
-
const cachedBp = await cacheBlockPackTemplate(this.pl, preparedBp);
|
|
558
|
+
const cachedBp = await cacheBlockPackTemplate(this.pl, preparedBp, { preparer: this.env.bpPreparer });
|
|
554
559
|
const checked = this.env.projectHelper.validateTemplateParamsInVM(blockCfg, documentEntry.params);
|
|
555
560
|
if (checked.error !== void 0) {
|
|
556
561
|
problems.push({
|
|
@@ -818,15 +823,26 @@ var MiddleLayer = class MiddleLayer {
|
|
|
818
823
|
return rid;
|
|
819
824
|
}
|
|
820
825
|
/** Permanently deletes project from the project list, this will result in
|
|
821
|
-
* destruction of all attached objects, like files, analysis results etc.
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
826
|
+
* destruction of all attached objects, like files, analysis results etc.
|
|
827
|
+
*
|
|
828
|
+
* A loaded project is taken from every holder: each {@link ProjectHandle} ends with `deleted`
|
|
829
|
+
* and the project is destroyed, with no snapshot. Holds do not prevent the delete unless
|
|
830
|
+
* `refuseIfHeldBy` is given. Holds taken during the delete end with it too.
|
|
831
|
+
*
|
|
832
|
+
* @param options.refuseIfHeldBy refuse the delete when it returns `true` for the label of any
|
|
833
|
+
* holder of the project.
|
|
834
|
+
* @throws {@link ProjectInUseError} when refused; the project is left as it was
|
|
835
|
+
*/
|
|
836
|
+
async deleteProject(id, options = {}) {
|
|
837
|
+
await this.projectSlots.delete(id, async () => {
|
|
838
|
+
await this.pl.withWriteTx("MLRemoveProject", async (tx) => {
|
|
839
|
+
const entry = (await listedById(tx, this.projectListResourceId)).get(id);
|
|
840
|
+
if (entry === void 0) throw notListedError("Project", id);
|
|
841
|
+
tx.removeField(field(this.projectListResourceId, entry.fieldName));
|
|
842
|
+
await tx.commit();
|
|
843
|
+
});
|
|
844
|
+
this.projectIdCache.delete(id);
|
|
845
|
+
}, options.refuseIfHeldBy);
|
|
830
846
|
await this.projectListTree.refreshState();
|
|
831
847
|
}
|
|
832
848
|
/**
|
|
@@ -1282,8 +1298,19 @@ var MiddleLayer = class MiddleLayer {
|
|
|
1282
1298
|
this.env.logger.warn(`envelope cleanup failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
1283
1299
|
}
|
|
1284
1300
|
}
|
|
1285
|
-
|
|
1286
|
-
|
|
1301
|
+
/** The loaded projects and who holds them. */
|
|
1302
|
+
projectSlots = new ProjectSlots({
|
|
1303
|
+
load: async (id, onGone) => await Project.init(this.env, id, await this.resolveProjectId(id), onGone),
|
|
1304
|
+
trackSnapshotWrite: (write) => this.trackSnapshotWrite(write),
|
|
1305
|
+
publish: (open) => this.openedProjectsList.setValue(open),
|
|
1306
|
+
logger: {
|
|
1307
|
+
info: (msg) => this.env.logger.info(msg),
|
|
1308
|
+
warn: (msg) => this.env.logger.warn(msg)
|
|
1309
|
+
}
|
|
1310
|
+
});
|
|
1311
|
+
/** The holds taken by {@link openProject}, one per project, released by {@link closeProject}. */
|
|
1312
|
+
legacyHolds = /* @__PURE__ */ new Map();
|
|
1313
|
+
/** Snapshot writes started by a project close and not yet finished. Held only so
|
|
1287
1314
|
* {@link close} can give them a bounded chance to land. */
|
|
1288
1315
|
pendingSnapshotWrites = /* @__PURE__ */ new Set();
|
|
1289
1316
|
trackSnapshotWrite(write) {
|
|
@@ -1309,31 +1336,73 @@ var MiddleLayer = class MiddleLayer {
|
|
|
1309
1336
|
if (timer !== void 0) clearTimeout(timer);
|
|
1310
1337
|
}
|
|
1311
1338
|
}
|
|
1312
|
-
/**
|
|
1339
|
+
/**
|
|
1340
|
+
* Takes a hold on a project, loading it when nobody holds it yet. The project stays loaded until
|
|
1341
|
+
* every hold is released, and is taken from all holders when it is deleted or the middle layer
|
|
1342
|
+
* closes; the handle's `signal` says when and why.
|
|
1343
|
+
*
|
|
1344
|
+
* Concurrent acquires share one load. An acquire while the project is being destroyed waits for
|
|
1345
|
+
* the destroy and loads the project anew, so there is never more than one {@link Project} per id.
|
|
1346
|
+
*
|
|
1347
|
+
* @param holder label for logs and {@link projectHolders}, e.g. `desktop` or `mcp:<instance>`
|
|
1348
|
+
* @throws when the project is not in the project list, its load fails, or the middle layer is
|
|
1349
|
+
* closed
|
|
1350
|
+
*/
|
|
1351
|
+
async acquireProject(id, holder) {
|
|
1352
|
+
return await this.projectSlots.acquire(id, holder);
|
|
1353
|
+
}
|
|
1354
|
+
/** Labels of the live holds on a project, one per hold; empty when nobody holds it. */
|
|
1355
|
+
projectHolders(id) {
|
|
1356
|
+
return this.projectSlots.holders(id);
|
|
1357
|
+
}
|
|
1358
|
+
/**
|
|
1359
|
+
* Opens a project, and starts corresponding project maintenance loop: takes a hold labelled
|
|
1360
|
+
* `legacy`, which {@link closeProject} releases. Opening a project this API already holds does
|
|
1361
|
+
* nothing.
|
|
1362
|
+
*
|
|
1363
|
+
* @deprecated use {@link acquireProject}, and release the handle
|
|
1364
|
+
*/
|
|
1313
1365
|
async openProject(id) {
|
|
1314
|
-
if (this.
|
|
1315
|
-
const
|
|
1316
|
-
this.
|
|
1317
|
-
|
|
1366
|
+
if (this.legacyHolds.get(id)?.ended === false) return;
|
|
1367
|
+
const handle = await this.projectSlots.acquire(id, LEGACY_HOLDER);
|
|
1368
|
+
if (this.legacyHolds.get(id)?.ended === false) {
|
|
1369
|
+
await handle.release();
|
|
1370
|
+
return;
|
|
1371
|
+
}
|
|
1372
|
+
this.legacyHolds.set(id, handle);
|
|
1373
|
+
handle.signal.addEventListener("abort", () => {
|
|
1374
|
+
if (this.legacyHolds.get(id) === handle) this.legacyHolds.delete(id);
|
|
1375
|
+
}, { once: true });
|
|
1318
1376
|
}
|
|
1319
|
-
/**
|
|
1377
|
+
/**
|
|
1378
|
+
* Releases the hold {@link openProject} took. The project is destroyed only when nobody else
|
|
1379
|
+
* holds it. Closing a project this API does not hold does nothing.
|
|
1380
|
+
*
|
|
1381
|
+
* @deprecated use {@link ProjectHandle.release}
|
|
1382
|
+
*/
|
|
1320
1383
|
async closeProject(id) {
|
|
1321
|
-
const
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1384
|
+
const handle = this.legacyHolds.get(id);
|
|
1385
|
+
this.legacyHolds.delete(id);
|
|
1386
|
+
if (handle === void 0 || handle.ended) {
|
|
1387
|
+
this.env.logger.info(`closeProject: project ${id} is not held by openProject`);
|
|
1388
|
+
return;
|
|
1389
|
+
}
|
|
1390
|
+
await handle.release();
|
|
1327
1391
|
}
|
|
1328
|
-
/**
|
|
1392
|
+
/**
|
|
1393
|
+
* Returns a project access object for a loaded project, whoever holds it.
|
|
1394
|
+
*
|
|
1395
|
+
* A caller holding no {@link ProjectHandle} must not keep the result across awaits: the project
|
|
1396
|
+
* can be destroyed meanwhile.
|
|
1397
|
+
*/
|
|
1329
1398
|
getOpenedProject(id) {
|
|
1330
|
-
const prj = this.
|
|
1399
|
+
const prj = this.projectSlots.get(id);
|
|
1331
1400
|
if (prj === void 0) throw new Error(`Project ${id} not found among opened projects`);
|
|
1332
1401
|
return prj;
|
|
1333
1402
|
}
|
|
1334
|
-
/** Returns true if project
|
|
1403
|
+
/** Returns true if the project is currently loaded, whoever holds it. */
|
|
1335
1404
|
isProjectOpened(id) {
|
|
1336
|
-
return this.
|
|
1405
|
+
return this.projectSlots.get(id) !== void 0;
|
|
1337
1406
|
}
|
|
1338
1407
|
/** Counters for the persisted project tree mirrors, or undefined when they are switched off.
|
|
1339
1408
|
* Reads and hits are what show whether a reopen was actually warm, and the miss breakdown
|
|
@@ -1360,7 +1429,7 @@ var MiddleLayer = class MiddleLayer {
|
|
|
1360
1429
|
async close() {
|
|
1361
1430
|
if (this.envelopeCleanupTimer !== void 0) clearInterval(this.envelopeCleanupTimer);
|
|
1362
1431
|
await this.instanceStatsWatch?.terminate();
|
|
1363
|
-
await
|
|
1432
|
+
await this.projectSlots.closeAll();
|
|
1364
1433
|
await Promise.all([
|
|
1365
1434
|
this.projectListTree.terminate(),
|
|
1366
1435
|
this.templateListTree.terminate(),
|