Skip to content

Commit

Permalink
ready -> login
Browse files Browse the repository at this point in the history
  • Loading branch information
cxxxr committed Jan 5, 2024
1 parent 205010d commit 2061400
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
16 changes: 9 additions & 7 deletions frontends/electron/lem-editor/jsonrpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down
15 changes: 10 additions & 5 deletions frontends/electron/lem-editor/lem-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -244,6 +248,7 @@ class LemEditor extends HTMLElement {

emitInput(kind, value) {
this.rpcDelegator.notify("input", {
userId: this.userId,
kind: kind,
value: value,
});
Expand Down
15 changes: 9 additions & 6 deletions frontends/jsonrpc/main.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand All @@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -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+)
Expand Down

0 comments on commit 2061400

Please sign in to comment.