File size: 1,148 Bytes
8fd7a1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import EventTargetShim from './event-target';
import AddonHooks from './hooks';

class AddonRedux extends EventTargetShim {
    constructor () {
        super();

        this._isInReducer = false;
        this._nextState = null;

        AddonHooks.appStateReducer = (action, prev, next) => {
            this._isInReducer = true;
            this._nextState = next;
            this.dispatchEvent(new CustomEvent('statechanged', {
                detail: {
                    action,
                    prev,
                    next
                }
            }));
            this._nextState = null;
            this._isInReducer = false;
        };
    }

    initialize () {
        // no-op; it is always initialized
    }

    dispatch (m) {
        if (this._isInReducer) {
            queueMicrotask(() => AddonHooks.appStateStore.dispatch(m));
        } else {
            AddonHooks.appStateStore.dispatch(m);
        }
    }

    get state () {
        if (this._nextState) return this._nextState;
        return AddonHooks.appStateStore.getState();
    }
}

const reduxInstance = new AddonRedux();

export default reduxInstance;