function-tree
Version:
When a function is not enough
282 lines (276 loc) • 10.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.DevtoolsBase = void 0;
var _Path = _interopRequireDefault(require("../Path"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var DevtoolsBase = exports.DevtoolsBase = /*#__PURE__*/function () {
function DevtoolsBase() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$https = _ref.https,
https = _ref$https === void 0 ? false : _ref$https,
_ref$host = _ref.host,
host = _ref$host === void 0 ? null : _ref$host,
_ref$reconnect = _ref.reconnect,
reconnect = _ref$reconnect === void 0 ? true : _ref$reconnect,
_ref$reconnectInterva = _ref.reconnectInterval,
reconnectInterval = _ref$reconnectInterva === void 0 ? 10000 : _ref$reconnectInterva;
_classCallCheck(this, DevtoolsBase);
this.host = host;
this.https = https;
this.version = 0;
if (!this.host) {
throw new Error("Devtools: You have to pass in the \"host\" option");
}
this.backlog = [];
this.isConnected = false;
this.ws = null;
this.reconnectInterval = reconnectInterval;
this.doReconnect = reconnect;
this.sendInitial = this.sendInitial.bind(this);
}
return _createClass(DevtoolsBase, [{
key: "createSocket",
value: function createSocket() {
throw new Error('You have to implement a "createSocket" method');
}
/*
Sets up the listeners to Chrome Extension or remote debugger
*/
}, {
key: "addListeners",
value: function addListeners() {
this.createSocket();
this.ws.onmessage = this.onMessage.bind(this);
}
}, {
key: "onMessage",
value: function onMessage(_event) {}
}, {
key: "reconnect",
value: function reconnect() {
var _this = this;
setTimeout(function () {
_this.init();
}, this.reconnectInterval);
}
/*
The debugger might be ready or it might not. The initial communication
with the debugger requires a "ping" -> "pong" to identify that it
is ready to receive messages.
1. Debugger is open when app loads
- Devtools sends "ping"
- Debugger sends "pong"
- Devtools sends "init"
2. Debugger is opened after app load
- Debugger sends "ping"
- Devtools sends "init"
*/
}, {
key: "init",
value: function init() {
var _this2 = this;
this.addListeners();
this.ws.onopen = function () {
_this2.ws.send(JSON.stringify({
type: 'ping'
}));
};
this.ws.onerror = function () {};
this.ws.onclose = function () {
_this2.isConnected = false;
if (_this2.doReconnect) {
console.warn('Debugger application is not running on selected port... will reconnect automatically behind the scenes');
_this2.reconnect();
}
};
}
/*
Sends message to chrome extension or remote debugger
*/
}, {
key: "sendMessage",
value: function sendMessage(stringifiedMessage) {
this.ws.send(stringifiedMessage);
}
/*
Sends multiple message in one batch to debugger, causing debugger
also to synchronously run all updates before rendering
*/
}, {
key: "sendBulkMessage",
value: function sendBulkMessage(messages, source) {
var message = JSON.stringify({
type: 'bulk',
source: source,
version: this.version,
data: {
messages: messages
}
});
this.sendMessage(message);
}
/*
Watches function tree for execution of signals. This is passed to
debugger to prevent time travelling when executing. It also tracks
latest executed signal for "remember" to know when signals can be
called again
*/
}, {
key: "watchExecution",
value: function watchExecution(tree, source) {
var _this3 = this;
tree.on('start', function (execution, payload) {
var message = JSON.stringify({
type: 'executionStart',
source: source,
version: _this3.version,
data: {
execution: {
executionId: execution.id,
name: execution.name,
staticTree: execution.staticTree,
datetime: execution.datetime,
executedBy: payload && payload._execution ? payload._execution : null
}
}
});
_this3.sendExecutionMessage(message);
});
tree.on('end', function (execution) {
var message = JSON.stringify({
type: 'executionEnd',
source: source,
version: _this3.version,
data: {
execution: {
executionId: execution.id
}
}
});
_this3.latestExecutionId = execution.id;
_this3.sendExecutionMessage(message);
});
tree.on('pathStart', function (path, execution, funcDetails) {
var message = JSON.stringify({
type: 'executionPathStart',
source: source,
version: _this3.version,
data: {
execution: {
executionId: execution.id,
functionIndex: funcDetails.functionIndex,
path: path
}
}
});
_this3.sendExecutionMessage(message);
});
tree.on('functionStart', function (execution, funcDetails, payload) {
var message = _this3.safeStringify({
type: 'execution',
source: source,
version: _this3.version,
data: {
execution: {
executionId: execution.id,
functionIndex: funcDetails.functionIndex,
payload: payload,
data: null
}
}
});
_this3.sendExecutionMessage(message);
});
tree.on('functionEnd', function (execution, funcDetails, payload, result) {
if (!result || result instanceof _Path["default"] && !result.payload) {
return;
}
var message = _this3.safeStringify({
type: 'executionFunctionEnd',
source: source,
version: _this3.version,
data: {
execution: {
executionId: execution.id,
functionIndex: funcDetails.functionIndex,
output: result instanceof _Path["default"] ? result.payload : result
}
}
});
_this3.sendExecutionMessage(message);
});
tree.on('error', function (error, execution, funcDetails) {
var message = JSON.stringify({
type: 'executionFunctionError',
source: source,
version: _this3.version,
data: {
execution: {
executionId: execution.id,
functionIndex: funcDetails.functionIndex,
error: {
name: error.name,
message: error.message,
stack: error.stack,
func: funcDetails["function"].toString()
}
}
}
});
_this3.sendExecutionMessage(message);
});
}
}, {
key: "safeStringify",
value: function safeStringify(object) {
var refs = [];
return JSON.stringify(object, function (key, value) {
var isObject = _typeof(value) === 'object' && value !== null && !Array.isArray(value);
if (isObject && refs.indexOf(value) > -1) {
return '[CIRCULAR]';
} else if (isObject) {
refs.push(value);
}
return value;
});
}
}, {
key: "sendExecutionMessage",
value: function sendExecutionMessage(message) {
if (this.isConnected) {
this.sendMessage(message);
} else {
this.backlog.push(message);
}
}
}, {
key: "sendInitial",
value: function sendInitial() {}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
}, {
key: "createExecutionMessage",
value: function createExecutionMessage(debuggingData, execution, functionDetails, payload) {}
/*
Sends execution data to the debugger. Whenever a signal starts
it will send a message to the debugger, but any functions in the
function tree might also use this to send debugging data. Like when
mutations are done or any wrapped methods run.
*/
}, {
key: "sendExecutionData",
value: function sendExecutionData(debuggingData, execution, functionDetails, payload) {
var message = this.createExecutionMessage(debuggingData, execution, functionDetails, payload);
this.sendExecutionMessage(message);
}
}]);
}();
var _default = exports["default"] = DevtoolsBase;
//# sourceMappingURL=base.js.map