simple-object-state
Version:
An experiemental object oriented state mangment lib
160 lines (144 loc) • 19.6 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else {
var a = factory();
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(window, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "./src/index.ts");
/******/ })
/************************************************************************/
/******/ ({
/***/ "./src/ShallowEqual.ts":
/*!*****************************!*\
!*** ./src/ShallowEqual.ts ***!
\*****************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
eval("\nObject.defineProperty(exports, \"__esModule\", { value: true });\n/**\n * Shallow Equal function should talk two states and compare if they are\n * the same. The same being the a shallow comparison on sub-objects\n * within the state and also accepting a partial of the current state,\n * as the next state parameter. Meaning that only the keys of the nextState\n * parameter will be checked\n */\nfunction shallowEqual(_a) {\n var nextState = _a.nextState, state = _a.state;\n for (var key in nextState) {\n if (nextState[key] !== state[key]) {\n return false;\n }\n }\n return true;\n}\nexports.shallowEqual = shallowEqual;\n\n\n//# sourceURL=webpack:///./src/ShallowEqual.ts?");
/***/ }),
/***/ "./src/SimpleObjectState.ts":
/*!**********************************!*\
!*** ./src/SimpleObjectState.ts ***!
\**********************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
eval("\nObject.defineProperty(exports, \"__esModule\", { value: true });\nvar SimpleObjectStateStoreWrapper_1 = __webpack_require__(/*! ./SimpleObjectStateStoreWrapper */ \"./src/SimpleObjectStateStoreWrapper.ts\");\n/** Helpers */\nfunction isClass(ref) {\n return typeof ref === \"function\";\n}\nfunction isInstance(ref) {\n return typeof ref === \"object\" && ref.constructor;\n}\nfunction isString(ref) {\n return typeof ref === \"string\";\n}\nfunction classStoreName(Class) {\n return Class.name;\n}\nfunction instanceStoreName(This) {\n return This.constructor.name;\n}\n/** Simple Object State Singleton */\nvar SimpleObjectState = /** @class */ (function () {\n function SimpleObjectState() {\n var _this = this;\n this.Stores = {};\n this.destroyStore = function (ref) {\n var store = _this.getWrapper(ref);\n if (store) {\n store.destructor();\n }\n else {\n console.warn(\"Store \", ref, \" was not available for creating!\");\n }\n };\n this.getState = function (ref) {\n var store = _this.getStore(ref);\n if (store) {\n return store.getState();\n }\n };\n this.createStore = function (ref) {\n var wrapper = _this.getWrapper(ref);\n if (wrapper) {\n wrapper.create();\n }\n else {\n console.warn(\"Store \", ref, \" was not available for creating!\");\n }\n };\n this.callAction = function (ref, action) {\n var args = [];\n for (var _i = 2; _i < arguments.length; _i++) {\n args[_i - 2] = arguments[_i];\n }\n var store = _this.getWrapper(ref);\n if (store) {\n store.callAction(action, args);\n }\n else {\n console.warn(\"Store \", ref, \" was not available for creating!\");\n }\n };\n /** Returns the Store Class if one */\n this.getClass = function (ref) {\n var wrapper = _this.getWrapper(ref);\n if (wrapper) {\n return wrapper.Class;\n }\n };\n /** Returns the Store Static Initial State if one */\n this.getInitialState = function (ref) {\n var store = _this.getStore(ref);\n if (store) {\n return store.InitialState;\n }\n };\n /** Registers the Store, this will not actually instantiate the Store */\n this.register = function (Class) {\n var store = _this.addStoreByClass(Class);\n return store;\n };\n this.unregister = function (Class) {\n if (_this.Stores[classStoreName(Class)]) {\n var store = _this.Stores[classStoreName(Class)];\n store.destructor();\n delete _this.Stores[classStoreName(Class)];\n }\n };\n /** Subscribes a callback to a stores setState function */\n this.subscribe = function (ref, callback) {\n var store = _this.getWrapper(ref);\n if (store) {\n store.subscribe(callback);\n }\n else {\n console.warn(\"Store \", ref, \" was not available for subscribe!\");\n }\n };\n this.unsubscribe = function (ref, callback) {\n var store = _this.getWrapper(ref);\n if (store) {\n store.unsubscribe(callback);\n }\n else {\n console.warn(\"Store \", ref, \" was not available for unsubscribe!\");\n }\n };\n /** Called by the Store when it sets its own state to call all subscribers */\n this.onSetState = function (This) {\n if (_this.debug) {\n console.groupCollapsed(\"SimpleObjectState State Update - \" + This.constructor.name);\n console.log(\"Store -\", This);\n console.log(\"New State -\", This.getState());\n console.groupCollapsed(\"Stack Trace\");\n console.trace(); // hidden in collapsed group\n console.groupEnd();\n console.groupEnd();\n }\n var wrapper = _this.Stores[instanceStoreName(This)];\n wrapper.onSetState();\n };\n /** Returns the Store Instance if one */\n this.getStore = function (ref) {\n var wrapper = _this.getWrapper(ref);\n if (wrapper) {\n return wrapper.getInstance();\n }\n };\n /** Private */\n /** Returns the Store Class if one */\n this.getString = function (ref) {\n if (isString(ref)) {\n return ref;\n }\n else if (isClass(ref)) {\n return classStoreName(ref);\n }\n else if (isInstance(ref)) {\n return instanceStoreName(ref);\n }\n };\n /** Returns the Store Wrapper if on or creates if ref is a Class */\n this.getWrapper = function (ref) {\n var storeString = _this.getString(ref);\n if (storeString) {\n return _this.Stores[storeString];\n }\n else {\n console.warn(\"Store has not been registered\");\n }\n };\n this.debug = false;\n if (window.SimpleObjectState) {\n SimpleObjectState.Instance = window.SimpleObjectState;\n return SimpleObjectState.Instance;\n }\n if (!SimpleObjectState.Instance) {\n SimpleObjectState.Instance = this;\n window.SimpleObjectState = this;\n }\n }\n /** Sets debug mode where a stack will be built of state updates */\n SimpleObjectState.prototype.setDebug = function (value) {\n this.debug = value;\n };\n SimpleObjectState.prototype.addStoreByClass = function (Class) {\n if (!this.Stores[classStoreName(Class)]) {\n this.Stores[classStoreName(Class)] = new SimpleObjectStateStoreWrapper_1.SimpleObjectStateStoreWrapper(Class);\n }\n return this.Stores[classStoreName(Class)];\n };\n return SimpleObjectState;\n}());\nvar simpleObjectState = new SimpleObjectState();\nexports.SimpleObjectState = simpleObjectState;\n\n\n//# sourceURL=webpack:///./src/SimpleObjectState.ts?");
/***/ }),
/***/ "./src/SimpleObjectStateStoreWrapper.ts":
/*!**********************************************!*\
!*** ./src/SimpleObjectStateStoreWrapper.ts ***!
\**********************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
eval("\nvar __spreadArrays = (this && this.__spreadArrays) || function () {\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\n r[k] = a[j];\n return r;\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nvar SimpleObjectStateStoreWrapper = /** @class */ (function () {\n /**\n * Initalize with the Class of the Store, we will setup the\n * store itself later with .Instance and .create()\n */\n function SimpleObjectStateStoreWrapper(Class) {\n this.Class = Class;\n this.Listeners = [];\n this.isCreatedThroughSub = false;\n }\n /** Just remove the instance, keep the class as we might re-instantiate */\n SimpleObjectStateStoreWrapper.prototype.destructor = function () {\n if (this.Instance) {\n this.Instance.destructor();\n this.Instance = undefined;\n this.Listeners = [];\n this.isCreatedThroughSub = false;\n }\n };\n /**\n * Actually setup the store, at this point we would want\n * the stores state to be changing on subscribed to\n */\n SimpleObjectStateStoreWrapper.prototype.create = function () {\n if (!this.Instance) {\n this.Instance = new this.Class();\n }\n this.isCreatedThroughSub = false;\n return this.Instance;\n };\n SimpleObjectStateStoreWrapper.prototype.getInstance = function () {\n return this.Instance;\n };\n SimpleObjectStateStoreWrapper.prototype.callAction = function (action, args) {\n if (this.Instance) {\n this.Instance.callAction(action, args);\n }\n };\n SimpleObjectStateStoreWrapper.prototype.onSetState = function () {\n var _this = this;\n if (this.Instance) {\n this.Listeners.forEach(function (callback) {\n if (callback) {\n callback(_this.getState());\n }\n else {\n // maybe they forgot to unsubscribe\n _this.unsubscribe(callback);\n }\n });\n }\n };\n SimpleObjectStateStoreWrapper.prototype.getState = function () {\n var instance = this.getInstance();\n if (instance) {\n return instance.getState();\n }\n else {\n console.warn('no instance setup yet!');\n }\n return {};\n };\n SimpleObjectStateStoreWrapper.prototype.subscribe = function (callback) {\n this.Listeners.push(callback);\n var hadInstanceBeforeCreate = Boolean(this.Instance);\n this.create();\n if (!hadInstanceBeforeCreate) {\n this.isCreatedThroughSub = true;\n }\n };\n SimpleObjectStateStoreWrapper.prototype.unsubscribe = function (callback) {\n if (!this.Instance) {\n return;\n }\n var listeners = __spreadArrays(this.Listeners);\n var nextListeners = [];\n var listener = listeners.pop();\n while (listener && listener !== callback) {\n nextListeners.push(listener);\n listener = listeners.pop();\n }\n nextListeners = __spreadArrays(nextListeners, listeners);\n this.Listeners = nextListeners;\n if (this.isCreatedThroughSub && !this.Listeners.length) {\n this.destructor();\n }\n };\n return SimpleObjectStateStoreWrapper;\n}());\nexports.SimpleObjectStateStoreWrapper = SimpleObjectStateStoreWrapper;\n\n\n//# sourceURL=webpack:///./src/SimpleObjectStateStoreWrapper.ts?");
/***/ }),
/***/ "./src/Store.ts":
/*!**********************!*\
!*** ./src/Store.ts ***!
\**********************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
eval("\nvar __assign = (this && this.__assign) || function () {\n __assign = Object.assign || function(t) {\n for (var s, i = 1, n = arguments.length; i < n; i++) {\n s = arguments[i];\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))\n t[p] = s[p];\n }\n return t;\n };\n return __assign.apply(this, arguments);\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nvar SimpleObjectState_1 = __webpack_require__(/*! ./SimpleObjectState */ \"./src/SimpleObjectState.ts\");\nvar ShallowEqual_1 = __webpack_require__(/*! ./ShallowEqual */ \"./src/ShallowEqual.ts\");\nvar Store = /** @class */ (function () {\n function Store() {\n this.state = {};\n this.actions = {};\n }\n Store.prototype.destructor = function () {\n return;\n };\n Store.prototype.getActions = function () {\n return this.actions;\n };\n Store.prototype.getState = function () {\n return this.state;\n };\n Store.prototype.callAction = function (action, args) {\n var actions = this.actions;\n if (this.actions && action && actions[action]) {\n var func = actions[action];\n func.apply(void 0, args);\n this.storeDidCallAction(action);\n }\n };\n Store.prototype.storeDidCallAction = function (action) {\n return;\n };\n Store.prototype.storeDidUpdate = function (prevState) {\n return;\n };\n Store.prototype.shouldStoreUpdate = function (nextState) {\n if (nextState) {\n var state = this.state;\n var isEqual = ShallowEqual_1.shallowEqual({ nextState: nextState, state: state });\n return !isEqual;\n }\n return false;\n };\n Store.prototype.setState = function (nextState) {\n var prevState = this.state;\n var combinedState = __assign(__assign({}, this.state), nextState);\n // forgo state update if they are equal\n if (!this.shouldStoreUpdate(combinedState)) {\n return;\n }\n this.state = combinedState;\n SimpleObjectState_1.SimpleObjectState.onSetState(this);\n this.storeDidUpdate(prevState);\n };\n return Store;\n}());\nexports.Store = Store;\n\n\n//# sourceURL=webpack:///./src/Store.ts?");
/***/ }),
/***/ "./src/index.ts":
/*!**********************!*\
!*** ./src/index.ts ***!
\**********************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
eval("\nObject.defineProperty(exports, \"__esModule\", { value: true });\nvar SimpleObjectState_1 = __webpack_require__(/*! ./SimpleObjectState */ \"./src/SimpleObjectState.ts\");\nvar Store_1 = __webpack_require__(/*! ./Store */ \"./src/Store.ts\");\nexports.Store = Store_1.Store;\nexports.subscribe = SimpleObjectState_1.SimpleObjectState.subscribe;\nexports.unsubscribe = SimpleObjectState_1.SimpleObjectState.unsubscribe;\nexports.register = SimpleObjectState_1.SimpleObjectState.register;\nexports.unregister = SimpleObjectState_1.SimpleObjectState.unregister;\nexports.getInitialState = SimpleObjectState_1.SimpleObjectState.getInitialState;\nexports.callAction = SimpleObjectState_1.SimpleObjectState.callAction;\nexports.createStore = SimpleObjectState_1.SimpleObjectState.createStore;\nexports.destroyStore = SimpleObjectState_1.SimpleObjectState.destroyStore;\nexports.getState = SimpleObjectState_1.SimpleObjectState.getState;\n\n\n//# sourceURL=webpack:///./src/index.ts?");
/***/ })
/******/ });
});