SharedCell Class
The SharedCell distributed data structure can be used to store a single serializable value.
Signature
export declare class SharedCell<T = any> extends SharedObject<ISharedCellEvents<T>> implements ISharedCell<T>
Extends: SharedObject<ISharedCellEvents
Implements: ISharedCell
Type Parameters
| Parameter | Default | Description |
|---|---|---|
| T | any |
Remarks
### Creation
To create a SharedCell, call the static create method:
const myCell = SharedCell.create(this.runtime, id);
### Usage
The value stored in the cell can be set with the .set() method and retrieved with the .get() method:
myCell.set(3);
console.log(myCell.get()); // 3
The value must only be plain JS objects or SharedObject handles (e.g. to another DDS or Fluid object). In collaborative scenarios, the value is settled with a policy of _last write wins_.
The .delete() method will delete the stored value from the cell:
myCell.delete();
console.log(myCell.get()); // undefined
The .empty() method will check if the value is undefined.
if (myCell.empty()) {
// myCell.get() will return undefined
} else {
// myCell.get() will return a non-undefined value
}
### Eventing
SharedCell is an EventEmitter, and will emit events when other clients make modifications. You should register for these events and respond appropriately as the data is modified. valueChanged will be emitted in response to a set, and delete will be emitted in response to a delete.
Constructors
| Constructor | Description |
|---|---|
| (constructor)(id, runtime, attributes) | Constructs a new shared cell. If the object is non-local an id and service interfaces will be provided |
Static Methods
| Method | Return Type | Description |
|---|---|---|
| create(runtime, id) | SharedCell<any> | Create a new shared cell |
| getFactory() | IChannelFactory | Get a factory for SharedCell to register with the data store. |
Methods
| Method | Return Type | Description |
|---|---|---|
| delete() | void | Delete the value from the cell. |
| empty() | boolean | Checks whether cell is empty or not. |
| get() | Serializable<T> | undefined | Retrieves the cell value. |
| initializeLocalCore() | void | Initialize a local instance of cell |
| loadCore(storage) | Promise<void> | |
| onDisconnect() | void | Call back on disconnect |
| processCore(message, local, localOpMetadata) | void | Process a cell operation |
| set(value) | void | Sets the cell value. |
| summarizeCore(serializer) | ISummaryTreeWithStats | Create a summary for the cell |
Constructor Details
(constructor)
Constructs a new shared cell. If the object is non-local an id and service interfaces will be provided
Signature
constructor(id: string, runtime: IFluidDataStoreRuntime, attributes: IChannelAttributes);
Parameters
| Parameter | Type | Description |
|---|---|---|
| id | string | optional name of the shared map |
| runtime | IFluidDataStoreRuntime | data store runtime the shared map belongs to |
| attributes | IChannelAttributes |
Method Details
create
Create a new shared cell
Signature
static create(runtime: IFluidDataStoreRuntime, id?: string): SharedCell<any>;
Parameters
| Parameter | Modifiers | Type | Description |
|---|---|---|---|
| runtime | IFluidDataStoreRuntime | data store runtime the new shared map belongs to | |
| id | optional | string | optional name of the shared map |
Returns
newly create shared map (but not attached yet)
Return type: SharedCell<any>
delete
Delete the value from the cell.
Signature
delete(): void;
empty
Checks whether cell is empty or not.
Signature
empty(): boolean;
Returns
trueif the value of cell isundefined,falseotherwise
Return type: boolean
get
Retrieves the cell value.
Signature
get(): Serializable<T> | undefined;
Returns
- the value of the cell
Return type: Serializable<T> | undefined
getFactory
Get a factory for SharedCell to register with the data store.
Signature
static getFactory(): IChannelFactory;
Returns
a factory that creates and load SharedCell
Return type: IChannelFactory
initializeLocalCore
Initialize a local instance of cell
Signature
protected initializeLocalCore(): void;
loadCore
Signature
protected loadCore(storage: IChannelStorageService): Promise<void>;
Parameters
| Parameter | Type | Description |
|---|---|---|
| storage | IChannelStorageService |
Returns
Return type: Promise<void>
onDisconnect
Call back on disconnect
Signature
protected onDisconnect(): void;
processCore
Process a cell operation
Signature
protected processCore(message: ISequencedDocumentMessage, local: boolean, localOpMetadata: unknown): void;
Parameters
| Parameter | Type | Description |
|---|---|---|
| message | ISequencedDocumentMessage | the message to prepare |
| local | boolean | whether the message was sent by the local client |
| 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. |
set
Sets the cell value.
Signature
set(value: Serializable<T>): void;
Parameters
| Parameter | Type | Description |
|---|---|---|
| value | Serializable<T> | a JSON-able or SharedObject value to set the cell to |
summarizeCore
Create a summary for the cell
Signature
protected summarizeCore(serializer: IFluidSerializer): ISummaryTreeWithStats;
Parameters
| Parameter | Type | Description |
|---|---|---|
| serializer | IFluidSerializer |
Returns
the summary of the current state of the cell
Return type: ISummaryTreeWithStats