From d825774e48813f7c3f28ab1a4cc03c270c87f4b9 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Fri, 24 May 2024 10:34:26 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=83=20WS=20message=20log=20for=20debug?= =?UTF-8?q?ging=20(#2926)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/common/PlutoConnection.js | 20 +++++++++++++------- frontend/common/Stack.js | 29 +++++++++++++++++++++++++++++ frontend/components/Editor.js | 12 +++--------- 3 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 frontend/common/Stack.js diff --git a/frontend/common/PlutoConnection.js b/frontend/common/PlutoConnection.js index c9f96bf1e0..8cacfd617a 100644 --- a/frontend/common/PlutoConnection.js +++ b/frontend/common/PlutoConnection.js @@ -1,6 +1,7 @@ import { Promises } from "../common/SetupCellEnvironment.js" import { pack, unpack } from "./MsgPack.js" import "./Polyfill.js" +import { Stack } from "./Stack.js" import { with_query_params } from "./URLTools.js" const reconnect_after_close_delay = 500 @@ -249,6 +250,7 @@ const default_ws_address = () => ws_address_from_base(window.location.href) * dismiss_update_notification: boolean, * }, * notebook_exists: boolean, + * message_log: import("./Stack.js").Stack, * }} */ @@ -279,18 +281,21 @@ export const create_pluto_connection = async ({ ws_address = default_ws_address(), }) => { let ws_connection = /** @type {WebsocketConnection?} */ (null) // will be defined later i promise + const message_log = new Stack(100) + // @ts-ignore + window.pluto_get_message_log = () => message_log.get() - /** @type {PlutoConnection} */ const client = { - send: null, - session_options: null, + // send: null, + // session_options: null, version_info: { julia: "unknown", pluto: "unknown", dismiss_update_notification: false, }, notebook_exists: true, - kill: null, + // kill: null, + message_log, } // same const client_id = get_unique_short_id() @@ -303,6 +308,7 @@ export const create_pluto_connection = async ({ } const request_id = get_unique_short_id() + // This data will be sent: const message = { type: message_type, client_id: client_id, @@ -311,8 +317,6 @@ export const create_pluto_connection = async ({ ...metadata, } - // Note: Message to be sent: message - let p = resolvable_promise() sent_requests.set(request_id, (response_message) => { @@ -350,6 +354,8 @@ export const create_pluto_connection = async ({ try { ws_connection = await create_ws_connection(String(ws_address), { on_message: (update) => { + message_log.push(update) + const by_me = update.initiator_id == client_id const request_id = update.request_id @@ -418,5 +424,5 @@ export const create_pluto_connection = async ({ } await connect() - return client + return /** @type {PlutoConnection} */ (client) } diff --git a/frontend/common/Stack.js b/frontend/common/Stack.js new file mode 100644 index 0000000000..57cae31615 --- /dev/null +++ b/frontend/common/Stack.js @@ -0,0 +1,29 @@ +/** + * @template T + * @type {Stack} + */ +export class Stack { + /** + * @param {number} max_size + */ + constructor(max_size) { + this.max_size = max_size + this.arr = [] + } + /** + * @param {T} item + * @returns {void} + */ + push(item) { + this.arr.push(item) + if (this.arr.length > this.max_size) { + this.arr.shift() + } + } + /** + * @returns {T[]} + */ + get() { + return this.arr + } +} diff --git a/frontend/components/Editor.js b/frontend/components/Editor.js index 0ad3ebafcc..85935512bd 100644 --- a/frontend/components/Editor.js +++ b/frontend/components/Editor.js @@ -811,15 +811,9 @@ patch: ${JSON.stringify( this.waiting_for_bond_to_trigger_execution = false } } - apply_promise - .then(set_waiting) - .catch((e) => { - set_waiting() - throw e - }) - .then(() => { - this.send_queued_bond_changes() - }) + apply_promise.finally(set_waiting).then(() => { + this.send_queued_bond_changes() + }) break default: