MemoryStorage
The MemoryStorage class is designed with reference to the Web Storage API's Storage interface.
Custom Model scripts can use MemoryStorage to communicate across Views. The system's unique instance is accessible as globalThis.memoryStorage.
Notes:
- Data is stored in HMI or Viewer memory. It is cleared on HMI reboot or Viewer close.
- The HMI and Viewer have independent MemoryStorage instances.
- Maximum data limit per instance: 2 MB.
Properties
| Property | Type | Description |
|---|---|---|
length | Number | The number of data items stored. Read-only. |
Methods
key(index)
Returns the name of the nth key.
Parameters
| Name | Type | Description |
|---|---|---|
index | Number | Zero-based index of the key. |
Returns: String — The key name, or null if the index does not exist.
Example
js
for (let i = 0; i < globalThis.memoryStorage.length; i++) {
console.log(globalThis.memoryStorage.key(i));
}getItem(keyName)
Returns the value of the specified key.
Parameters
| Name | Type | Description |
|---|---|---|
keyName | String | The name of the key to retrieve. |
Returns: String — The value, or null if the key does not exist.
Example
js
const currentFont = globalThis.memoryStorage.getItem("currentFont");setItem(keyName, keyValue)
Creates or updates a key-value pair.
Parameters
| Name | Type | Description |
|---|---|---|
keyName | String | The key to create or update. |
keyValue | String | The value to store. |
Example
js
globalThis.memoryStorage.setItem("currentFont", "Helvetica");removeItem(keyName)
Removes the specified key. Does nothing if the key does not exist.
Parameters
| Name | Type | Description |
|---|---|---|
keyName | String | The key to remove. |
Example
js
globalThis.memoryStorage.removeItem("currentFont");clear()
Removes all keys from the storage.
Example
js
globalThis.memoryStorage.clear();