/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ import type { IFluidLoadable, FluidObject } from "@fluidframework/core-interfaces"; import { type ISharedObjectRegistry, FluidDataStoreRuntime, } from "@fluidframework/datastore/internal"; import type { IFluidDataStoreRuntime } from "@fluidframework/datastore-definitions/internal"; import { type Registry, type DataStoreKind, lookupInRegistry, } from "@fluidframework/driver-definitions/internal"; import type { IFluidDataStoreContext, IFluidDataStoreChannel, } from "@fluidframework/runtime-definitions/internal"; import { DataStoreKindImplementation } from "@fluidframework/runtime-utils/internal"; import { UsageError } from "@fluidframework/telemetry-utils/internal"; import type { ISharedObjectKind, SharedObjectKey, SharedObjectKindAlpha, } from "./sharedObject.js"; import type { ISharedObject } from "./types.js"; /** * This file provides the implementation of {@link @fluidframework/driver-definitions#DataStoreKind} * and is therefore a key part of the implementation of the {@link @fluidframework/driver-definitions#ServiceClient} API surface. * * Note that much of the API surface beyond {@link defineDataStore} could be removed/deduplicated * if the unification noted in the TODOs in {@link SharedObjectRegistry} is implemented. */ /** * A {@link @fluidframework/driver-definitions#Registry} of shared object kinds that can be created or loaded within a data store. * @remarks * Supports lazy code loading in a limited way (a single lazy load per registry). * @privateRemarks * TODO: The framework provided SharedObjects should be exposed in a way indistinguishable from custom sub-data stores. * This can be done by unifying the DataStoreKind and SharedObjectKindAlpha types. * For now, this would mean having DataStoreKind extend SharedObjectKindAlpha, since we can allow a data store in all places SharedObjects are allowed, * but do not allow SharedObjects at the root. * Fixing this, and allowing shared objects at the root (maybe use a trivial wrapper data store) could simplify things, allowing data stores and Containers to share some types (like how they create detached contents, have registries, have a root etc). * * Part of this unification could be to relax the output from the factories / registries. Allowing the output to be an arbitrary type, which might be a promise, and might not be one could help. * Removal of the IFluidLoadable requirement, and allowing the returned type to expose handles to itself however it wants (or not at all) might be viable and simplify typing and allow for strongly typed handles at creation time at least). * Maybe when Registry(type) gives a promise, it could instead give a factory which outputs a promise wrapped type? The check that the provided creation key is valid for that factory can be deferred until the promise resolves. * * Idea: creation key can have an interface that subsets the factory / SharedObjectKindAlpha / DataStoreKind so they can be used, or some branded key (string, and/or object with stronger identity what knows the type string) can be used. * Have Key interface contain validation function to check that the factory used (or maybe the value produced from it) is valid for that key. * During load, validation would simply check the factory's type string matches the key's type string. * When using SharedObjectKindAlpha or DataStoreKind, validation can check factory object identity against key. * * Goal: Mostly unify container, data store and shared object abstractions. * Maybe unify a bit with service client since it also has a way to create detached things with an initialized root then attach them. * SharedObjects are just built in leaf data stores. * * @input * @alpha */ export type SharedObjectRegistry = () => Promise< Registry> >; /** * Creates a {@link SharedObjectRegistry} from an iterable of {@link SharedObjectKindAlpha}s or async getters for them. * @alpha */ export function sharedObjectRegistryFromIterable( entries: Iterable< | SharedObjectKindAlpha | { type: string; kind: () => Promise> } >, ): SharedObjectRegistry { return async () => { const map = new Map>(); for (const entry of entries) { if ("kind" in entry) { map.set(entry.type, await entry.kind()); } else { map.set(asSharedObjectKind(entry).getFactory().type, entry); } } return (type: string) => { const entry = map.get(type); if (entry === undefined) { throw new UsageError(`Unknown shared object type: ${type}`); } return entry; }; }; } /** * Options which define how to construct a particular {@link @fluidframework/driver-definitions#DataStoreKind}. * @remarks * Use {@link defineDataStore} to create a {@link @fluidframework/driver-definitions#DataStoreKind} from these options. * @input * @alpha */ export interface DataStoreOptions { /** * The type identifier for the data object factory. * @remarks * Persisted identifier which specifies which {@link @fluidframework/driver-definitions#DataStoreKind} to use when loading it. * @privateRemarks * Equivalent to `DataObjectFactoryProps.type`. */ readonly type: string; /** * The registry of shared object kinds (including other data stores) that can be loaded or created within this data store. * * TODO: actually allow this to contain data stores. */ readonly registry: SharedObjectRegistry; /** * Create the initial content of the data store, and return the root shared object. * @privateRemarks * TODO: * This requires the caller to produce a single root shared object (which is keyed by {@link rootSharedObjectId}). * This should be fine for new code, but code migrated from legacy APIs might need more flexibility. * Such use-cases could be accommodated providing a legacy alternative to `defineDataStore` where `instantiateFirstTime` and `view` directly expose access to named root shared objects. * This should be easy to implement, but is currently not included. */ instantiateFirstTime( rootCreator: SharedObjectCreator, context: DataStoreContext, ): Promise; /** * Construct a view of the data store's root shared object. * * @param root - The root shared object of the data store, created by `instantiateFirstTime` (though possibly created by another client and loaded by this one). * @param context - A {@link DataStoreContext} that can be used to create additional shared objects. */ view(root: TRoot, context: DataStoreContext): Promise; } /** * Creates a {@link @fluidframework/driver-definitions#DataStoreKind} from {@link DataStoreOptions}. * @alpha */ export function defineDataStore( options: DataStoreOptions, ): DataStoreKind { return new DataStoreKindImplementation({ type: options.type, async instantiateDataStore( context: IFluidDataStoreContext, existing: boolean, ): Promise { return createDataStore(context, existing, options); }, }); } /** * Casts a {@link SharedObjectKindAlpha} to its encapsulated {@link ISharedObjectKind} view. * * @remarks * {@link SharedObjectKindAlpha} is a sealed type, * so we can assume it implements {@link ISharedObjectKind} and down cast to it. */ function asSharedObjectKind( kind: SharedObjectKindAlpha, ): ISharedObjectKind { const candidate = kind as unknown as Partial>; // Sanity check to help catch misuse since SharedObjectKindAlpha is typed structurally and seems implementable. if (typeof candidate.getFactory !== "function" || typeof candidate.create !== "function") { throw new UsageError( "Invalid SharedObjectKindAlpha: this type is sealed and may not have custom implementations.", ); } return candidate as ISharedObjectKind; } function convertRegistry( lookup: Registry>, ): ISharedObjectRegistry { return { get: (type: string) => { const entry = lookup(type); return asSharedObjectKind(entry).getFactory(); }, }; } /** * Data stores keep their shared objects inside channels which get names. * This is the name of the channel which we conventionally use for the root shared object of a data store in most cases. * @remarks * There can be other named channels, or the root could use a different name, but we are trying to migrate away from such patterns. * Currently the DataStoreKind pattern used in this file follows and requires this convention, * but we may relax that in the future for interop with legacy data if necessary. */ const rootSharedObjectId = "root"; /** * Creates a data store channel. * @param context - The data store context. * @param existing - Whether the data store already exists. When true, this loads it, when false a new one is created. * @param options - The data store options. * @returns A promise that resolves to the created data store channel. * @remarks * Currently this is limited to data stores which have a single named channel, * which must have the {@link rootSharedObjectId} and is attached at creation time. * This limitation may be relaxed in the future. */ async function createDataStore( context: IFluidDataStoreContext, existing: boolean, options: DataStoreOptions, ): Promise { const sharedObjectRegistry = await options.registry(); const runtime: FluidDataStoreRuntime = new FluidDataStoreRuntime( context, convertRegistry(sharedObjectRegistry), existing, async (runtimeInner: IFluidDataStoreRuntime) => { const innerContext: DataStoreContext = { async createSharedObject( key: SharedObjectKey, ): Promise { const kind = lookupInRegistry(sharedObjectRegistry, key); // Create detached channel. return asSharedObjectKind(kind).create(runtimeInner); }, }; let createdRoot: TRoot | undefined; const rootCreator: SharedObjectCreator = { async createSharedObject(key: SharedObjectKey): Promise { // Create named channel under the root id. // Error if called twice. if (createdRoot !== undefined) { throw new UsageError("Root shared object already created"); } const kind = lookupInRegistry(sharedObjectRegistry, key); const result = asSharedObjectKind(kind).create(runtimeInner, rootSharedObjectId); // Every shared object is also an ISharedObject; const rootSharedObject = result as IFluidLoadable as ISharedObject; // bind the newly created root so it becomes part of this data store. rootSharedObject.bindToContext(); createdRoot = result; return result; }, }; let root: TRoot | undefined; if (existing) { // getChannel returns the type-erased IChannel; the registered root kind guarantees it is a TRoot. root = (await runtimeInner.getChannel(rootSharedObjectId)) as IFluidLoadable as TRoot; } else { root = await options.instantiateFirstTime(rootCreator, innerContext); if (root !== createdRoot) { throw new UsageError( "instantiateFirstTime did not return root created with rootCreator", ); } } // view returns the data store's output type; the runtime only needs it as an opaque FluidObject entry point. return (await options.view(root, innerContext)) as unknown as FluidObject; }, ); return runtime; } /** * Creates instances of SharedObjectKinds. * @privateRemarks * See IFluidContainer.create. * @sealed * @alpha */ export interface SharedObjectCreator { /** * Create an instance of `kind`, which must be registered in the registry of the surrounding data store. */ createSharedObject(kind: SharedObjectKey): Promise; } /** * Contextual information about a data store which is provided when instantiating or loading it. * @privateRemarks * TODO: this can expose more contextual information about the data store as needed. * @sealed * @alpha */ export interface DataStoreContext extends SharedObjectCreator {}