/**----**/
βββ app.js
βββ bootstrap.js
βββ classes
β βββ QueueBroker
β βββ index.js
β βββ Library
β βββ Broker.js
β βββ Storage.js
βββ components
/**----**/
const Broker = require('./Library/Broker'); const Storage = require('./Library/Storage'); module.exports.Broker = Broker; module.exports.Storage = Storage;
import {Storage, Broker} from '../../classes/QueueBroker/index';
const axios = require('axios'); // axios /* . , front-end */ function Broker(storage, prefix='storageKey') { this.storage = storage; this.prefix = prefix; /* , . storage add json */ if(this.storage.get('broker') === null) { this.broker = {}; this.storage.add('broker', this.broker) } else { // , Storage Json this.broker = this.storage.getObject('broker'); } }; // , Broker.prototype.queueCount = function () { return Object.keys(this.broker).length; }; // "" Storage, Broker.prototype.saveToStorage = function (method, url, data) { let key = this.prefix + '_' + (Object.keys(this.broker).length + 1); this.broker[key] = {method, url, data}; // broker, this.storage.add('broker', this.broker); }; // , , Broker.prototype.run = function () { for (let key in this.broker) { this.sendToServer(this.broker[key], key) } } /* . , method, url data, , , */ Broker.prototype.sendToServer = function (object, brokerKey) { axios({ method: object.method, url: object.url, data: object.data, }) .then(response => { if(response.data.status == 200) { // , delete this.broker[brokerKey]; // this.storage.add('broker', this.broker); } else { // ;-) console.log(response.data) } }) .catch(error => { /* , */ }); }; // export module.exports = Broker;
// debug- function Storage(debug) { if(debug === true) { this.debugMode = true; } this.storage = window.localStorage; }; // , Json Storage.prototype.addObjectToStorage = function (key, object) { this.storage.setItem(key, JSON.stringify(object)); }; // (, ) Storage.prototype.addStringToStorage = function (key, value) { this.storage.setItem(key, value); }; // Storage.prototype.get = function (key) { return this.storage.getItem(key); }; // Json , Storage.prototype.getObject = function (key) { try { return JSON.parse(this.storage.getItem(key)); } catch (e) { this._debug(e); this._debug(key + ' = ' + this.storage.getItem(key)); return false; } }; /* , , , , Json */ Storage.prototype.add = function (key, value) { try { if(typeof value === 'object') { this.addObjectToStorage(key, value); } else if (typeof value === 'string' || typeof value === 'number') { this.addStringToStorage(key, value); } else { // this._debug('2 parameter does not belong to a known type') } return this.storage; } catch (e) { // , , if (e === QUOTA_EXCEEDED_ERR) { this._debug('LocalStorage is exceeded the free space limit') } else { this._debug(e) } } }; // Storage.prototype.clear = function () { try { this.storage.clear(); return true; } catch (e) { this._debug(e) return false; } }; // Storage.prototype.delete = function(key) { try { this.storage.removeItem(key); return true; } catch (e) { this._debug(e) return false; } }; // , Storage.prototype._debug = function(error) { if(this.debugMode) { console.error(error); } return null; }; // module.exports = Storage;
// Vue (methods) /*----*/ // Storage sendBroker(method, url, data) { let storage = new Storage(true); let broker = new Broker(storage, 'fundsControl'); broker.saveToStorage(method, url, data); }, // fundsSave() { let url = '/pa/funds'; let method = ''; if(this.fundsFormType === 'create') { method = 'post'; } else if(this.fundsFormType === 'update') { method = 'put'; } else if(this.fundsFormType === 'delete') { method = 'delete'; } this.$store.commit('setPreloader', true); axios({ method: method, url: url, data: this.fundsFormData, }) .then(response=> { if(response.data.status == 200) { this.fundsFormShow = false; this.getFunds(); this.$store.commit('setPreloader', false); } else { this.$store.commit('AlertError', ' '); } }) // .catch(error => { this.$store.commit('setAlert', { type: 'warning', status: true, message: ' . , , ' } ); this.fundsFormShow = false; this.$store.commit('setPreloader', false); // "" this.sendBroker(method, url, this.fundsFormData); console.error(error); }); },
// Vue /*--*/ methods: { /*----*/ /* , , , , */ brokerSendRun() { let storage = new Storage(true); let broker = new Broker(storage, 'fundsControl'); //, - if(broker.queueCount() > 0) { // , broker.run(); // , this.$store.commit('setAlert', {type: 'info', status: true, message: ' - , , '}); } } } /*---*/ /* , , , , , */ mounted() { this.brokerSendRun(); } /*---*/
Source: https://habr.com/ru/post/428980/