summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/net/data-provider.js
blob: d8a70d72d81b059955ea33b608ac6497a5fabc37 (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
/* 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/. */
"use strict";

const promise = require("promise");

/**
 * Map of pending requests. Used mainly by tests to wait
 * till things are ready.
 */
var promises = new Map();

/**
 * This object is used to fetch network data from the backend.
 * Communication with the chrome scope is based on message
 * exchange.
 */
var DataProvider = {
  hasPendingRequests: function () {
    return promises.size > 0;
  },

  requestData: function (client, actor, method) {
    let key = actor + ":" + method;
    let p = promises.get(key);
    if (p) {
      return p;
    }

    let deferred = promise.defer();
    let realMethodName = "get" + method.charAt(0).toUpperCase() +
      method.slice(1);

    if (!client[realMethodName]) {
      return null;
    }

    client[realMethodName](actor, response => {
      promises.delete(key);
      deferred.resolve(response);
    });

    promises.set(key, deferred.promise);
    return deferred.promise;
  },

  resolveString: function (client, stringGrip) {
    let key = stringGrip.actor + ":getString";
    let p = promises.get(key);
    if (p) {
      return p;
    }

    p = client.getString(stringGrip).then(result => {
      promises.delete(key);
      return result;
    });

    promises.set(key, p);
    return p;
  },
};

// Exports from this module
module.exports = DataProvider;