summaryrefslogtreecommitdiffstats
path: root/devtools/client/fronts/eventsource.js
blob: 43b667dcd02ec3917eff4c138f162dd3a6542fd8 (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
67
68
69
70
71
72
73
74
75
/* 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 {
  FrontClassWithSpec,
  registerFront,
} = require("resource://devtools/shared/protocol.js");
const {
  eventSourceSpec,
} = require("resource://devtools/shared/specs/eventsource.js");

/**
 * A EventSourceFront is used as a front end for the EventSourceActor that is
 * created on the server, hiding implementation details.
 */
class EventSourceFront extends FrontClassWithSpec(eventSourceSpec) {
  constructor(client, targetFront, parentFront) {
    super(client, targetFront, parentFront);

    this._onEventSourceConnectionClosed = this._onEventSourceConnectionClosed.bind(
      this
    );
    this._onEventReceived = this._onEventReceived.bind(this);

    // Attribute name from which to retrieve the actorID
    // out of the target actor's form
    this.formAttributeName = "eventSourceActor";

    this.on(
      "serverEventSourceConnectionClosed",
      this._onEventSourceConnectionClosed
    );
    this.on("serverEventReceived", this._onEventReceived);
  }

  /**
   * Close the EventSourceFront.
   */
  destroy() {
    this.off("serverEventSourceConnectionClosed");
    this.off("serverEventReceived");
    return super.destroy();
  }

  /**
   * The "eventSourceConnectionClosed" message type handler. We redirect any message to
   * the UI for displaying.
   *
   * @private
   * @param number httpChannelId
   */
  async _onEventSourceConnectionClosed(httpChannelId) {
    this.emit("eventSourceConnectionClosed", httpChannelId);
  }

  /**
   * The "eventReceived" message type handler. We redirect any message to
   * the UI for displaying.
   *
   * @private
   * @param string httpChannelId
   *        Channel ID of the eventSource connection.
   * @param object data
   *        The data received from the server.
   */
  async _onEventReceived(httpChannelId, data) {
    this.emit("eventReceived", httpChannelId, data);
  }
}

exports.EventSourceFront = EventSourceFront;
registerFront(EventSourceFront);