var psStore = (function(){ function NewStore(){ return new Store; } function Store(){ var data = {}; this.Get = Get; this.Put = Put; this.Delete = Delete; this.Sub = Sub; this.Unsub = Unsub; this.Exit = Exit; function Get(key){ var d = data[key] if(isDefined(d)){ return d.get(); } return null; } function Put(key, value){ var d = data[key]; if(!isDefined(d)){ d = data[key] = new StoreItem; } d.data = isObject(value) || isArray(value) ? copy(value) : value; pub(key); return true; } function Delete(key){ return Put(key, null); } function Sub(key, fn){ var d = data[key]; if(!isDefined(d)){ d = data[key] = new StoreItem; } if(isFunction(fn)){ return d.subscribers.Set(fn); } return null; } function Unsub(key, fnKey){ var d = data[key]; if(isDefined(d)){ d.subscribers.Delete(fnKey); } return null; } function Exit(){ data = null; } function pub(key){ var d = data[key] if(isDefined(d)){ d.subscribers.ForEach(p); } return true function p(k, v){ v(d.get()); } } } function StoreItem(){ this.data = null; this.subscribers = new Subscribers; } StoreItem.prototype.get = function(){ var d = this.data; if(isObject(d) || isArray(d)){ return copy(d); } else { return d; } } function Subscribers(){ var list = {}, indx = 0; this.Set = function(fn){ var key = indx.toString(); indx++; list[key] = fn; return key; }; this.Delete = function(key){ delete list[key]; return null; }; this.ForEach = function(fn){ loop(list, fn); return null }; } function copy(d){ return JSON.parse(JSON.stringify(d)); } function isDefined(v){ return v !== null && v !== undefined; } function isFunction(fn) { return !!fn && typeof fn === "function"; } function isObject(t){ return t instanceof Object && t.constructor === Object; } function isArray(t){ return Array.isArray(t); } return NewStore; })(); /* if(!!define){ define(function(){ return psStore; }); } */