HomeAccounting
57 строк · 1.2 Кб
1const axios = require('axios');2
3function Broker(storage, prefix='storageKey') {4this.storage = storage;5this.prefix = prefix;6
7if(this.storage.get('broker') === null) {8this.broker = {};9this.storage.add('broker', this.broker)10}11else {12this.broker = this.storage.getObject('broker');13}14};15
16Broker.prototype.queueCount = function () {17return Object.keys(this.broker).length;18};19
20Broker.prototype.saveToStorage = function (method, url, data) {21let key = this.prefix + '_' + (Object.keys(this.broker).length + 1);22
23this.broker[key] = {method, url, data};24
25this.storage.add('broker', this.broker);26};27
28Broker.prototype.run = function (VueObject) {29for (let key in this.broker) {30this.sendToServer(this.broker[key], key)31}32}
33
34Broker.prototype.sendToServer = function (object, brokerKey) {35
36axios({37method: object.method,38url: object.url,39data: object.data,40})41.then(response => {42if(response.data.status == 200) {43delete this.broker[brokerKey];44this.storage.add('broker', this.broker);45}46else {47console.log(response.data)48}49
50})51.catch(error => {52
53});54
55};56
57module.exports = Broker;