@@ -34,6 +34,11 @@ import {
3434 type URI ,
3535} from "./sigil-types.ts" ;
3636import { areLinksSame , isLink } from "./link-utils.ts" ;
37+ import { type IRuntime } from "./runtime.ts" ;
38+ import {
39+ type NormalizedFullLink ,
40+ parseNormalizedFullLinktoLegacyDocCellLink ,
41+ } from "./link-utils.ts" ;
3742
3843/**
3944 * This is the regular Cell interface, generated by DocImpl.asCell().
@@ -90,6 +95,9 @@ import { areLinksSame, isLink } from "./link-utils.ts";
9095 * @method getAsLegacyCellLink Returns a cell link for the cell (legacy format).
9196 * @returns {LegacyDocCellLink }
9297 *
98+ * @method getAsNormalizedFullLink Returns a normalized full link for the cell.
99+ * @returns {NormalizedFullLink }
100+ *
93101 * @method getAsLink Returns a cell link for the cell (new sigil format).
94102 * @returns {SigilLink }
95103 *
@@ -170,6 +178,7 @@ declare module "@commontools/api" {
170178 log ?: ReactivityLog ,
171179 ) : QueryResult < DeepKeyLookup < T , Path > > ;
172180 getAsLegacyCellLink ( ) : LegacyDocCellLink ;
181+ getAsNormalizedFullLink ( ) : NormalizedFullLink ;
173182 getAsLink (
174183 options ?: {
175184 base ?: Cell < any > ;
@@ -255,20 +264,22 @@ export interface Stream<T> {
255264 send ( event : T ) : void ;
256265 sink ( callback : ( event : T ) => Cancel | undefined | void ) : Cancel ;
257266 getRaw ( ) : any ;
267+ getAsNormalizedFullLink ( ) : NormalizedFullLink ;
258268 getDoc ( ) : DocImpl < any > ;
259269 schema ?: JSONSchema ;
260270 rootSchema ?: JSONSchema ;
261271 [ isStreamMarker ] : true ;
262272}
263273
264274export function createCell < T > (
265- doc : DocImpl < any > ,
266- path : PropertyKey [ ] = [ ] ,
275+ runtime : IRuntime ,
276+ link : NormalizedFullLink ,
267277 log ?: ReactivityLog ,
268- schema ?: JSONSchema ,
269- rootSchema : JSONSchema | undefined = schema ,
270278 noResolve = false ,
271279) : Cell < T > {
280+ const doc = runtime . documentMap . getDocByEntityId ( link . space , link . id , true ) ;
281+ let { path, schema, rootSchema } = link ;
282+
272283 // Resolve the path to check whether it's a stream. We're not logging this right now.
273284 // The corner case where during it's lifetime this changes from non-stream to stream
274285 // or vice versa will not be detected.
@@ -283,23 +294,26 @@ export function createCell<T>(
283294
284295 if ( isStreamValue ( ref . cell . getAtPath ( ref . path ) ) ) {
285296 return createStreamCell (
286- ref . cell ,
287- ref . path ,
297+ runtime ,
298+ {
299+ ...link ,
300+ space : ref . cell . space ,
301+ id : toURI ( ref . cell . entityId ) ,
302+ path : ref . path as string [ ] ,
303+ schema,
304+ rootSchema,
305+ } ,
288306 log ,
289- schema ,
290- rootSchema ,
291307 ) as unknown as Cell < T > ;
292308 } else {
293- return createRegularCell ( doc , path , log , schema , rootSchema ) ;
309+ return createRegularCell ( runtime , { ... link , schema, rootSchema } , log ) ;
294310 }
295311}
296312
297313function createStreamCell < T > (
298- doc : DocImpl < any > ,
299- path : PropertyKey [ ] ,
314+ runtime : IRuntime ,
315+ link : NormalizedFullLink ,
300316 _log ?: ReactivityLog ,
301- schema ?: JSONSchema ,
302- rootSchema ?: JSONSchema ,
303317) : Stream < T > {
304318 const listeners = new Set < ( event : T ) => Cancel | undefined > ( ) ;
305319
@@ -309,11 +323,7 @@ function createStreamCell<T>(
309323 // Implementing just the subset of Cell<T> that is needed for streams.
310324 send : ( event : T ) => {
311325 // Use runtime from doc if available
312- if ( doc . runtime ) {
313- doc . runtime . scheduler . queueEvent ( { cell : doc , path } , event ) ;
314- } else {
315- throw new Error ( "No runtime available for queueEvent" ) ;
316- }
326+ runtime . scheduler . queueEvent ( link , event ) ;
317327
318328 cleanup ?.( ) ;
319329 const [ cancel , addCancel ] = useCancelGroup ( ) ;
@@ -325,23 +335,25 @@ function createStreamCell<T>(
325335 listeners . add ( callback ) ;
326336 return ( ) => listeners . delete ( callback ) ;
327337 } ,
328- getRaw : ( ) => doc . getAtPath ( path ) ,
329- getDoc : ( ) => doc ,
330- schema,
331- rootSchema,
338+ getRaw : ( ) => self . getDoc ( ) . getAtPath ( link . path ) ,
339+ getAsNormalizedFullLink : ( ) => link ,
340+ getDoc : ( ) => runtime . documentMap . getDocByEntityId ( link . space , link . id ) ,
341+ schema : link . schema ,
342+ rootSchema : link . rootSchema ,
332343 [ isStreamMarker ] : true ,
333344 } satisfies Stream < T > ;
334345
335346 return self ;
336347}
337348
338349function createRegularCell < T > (
339- doc : DocImpl < any > ,
340- path : PropertyKey [ ] ,
350+ runtime : IRuntime ,
351+ link : NormalizedFullLink ,
341352 log ?: ReactivityLog ,
342- schema ?: JSONSchema ,
343- rootSchema ?: JSONSchema ,
344353) : Cell < T > {
354+ const doc = runtime . documentMap . getDocByEntityId ( link . space , link . id ) ;
355+ const { path, schema, rootSchema } = link ;
356+
345357 const self = {
346358 get : ( ) => validateAndTransform ( doc , path , schema , log , rootSchema ) ,
347359 set : ( newValue : Cellify < T > ) =>
@@ -453,25 +465,31 @@ function createRegularCell<T>(
453465 rootSchema ,
454466 ) ;
455467 return createCell (
456- doc ,
457- [ ...path , valueKey ] ,
468+ runtime ,
469+ {
470+ ...link ,
471+ path : [ ...path , valueKey . toString ( ) ] ,
472+ schema : childSchema ,
473+ } ,
458474 log ,
459- childSchema ,
460- rootSchema ,
461475 ) as T extends Cell < infer S > ? Cell < S [ K & keyof S ] > : Cell < T [ K ] > ;
462476 } ,
463477
464478 asSchema : ( newSchema ?: JSONSchema ) =>
465- createCell ( doc , path , log , newSchema , newSchema ) ,
466- withLog : ( newLog : ReactivityLog ) =>
467- createCell ( doc , path , newLog , schema , rootSchema ) ,
479+ createCell (
480+ runtime ,
481+ { ...link , schema : newSchema , rootSchema : newSchema } ,
482+ log ,
483+ ) ,
484+ withLog : ( newLog : ReactivityLog ) => createCell ( runtime , link , newLog ) ,
468485 sink : ( callback : ( value : T ) => Cancel | undefined ) =>
469486 subscribeToReferencedDocs ( callback , doc , path , schema , rootSchema ) ,
470487 getAsQueryResult : ( subPath : PropertyKey [ ] = [ ] , newLog ?: ReactivityLog ) =>
471488 createQueryResultProxy ( doc , [ ...path , ...subPath ] , newLog ?? log ) ,
472489 getAsLegacyCellLink : ( ) : LegacyDocCellLink => {
473490 return { space : doc . space , cell : doc , path, schema, rootSchema } ;
474491 } ,
492+ getAsNormalizedFullLink : ( ) => link ,
475493 getAsLink : (
476494 options ?: {
477495 base ?: Cell < any > ;
0 commit comments