summaryrefslogtreecommitdiffstats
path: root/webapp/stores/opengraph_store.jsx
blob: 4ad156df057a60a485355c603c952d1ff383b8e3 (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
import EventEmitter from 'events';

import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
import Constants from 'utils/constants.jsx';

const ActionTypes = Constants.ActionTypes;

const CHANGE_EVENT = 'change';
const URL_DATA_CHANGE_EVENT = 'url_data_change';

class OpenGraphStoreClass extends EventEmitter {
    constructor() {
        super();
        this.ogDataObject = {};  // Format: {<url>: <data-object>}
    }

    emitChange() {
        this.emit(CHANGE_EVENT);
    }

    addChangeListener(callback) {
        this.on(CHANGE_EVENT, callback);
    }

    removeChangeListener(callback) {
        this.removeListener(CHANGE_EVENT, callback);
    }

    emitUrlDataChange(url) {
        this.emit(URL_DATA_CHANGE_EVENT, url);
    }

    addUrlDataChangeListener(callback) {
        this.on(URL_DATA_CHANGE_EVENT, callback);
    }

    removeUrlDataChangeListener(callback) {
        this.removeListener(URL_DATA_CHANGE_EVENT, callback);
    }

    storeOgInfo(url, ogInfo) {
        this.ogDataObject[url] = ogInfo;
    }

    getOgInfo(url) {
        return this.ogDataObject[url];
    }
}

var OpenGraphStore = new OpenGraphStoreClass();

// Not expecting more that `Constants.POST_CHUNK_SIZE` post previews rendered at a time
OpenGraphStore.setMaxListeners(Constants.POST_CHUNK_SIZE);

OpenGraphStore.dispatchToken = AppDispatcher.register((payload) => {
    var action = payload.action;

    switch (action.type) {
    case ActionTypes.RECIVED_OPEN_GRAPH_METADATA:
        OpenGraphStore.storeOgInfo(action.url, action.data);
        OpenGraphStore.emitUrlDataChange(action.url);
        OpenGraphStore.emitChange();
        break;
    default:
    }
});

export default OpenGraphStore;