Skip to content 

SharedObject Class

Packages > @fluidframework/shared-object-base > SharedObject

Base class from which all shared objects derive

Signature:

export declare abstract class SharedObject<TEvent extends ISharedObjectEvents = ISharedObjectEvents> extends EventEmitterWithErrorHandling<TEvent> implements ISharedObject<TEvent> 

Extends: EventEmitterWithErrorHandling<TEvent>

Implements: ISharedObject<TEvent>

Constructors

List of constructors for this class
Constructor Modifiers Description
(constructor)(id, runtime, attributes) Constructs a new instance of the SharedObject class

Properties

List of properties for this class
Property Modifiers Type Description
attributes IChannelAttributes
connected boolean Gets the connection state
handle IFluidHandle The handle referring to this SharedObject
IChannel this
id string
IFluidLoadable this
ISharedObject this
logger ITelemetryLogger Telemetry logger for the shared object
runtime IFluidDataStoreRuntime
url string The loadable URL for this SharedObject

Methods

List of methods on this class
Method Modifiers Description
bindToContext() Binds the given shared object to its containing data store runtime, causing it to attach once the runtime attaches.
connect(services) Enables the channel to send and receive ops.
debugAssert(condition, event) Report ignorable errors in code logic or data integrity to the logger. Hosting app / container may want to optimize out these call sites and make them no-op. It may also show assert dialog in non-production builds of application.
didAttach() Allows the distributive data type the ability to perform custom processing once an attach has happened. Also called after non-local data type get loaded.
dirty() Marks this object as dirty so that it is part of the next summary. It is called by a SharedSummaryBlock that want to be part of summary but does not generate ops.
initializeLocal() Initializes the object as a local, non-shared object. This object can become shared after it is attached to the document.
initializeLocalCore() Allows the distributed data type to perform custom local loading.
is(obj) static
isAttached() Returns whether the given shared object is attached to storage.
load(branchId, services) A shared object, after construction, can either be loaded in the case that it is already part of a shared document. Or later attached if it is being newly added.
loadCore(branchId, services) Allows the distributed data type to perform custom loading
newAckBasedPromise(executor) Promises that are waiting for an ack from the server before resolving should use this instead of new Promise. It ensures that if something changes that will interrupt that ack (e.g. the FluidDataStoreRuntime disposes), the Promise will reject. If runtime is disposed when this call is made, executor is not run and promise is rejected right away.
onConnect() Called when the object has fully connected to the delta stream Default implementation for DDS, override if different behavior is required.
onDisconnect() Called when the object has disconnected from the delta stream.
processCore(message, local, localOpMetadata) Derived classes must override this to do custom processing on a remote message.
registerCore() Allows the distributed data type the ability to perform custom processing once an attach has happened.
reSubmitCore(content, localOpMetadata) Called when a message has to be resubmitted. This typically happens after a reconnection for unacked messages. The default implementation here is to resubmit the same message. The client can override if different behavior is required. It can choose to resubmit the same message, submit different / multiple messages or not submit anything at all.
setOwner() Set the owner of the object if it is an OwnedSharedObject
snapshot() Gets a form of the object that can be serialized.
submitLocalMessage(content, localOpMetadata) Submits a message by the local client to the runtime.
toJSON() Not supported - use handles instead

Constructors

SharedObject.(constructor)

Constructs a new instance of the SharedObject class

Signature:

constructor(id: string, runtime: IFluidDataStoreRuntime, attributes: IChannelAttributes);

Parameters

List of parameters
Parameter Type Description
id string The id of the shared object
runtime IFluidDataStoreRuntime The IFluidDataStoreRuntime which contains the shared object
attributes IChannelAttributes Attributes of the shared object

Properties

attributes

Signature:

readonly attributes: IChannelAttributes;

connected

Gets the connection state

Signature:

get connected(): boolean;

handle

The handle referring to this SharedObject

Signature:

readonly handle: IFluidHandle;

IChannel

Signature:

get IChannel(): this;

id

Signature:

id: string;

IFluidLoadable

Signature:

get IFluidLoadable(): this;

ISharedObject

Signature:

get ISharedObject(): this;

logger

Telemetry logger for the shared object

Signature:

protected readonly logger: ITelemetryLogger;

runtime

Signature:

protected runtime: IFluidDataStoreRuntime;

url

The loadable URL for this SharedObject

Signature:

get url(): string;

Methods

bindToContext

Binds the given shared object to its containing data store runtime, causing it to attach once the runtime attaches.

Signature:

bindToContext(): void;

Returns:

void

connect

Enables the channel to send and receive ops.

Signature:

connect(services: IChannelServices): void;

Parameters

List of parameters
Parameter Type Description
services IChannelServices Services to connect to

Returns:

void

debugAssert

Report ignorable errors in code logic or data integrity to the logger. Hosting app / container may want to optimize out these call sites and make them no-op. It may also show assert dialog in non-production builds of application.

Signature:

protected debugAssert(condition: boolean, event: ITelemetryErrorEvent): void;

Parameters

List of parameters
Parameter Type Description
condition boolean If false, assert is logged
event ITelemetryErrorEvent

Returns:

void

didAttach

Allows the distributive data type the ability to perform custom processing once an attach has happened. Also called after non-local data type get loaded.

Signature:

protected didAttach(): void;

Returns:

void

dirty

Marks this object as dirty so that it is part of the next summary. It is called by a SharedSummaryBlock that want to be part of summary but does not generate ops.

Signature:

protected dirty(): void;

Returns:

void

initializeLocal

Initializes the object as a local, non-shared object. This object can become shared after it is attached to the document.

Signature:

initializeLocal(): void;

Returns:

void

initializeLocalCore

Allows the distributed data type to perform custom local loading.

Signature:

protected initializeLocalCore(): void;

Returns:

void

is

Signature:

static is(obj: any): obj is SharedObject;

Parameters

List of parameters
Parameter Type Description
obj any The thing to check if it is a SharedObject

Returns:

obj is SharedObject

Returns true if the thing is a SharedObject

isAttached

Returns whether the given shared object is attached to storage.

Signature:

isAttached(): boolean;

Returns:

boolean

True if the given shared object is attached

load

A shared object, after construction, can either be loaded in the case that it is already part of a shared document. Or later attached if it is being newly added.

Signature:

load(branchId: string | undefined, services: IChannelServices): Promise<void>;

Parameters

List of parameters
Parameter Type Description
branchId string | undefined Branch ID
services IChannelServices Services used by the shared object

Returns:

Promise<void>

loadCore

Allows the distributed data type to perform custom loading

Signature:

protected abstract loadCore(branchId: string | undefined, services: IChannelStorageService): Promise<void>;

Parameters

List of parameters
Parameter Type Description
branchId string | undefined Branch ID
services IChannelStorageService Storage used by the shared object

Returns:

Promise<void>

newAckBasedPromise

Promises that are waiting for an ack from the server before resolving should use this instead of new Promise. It ensures that if something changes that will interrupt that ack (e.g. the FluidDataStoreRuntime disposes), the Promise will reject. If runtime is disposed when this call is made, executor is not run and promise is rejected right away.

Signature:

protected newAckBasedPromise<T>(executor: (resolve: (value?: T | PromiseLike<T> | undefined) => void, reject: (reason?: any) => void) => void): Promise<T>;

Parameters

List of parameters
Parameter Type Description
executor (resolve: (value?: T | PromiseLike | undefined) => void, reject: (reason?: any) => void) => void

Returns:

Promise<T>

onConnect

Called when the object has fully connected to the delta stream Default implementation for DDS, override if different behavior is required.

Signature:

protected onConnect(): void;

Returns:

void

onDisconnect

Called when the object has disconnected from the delta stream.

Signature:

protected abstract onDisconnect(): any;

Returns:

any

processCore

Derived classes must override this to do custom processing on a remote message.

Signature:

protected abstract processCore(message: ISequencedDocumentMessage, local: boolean, localOpMetadata: unknown): any;

Parameters

List of parameters
Parameter Type Description
message ISequencedDocumentMessage The message to process
local boolean True if the shared object is local
localOpMetadata unknown For local client messages, this is the metadata that was submitted with the message. For messages from a remote client, this will be undefined.

Returns:

any

registerCore

Allows the distributed data type the ability to perform custom processing once an attach has happened.

Signature:

protected abstract registerCore(): any;

Returns:

any

reSubmitCore

Called when a message has to be resubmitted. This typically happens after a reconnection for unacked messages. The default implementation here is to resubmit the same message. The client can override if different behavior is required. It can choose to resubmit the same message, submit different / multiple messages or not submit anything at all.

Signature:

protected reSubmitCore(content: any, localOpMetadata: unknown): void;

Parameters

List of parameters
Parameter Type Description
content any The content of the original message.
localOpMetadata unknown The local metadata associated with the original message.

Returns:

void

setOwner

Set the owner of the object if it is an OwnedSharedObject

Signature:

protected setOwner(): string | undefined;

Returns:

string | undefined

The owner of the object if it is an OwnedSharedObject, otherwise undefined

snapshot

Gets a form of the object that can be serialized.

Signature:

abstract snapshot(): ITree;

Returns:

ITree

A tree representing the snapshot of the shared object

submitLocalMessage

Submits a message by the local client to the runtime.

Signature:

protected submitLocalMessage(content: any, localOpMetadata?: unknown): void;

Parameters

List of parameters
Parameter Type Description
content any Content of the message
localOpMetadata unknown The local metadata associated with the message. This is kept locally by the runtime and not sent to the server. This will be sent back when this message is received back from the server. This is also sent if we are asked to resubmit the message.

Returns:

void

toJSON

Not supported - use handles instead

Signature:

toJSON(): void;

Returns:

void