Foundation.Session
A very simplified weak abstraction around the window.sessionStorage API. Databases values will be lost at the end of each session.
Each instance can be given a prefix value which will be prefixed to each key used through the instance. For example, with a prefix of levelOne_ a set('score', value) call would actually store the value in sessionStorage as levelOne_score. From an instance with the same prefix, however, retrieval would be through get('score'). A prefix value of '' (an empty string) can be used to nullify this behavior.
All values will be stringifyied / parsed automatically when setting / getting.
Example
// Create instance with default prefix (foundation_)
var dbDefault = new Foundation.Session();
// Create instance with custom prefix (custom_)
var dbCustom = new Foundation.Session('custom_');
// Set a number
dbDefault.set('foo', 5);
// Get a number (already parsed)
console.log(dbDefault.get('foo') === 5); // true
// Check if a key exists in default
console.log(dbDefault.has('foo')); // true
// Check if a key exists in custom
console.log(dbCustom.has('foo')); // false
Implementation Details
Reference paths
'/foundation/_digest.ts''/foundation/core/actors/_digest.ts''/foundation/core/actors/Session.ts'
Extends
This type extends:
equals check is based on the prefix property and active state.
Implements
This type does not implement any interfaces (except those in the base).
Constructors
()
- Default constructor.
- Sets the storage prefix to
foundation_.
(prefix: string)
- Explicitly set
prefix.
Public Properties
has(key: string): boolean
- Returns true if
keyis a valid entry in the current session (identified by prefix).
set(key: string, value: any): Session
- Sets
keyin the current session (identified by prefix) tovalue. Value will be automatically stringified for API insertion. - Returns the current instance for chaining.
get(key: string): any
- Get the value store at
keythe current session (identified by prefix). The value will be automatically parsed from API retrieval. - If
keydoes not exist,nullwill be returned.
remove(key: string): Session
- Removes the value of
keyfrom the current session (identified by prefix). - Returns the current instance for chaining.
clear(): Session
- Removes all entries from the current session (identified by prefix).
- Returns the current instance for chaining.