From 2061400fdae4abdfdcd136cd4c87eba4474e529e Mon Sep 17 00:00:00 2001 From: cxxxr Date: Sat, 6 Jan 2024 04:16:16 +0900 Subject: [PATCH] ready -> login --- frontends/electron/lem-editor/jsonrpc.js | 16 +++++++++------- frontends/electron/lem-editor/lem-editor.js | 15 ++++++++++----- frontends/jsonrpc/main.lisp | 15 +++++++++------ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/frontends/electron/lem-editor/jsonrpc.js b/frontends/electron/lem-editor/jsonrpc.js index bfc035ac5..8ec5fa531 100644 --- a/frontends/electron/lem-editor/jsonrpc.js +++ b/frontends/electron/lem-editor/jsonrpc.js @@ -17,24 +17,26 @@ class JSONRPC { this.serverAndClient.addMethod(method, handler); } - async requestInternal(method, arg) { + async requestInternal(method, arg, callback) { const result = await this.serverAndClient.request(method, arg); - console.log(result); + if (callback) { + callback(result); + } } requestMessageQueue() { this.messageQueue.forEach((value) => { - const [method, arg] = value; - this.requestInternal(method, arg); + const [method, arg, callback] = value; + this.requestInternal(method, arg, callback); }); this.messageQueue = []; } - request(method, arg) { + request(method, arg, callback) { if (this.webSocket.readyState === WebSocket.OPEN) { - this.requestInternal(method, arg); + this.requestInternal(method, arg, callback); } else { - this.messageQueue.push([method, arg]); + this.messageQueue.push([method, arg, callback]); } } diff --git a/frontends/electron/lem-editor/lem-editor.js b/frontends/electron/lem-editor/lem-editor.js index 2a00b9ee6..8091703f7 100644 --- a/frontends/electron/lem-editor/lem-editor.js +++ b/frontends/electron/lem-editor/lem-editor.js @@ -69,8 +69,8 @@ class WebSocketRPCDelegator { this.jsonrpc.on(method, handler); } - request(method, arg) { - this.jsonrpc.request(method, arg); + request(method, arg, callback) { + this.jsonrpc.request(method, arg, callback); } notify(method, arg) { @@ -95,8 +95,8 @@ class VSCodeJSONRPCDelegator { this.rpcConnection.onNotification(method, handler); } - request(method, arg) { - this.rpcConnection.sendRequest(method, arg); + request(method, arg, callback) { + this.rpcConnection.sendRequest(method, arg, callback); } notify(method, arg) { @@ -142,11 +142,15 @@ class LemEditor extends HTMLElement { this.width = contentBounds.width; this.height = contentBounds.height; - this.rpcDelegator.request("ready", { + this.userId = null; + + this.rpcDelegator.request("login", { width: calcDisplayCols(this.width), height: calcDisplayRows(this.height), foreground: option.foreground, background: option.background, + }, (response) => { + this.userId = response.userId; }); // will updated by setFont() @@ -244,6 +248,7 @@ class LemEditor extends HTMLElement { emitInput(kind, value) { this.rpcDelegator.notify("input", { + userId: this.userId, kind: kind, value: value, }); diff --git a/frontends/jsonrpc/main.lisp b/frontends/jsonrpc/main.lisp index e2f45662c..820d84056 100644 --- a/frontends/jsonrpc/main.lisp +++ b/frontends/jsonrpc/main.lisp @@ -46,7 +46,7 @@ (jsonrpc:notify (jsonrpc-server jsonrpc) method argument)) (jsonrpc:broadcast (jsonrpc-server jsonrpc) method argument))) -(defun ready (jsonrpc loaded-fn) +(defun login (jsonrpc logged-in-callback) (lambda (params) (pdebug "ready: ~A" (pretty-json params)) (with-error-handler () @@ -59,9 +59,10 @@ (setf (jsonrpc-background-color jsonrpc) color)) (alexandria:when-let (color (lem:parse-color foreground)) (setf (jsonrpc-foreground-color jsonrpc) color)) - (funcall loaded-fn) + (funcall logged-in-callback) (hash "width" width - "height" height))))) + "height" height + "userId" 0))))) (defmethod lem-if:invoke ((jsonrpc jsonrpc) function) (let ((ready nil)) @@ -70,8 +71,8 @@ (lambda () (loop :until ready)))) (jsonrpc:expose (jsonrpc-server jsonrpc) - "ready" - (ready jsonrpc + "login" + (login jsonrpc (lambda () (setf ready t)))) (jsonrpc:expose (jsonrpc-server jsonrpc) @@ -472,7 +473,9 @@ (defun input-callback (jsonrpc args) (handler-case (let ((kind (gethash "kind" args)) - (value (gethash "value" args))) + (value (gethash "value" args)) + (user-id (gethash "userId" args))) + (declare (ignore user-id)) (cond ((= kind +abort+) (lem:send-abort-event *editor-thread* nil)) ((= kind +keyevent+)