Skip to main content
Version: v1

SharedMap Class

The SharedMap distributed data structure can be used to store key-value pairs. It provides the same API for setting and retrieving values that JavaScript developers are accustomed to with the Map built-in object. However, the keys of a SharedMap must be strings.

Signature

export declare class SharedMap extends SharedObject<ISharedMapEvents> implements ISharedMap

Extends: SharedObject<ISharedMapEvents

Implements: ISharedMap

Constructors

ConstructorDescription
(constructor)(id, runtime, attributes)Do not call the constructor. Instead, you should use the create method.

Static Methods

MethodReturn TypeDescription
create(runtime, id)SharedMapCreate a new shared map.
getFactory()IChannelFactoryGet a factory for SharedMap to register with the data store.

Properties

PropertyTypeDescription
[Symbol.toStringTag]stringString representation for the class.
sizenumberThe number of key/value pairs stored in the map.

Methods

MethodReturn TypeDescription
[Symbol.iterator]()IterableIterator<[string, any]>Get an iterator over the entries in this map.
clear()voidClear all data from the map.
delete(key)booleanDelete a key from the map.
entries()IterableIterator<[string, any]>Get an iterator over the entries in this map.
forEach(callbackFn)voidExecutes the given callback on each entry in the map.
get(key)T | undefinedRetrieves the given key from the map.
has(key)booleanCheck if a key exists in the map.
keys()IterableIterator<string>Get an iterator over the keys in this map.
set(key, value)thisSets the value stored at key to the provided value.
values()IterableIterator<any>Get an iterator over the values in this map.

Constructor Details

(constructor)

Do not call the constructor. Instead, you should use the create method.

Signature

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

Parameters

ParameterTypeDescription
idstringString identifier.
runtimeIFluidDataStoreRuntimeData store runtime.
attributesIChannelAttributesThe attributes for the map.

Property Details

[Symbol.toStringTag]

String representation for the class.

Signature

readonly [Symbol.toStringTag]: string;

Type: string

size

The number of key/value pairs stored in the map.

Signature

get size(): number;

Type: number

Method Details

[Symbol.iterator]

Get an iterator over the entries in this map.

Signature

[Symbol.iterator](): IterableIterator<[string, any]>;

Returns

The iterator

Return type: IterableIterator<[string, any]>

clear

Clear all data from the map.

Signature

clear(): void;

create

Create a new shared map.

Signature

static create(runtime: IFluidDataStoreRuntime, id?: string): SharedMap;

Example

To create a SharedMap, call the static create method:

const myMap = SharedMap.create(this.runtime, id);

Parameters

ParameterModifiersTypeDescription
runtimeIFluidDataStoreRuntimeThe data store runtime that the new shared map belongs to.
idoptionalstringOptional name of the shared map.

Returns

Newly created shared map.

Return type: SharedMap

delete

Delete a key from the map.

Signature

delete(key: string): boolean;

Parameters

ParameterTypeDescription
keystringKey to delete

Returns

True if the key existed and was deleted, false if it did not exist

Return type: boolean

entries

Get an iterator over the entries in this map.

Signature

entries(): IterableIterator<[string, any]>;

Returns

The iterator

Return type: IterableIterator<[string, any]>

forEach

Executes the given callback on each entry in the map.

Signature

forEach(callbackFn: (value: any, key: string, map: Map<string, any>) => void): void;

Parameters

ParameterTypeDescription
callbackFn(value: any, key: string, map: Map<string, any>) => voidCallback function

get

Retrieves the given key from the map.

Signature

get<T = any>(key: string): T | undefined;
Type Parameters
ParameterDefaultDescription
Tany

Parameters

ParameterTypeDescription
keystringKey to retrieve from

Returns

The stored value, or undefined if the key is not set

Return type: T | undefined

getFactory

Get a factory for SharedMap to register with the data store.

Signature

static getFactory(): IChannelFactory;

Returns

A factory that creates SharedMaps and loads them from storage.

Return type: IChannelFactory

has

Check if a key exists in the map.

Signature

has(key: string): boolean;

Parameters

ParameterTypeDescription
keystringThe key to check

Returns

True if the key exists, false otherwise

Return type: boolean

keys

Get an iterator over the keys in this map.

Signature

keys(): IterableIterator<string>;

Returns

The iterator

Return type: IterableIterator<string>

set

Sets the value stored at key to the provided value.

Signature

set(key: string, value: any): this;

Parameters

ParameterTypeDescription
keystringKey to set at
valueanyValue to set

Returns

The ISharedMap itself

Return type: this

values

Get an iterator over the values in this map.

Signature

values(): IterableIterator<any>;

Returns

The iterator

Return type: IterableIterator<any>