UNPKG

@zoranwong/acc-engine.js

Version:

A javascript framework to accelerate your applicatin developement

188 lines (157 loc) 5.94 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _newArrowCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/newArrowCheck")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _underscore = require("underscore"); var _ = _interopRequireWildcard(require("..")); var Model = /*#__PURE__*/function () { function Model() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { cacheKey: '' }; (0, _classCallCheck2["default"])(this, Model); this.cacheAttributes = ['*']; this.cacheKey = ''; this.needCache = false; this.cacheKey += options.cacheKey || ''; } (0, _createClass2["default"])(Model, [{ key: "getApplication", value: function getApplication() { return _["default"].getInstance(); } }, { key: "excludeCacheKeys", value: function excludeCacheKeys() { return ['savingCache', 'needCache', 'cacheAttributes', 'cacheKey']; } }, { key: "resetModelFromCache", value: function resetModelFromCache() { var _this = this; if (!this.cacheKey) { return; } var app = this.getApplication(); if (this.needCache && app['cache']) { var cachedData = app['cache'].get(this.cacheKey); var reset = function reset(cachedData) { var _this2 = this; (0, _newArrowCheck2["default"])(this, _this); if (cachedData) { if (this.cacheAttributes.length !== 1 || !this.cacheAttributes.includes('*')) { cachedData = (0, _underscore.pick)(cachedData, function (item, key) { (0, _newArrowCheck2["default"])(this, _this2); return this.cacheAttributes.includes(key); }.bind(this)); } (0, _underscore.forEach)(cachedData, function (item, key) { (0, _newArrowCheck2["default"])(this, _this2); this.set(key, item); }.bind(this)); } }.bind(this); if (cachedData instanceof Promise) { cachedData.then(function (cachedData) { (0, _newArrowCheck2["default"])(this, _this); reset(cachedData); }.bind(this)); } else { reset(cachedData); } } } }, { key: "setCache", value: function setCache() { var _this3 = this; var app = this.getApplication(); if (this.needCache && app['cache'] && !this.constructor.savingCache) { this.constructor.savingCache = true; var excludeCacheKeys = this.excludeCacheKeys(); var id = setTimeout( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() { var _this4 = this; var cacheData; return _regenerator["default"].wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: (0, _newArrowCheck2["default"])(this, _this3); cacheData = {}; if (this.cacheAttributes.length === 1 && this.cacheAttributes[0] === '*') { (0, _underscore.forEach)(this, function (item, key) { (0, _newArrowCheck2["default"])(this, _this4); if (excludeCacheKeys.indexOf(key) === -1 && !(0, _underscore.isFunction)(this[key])) { cacheData[key] = item; } }.bind(this)); } else { (0, _underscore.forEach)(this.cacheAttributes, function (key) { (0, _newArrowCheck2["default"])(this, _this4); cacheData[key] = this[key]; }.bind(this)); } _context.next = 5; return app['cache'].set(this.cacheKey, cacheData); case 5: this.constructor.savingCache = false; clearTimeout(id); case 7: case "end": return _context.stop(); } } }, _callee, this); })).bind(this), 200); } } }, { key: "initial", value: function initial() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; this.resetModelFromCache(); if (!(0, _underscore.isEmpty)(options)) this.setModel(options); return this; } }, { key: "set", value: function set(key, value) { var tmp = {}; tmp[key] = value; (0, _underscore.extend)(this, tmp); return value; } }, { key: "setModel", value: function setModel() { var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; for (var key in data) { var camelKey = (0, _.caseKeyName)(key); if (typeof this[camelKey] !== 'undefined') { this[camelKey] = data[key]; } } } }], [{ key: "instance", value: function instance() { var cacheKey = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var instance = new this({ cacheKey: cacheKey }); instance.initial(); return instance; } }]); return Model; }(); exports["default"] = Model; Model.savingCache = false;