aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/stores/eventlogstore.js
blob: dbecc7495d6555cd512aa6409f330b84d2edb4d8 (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
"use strict";
//
// We have an EventLogView and an EventLogStore:
// The basic architecture is that one can request views on the event log
// from the store, which returns a view object and then deals with getting the data required for the view.
// The view object is accessed by React components and distributes updates etc.
//
// See also: components/EventLog.react.js
function EventLogView(store, live) {
    EventEmitter.call(this);
    this.$EventLogView_store = store;
    this.live = live;
    this.log = [];

    this.add = this.add.bind(this);

    if (live) {
        this.$EventLogView_store.addListener("new_entry", this.add);
    }
}
_.extend(EventLogView.prototype, EventEmitter.prototype, {
    close: function() {
        this.$EventLogView_store.removeListener("new_entry", this.add);
    },
    getAll: function() {
        return this.log;
    },
    add: function(entry) {
        this.log.push(entry);
        this.emit("change");
    },
    add_bulk: function(messages) {
        var log = messages;
        var last_id = log[log.length - 1].id;
        var to_add = _.filter(this.log, function(entry)  {return entry.id > last_id;});
        this.log = log.concat(to_add);
        this.emit("change");
    }
});


function _EventLogStore(){
    EventEmitter.call(this);
}
_.extend(_EventLogStore.prototype, EventEmitter.prototype, {
    getView: function(since) {
        var view = new EventLogView(this, !since);

        //TODO: Really do bulk retrieval of last messages.
        window.setTimeout(function() {
            view.add_bulk([{
                id: 1,
                message: "Hello World"
            }, {
                id: 2,
                message: "I was already transmitted as an event."
            }]);
        }, 100);

        var id = 2;
        view.add({
            id: id++,
            message: "I was already transmitted as an event."
        });
        view.add({
            id: id++,
            message: "I was only transmitted as an event before the bulk was added.."
        });
        window.setInterval(function() {
            view.add({
                id: id++,
                message: "."
            });
        }, 1000);
        return view;
    },
    handle: function(action) {
        switch (action.actionType) {
            case ActionTypes.EVENTLOG_ADD:
                this.emit("new_message", action.message);
                break;
            default:
                return;
        }
    }
});


var EventLogStore = new _EventLogStore();
AppDispatcher.register(EventLogStore.handle.bind(EventLogStore));