Skip to main content
Version: LOC v0.8 (legacy)

Session Storage Agent

import { SessionStorageAgent } from "@fstnetwork/loc-logic-sdk";

For sharing data between logic during a task (execution) of a data process.

The session will be purged once the task is completed. This is a very common way to pass data from one logic to the next, including the aggregator logic.

Availability

  • ✓ Generic logic
  • ✓ Aggregator logic

Write to Session Storage

async SessionStorageAgent.putJson(key: string, value: any): Promise<boolean>
ParameterDescription
keyKey of session data
valueValid JSON data (JavaScript object)

If value is not valid JSON, the agent will throw an error.

tip

value can be a single number (which is also valid for JSON).

Write a value into session storage. The key/value pair will be created if not exist.

These functions return a boolean to indicate if the write action is successful.

Example: write JSON object to session storage

const data = {
name: "Arthur Dent",
age: 42,
};

await SessionStorageAgent.putJson("data", data);
await SessionStorageAgent.putJson("age", data.age); // single number is valid JSON
warning

The data may not be properly written into session storage without using await.

Example: write string to session storage

await SessionStorageAgent.putString("name", "Arthur Dent"");
await SessionStorageAgent.putString("age", String(age)); // convert a single number to string

Example: write Uint8Array to session storage

const data = (await ctx.payload()).http.request.data;
await SessionStorageAgent.putByteArray("payload", new Uint8Array(data));

Read from Session Storage

async SessionStorageAgent.get(key: string): Promise<string | number | object | Uint8Array>

Read a session data. The type depends on how the data was written into session storage. If the data is a single number stored with putJson, the return type will be number.

If the key is not exist, the agent will return null.

Example

const name = await SessionStorageAgent.get("name"); // returns a string
const data = await SessionStorageAgent.get("data"); // returns a JSON object

const name_in_data = data?.name;
const age_in_data = data?.age;
tip

If you are using TypeScript, you can use type assertions to enforce typeing in your editor:

const name = (await SessionStorageAgent.get("name")) as string;
const age = (await SessionStorageAgent.get("age")) as number;
const data = (await SessionStorageAgent.get("data")) as {
name: string;
age: number;
};

Remove from Session Storage

async SessionStorageAgent.delete(key: string): Promise<void>

// or
async SessionStorageAgent.remove(key: string): Promise<void>

Example

await SessionStorageAgent.remove("data");