SharedMap Class

Packages > fluid-framework > SharedMap

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: ISharedMapEvents

Implements: ISharedMap

Constructors

List of constructors for this class
Constructor Modifiers Description
(constructor)(id, runtime, attributes) Do not call the constructor. Instead, you should use the create method.

Properties

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

Methods

List of methods on this class
Method Modifiers Description
[Symbol.iterator]() Get an iterator over the entries in this map.
clear() Clear all data from the map.
create(runtime, id) static Create a new shared map.
delete(key) Delete a key from the map.
entries() Get an iterator over the entries in this map.
forEach(callbackFn) Executes the given callback on each entry in the map.
get(key) Retrieves the given key from the map.
getFactory() static Get a factory for SharedMap to register with the data store.
has(key) Check if a key exists in the map.
keys() Get an iterator over the keys in this map.
set(key, value) Sets the value stored at key to the provided value.
values() Get an iterator over the values in this map.

Constructors

SharedMap.(constructor)

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

Signature:

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

Parameters

List of parameters
Parameter Type Description
id String identifier.
runtime Data store runtime.
attributes The attributes for the map.

Properties

[Symbol.toStringTag]

String representation for the class.

Signature:

readonly [Symbol.toStringTag]: string;

size

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

Signature:

get size(): number;

Methods

[Symbol.iterator]

Get an iterator over the entries in this map.

Signature:

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

Returns:

The iterator

clear

Clear all data from the map.

Signature:

clear(): void;

Returns:

create

Create a new shared map.

Signature:

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

Parameters

List of parameters
Parameter Type Description
runtime The data store runtime that the new shared map belongs to.
id Optional name of the shared map.

Returns:

SharedMap

Newly created shared map.

Example

To create a SharedMap, call the static create method:

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

delete

Delete a key from the map.

Signature:

delete(key: string): boolean;

Parameters

List of parameters
Parameter Type Description
key Key to delete

Returns:

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

entries

Get an iterator over the entries in this map.

Signature:

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

Returns:

The iterator

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

List of parameters
Parameter Type Description
callbackFn Callback function

Returns:

get

Retrieves the given key from the map.

Signature:

get<T = any>(key: string): T | undefined;

Parameters

List of parameters
Parameter Type Description
key Key to retrieve from

Returns:

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

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.

has

Check if a key exists in the map.

Signature:

has(key: string): boolean;

Parameters

List of parameters
Parameter Type Description
key The key to check

Returns:

True if the key exists, false otherwise

keys

Get an iterator over the keys in this map.

Signature:

keys(): IterableIterator<string>;

Returns:

The iterator

set

Sets the value stored at key to the provided value.

Signature:

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

Parameters

List of parameters
Parameter Type Description
key Key to set at
value Value to set

Returns:

The ISharedMap itself

values

Get an iterator over the values in this map.

Signature:

values(): IterableIterator<any>;

Returns:

The iterator