Skip to content

Commit af1dd59

Browse files
committed
Store checkout per shared branch
1 parent dce6c6d commit af1dd59

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

packages/dds/tree/src/shared-tree/sharedTree.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ export class SharedTreeKernel
222222
return this.checkout.storedSchema;
223223
}
224224

225+
private readonly checkouts: Map<BranchId, TreeCheckout> = new Map();
226+
225227
/**
226228
* The app-facing API for SharedTree implemented by this Kernel.
227229
* @remarks
@@ -360,6 +362,7 @@ export class SharedTreeKernel
360362
}
361363

362364
private registerCheckout(branchId: BranchId, checkout: TreeCheckout): void {
365+
this.checkouts.set(branchId, checkout);
363366
const enricher = this.getCommitEnricher(branchId);
364367
checkout.transaction.events.on("started", () => {
365368
if (this.sharedObject.isAttached()) {
@@ -382,7 +385,7 @@ export class SharedTreeKernel
382385
});
383386
checkout.events.on("beforeBatch", (event) => {
384387
if (event.type === "append" && this.sharedObject.isAttached()) {
385-
if (this.checkout.transaction.isInProgress()) {
388+
if (checkout.transaction.isInProgress()) {
386389
enricher.addTransactionCommits(event.newCommits);
387390
}
388391
}
@@ -438,9 +441,12 @@ export class SharedTreeKernel
438441
branchId: BranchId,
439442
config: TreeViewConfiguration<ReadSchema<TRoot>>,
440443
): SchematizingSimpleTreeView<TRoot> & TreeView<ReadSchema<TRoot>> {
441-
return this.checkoutBranch(branchId).viewWith(
442-
config,
443-
) as SchematizingSimpleTreeView<TRoot> & TreeView<ReadSchema<TRoot>>;
444+
return this.getCheckout(branchId).viewWith(config) as SchematizingSimpleTreeView<TRoot> &
445+
TreeView<ReadSchema<TRoot>>;
446+
}
447+
448+
private getCheckout(branchId: BranchId): TreeCheckout {
449+
return this.checkouts.get(branchId) ?? this.checkoutBranch(branchId);
444450
}
445451

446452
private checkoutBranch(branchId: BranchId): TreeCheckout {
@@ -463,12 +469,14 @@ export class SharedTreeKernel
463469
}
464470

465471
public override didAttach(): void {
466-
if (this.checkout.transaction.isInProgress()) {
467-
// Attaching during a transaction is not currently supported.
468-
// At least part of of the system is known to not handle this case correctly - commit enrichment - and there may be others.
469-
throw new UsageError(
470-
"Cannot attach while a transaction is in progress. Commit or abort the transaction before attaching.",
471-
);
472+
for (const checkout of this.checkouts.values()) {
473+
if (checkout.transaction.isInProgress()) {
474+
// Attaching during a transaction is not currently supported.
475+
// At least part of of the system is known to not handle this case correctly - commit enrichment - and there may be others.
476+
throw new UsageError(
477+
"Cannot attach while a transaction is in progress. Commit or abort the transaction before attaching.",
478+
);
479+
}
472480
}
473481
super.didAttach();
474482
}
@@ -478,10 +486,12 @@ export class SharedTreeKernel
478486
SharedTreeCore<SharedTreeEditBuilder, SharedTreeChange>["applyStashedOp"]
479487
>
480488
): void {
481-
assert(
482-
!this.checkout.transaction.isInProgress(),
483-
0x674 /* Unexpected transaction is open while applying stashed ops */,
484-
);
489+
for (const checkout of this.checkouts.values()) {
490+
assert(
491+
!checkout.transaction.isInProgress(),
492+
0x674 /* Unexpected transaction is open while applying stashed ops */,
493+
);
494+
}
485495
super.applyStashedOp(...args);
486496
}
487497

@@ -491,8 +501,9 @@ export class SharedTreeKernel
491501
schemaAndPolicy: ClonableSchemaAndPolicy,
492502
isResubmit: boolean,
493503
): void {
504+
const checkout = this.getCheckout(branchId);
494505
assert(
495-
!this.checkout.transaction.isInProgress(),
506+
!checkout.transaction.isInProgress(),
496507
0xaa6 /* Cannot submit a commit while a transaction is in progress */,
497508
);
498509
if (isResubmit) {
@@ -502,14 +513,9 @@ export class SharedTreeKernel
502513
// Refrain from submitting new commits until they are validated by the checkout.
503514
// This is not a strict requirement for correctness in our system, but in the event that there is a bug when applying commits to the checkout
504515
// that causes a crash (e.g. in the forest), this will at least prevent this client from sending the problematic commit to any other clients.
505-
if (branchId === "main") {
506-
this.checkout.onCommitValid(commit, () =>
507-
super.submitCommit(branchId, commit, schemaAndPolicy, isResubmit),
508-
);
509-
} else {
510-
// XXX: Should we store a cache from branch to checkout?
511-
super.submitCommit(branchId, commit, schemaAndPolicy, isResubmit);
512-
}
516+
checkout.onCommitValid(commit, () =>
517+
super.submitCommit(branchId, commit, schemaAndPolicy, isResubmit),
518+
);
513519
}
514520

515521
public onDisconnect(): void {}

0 commit comments

Comments
 (0)