summaryrefslogtreecommitdiffstats
path: root/js/service/Publisher.js
blob: 9e17e1494e94ab1004ed31c95d7e3e15b937dbe6 (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
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright Bernhard Posselt 2014
 */

/*jshint undef:false*/
app.service('Publisher', function () {
    'use strict';

    this.channels = {};

    this.subscribe = (obj) => {
        return {
            toChannels: (...channels) => {
                for (let channel of channels) {
                    this.channels[channel] = this.channels[channel] || [];
                    this.channels[channel].push(obj);
                }
            }
        };

    };

    this.publishAll = (data) => {
        for (let [channel, messages] of items(data)) {
            if (this.channels[channel] !== undefined) {
                for(let listener of this.channels[channel]) {
                    listener.receive(messages, channel);
                }
            }
        }
    };

});