HomeAccounting

Форк
0
57 строк · 1.2 Кб
1
const axios = require('axios');
2

3
function Broker(storage, prefix='storageKey') {
4
    this.storage = storage;
5
    this.prefix = prefix;
6

7
    if(this.storage.get('broker') === null) {
8
        this.broker = {};
9
        this.storage.add('broker', this.broker)
10
    }
11
    else {
12
        this.broker = this.storage.getObject('broker');
13
    }
14
};
15

16
Broker.prototype.queueCount = function () {
17
    return Object.keys(this.broker).length;
18
};
19

20
Broker.prototype.saveToStorage = function (method, url, data) {
21
    let key = this.prefix + '_' + (Object.keys(this.broker).length + 1);
22

23
    this.broker[key] = {method, url, data};
24

25
    this.storage.add('broker', this.broker);
26
};
27

28
Broker.prototype.run = function (VueObject) {
29
    for (let key in this.broker) {
30
        this.sendToServer(this.broker[key], key)
31
    }
32
}
33

34
Broker.prototype.sendToServer = function (object, brokerKey) {
35

36
    axios({
37
        method: object.method,
38
        url: object.url,
39
        data: object.data,
40
    })
41
    .then(response => {
42
        if(response.data.status == 200) {
43
            delete this.broker[brokerKey];
44
            this.storage.add('broker', this.broker);
45
        }
46
        else {
47
            console.log(response.data)
48
        }
49

50
    })
51
    .catch(error => {
52

53
    });
54

55
};
56

57
module.exports = Broker;

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.