import { IChannelFactory } from "@fluidframework/datastore-definitions" ; import { NamedFluidDataStoreRegistryEntry } from "@fluidframework/runtime-definitions" ; import { ContainerSchema , DataObjectClass , LoadableObjectClass , SharedObjectClass , } from "./types" ; /**
 * Runtime check to determine if a class is a DataObject type
 */ export const isDataObjectClass = ( obj ) : obj is DataObjectClass < any > => { return obj ?. factory !== undefined ; } ; /**
 * Runtime check to determine if a class is a SharedObject type
 */ export const isSharedObjectClass = ( obj ) : obj is SharedObjectClass < any > => { return obj ?. getFactory !== undefined ; } ; /**
 * The ContainerSchema consists of initialObjects and dynamicObjectTypes. These types can be
 * of both SharedObject or DataObject. This function seperates the two and returns a registery
 * of DataObject types and an array of SharedObjects.
 */ export const parseDataObjectsFromSharedObjects = ( schema ) : [ NamedFluidDataStoreRegistryEntry [ ] , IChannelFactory [ ] ] => { const registryEntries = new Set ( ) ; const sharedObjects = new Set ( ) ; const tryAddObject = ( obj ) => { if ( isSharedObjectClass ( obj ) ) { sharedObjects . add ( obj . getFactory ( ) ) ; } else if ( isDataObjectClass ( obj ) ) { registryEntries . add ( [ obj . factory . type , Promise . resolve ( obj . factory ) ] ) ; } else { throw new Error ( `Entry is neither a DataObject or a SharedObject` ) ; } } ; Object . values ( schema . initialObjects ) . forEach ( ( obj ) => { tryAddObject ( obj ) ; } ) ; if ( schema . dynamicObjectTypes ) { for ( const obj of schema . dynamicObjectTypes ) { tryAddObject ( obj ) ; } } if ( registryEntries . size === 0 && sharedObjects . size === 0 ) { throw new Error ( "Container cannot be initialized without any DataTypes" ) ; } return [ Array . from ( registryEntries ) , Array . from ( sharedObjects ) ] ; } ;