Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor forest's applyDelta to return a visitor #17163

Merged
merged 8 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions api-report/tree2.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export type AnchorsCompare = CompareFunction<UpPath>;

// @alpha @sealed
export class AnchorSet implements ISubscribable<AnchorSetRootEvents>, AnchorLocator {
applyDelta(delta: Delta.Root): void;
acquireVisitor(): DeltaVisitor;
// (undocumented)
forget(anchor: Anchor): void;
internalizePath(originalPath: UpPath): UpPath;
Expand Down Expand Up @@ -428,6 +428,28 @@ declare namespace Delta {
}
export { Delta }

// @alpha
export interface DeltaVisitor {
// (undocumented)
enterField(key: FieldKey): void;
// (undocumented)
enterNode(index: number): void;
// (undocumented)
exitField(key: FieldKey): void;
// (undocumented)
exitNode(index: number): void;
// (undocumented)
free(): void;
// (undocumented)
onDelete(index: number, count: number): void;
// (undocumented)
onInsert(index: number, content: Delta.ProtoNodes): void;
// (undocumented)
onMoveIn(index: number, count: number, id: Delta.MoveId): void;
// (undocumented)
onMoveOut(index: number, count: number, id: Delta.MoveId): void;
}

// @alpha
export interface Dependee extends NamedComputation {
registerDependent(dependent: Dependent): boolean;
Expand Down Expand Up @@ -747,8 +769,8 @@ export const forbiddenFieldKindIdentifier = "Forbidden";

// @alpha
export interface ForestEvents {
afterDelta(delta: Delta.Root): void;
beforeDelta(delta: Delta.Root): void;
afterChange(): void;
beforeChange(): void;
}

// @alpha
Expand Down Expand Up @@ -849,8 +871,9 @@ export interface IdRange {

// @alpha
export interface IEditableForest extends IForestSubscription {
// (undocumented)
acquireVisitor(): DeltaVisitor;
readonly anchors: AnchorSet;
applyDelta(delta: Delta.Root): void;
}

// @alpha
Expand Down
13 changes: 9 additions & 4 deletions experimental/dds/tree2/src/core/forest/editableForest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
Anchor,
ITreeCursorSynchronous,
rootFieldKey,
DeltaVisitor,
applyDelta,
} from "../tree";
import { IForestSubscription, ITreeSubscriptionCursor } from "./forest";

Expand All @@ -31,10 +33,13 @@ export interface IEditableForest extends IForestSubscription {
readonly anchors: AnchorSet;

/**
* Applies the supplied Delta to the forest.
* Does NOT update anchors.
* @returns a visitor that can be used to mutate the forest.
*
* Mutating the forest does NOT update anchors.
* The visitor must be released after use.
* It is invalid to acquire a visitor without releasing the previous one.
*/
applyDelta(delta: Delta.Root): void;
acquireVisitor(): DeltaVisitor;
}

/**
Expand All @@ -50,7 +55,7 @@ export function initializeForest(
): void {
assert(forest.isEmpty, 0x747 /* forest must be empty */);
const insert: Delta.Insert = { type: Delta.MarkType.Insert, content };
forest.applyDelta(new Map([[rootFieldKey, [insert]]]));
applyDelta(new Map([[rootFieldKey, [insert]]]), forest);
}

// TODO: Types below here may be useful for input into edit building APIs, but are no longer used here directly.
Expand Down
11 changes: 6 additions & 5 deletions experimental/dds/tree2/src/core/forest/forest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { StoredSchemaRepository, FieldKey } from "../schema-stored";
import {
Anchor,
AnchorSet,
Delta,
DetachedField,
detachedFieldAsKey,
ITreeCursor,
Expand All @@ -35,14 +34,16 @@ import type { IEditableForest } from "./editableForest";
*/
export interface ForestEvents {
/**
* Delta is about to be applied to forest.
* The forest is about to be changed.
* Emitted before the first change in a batch of changes.
*/
beforeDelta(delta: Delta.Root): void;
beforeChange(): void;

/**
* Delta was just applied to forest.
* The forest was just changed.
* Emitted after the last change in a batch of changes.
*/
afterDelta(delta: Delta.Root): void;
afterChange(): void;
}

/**
Expand Down
1 change: 1 addition & 0 deletions experimental/dds/tree2/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export {
detachedFieldAsKey,
keyAsDetachedField,
visitDelta,
applyDelta,
setGenericTreeField,
DeltaVisitor,
PathVisitor,
Expand Down
Loading