Session

View source

Provides tab-scoped JSON storage as a world resource.

StoragePlugin installs a fresh Session resource. Values are written to the browser’s sessionStorage, so they survive reloads in the same tab but are discarded when that tab or its top-level browsing context ends. Each value is serialized with JSON.stringify by Session#set and deserialized with JSON.parse by Session#get; values must be JSON-serializable and returned objects are new parsed values. The browser must provide the global sessionStorage object when these methods run.

Examples

import { Session } from '@wimaengine/storage'

const session = world.getResource(Session)
session.set('screen', { name: 'home' })
const screen = session.get('screen')
session.clear()

Constructor


constructor()

Methods


set(k: string, v: any): void

View source

Serializes v and stores it under k in the current tab’s sessionStorage.

Parameters

NameDescriptionDefault
k

Storage key.

—
v

JSON-serializable value to store.

—

get<T>(k: string): T

View source

Reads and parses the value stored under k.

Returns undefined when sessionStorage.getItem(k) returns a falsy value. Otherwise, returns the result of JSON.parse; malformed stored JSON therefore throws the parser’s error.

Type Parameters

T

Parameters

NameDescriptionDefault
k

Storage key to retrieve.

—

Returns

The parsed value, or undefined when no value is stored.

clear(): void

View source

Removes every key from the current tab’s sessionStorage.

This clears the browser storage area shared by the current top-level browsing context, not only values written through this Session instance.