Packages > @fluidframework/map > SharedMap

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

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

Static Methods

Method Return Type Description
create(runtime, id) SharedMap Create a new shared map.
getFactory() IChannelFactory Get a factory for SharedMap to register with the data store.

Properties

Property Type Description
[Symbol.toStringTag] string String representation for the class.
size number The number of key/value pairs stored in the map.

Methods

Method Return Type Description
[Symbol.iterator]() IterableIterator<[string, any]> Get an iterator over the entries in this map.
clear() void Clear all data from the map.
delete(key) boolean Delete a key from the map.
entries() IterableIterator<[string, any]> Get an iterator over the entries in this map.
forEach(callbackFn) void Executes the given callback on each entry in the map.
get(key) T | undefined Retrieves the given key from the map.
has(key) boolean Check if a key exists in the map.
keys() IterableIterator<string> Get an iterator over the keys in this map.
set(key, value) this Sets 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

Parameter Type Description
id string String identifier.
runtime IFluidDataStoreRuntime Data store runtime.
attributes IChannelAttributes The 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

Parameter Modifiers Type Description
runtime IFluidDataStoreRuntime The data store runtime that the new shared map belongs to.
id optional string Optional 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

Parameter Type Description
key string Key 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

Parameter Type Description
callbackFn (value: any, key: string, map: Map<string, any>) => void Callback function

get

Retrieves the given key from the map.

Signature

get<T = any>(key: string): T | undefined;
Type Parameters
Parameter Default Description
T any

Parameters

Parameter Type Description
key string Key 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

Parameter Type Description
key string The 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

Parameter Type Description
key string Key to set at
value any Value 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>