/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ import type { FluidObject, IRequest } from "@fluidframework/core-interfaces"; import type { IFluidHandleContext } from "@fluidframework/core-interfaces/internal"; import { assert } from "@fluidframework/core-utils/internal"; import { responseToException } from "./dataStoreHelpers.js"; import { FluidHandleBase } from "./handles.js"; import { RuntimeHeaders } from "./utils.js"; /** * This handle is used to dynamically load a Fluid object on a remote client and is created on parsing a serialized * FluidObjectHandle. * This class is used to generate an IFluidHandle when de-serializing any all handles (including handles to DDSes, * custom objects) that are stored in SharedObjects. The Data Store or SharedObject corresponding to the * IFluidHandle can be retrieved by calling `get` on it. * * @internal */ export class RemoteFluidObjectHandle extends FluidHandleBase { public readonly isAttached = true; private objectP: Promise | undefined; /** * Creates a new RemoteFluidObjectHandle when parsing an IFluidHandle. * @param absolutePath - The absolute path to the handle from the container runtime. * @param routeContext - The root IFluidHandleContext that has a route to this handle. * @param payloadPending - Whether the handle may have a pending payload that is not yet available. */ public constructor( public readonly absolutePath: string, public readonly routeContext: IFluidHandleContext, public readonly payloadPending: boolean, ) { super(); assert( absolutePath.startsWith("/"), 0x19d /* "Handles should always have absolute paths" */, ); } public async get(): Promise { if (this.objectP === undefined) { // Add `viaHandle` header to distinguish from requests from non-handle paths. const request: IRequest = { url: this.absolutePath, headers: { [RuntimeHeaders.viaHandle]: true, [RuntimeHeaders.payloadPending]: this.payloadPending, }, }; this.objectP = this.routeContext.resolveHandle(request).then((response) => { if (response.mimeType === "fluid/object") { // Responses with mimeType == "fluid/object" are produced by Fluid, and thus they can reasonably be expected to always be a FluidObject const fluidObject: FluidObject = response.value as FluidObject; return fluidObject; } throw responseToException(response, request); }); } return this.objectP; } public attachGraph(): void { return; } }