summaryrefslogtreecommitdiffstats
path: root/devtools/shared/protocol/Actor/generateActorSpec.js
blob: 9b3b166c8b06faf5a83b7c8edc94c62af8a43e0d (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
/* 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";

var { Request } = require("resource://devtools/shared/protocol/Request.js");
const { Response } = require("resource://devtools/shared/protocol/Response.js");
var {
  types,
  registeredTypes,
} = require("resource://devtools/shared/protocol/types.js");

/**
 * Generates an actor specification from an actor description.
 */
var generateActorSpec = function (actorDesc) {
  const actorSpec = {
    typeName: actorDesc.typeName,
    methods: [],
  };

  // Find additional method specifications
  if (actorDesc.methods) {
    for (const name in actorDesc.methods) {
      const methodSpec = actorDesc.methods[name];
      const spec = {};

      spec.name = methodSpec.name || name;
      spec.request = new Request(
        Object.assign({ type: spec.name }, methodSpec.request || undefined)
      );
      spec.response = new Response(methodSpec.response || undefined);
      spec.release = methodSpec.release;
      spec.oneway = methodSpec.oneway;

      actorSpec.methods.push(spec);
    }
  }

  // Find event specifications
  if (actorDesc.events) {
    actorSpec.events = new Map();
    for (const name in actorDesc.events) {
      const eventRequest = actorDesc.events[name];
      Object.freeze(eventRequest);
      actorSpec.events.set(
        name,
        new Request(Object.assign({ type: name }, eventRequest))
      );
    }
  }

  if (!registeredTypes.has(actorSpec.typeName)) {
    types.addActorType(actorSpec.typeName);
  }
  registeredTypes.get(actorSpec.typeName).actorSpec = actorSpec;

  return actorSpec;
};

exports.generateActorSpec = generateActorSpec;