Skip to content

SmartMap

The SmartMap class provides a simple way to cache the values of an arbitrary mapping function. This is useful when there is some expensive operation that is regularly performed on a specific set of values and caching the result gives a significant performance improvement. There is also a mechanism to invalidate the whole cache, as well as just specific keys.

Example

lua
local MyModule = select(2, ...).MyModule
local SmartMap = MyModule:From("LibTSMUtil"):IncludeClassType("SmartMap")

local function LookupValue(key)
   -- Assume this is an expensive and non-trivial operation
   return strupper(key)
end

local map = SmartMap.New("string", "string", LookupValue)

local reader = map:CreateReader(function(_, pendingChanges)
   for key, prevValue in pairs(pendingChanges) do print("CHANGE", key, prevValue) end
end)
print(reader["a"]) -- A
print(reader["b"]) -- B

-- Invalidate the mapping for just one key
map:ValueChanged("a")
-- CHANGE   a   A

-- Invalidate the entire map if the underlying operation changes significantly
map:Invalidate()
-- CHANGE   b   B

Memory Management

Both SmartMap and SmartMapReader objects are intended to never be GC'd and have a static lifecycle (i.e. one that's equal to the lifecycle of the application).

API

Inherits: Class

Methods

CreateReader

lua
function SmartMap:CreateReader(callback)
  -> SmartMapReader

Creates a new reader.

Parameters:

NameTypeDescription
callbackfun(reader: SmartMapReader<K, V>, pendingChanges: table)The function to call when a value within the map changes

Returns:

TypeDescription
SmartMapReader

GetKeyType

lua
function SmartMap:GetKeyType()
  -> <K:"string" | "number">

Gets the type of the smart map's keys.

Returns:

TypeDescription
<K:"string" | "number">

GetValueType

lua
function SmartMap:GetValueType()
  -> <V:"string" | "number" | "boolean">

Gets the type of the smart map's values.

Returns:

TypeDescription
<V:"string" | "number" | "boolean">

Invalidate

lua
function SmartMap:Invalidate()

Invalidates all data in the smart map.

Iterator

lua
function SmartMap:Iterator()
  -> fun():K, V, table

Iterates over all data in the smart map.

Returns:

TypeDescription
fun():K, VIterator with fields: key, value
table

SetCallbacksPaused

lua
function SmartMap:SetCallbacksPaused(paused)

Pausese or unpauses reader callbacks.

Parameters:

NameTypeDescription
pausedbooleanWhether or not callbacks are paused

ValueChanged

lua
function SmartMap:ValueChanged(key)

Called when the value has changed for a given key to fetch the new one and notify the readers.

Parameters:

NameTypeDescription
keyKThe key which changed

_Get

lua
function SmartMap:_Get(key)

Parameters:

NameTypeDescription
key``

__init

lua
function SmartMap:__init(keyType, valueType, lookupFunc)

Parameters:

NameTypeDescription
keyType<K>
valueType<V>
lookupFuncfun(key: K): VA function which looks up the value for a specific key

Functions

New

lua
function SmartMap.New(keyType, valueType, lookupFunc)
  -> SmartMap

Create a new smart map object.

Parameters:

NameTypeDescription
keyType<K>The type of the keys
valueType<V>The type of the values
lookupFuncfun(key: K): VA function which looks up the value for a specific key

Returns:

TypeDescription
SmartMap