comparison lib/GeoTemCo/js/Util/Publisher.js @ 0:b57c7821382f

initial
author Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de>
date Thu, 28 May 2015 10:28:12 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:b57c7821382f
1 /*
2 * Dropdown.js
3 *
4 * Copyright (c) 2012, Stefan Jänicke. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
20 */
21
22 /**
23 * @class Publisher
24 * Publish/Subscribe mechanism
25 * @author Stefan Jänicke (stjaenicke@informatik.uni-leipzig.de)
26 * @release 1.0
27 * @release date: 2012-07-27
28 * @version date: 2012-07-27
29 */
30 if ( typeof Publisher == 'undefined') {
31
32 Publisher = function() {
33
34 var topics = [];
35
36 this.Get = function(topic) {
37 var value = topics[topic];
38 if (!value || !(value instanceof Array)) {
39 value = topics[topic] = [];
40 }
41 return value;
42 };
43
44 this.Publish = function(topic, data, publisher) {
45 var subscribers = this.Get(topic);
46 for (var i = 0; i < subscribers.length; i++) {
47 if (publisher == null || subscribers[i].client != publisher) {
48 subscribers[i].callback(data);
49 }
50 }
51 };
52
53 this.Subscribe = function(topic, subscriber, callback) {
54 var subscribers = this.Get(topic);
55 subscribers.push({
56 client : subscriber,
57 callback : callback
58 });
59 };
60
61 this.Unsubscribe = function(topic, unsubscriber) {
62 var subscribers = this.Get(topic);
63 for (var i = 0; i < subscribers.length; i++) {
64 if (subscribers[i].client == unsubscriber) {
65 subscribers.splice(i, 1);
66 return;
67 }
68 }
69 };
70
71 return this;
72
73 }();
74
75 }