@@ -12,7 +12,6 @@ import {
1212 type ITreeCursorSynchronous ,
1313 LeafNodeStoredSchema ,
1414 ObjectNodeStoredSchema ,
15- type StoredSchemaCollection ,
1615 type TreeFieldStoredSchema ,
1716 type TreeNodeSchemaIdentifier ,
1817 type TreeStoredSchemaSubscription ,
@@ -23,6 +22,7 @@ import {
2322 ValueSchema ,
2423 type TreeChunk ,
2524 tryGetChunk ,
25+ type SchemaAndPolicy ,
2626} from "../../core/index.js" ;
2727import { getOrCreate } from "../../util/index.js" ;
2828import type { FullSchemaPolicy } from "../modular-schema/index.js" ;
@@ -54,13 +54,15 @@ export function makeTreeChunker(
5454 defaultChunkPolicy . sequenceChunkInlineThreshold ,
5555 defaultChunkPolicy . uniformChunkNodeCount ,
5656 ( type : TreeNodeSchemaIdentifier , shapes : Map < TreeNodeSchemaIdentifier , ShapeInfo > ) =>
57- tryShapeFromNodeSchema ( {
58- schema,
59- policy,
60- shouldEncodeIncrementally,
61- shapes,
62- nodeSchema : type ,
63- } ) ,
57+ tryShapeFromNodeSchema (
58+ {
59+ schema,
60+ policy,
61+ shouldEncodeIncrementally,
62+ shapes,
63+ } ,
64+ type ,
65+ ) ,
6466 ) ;
6567}
6668
@@ -231,15 +233,7 @@ export function makePolicy(policy?: Partial<ChunkPolicy>): ChunkPolicy {
231233 return withDefaults ;
232234}
233235
234- export interface ShapeFromSchemaParameters {
235- /**
236- * The collection of stored schemas containing node and field definitions.
237- */
238- readonly schema : StoredSchemaCollection ;
239- /**
240- * Policy from the app for interpreting the stored schema.
241- */
242- readonly policy : FullSchemaPolicy ;
236+ export interface ShapeFromSchemaParameters extends SchemaAndPolicy {
243237 /**
244238 * Policy function to determine if a field should be encoded incrementally.
245239 * Incrementally encoding requires the subtree to not start in the middle of a larger uniform chunk.
@@ -248,29 +242,26 @@ export interface ShapeFromSchemaParameters {
248242 */
249243 readonly shouldEncodeIncrementally : IncrementalEncodingPolicy ;
250244 /**
251- * A cache for shapes which may be read and/or updated. A new (or cleared) cache must be provided
252- * if recalling this function after any of the parameters except in `ShapeFromSchemaParameters` have changed.
245+ * A cache for shapes which may be read and/or updated.
246+ * As the shape is a function of the other members of `ShapeFromSchemaParameters`,
247+ * it must be replaced or cleared if any of the properties other than this cache are modified.
253248 */
254249 readonly shapes : Map < TreeNodeSchemaIdentifier , ShapeInfo > ;
255250}
256251
257- export interface ShapeFromNodeSchemaParameters extends ShapeFromSchemaParameters {
258- /**
259- * The identifier of the specific node schema to analyze for shape uniformity.
260- */
261- readonly nodeSchema : TreeNodeSchemaIdentifier ;
262- }
263-
264- export interface ShapeFromFieldSchemaParameters extends ShapeFromSchemaParameters {
252+ /**
253+ * A TreeFieldStoredSchema with some additional context about where it is in the tree.
254+ */
255+ export interface FieldSchemaWithContext {
265256 /**
266257 * The identifier of the specific field schema to analyze for shape uniformity.
267258 */
268259 readonly fieldSchema : TreeFieldStoredSchema ;
269-
270260 /**
271261 * The identifier of the parent node schema containing this field.
262+ * If undefined, this is a root field.
272263 */
273- readonly parentNodeSchema : TreeNodeSchemaIdentifier ;
264+ readonly parentNodeSchema ? : TreeNodeSchemaIdentifier ;
274265 /**
275266 * The field key/name used to identify this field within the parent node.
276267 */
@@ -283,7 +274,8 @@ export interface ShapeFromFieldSchemaParameters extends ShapeFromSchemaParameter
283274 * single child types), returns a TreeShape that can be used for efficient uniform chunking. Otherwise,
284275 * returns Polymorphic to indicate the shape varies and should use basic chunking.
285276 *
286- * @param params - {@link ShapeFromNodeSchemaParameters}.
277+ * @param params - {@link ShapeFromSchemaParameters}.
278+ * @param nodeSchema - The identifier of the specific node schema to analyze for shape uniformity.
287279 * @returns TreeShape if the schema has a uniform shape, or Polymorphic if shape varies.
288280 *
289281 * @remarks
@@ -292,8 +284,11 @@ export interface ShapeFromFieldSchemaParameters extends ShapeFromSchemaParameter
292284 * optimize for patterns of specific values.
293285 *
294286 */
295- export function tryShapeFromNodeSchema ( params : ShapeFromNodeSchemaParameters ) : ShapeInfo {
296- const { schema, policy, shouldEncodeIncrementally, shapes, nodeSchema } = params ;
287+ export function tryShapeFromNodeSchema (
288+ params : ShapeFromSchemaParameters ,
289+ nodeSchema : TreeNodeSchemaIdentifier ,
290+ ) : ShapeInfo {
291+ const { schema, policy, shouldEncodeIncrementally, shapes } = params ;
297292 return getOrCreate ( shapes , nodeSchema , ( ) => {
298293 const treeSchema = schema . nodeSchema . get ( nodeSchema ) ?? fail ( 0xaf9 /* missing schema */ ) ;
299294 if ( treeSchema instanceof LeafNodeStoredSchema ) {
@@ -308,15 +303,15 @@ export function tryShapeFromNodeSchema(params: ShapeFromNodeSchemaParameters): S
308303 if ( treeSchema instanceof ObjectNodeStoredSchema ) {
309304 const fieldsArray : FieldShape [ ] = [ ] ;
310305 for ( const [ key , fieldSchema ] of treeSchema . objectNodeFields ) {
311- const fieldShape = tryShapeFromFieldSchema ( {
312- schema ,
313- policy ,
314- shouldEncodeIncrementally ,
315- shapes ,
316- parentNodeSchema : nodeSchema ,
317- fieldSchema ,
318- key,
319- } ) ;
306+ const fieldShape = tryShapeFromFieldSchema (
307+ {
308+ schema ,
309+ policy ,
310+ shouldEncodeIncrementally ,
311+ shapes ,
312+ } ,
313+ { fieldSchema , parentNodeSchema : nodeSchema , key } ,
314+ ) ;
320315 if ( fieldShape === undefined ) {
321316 return polymorphic ;
322317 }
@@ -335,6 +330,7 @@ export function tryShapeFromNodeSchema(params: ShapeFromNodeSchemaParameters): S
335330 * or should be encoded incrementally, which requires separate chunking.
336331 *
337332 * @param params - {@link ShapeFromFieldSchemaParameters}.
333+ * @param fieldSchemaWithContext - {@link FieldSchemaWithContext}.
338334 * @returns FieldShape if the field has a uniform shape, or undefined if the field is polymorphic.
339335 *
340336 * @remarks
@@ -343,17 +339,11 @@ export function tryShapeFromNodeSchema(params: ShapeFromNodeSchemaParameters): S
343339 * optimize for patterns of specific values.
344340 */
345341export function tryShapeFromFieldSchema (
346- params : ShapeFromFieldSchemaParameters ,
342+ params : ShapeFromSchemaParameters ,
343+ fieldSchemaWithContext : FieldSchemaWithContext ,
347344) : FieldShape | undefined {
348- const {
349- schema,
350- policy,
351- shouldEncodeIncrementally,
352- shapes,
353- parentNodeSchema,
354- fieldSchema,
355- key,
356- } = params ;
345+ const { schema, policy, shouldEncodeIncrementally, shapes } = params ;
346+ const { fieldSchema, parentNodeSchema, key } = fieldSchemaWithContext ;
357347 // If this field should be encoded incrementally, use polymorphic shape so that they
358348 // are chunked separately and can be re-used across encodings if they do not change.
359349 if ( shouldEncodeIncrementally ( parentNodeSchema , key ) ) {
@@ -367,13 +357,15 @@ export function tryShapeFromFieldSchema(
367357 return undefined ;
368358 }
369359 const childType = [ ...fieldSchema . types ] [ 0 ] ?? oob ( ) ;
370- const childShape = tryShapeFromNodeSchema ( {
371- schema,
372- policy,
373- shouldEncodeIncrementally,
374- shapes,
375- nodeSchema : childType ,
376- } ) ;
360+ const childShape = tryShapeFromNodeSchema (
361+ {
362+ schema,
363+ policy,
364+ shouldEncodeIncrementally,
365+ shapes,
366+ } ,
367+ childType ,
368+ ) ;
377369 if ( childShape instanceof Polymorphic ) {
378370 return undefined ;
379371 }
0 commit comments