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

Expose configuredSharedTree #20332

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion examples/data-objects/inventory-app/src/reactSharedTreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,29 @@ import {
ITree,
type ImplicitFieldSchema,
SchemaIncompatible,
SharedTree,
TreeConfiguration,
TreeFieldFromImplicitField,
TreeView,
} from "@fluidframework/tree";
import {
configuredSharedTree,
typeboxValidator,
// eslint-disable-next-line import/no-internal-modules
} from "@fluidframework/tree/internal";
import * as React from "react";

/**
* This file contains logic not specific to this particular sample that other apps may want to use.
* Eventually this should be published as part of a package apps can use.
*/

/**
* Opt into extra validation to detect detect encoding bugs and data corruption.
CraigMacomber marked this conversation as resolved.
Show resolved Hide resolved
*/
const SharedTree = configuredSharedTree({
jsonValidator: typeboxValidator,
});

/**
* TODO: once we add options to factory (for example controlling the write format),
* apps will need a way to provide those.
Expand Down
3 changes: 3 additions & 0 deletions packages/dds/tree/api-report/tree.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ export interface CommitMetadata {
// @internal
export function compareLocalNodeKeys(a: LocalNodeKey, b: LocalNodeKey): -1 | 0 | 1;

// @internal
export function configuredSharedTree(options: SharedTreeOptions): ISharedObjectKind<ITree>;

// @internal
export type ContextuallyTypedFieldData = ContextuallyTypedNodeData | undefined;

Expand Down
2 changes: 1 addition & 1 deletion packages/dds/tree/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export {
test_RecursiveObject_base,
test_RecursiveObjectPojoMode,
} from "./simple-tree/index.js";
export { SharedTree } from "./treeFactory.js";
export { SharedTree, configuredSharedTree } from "./treeFactory.js";

export type { ICodecOptions, JsonValidator, SchemaValidationFunction } from "./codec/index.js";
export { noopValidator } from "./codec/index.js";
Expand Down
49 changes: 38 additions & 11 deletions packages/dds/tree/src/treeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,44 @@ export class TreeFactory implements IChannelFactory<ITree> {
/**
* SharedTree is a hierarchical data structure for collaboratively editing strongly typed JSON-like trees
* of objects, arrays, and other data types.
* @privateRemarks
* Due to the dependency structure and the placement of that interface SharedObjectClass,
* this interface implementation can not be recorded in the type here.
* @public
*/
export const SharedTree: ISharedObjectKind<ITree> = {
getFactory(): IChannelFactory<ITree> {
return new TreeFactory({});
},
export const SharedTree: ISharedObjectKind<ITree> = configuredSharedTree({});

create(runtime: IFluidDataStoreRuntime, id?: string): ITree {
return runtime.createChannel(id, TreeFactory.type) as ITree;
},
};
/**
* {@link SharedTree} but allowing a non-default configuration.
* @remarks
* This is useful for debugging and testing to opt into extra validation or see if opting out of some optimizations fixes an issue.
* @example
* ```typescript
* import {
* ForestType,
* TreeCompressionStrategy,
* configuredSharedTree,
* typeboxValidator,
* // eslint-disable-next-line import/no-internal-modules
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this eslint directive need to be in the example?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered removing it. I kinda like that it helps highlight that despite this pattern usually not being allowed, in this case (reaching into a package for on of our release tag rollups) is supported.

I don't have a strong opinion either way though, so if anyone has a preference, I'll go with that.

Copy link
Contributor

Choose a reason for hiding this comment

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

No strong opinion either. Feel free to resolve without further changes.

* } from "@fluidframework/tree/internal";
* const SharedTree = configuredSharedTree({
* forest: ForestType.Reference,
* jsonValidator: typeboxValidator,
* treeEncodeType: TreeCompressionStrategy.Uncompressed,
* });
* ```
* @privateRemarks
* TODO:
* Expose Ajv validator for better error message quality somehow.
* Maybe as part of a test utils or dev-tool package?
* @internal
*/
export function configuredSharedTree(options: SharedTreeOptions): ISharedObjectKind<ITree> {
const factory = new TreeFactory(options);
return {
getFactory(): IChannelFactory<ITree> {
return factory;
},

create(runtime: IFluidDataStoreRuntime, id?: string): ITree {
return runtime.createChannel(id, TreeFactory.type) as ITree;
},
};
}
Loading