Bug 1909986 - For sidebar revamp, only show chatbot entrypoints (context menu, shortc...
[gecko.git] / devtools / server / actors / common.js
blobcdf22ccae60578aacf7006f53a8aae6b27684f6e
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 class SourceLocation {
8   /**
9    * A SourceLocation represents a location in a source.
10    *
11    * @param SourceActor actor
12    *        A SourceActor representing a source.
13    * @param Number line
14    *        A line within the given source.
15    * @param Number column
16    *        A column within the given line.
17    */
18   constructor(actor, line, column) {
19     this._connection = actor ? actor.conn : null;
20     this._actorID = actor ? actor.actorID : undefined;
21     this._line = line;
22     this._column = column;
23   }
25   get sourceActor() {
26     return this._connection ? this._connection.getActor(this._actorID) : null;
27   }
29   get url() {
30     return this.sourceActor.url;
31   }
33   get line() {
34     return this._line;
35   }
37   get column() {
38     return this._column;
39   }
41   get sourceUrl() {
42     return this.sourceActor.url;
43   }
45   equals(other) {
46     return (
47       this.sourceActor.url == other.sourceActor.url &&
48       this.line === other.line &&
49       (this.column === undefined ||
50         other.column === undefined ||
51         this.column === other.column)
52     );
53   }
55   toJSON() {
56     return {
57       source: this.sourceActor.form(),
58       line: this.line,
59       column: this.column,
60     };
61   }
64 exports.SourceLocation = SourceLocation;
66 /**
67  * A method decorator that ensures the actor is in the expected state before
68  * proceeding. If the actor is not in the expected state, the decorated method
69  * returns a rejected promise.
70  *
71  * The actor's state must be at this.state property.
72  *
73  * @param String expectedState
74  *        The expected state.
75  * @param String activity
76  *        Additional info about what's going on.
77  * @param Function methodFunc
78  *        The actor method to proceed with when the actor is in the expected
79  *        state.
80  *
81  * @returns Function
82  *          The decorated method.
83  */
84 function expectState(expectedState, methodFunc, activity) {
85   return function (...args) {
86     if (this.state !== expectedState) {
87       const msg =
88         `Wrong state while ${activity}:` +
89         `Expected '${expectedState}', ` +
90         `but current state is '${this.state}'.`;
91       return Promise.reject(new Error(msg));
92     }
94     return methodFunc.apply(this, args);
95   };
98 exports.expectState = expectState;
101  * Autobind method from a `bridge` property set on some actors where the
102  * implementation is delegated to a separate class, and where `bridge` points
103  * to an instance of this class.
104  */
105 function actorBridgeWithSpec(methodName) {
106   return function () {
107     return this.bridge[methodName].apply(this.bridge, arguments);
108   };
110 exports.actorBridgeWithSpec = actorBridgeWithSpec;