Members
length
Number of elements present
size
Maximum number of elements allowed
Methods
clear() → {void}
Clear the SharedMap
Returns:
- Type
- void
delete(key, optopt) → {void}
Delete an element, fully thread-safe, acquires an exlusive lock and it is very expensive
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
string | ||
opt |
SharedMapOptions |
<optional> |
options, { lockExclusive: true } if manually calling lockExlusive |
Throws:
-
-
when the key does not exit
- Type
- RangeError
-
-
-
when calling map.delete(key, value, { lockWrite: true, lockExclusive: false })
- Type
- Error
-
Returns:
- Type
- void
get(key, optopt) → {string|undefined}
Get an element, fully thread-safe, multiple get/set can execute in parallel
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
string | ||
opt |
SharedMapOptions |
<optional> |
options, { lockWrite: true } if manually calling lockWrite |
Returns:
- Type
- string | undefined
has(key, optopt) → {boolean}
Find an element, fully thread-safe, identical to get(key) !== undefined
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
string | ||
opt |
SharedMapOptions |
<optional> |
options, { lockWrite: true } if manually calling lockWrite |
Returns:
- Type
- boolean
(generator) keys(optopt) → {Iterable}
A generator that can be used to iterate over the keys, thread-safe but allows
additions and deletions during the iteration
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
opt |
SharedMapOptions |
<optional> |
options, { lockWrite: true } if manually calling lockWrite |
Returns:
- Type
- Iterable
lockExclusive() → {void}
Acquire an exclusive lock,
All operations that need it, automatically acquire it,
Use only if you need to block all other threads from accessing the map;
The thread holding the lock can then call map.set(k, v, {lockHeld: true})
Returns:
- Type
- void
lockWrite() → {void}
Acquire a write lock,
All operations that need it, automatically acquire it,
Use only if you need to block all other threads from writing to the map,
The thread holding the lock can then call map.set(k, v, {lockHeld: true})
Returns:
- Type
- void
Example
myMap.lockWrite();
for (let k of myMap.keys({lockWrite: true}))
myMap.set(k,
myMap.get(k, {lockWrite: true}).toUpperCase(),
{lockWrite: true});
myMap.unlockWrite();
map(cb, thisArgopt) → {Array}
A thread-safe map(). Doesn't block additions or deletions
between two calls of the callback,
all map operations are guaranteed atomic,
map.get(index)=currentValue is guaranteed while the callback runs,
You shall not manipulate the map in the callback, use an explicitly-locked
keys() in this case (look at the example for lockWrite)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
cb |
mapCallback | callback | |
thisArg |
* |
<optional> |
callback will have its this set to thisArg |
Returns:
- Type
- Array
reduce(cb, initialValue) → {*}
A thread-safe reduce(). Doesn't block additions or deletions
between two calls of the callback,
map.get(key)=currentValue is guaranteed while the callback runs,
You shall not manipulate the map in the callback, use an explicitly-locked
keys() in this case (look at the example for lockWrite)
Parameters:
Name | Type | Description |
---|---|---|
cb |
reduceCallback | callback |
initialValue |
* | initial value of the accumulator |
Returns:
- Type
- *
set(key, value, optopt) → {void}
Add/replace an element, fully thread-safe, multiple get/set can execute in parallel
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
string | ||
value |
string | number | ||
opt |
SharedMapOptions |
<optional> |
options, { lockWrite: true } if manually calling lockWrite |
Throws:
-
-
when the map is full
- Type
- RangeError
-
-
-
when the input values do not fit
- Type
- RangeError
-
-
-
when the input values are of a wrong type
- Type
- TypeError
-
Returns:
- Type
- void
unlockExclusive() → {void}
Release the exclusive lock
Returns:
- Type
- void
unlockWrite() → {void}
Release the write lock
Returns:
- Type
- void
Type Definitions
mapCallback(currentValue, keyopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
currentValue |
string | ||
key |
string |
<optional> |
reduceCallback(accumulator, currentValue, keyopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
accumulator |
|||
currentValue |
string | ||
key |
string |
<optional> |
SharedMapOptions
Type:
- object
Properties:
Name | Type | Description |
---|---|---|
lockWrite |
boolean | Already holding write lock, useful when manually locking with lockWrite |
lockExclusive |
boolean | Already holding exclusive lock, useful when manually locking with lockExclusive |