/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ import type { ILayerCompatDetails, IProvideLayerCompatDetails, } from "@fluid-internal/client-utils"; import type { AttachState, IAudience, ICriticalContainerError, } from "@fluidframework/container-definitions"; import type { IBatchMessage, IContainerContext, IContainerContextInternal, ILoader, ILoaderOptions, IDeltaManager, IContainerStorageService, } from "@fluidframework/container-definitions/internal"; import type { FluidObject } from "@fluidframework/core-interfaces"; import type { ISignalEnvelope } from "@fluidframework/core-interfaces/internal"; import type { IClientDetails, IQuorumClients } from "@fluidframework/driver-definitions"; import type { ISnapshot, IDocumentMessage, ISnapshotTree, ISummaryContent, IVersion, MessageType, ISequencedDocumentMessage, } from "@fluidframework/driver-definitions/internal"; import type { TelemetryLoggerExt } from "@fluidframework/telemetry-utils/internal"; import type { ConnectionState } from "./connectionState.js"; import { loaderCompatDetailsForRuntime } from "./loaderLayerCompatState.js"; /** * Configuration object for ContainerContext constructor. * * @remarks * A large subset of properties are from {@link IContainerContext}. Select * properties are explicitly omitted so that, as {@link ContainerContext} is * extended, newly added properties are not overlooked (but may also be * explicitly omitted here by adding to the list). */ export interface IContainerContextConfig extends Readonly< Required< Omit< IContainerContext, | "clientId" | "connected" | "attachState" | "getLoadedFromVersion" | "supportedFeatures" | "id" | "snapshotWithContents" > > > { // This overrides IContainerContext.options with specific type. readonly options: ILoaderOptions; readonly version: IVersion | undefined; readonly getContainerDiagnosticId: () => string | undefined; readonly getClientId: () => string | undefined; readonly getAttachState: () => AttachState; readonly getConnected: () => boolean; readonly existing: boolean; readonly taggedLogger: TelemetryLoggerExt; // This "overrides" IContainerContext.snapshotWithContents to be required but allow `undefined`. readonly snapshotWithContents: IContainerContext["snapshotWithContents"] | undefined; // fetchOps is an internal-only capability (IContainerContextInternal), not part of the public // IContainerContext contract. Optional: hosts may not provide op reading. readonly fetchOps: IContainerContextInternal["fetchOps"]; // Allows the runtime to report op-only dirty state separately from host-facing aggregate dirty state. readonly updatePendingOpState: NonNullable< IContainerContextInternal["updatePendingOpState"] >; } /** * {@inheritDoc @fluidframework/container-definitions#IContainerContext} */ export class ContainerContext implements Required>, Pick, Pick, IProvideLayerCompatDetails { /** * @deprecated This has been replaced by ILayerCompatDetails. */ public readonly supportedFeatures: ReadonlyMap = new Map([ /** * This version of the loader accepts `referenceSequenceNumber`, provided by the container runtime, * as a parameter to the `submitBatchFn` and `submitSummaryFn` functions. * This is then used to set the reference sequence numbers of the submitted ops in the DeltaManager. */ ["referenceSequenceNumbers", true], ]); public readonly options: ILoaderOptions; public readonly scope: FluidObject; public readonly baseSnapshot: ISnapshotTree | undefined; public readonly deltaManager: IDeltaManager; public readonly storage: IContainerStorageService; public readonly quorum: IQuorumClients; public readonly audience: IAudience; public readonly signalAudience: IAudience; public readonly loader: ILoader; public readonly submitFn: ( type: MessageType, contents: unknown, batch: boolean, appData: unknown, ) => number; public readonly submitSummaryFn: ( summaryOp: ISummaryContent, referenceSequenceNumber?: number, ) => number; /** * @returns clientSequenceNumber of last message in a batch */ public readonly submitBatchFn: ( batch: IBatchMessage[], referenceSequenceNumber?: number, ) => number; /** * `unknown` should be removed once `@alpha` tag is removed from IContainerContext * @see {@link https://dev.azure.com/fluidframework/internal/_workitems/edit/7462} * Any changes to submitSignalFn `content` should be checked internally by temporarily changing IContainerContext and removing all `unknown`s */ public readonly submitSignalFn: ( content: unknown | ISignalEnvelope, targetClientId?: string, ) => void; public readonly fetchOps: IContainerContextInternal["fetchOps"]; public readonly updatePendingOpState: NonNullable< IContainerContextInternal["updatePendingOpState"] >; public readonly disposeFn: (error?: ICriticalContainerError) => void; public readonly closeFn: (error?: ICriticalContainerError) => void; public readonly updateDirtyContainerState: (dirty: boolean) => void; public readonly getAbsoluteUrl: (relativeUrl: string) => Promise; public readonly clientDetails: IClientDetails; public readonly existing: boolean; public readonly taggedLogger: TelemetryLoggerExt; public readonly pendingLocalState: unknown; public readonly snapshotWithContents?: ISnapshot; public readonly getConnectionState: () => ConnectionState; private readonly _getClientId: () => string | undefined; private readonly _getContainerDiagnosticId: () => string | undefined; private readonly _getConnected: () => boolean; private readonly _getAttachState: () => AttachState; private readonly version: IVersion | undefined; public get clientId(): string | undefined { return this._getClientId(); } /** * DISCLAIMER: this id is only for telemetry purposes. Not suitable for any other usages. */ public get id(): string { return this._getContainerDiagnosticId() ?? ""; } /** * When true, ops are free to flow * When false, ops should be kept as pending or rejected */ public get connected(): boolean { return this._getConnected(); } /** * The compatibility details of the Loader layer that is exposed to the Runtime layer * for validating Runtime-Loader compatibility. */ public get ILayerCompatDetails(): ILayerCompatDetails { return loaderCompatDetailsForRuntime; } constructor(config: IContainerContextConfig) { this.options = config.options; this.scope = config.scope; this.baseSnapshot = config.baseSnapshot; this.deltaManager = config.deltaManager; this.storage = config.storage; this.quorum = config.quorum; this.audience = config.audience; this.signalAudience = config.signalAudience; this.loader = config.loader; this.submitFn = config.submitFn; this.submitSummaryFn = config.submitSummaryFn; this.submitBatchFn = config.submitBatchFn; this.submitSignalFn = config.submitSignalFn; this.fetchOps = config.fetchOps; this.updatePendingOpState = config.updatePendingOpState; this.disposeFn = config.disposeFn; this.closeFn = config.closeFn; this.updateDirtyContainerState = config.updateDirtyContainerState; this.getAbsoluteUrl = config.getAbsoluteUrl; this.clientDetails = config.clientDetails; this.existing = config.existing; this.taggedLogger = config.taggedLogger; this.pendingLocalState = config.pendingLocalState; if (config.snapshotWithContents !== undefined) { this.snapshotWithContents = config.snapshotWithContents; } this.getConnectionState = config.getConnectionState; this._getClientId = config.getClientId; this._getContainerDiagnosticId = config.getContainerDiagnosticId; this._getConnected = config.getConnected; this._getAttachState = config.getAttachState; this.version = config.version; } public getLoadedFromVersion(): IVersion | undefined { return this.version; } public get attachState(): AttachState { return this._getAttachState(); } }