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

Add Clone to SharedTree Revertible #23044

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
408ff9e
first commit
jikim-msft Nov 8, 2024
bdfbd3a
add getCheckout & test case
jikim-msft Nov 8, 2024
7f0777e
no nuke map
jikim-msft Nov 8, 2024
94b8b5a
add test
jikim-msft Nov 9, 2024
73dfa36
Merge branch 'main' into revertible-alpha
jikim-msft Nov 9, 2024
1364971
policy fix
jikim-msft Nov 9, 2024
f0a7c6e
Merge branch 'revertible-alpha' of https://github.com/jikim-msft/Flui…
jikim-msft Nov 9, 2024
0d593b4
refactor
jikim-msft Nov 9, 2024
09f10e5
change test suite name
jikim-msft Nov 9, 2024
896f22f
change name to RevertibleAlpha & remove DisposableRevertible
jikim-msft Nov 11, 2024
b967512
inline logic
jikim-msft Nov 11, 2024
33e1fde
refactor create undo/redo stack function & change name
jikim-msft Nov 12, 2024
0f62865
add doc
jikim-msft Nov 12, 2024
698e1c1
add failing test case
jikim-msft Nov 12, 2024
5caf3c6
remove only
jikim-msft Nov 12, 2024
1784895
change onDispose to required
jikim-msft Nov 12, 2024
0d2bef6
Merge branch 'microsoft:main' into revertible-alpha
jikim-msft Nov 12, 2024
022404a
Revert "refactor create undo/redo stack function & change name"
jikim-msft Nov 12, 2024
2ab318c
revert & replace
jikim-msft Nov 12, 2024
aaff66b
Update packages/dds/tree/src/shared-tree/treeCheckout.ts
jikim-msft Nov 12, 2024
115354b
Update packages/dds/tree/src/core/revertible.ts
jikim-msft Nov 12, 2024
f0c989c
Update packages/dds/tree/src/core/revertible.ts
jikim-msft Nov 12, 2024
dea35f7
Update packages/dds/tree/src/core/revertible.ts
jikim-msft Nov 12, 2024
7596fa7
Update packages/dds/tree/src/core/revertible.ts
jikim-msft Nov 12, 2024
0ec8140
Update packages/dds/tree/src/core/revertible.ts
jikim-msft Nov 12, 2024
e518bb7
change API tag
jikim-msft Nov 12, 2024
13be6d4
Merge branch 'revertible-alpha' of https://github.com/jikim-msft/Flui…
jikim-msft Nov 12, 2024
b3ac268
Merge branch 'microsoft:main' into revertible-alpha
jikim-msft Nov 12, 2024
855fe89
add TODO & update api
jikim-msft Nov 13, 2024
cec77a5
change name back to createTestUndoRedoStacks
jikim-msft Nov 13, 2024
c4ed841
add doc
jikim-msft Nov 13, 2024
85319e7
fix import
jikim-msft Nov 13, 2024
80946ab
change from commit to changed
jikim-msft Nov 13, 2024
7d87eb7
change name of forkedBranch
jikim-msft Nov 13, 2024
bd83bd2
update api doc
jikim-msft Nov 13, 2024
684943e
Update packages/dds/tree/src/test/utils.ts
jikim-msft Nov 13, 2024
9969510
fix doc
jikim-msft Nov 13, 2024
41acc0d
change function param
jikim-msft Nov 13, 2024
b7b2b2f
Merge branch 'microsoft:main' into revertible-alpha
jikim-msft Nov 13, 2024
fa5e774
simplify test
jikim-msft Nov 13, 2024
ec15843
update api
jikim-msft Nov 13, 2024
233749d
update doc
jikim-msft Nov 13, 2024
e50bc39
Update packages/dds/tree/src/core/revertible.ts
jikim-msft Nov 14, 2024
fec248e
Update packages/dds/tree/src/core/revertible.ts
jikim-msft Nov 14, 2024
5603232
Update packages/dds/tree/src/shared-tree/treeCheckout.ts
jikim-msft Nov 14, 2024
eddc5a3
fix nit
jikim-msft Nov 14, 2024
04fdc50
add test
jikim-msft Nov 14, 2024
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
7 changes: 6 additions & 1 deletion packages/dds/tree/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,9 @@ export {
AllowedUpdateType,
} from "./schema-view/index.js";

export { type Revertible, RevertibleStatus, type RevertibleFactory } from "./revertible.js";
export {
type Revertible,
RevertibleStatus,
type RevertibleFactory,
type RevertibleAlphaFactory,
} from "./revertible.js";
23 changes: 23 additions & 0 deletions packages/dds/tree/src/core/revertible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Licensed under the MIT License.
*/

import type { TreeBranch } from "../simple-tree/index.js";

/**
* Allows reversion of a change made to SharedTree.
*
Expand Down Expand Up @@ -36,6 +38,13 @@ export interface Revertible {
dispose(): void;
}

/**
* @alpha
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
*/
export interface RevertibleAlpha extends Revertible {
clone: (forkedBranch?: TreeBranch) => RevertibleAlpha;
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* The status of a {@link Revertible}.
*
Expand All @@ -62,3 +71,17 @@ export enum RevertibleStatus {
export type RevertibleFactory = (
onRevertibleDisposed?: (revertible: Revertible) => void,
) => Revertible;

/**
* Will error if invoked outside the scope of the `commitApplied` event that provides it, or if invoked multiple times.
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
*
* @param onRevertibleDisposed - A callback that will be invoked when the `Revertible` generated by this factory is disposed.
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
* This happens when the `Revertible` is disposed manually, or when the `TreeView` that the `Revertible` belongs to is disposed,
* whichever happens first.
* This is typically used to clean up any resources associated with the `Revertible` in the host application.
*
* @sealed @public
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
*/
export type RevertibleAlphaFactory = (
onRevertibleDisposed?: (revertible: RevertibleAlpha) => void,
) => RevertibleAlpha;
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ export class SchematizingSimpleTreeView<
* @remarks Currently, all contexts are also {@link SchematizingSimpleTreeView}s.
* Other checkout implementations (e.g. not associated with a view) may be supported in the future.
*/
function getCheckout(context: TreeBranch): TreeCheckout {
export function getCheckout(context: TreeBranch): TreeCheckout {
jenn-le marked this conversation as resolved.
Show resolved Hide resolved
if (context instanceof SchematizingSimpleTreeView) {
return context.checkout;
}
Expand Down
125 changes: 81 additions & 44 deletions packages/dds/tree/src/shared-tree/treeCheckout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
tagChange,
visitDelta,
type RevertibleFactory,
type RevertibleAlphaFactory,
} from "../core/index.js";
import {
type HasListeners,
Expand Down Expand Up @@ -68,8 +69,11 @@ import type {
TreeViewConfiguration,
UnsafeUnknownSchema,
ViewableTree,
TreeBranch,
} from "../simple-tree/index.js";
import { SchematizingSimpleTreeView } from "./schematizingTreeView.js";
import { getCheckout, SchematizingSimpleTreeView } from "./schematizingTreeView.js";
// eslint-disable-next-line import/no-internal-modules
import type { RevertibleAlpha } from "../core/revertible.js";

/**
* Events for {@link ITreeCheckout}.
Expand All @@ -92,7 +96,10 @@ export interface CheckoutEvents {
* @param getRevertible - a function provided that allows users to get a revertible for the change. If not provided,
* this change is not revertible.
*/
changed(data: CommitMetadata, getRevertible?: RevertibleFactory): void;
changed(
data: CommitMetadata,
getRevertible?: RevertibleFactory | RevertibleAlphaFactory,
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
): void;
}

/**
Expand Down Expand Up @@ -397,7 +404,7 @@ export class TreeCheckout implements ITreeCheckoutFork {
/**
* Set of revertibles maintained for automatic disposal
*/
private readonly revertibles = new Set<DisposableRevertible>();
private readonly revertibles = new Set<RevertibleAlpha>();

/**
* Each branch's head commit corresponds to a revertible commit.
Expand Down Expand Up @@ -536,42 +543,12 @@ export class TreeCheckout implements ITreeCheckoutFork {
"Cannot generate the same revertible more than once. Note that this can happen when multiple changed event listeners are registered.",
);
}
const revertibleCommits = this.revertibleCommitBranches;
const revertible: DisposableRevertible = {
get status(): RevertibleStatus {
const revertibleCommit = revertibleCommits.get(revision);
return revertibleCommit === undefined
? RevertibleStatus.Disposed
: RevertibleStatus.Valid;
},
revert: (release: boolean = true) => {
if (revertible.status === RevertibleStatus.Disposed) {
throw new UsageError(
"Unable to revert a revertible that has been disposed.",
);
}

const revertMetrics = this.revertRevertible(revision, kind);
this.logger?.sendTelemetryEvent({
eventName: TreeCheckout.revertTelemetryEventName,
...revertMetrics,
});

if (release) {
revertible.dispose();
}
},
dispose: () => {
if (revertible.status === RevertibleStatus.Disposed) {
throw new UsageError(
"Unable to dispose a revertible that has already been disposed.",
);
}
this.disposeRevertible(revertible, revision);
onRevertibleDisposed?.(revertible);
},
};

const revertible = this.createRevertible(
revision,
kind,
this,
onRevertibleDisposed,
);
this.revertibleCommitBranches.set(revision, _branch.fork(commit));
this.revertibles.add(revertible);
return revertible;
Expand Down Expand Up @@ -626,6 +603,70 @@ export class TreeCheckout implements ITreeCheckoutFork {
}
}

private createRevertible(
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
revision: RevisionTag,
kind: CommitKind,
checkout: TreeCheckout = this,
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
onRevertibleDisposed?: (revertible: Revertible) => void,
): RevertibleAlpha {
const commitBranches = checkout.revertibleCommitBranches;

const revertible: RevertibleAlpha = {
get status(): RevertibleStatus {
const revertibleCommit = commitBranches.get(revision);
return revertibleCommit === undefined
? RevertibleStatus.Disposed
: RevertibleStatus.Valid;
},
revert: (release: boolean = true) => {
if (revertible.status === RevertibleStatus.Disposed) {
throw new UsageError("Unable to revert a revertible that has been disposed.");
}

const revertMetrics = checkout.revertRevertible(revision, kind);
checkout.logger?.sendTelemetryEvent({
eventName: TreeCheckout.revertTelemetryEventName,
...revertMetrics,
});

if (release) {
revertible.dispose();
}
},
clone: (forkedBranch?: TreeBranch) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was the reasoning behind allowing forkedBranch to be optional? why not just make it required and not allow clone to be called without a fork? the behavior for cloning a revertible onto the same checkout doesn't seem obvious to me and i'm not sure why we'd want to do so

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cloning a revertible onto the same branch is useful for an application that wants to provide ownership of a revertible to multiple components. Suppose I have two components in my application that:

  1. Don't know about each other
  2. Are operating on the same branch (e.g. the main branch)
  3. Both want to be able to revert (e.g. they both have undo buttons)

You'd want to clone the revertible, giving one to the first component and the clone to the second. Or, perhaps the parent component retains ownership of the original revertible and gives a new clone to each subcomponent. If you can't clone the revertible for the same branch, then you have to share the same revertible between the parent and both components, and now they do need to know about each other so that they know when to (or when not to) dispose the revertible.

Regarding the parameter being optional, it's just a convenience. For an application like the above, which operates solely on the main branch, it would be annoying at best and confusing at worst to be forced to pass in the main branch every time it wants to do a clone. With it being optional, the user doesn't need to understand the branching feature at all in order to clone revertibles.

return checkout.cloneRevertible(revision, kind, forkedBranch);
},
dispose: () => {
if (revertible.status === RevertibleStatus.Disposed) {
throw new UsageError(
"Unable to dispose a revertible that has already been disposed.",
);
}
checkout.disposeRevertible(revertible, revision);
onRevertibleDisposed?.(revertible);
},
};

return revertible;
}

private cloneRevertible(
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
revision: RevisionTag,
kind: CommitKind,
branch?: TreeBranch,
onRevertibleDisposed?: (revertible: Revertible) => void,
): RevertibleAlpha {
const checkout = branch ? getCheckout(branch) : this;

if (branch !== undefined) {
const revertibleBranch = this.revertibleCommitBranches.get(revision);
assert(revertibleBranch !== undefined, "SharedTreeBranch for revertible not found.");
checkout.revertibleCommitBranches.set(revision, revertibleBranch.fork());
}

return this.createRevertible(revision, kind, checkout, onRevertibleDisposed);
}

// For the new TreeViewAlpha API
public viewWith<TRoot extends ImplicitFieldSchema | UnsafeUnknownSchema>(
config: TreeViewConfiguration<ReadSchema<TRoot>>,
Expand Down Expand Up @@ -792,7 +833,7 @@ export class TreeCheckout implements ITreeCheckoutFork {
}
}

private disposeRevertible(revertible: DisposableRevertible, revision: RevisionTag): void {
private disposeRevertible(revertible: RevertibleAlpha, revision: RevisionTag): void {
this.revertibleCommitBranches.get(revision)?.dispose();
this.revertibleCommitBranches.delete(revision);
this.revertibles.delete(revertible);
Expand Down Expand Up @@ -890,7 +931,3 @@ export function runSynchronous(
? view.transaction.abort()
: view.transaction.commit();
}

interface DisposableRevertible extends Revertible {
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
dispose: () => void;
}
Loading
Loading