Skip to content

Commit

Permalink
tree: Convert directly from simple to stored schema (microsoft#22651)
Browse files Browse the repository at this point in the history
## Description

Convert directly from simple to stored schema.
This removes the last known production use of flex-tree-schema.
  • Loading branch information
CraigMacomber authored Sep 26, 2024
1 parent a5eb7a2 commit b902d4c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 265 deletions.
8 changes: 1 addition & 7 deletions packages/dds/tree/src/simple-tree/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ export {
type TreeNodeSchemaCore,
type TreeNodeSchemaBoth,
} from "./treeNodeSchema.js";
export {
getSimpleNodeSchema,
setFlexSchemaFromClassSchema,
tryGetSimpleNodeSchema,
cachedFlexSchemaFromClassSchema,
getSimpleNodeSchemaFromInnerNode,
} from "./schemaCaching.js";
export { getSimpleNodeSchemaFromInnerNode } from "./schemaCaching.js";
export { walkAllowedTypes, type SchemaVisitor } from "./walkSchema.js";
export { Context, HydratedContext, SimpleContextSlot } from "./context.js";
export { getOrCreateNodeFromInnerNode } from "./getOrCreateNode.js";
Expand Down
52 changes: 0 additions & 52 deletions packages/dds/tree/src/simple-tree/core/schemaCaching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,13 @@

import { assert } from "@fluidframework/core-utils/internal";

import type { FlexTreeNodeSchema, TreeNodeSchemaBase } from "../../feature-libraries/index.js";
import { fail } from "../../util/index.js";

import type { TreeNodeSchema } from "./treeNodeSchema.js";
import type { InnerNode } from "./treeNodeKernel.js";
import { UnhydratedFlexTreeNode } from "./unhydratedFlexTree.js";
import { SimpleContextSlot, type Context } from "./context.js";

/**
* A symbol for storing FlexTreeSchema on TreeNodeSchema.
* Eagerly set on leaves, and lazily set for other cases.
*/
const flexSchemaSymbol: unique symbol = Symbol(`flexSchema`);

/**
* A symbol for storing TreeNodeSchema on FlexTreeNode's schema.
*/
const simpleNodeSchemaSymbol: unique symbol = Symbol(`simpleNodeSchema`);

export function cachedFlexSchemaFromClassSchema(
schema: TreeNodeSchema,
): TreeNodeSchemaBase | undefined {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (schema as any)[flexSchemaSymbol] as TreeNodeSchemaBase | undefined;
}

export function setFlexSchemaFromClassSchema(
simple: TreeNodeSchema,
flex: TreeNodeSchemaBase,
): void {
assert(!(flexSchemaSymbol in simple), 0x91f /* simple schema already marked */);
assert(!(simpleNodeSchemaSymbol in flex), 0x920 /* flex schema already marked */);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(simple as any)[flexSchemaSymbol] = flex;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(flex as any)[simpleNodeSchemaSymbol] = simple;
}

/**
* Gets the {@link TreeNodeSchema} cached on the provided {@link FlexTreeNodeSchema | flexSchema}.
* Returns `undefined` if no cached value is found.
*/
export function tryGetSimpleNodeSchema(
flexSchema: FlexTreeNodeSchema,
): TreeNodeSchema | undefined {
if (simpleNodeSchemaSymbol in flexSchema) {
return flexSchema[simpleNodeSchemaSymbol] as TreeNodeSchema;
}
return undefined;
}

/**
* Gets the {@link TreeNodeSchema} cached on the provided {@link FlexTreeNodeSchema | flexSchema}.
* Fails if no cached value is found.
*/
export function getSimpleNodeSchema(flexSchema: FlexTreeNodeSchema): TreeNodeSchema {
return tryGetSimpleNodeSchema(flexSchema) ?? fail("missing simple schema");
}

/**
* Gets the {@link TreeNodeSchema} for the {@link InnerNode}.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/dds/tree/src/simple-tree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export {
} from "./objectNode.js";
export type { TreeMapNode, MapNodeInsertableData } from "./mapNode.js";
export { mapTreeFromNodeData, type InsertableContent } from "./toMapTree.js";
export { toStoredSchema, getStoredSchema, getFlexSchema } from "./toFlexSchema.js";
export { toStoredSchema, getStoredSchema } from "./toFlexSchema.js";
export {
numberSchema,
stringSchema,
Expand Down
10 changes: 1 addition & 9 deletions packages/dds/tree/src/simple-tree/leafNodeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ import { assert } from "@fluidframework/core-utils/internal";

import { type TreeValue, ValueSchema } from "../core/index.js";
import {
LeafNodeSchema as FlexLeafNodeSchema,
type FlexTreeNode,
isFlexTreeNode,
valueSchemaAllows,
} from "../feature-libraries/index.js";
import {
setFlexSchemaFromClassSchema,
NodeKind,
type TreeNodeSchema,
type TreeNodeSchemaNonClass,
} from "./core/index.js";
import { NodeKind, type TreeNodeSchema, type TreeNodeSchemaNonClass } from "./core/index.js";

/**
* Instances of this class are schema for leaf nodes.
Expand Down Expand Up @@ -47,8 +41,6 @@ export class LeafNodeSchema<Name extends string, const T extends ValueSchema>
}

public constructor(name: Name, t: T) {
const schema: FlexLeafNodeSchema = new FlexLeafNodeSchema({ name: "makeLeaf" }, name, t);
setFlexSchemaFromClassSchema(this, schema);
this.identifier = name;
this.info = t;
}
Expand Down
Loading

0 comments on commit b902d4c

Please sign in to comment.