summaryrefslogtreecommitdiffstats
path: root/services/sync/tps/extensions/mozmill/resource/driver/msgbroker.js
blob: 95e431f084d22bcf88e9faabdd1c95d686f1a8c4 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/. */

var EXPORTED_SYMBOLS = ['addListener', 'addObject',
                        'removeListener',
                        'sendMessage', 'log', 'pass', 'fail'];

var listeners = {};

// add a listener for a specific message type
function addListener(msgType, listener) {
  if (listeners[msgType] === undefined) {
    listeners[msgType] = [];
  }

  listeners[msgType].push(listener);
}

// add each method in an object as a message listener
function addObject(object) {
  for (var msgType in object) {
    addListener(msgType, object[msgType]);
  }
}

// remove a listener for all message types
function removeListener(listener) {
  for (var msgType in listeners) {
    for (let i = 0; i < listeners.length; ++i) {
      if (listeners[msgType][i] == listener) {
        listeners[msgType].splice(i, 1); // remove listener from array
      }
    }
  }
}

function sendMessage(msgType, obj) {
  if (listeners[msgType] === undefined) {
    return;
  }

  for (let i = 0; i < listeners[msgType].length; ++i) {
    listeners[msgType][i](obj);
  }
}

function log(obj) {
  sendMessage('log', obj);
}

function pass(obj) {
  sendMessage('pass', obj);
}

function fail(obj) {
  sendMessage('fail', obj);
}