Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AuthorisationClient.js « Client « Queue « js « src - github.com/marius-wieschollek/passwords-webextension.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5066d0c1bf1a7824e05af274999026e7a193fd18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import Popup from '@js/App/Popup';
import AuthorisationItem from '@js/Models/Queue/AuthorisationItem';
import FeedbackClient from '@js/Queue/Client/FeedbackClient';

export default class AuthorisationClient extends FeedbackClient {

    constructor() {
        let worker = (i) => { return this._worker(i); };
        let feedbackWorker = (i) => { return this._feedbackWorker(i); };
        super('authorisation', worker, feedbackWorker, AuthorisationItem);

        this._items = [];
        this._solvedItems = [];
        this._current = null;
    }

    /**
     *
     * @returns {(AuthorisationItem|null)}
     */
    getCurrent() {
        if(this._current == null) {
            let keys = Object.keys(this._items);
            if(keys.length === 0) return null;
            this._current = keys.shift();
        }

        return this._items[this._current].item;
    }

    /**
     *
     * @returns {Promise<unknown>}
     */
    solveCurrent() {
        return new Promise((resolve, reject) => {
            let current = this._items[this._current],
                item    = current.item;

            this._solvedItems[item.getId()] = {
                item,
                resolve,
                reject
            };

            current.resolve(item);
        });
    }

    /**
     *
     * @param {AuthorisationItem} item
     * @returns {Promise<AuthorisationItem>}
     * @private
     */
    _worker(item) {
        return new Promise((resolve, reject) => {
            this._items[item.getId()] = {
                item,
                resolve,
                reject
            };

            if(Popup.app) Popup.app.authorized = false;
        });
    }

    /**
     *
     * @param {AuthorisationItem} item
     * @returns {Promise<AuthorisationItem>}
     * @private
     */
    _feedbackWorker(item) {
        if(!this._solvedItems.hasOwnProperty(item.getId())) {
            return this._worker(item);
        }

        return new Promise((resolve, reject) => {
            let queue = this._solvedItems[item.getId()];
            delete this._solvedItems[item.getId()];

            if(item.getAccepted()) {
                if(this._current === item.getId()) this._current = null;
                delete this._items[item.getId()];
                queue.resolve(item);
                resolve();

                if(Object.keys(this._items).length === 0) {
                    if(Popup.app) Popup.app.authorized = true;
                }

                return;
            }

            queue.reject(item);
            this._items[item.getId()] = {
                item,
                resolve,
                reject
            };

            if(Popup.app) Popup.app.authorized = false;
        });
    }
}