Skip to content

Commit 695e7bb

Browse files
committed
feat: use aio when show problem description, kaiwk#33
1 parent cab4f6d commit 695e7bb

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

leetcode.el

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
;; Author: Wang Kai <[email protected]>
66
;; Keywords: extensions, tools
77
;; URL: https://github.com/kaiwk/leetcode.el
8-
;; Package-Requires: ((emacs "25") (request-deferred "0.2.0") (graphql "0.1.1") (spinner "1.7.3"))
9-
;; Version: 0.1.3
8+
;; Package-Requires: ((emacs "26") (request-deferred "0.2.0") (graphql "0.1.1") (spinner "1.7.3") (aio "1.0"))
9+
;; Version: 0.1.4
1010

1111
;; This program is free software; you can redistribute it and/or modify
1212
;; it under the terms of the GNU General Public License as published by
@@ -261,7 +261,7 @@ Such as 'Two Sum' will be converted to 'two-sum'."
261261
(res (replace-regexp-in-string "[(),]" "" str1)))
262262
res))
263263

264-
(defun leetcode--problem-descr-graphql-params (operation &optional vars)
264+
(defun leetcode--problem-graphql-params (operation &optional vars)
265265
"Construct a GraphQL parameter.
266266
OPERATION and VARS are LeetCode GraphQL parameters."
267267
(list
@@ -278,7 +278,7 @@ OPERATION and VARS are LeetCode GraphQL parameters."
278278
(codeSnippets langSlug code)))))
279279
(if vars (cons "variables" vars))))
280280

281-
(defun leetcode--parse-problem (title)
281+
(aio-defun leetcode--fetch-problem (title)
282282
"Fetch single problem.
283283
TITLE is a problem's title.
284284
Return a object with following attributes:
@@ -287,16 +287,20 @@ Return a object with following attributes:
287287
:content String
288288
:topicTags String"
289289
(let* ((slug-title (leetcode--slugify-title title))
290-
(resp (request
291-
leetcode--api-graphql
292-
:type "POST"
293-
:headers `(,leetcode--User-Agent
294-
,(cons "Content-Type" "application/json"))
295-
:data (json-encode (leetcode--problem-descr-graphql-params
296-
"questionData"
297-
(list (cons "titleSlug" slug-title))))
298-
:parser 'json-read :sync t)))
299-
(alist-get 'question (alist-get 'data (request-response-data resp)))))
290+
(url-request-method "POST")
291+
(url-request-extra-headers
292+
`(,leetcode--User-Agent
293+
,(cons "Content-Type" "application/json")))
294+
(url-request-data
295+
(json-encode (leetcode--problem-graphql-params
296+
"questionData"
297+
(list (cons "titleSlug" slug-title)))))
298+
(result (aio-await (aio-url-retrieve leetcode--api-graphql))))
299+
(if-let ((error-info (plist-get (car result) :error)))
300+
(message "LeetCode Login ERROR: %S" error-info)
301+
(with-current-buffer (cdr result)
302+
(goto-char url-http-end-of-headers)
303+
(alist-get 'question (alist-get 'data (json-read)))))))
300304

301305
(defun leetcode--replace-in-buffer (regex to)
302306
"Replace string matched REGEX in `current-buffer' to TO."
@@ -662,7 +666,7 @@ following possible value:
662666
"Generate problem link from title."
663667
(concat leetcode--base-url "/problems/" (leetcode--slugify-title title)))
664668

665-
(defun leetcode-show-description ()
669+
(aio-defun leetcode-show-current-problem ()
666670
"Show current entry problem description.
667671
Get current entry by using `tabulated-list-get-entry' and use
668672
`shr-render-buffer' to render problem description."
@@ -671,7 +675,7 @@ Get current entry by using `tabulated-list-get-entry' and use
671675
(pos (aref entry 1))
672676
(title (aref entry 2))
673677
(difficulty (aref entry 4))
674-
(problem (leetcode--parse-problem title))
678+
(problem (aio-await (leetcode--fetch-problem title)))
675679
(buf-name leetcode--description-buffer-name)
676680
(html-margin "&nbsp;&nbsp;&nbsp;&nbsp;"))
677681
(let-alist problem
@@ -810,7 +814,7 @@ for current problem."
810814
(let ((map (make-sparse-keymap)))
811815
(prog1 map
812816
(suppress-keymap map)
813-
(define-key map (kbd "RET") #'leetcode-show-description)
817+
(define-key map (kbd "RET") #'leetcode-show-current-problem)
814818
(define-key map "n" #'next-line)
815819
(define-key map "p" #'previous-line)
816820
(define-key map "g" #'leetcode-problems-refresh)

0 commit comments

Comments
 (0)