JSFiddle - React, Tailwind, and code Playground
by Sean Cannon
JavaScript
function SessionManager(require) {
/**
* Singleton constructor for our SessionManager
* @example Simple singleton persistence with getter and setter
*
* var SessionManager = require('/path/to/SessionManager');
* var sessionFoo = SessionManager.use('foo');
* var sessionBar = SessionManager.use('bar');
*
* sessionFoo.set('someKey', 'someVal');
* sessionBar.set('someKey', 'someOtherVal');
*
* console.log(sessionFoo.get('someKey')); // someVal
* console.log(sessionBar.get('someKey')); // someOtherVal
*
* @returns {{}}
* @constructor
*/
var Session = function () {
/**
* Exposed public object.
* @type {{sessions: {}, use: Function}}
*/
var API = {
/**
* Object to hold the namespace properties for each session collection.
* @type {}
*/
sessions : {},
/**
* Set the namespace for subsequent get() and set() methods.
* @param {string} namespace
*/
use : function (namespace) {
if (!API.sessions.hasOwnProperty(namespace)) {
API.sessions[namespace] = function () {};
API.sessions[namespace].prototype = {
/**
* Gets a value from a provided key.
* @param {string} key
* @returns {*}
*/
get : function (key) {
return API.sessions[namespace][key];
},
/**
* Sets a value for a provided key.
* @param {string} key
* @param {*} value
* @returns {*}
*/
set : function (key, value) {
...