emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/pyim 08c44262ff 2/2: Refactor: 异步获取词条时需要时间延迟。


From: ELPA Syncer
Subject: [elpa] externals/pyim 08c44262ff 2/2: Refactor: 异步获取词条时需要时间延迟。
Date: Tue, 7 Jun 2022 00:57:58 -0400 (EDT)

branch: externals/pyim
commit 08c44262ffb624822035dedea65e198b3c20401d
Author: Feng Shu <tumashu@163.com>
Commit: Feng Shu <tumashu@163.com>

    Refactor: 异步获取词条时需要时间延迟。
    
    因为频繁的请求 baidu 或者谷歌输入法,会被屏蔽。
---
 pyim-candidates.el | 12 +++++------
 pyim-liberime.el   |  4 ++--
 pyim-process.el    | 60 +++++++++++++++++++++++++++++-------------------------
 3 files changed, 39 insertions(+), 37 deletions(-)

diff --git a/pyim-candidates.el b/pyim-candidates.el
index d3b84f4360..b520367caf 100644
--- a/pyim-candidates.el
+++ b/pyim-candidates.el
@@ -280,20 +280,18 @@
                                 (pyim-pymap-py2cchar-get pinyin))))))
     (cl-subseq chars 0 num)))
 
-(cl-defgeneric pyim-candidates-create-delay (imobjs scheme orig-candidates)
+(cl-defgeneric pyim-candidates-create-limit-time (imobjs scheme 
orig-candidates)
   "按照 SCHEME, 使用延迟的方式从 IMOBJS 获得候选词条。
 
-如果用户输入等待时间超过某个阈值时,输入法就会调用这个函数来获取额
-外词条,这个函数有三个特点:
 1. 这个函数是同步运行。
 2. 这个函数运行有时间限制,运行超过某个时间后,无论有没有结果,必须结束。
 3. 这个函数需要探测用户是否输入,如果用户开始输入,这个函数运行必须结束。")
 
-(cl-defmethod pyim-candidates-create-delay (_imobjs _scheme orig-candidates)
+(cl-defmethod pyim-candidates-create-limit-time (_imobjs _scheme 
orig-candidates)
   "按照 SCHEME, 使用延迟的方式从 IMOBJS 获得候选词条。"
   orig-candidates)
 
-(cl-defmethod pyim-candidates-create-delay (imobjs (scheme 
pyim-scheme-quanpin) orig-candidates)
+(cl-defmethod pyim-candidates-create-limit-time (imobjs (scheme 
pyim-scheme-quanpin) orig-candidates)
   "按照 SCHEME, 用延迟的方式从 IMOBJS 获得候选词条,用于全拼输入法。"
   ;; 构建一个搜索中文的正则表达式, 然后使用这个正则表达式在当前 buffer 中搜
   ;; 索词条。
@@ -328,10 +326,10 @@
                       (> (or (gethash a counts) 0)
                          (or (gethash b counts) 0))))))))
 
-(cl-defmethod pyim-candidates-create-delay (imobjs (_scheme 
pyim-scheme-shuangpin) orig-candidates)
+(cl-defmethod pyim-candidates-create-limit-time (imobjs (_scheme 
pyim-scheme-shuangpin) orig-candidates)
   "按照 SCHEME, 用延迟的方式从 IMOBJS 获得候选词条,用于双拼输入法。"
   ;; 注意:pyim 支持的双拼输入法,内部使用全拼的 imobjs, 所以这里直接调用全拼的
-  ;; `pyim-candidates-create-delay' 方法来处理 imobjs。
+  ;; `pyim-candidates-create-limit-time' 方法来处理 imobjs。
   (cl-call-next-method imobjs (pyim-scheme-get 'quanpin) orig-candidates))
 
 (cl-defgeneric pyim-candidates-create-async (imobjs scheme callback)
diff --git a/pyim-liberime.el b/pyim-liberime.el
index efd6faaaa2..5372abe449 100644
--- a/pyim-liberime.el
+++ b/pyim-liberime.el
@@ -141,8 +141,8 @@
          (words (liberime-search s (* pyim-page-length 2))))
     words))
 
-(cl-defmethod pyim-candidates-create-delay (imobjs (scheme pyim-scheme-rime) 
_orig-candidates)
-  "适用于 rime 的 `pyim-candidates-create-delay' 方法。"
+(cl-defmethod pyim-candidates-create-limit-time (imobjs (scheme 
pyim-scheme-rime) _orig-candidates)
+  "适用于 rime 的 `pyim-candidates-create-limit-time' 方法。"
   (let* ((code (car (pyim-codes-create (car imobjs) scheme)))
          (code-prefix (pyim-scheme-code-prefix scheme))
          (s (replace-regexp-in-string "-" "" code))
diff --git a/pyim-process.el b/pyim-process.el
index 9c636383a2..99899dfe94 100644
--- a/pyim-process.el
+++ b/pyim-process.el
@@ -88,9 +88,9 @@ entered (nihaom) 的第一个候选词。
   :type 'integer)
 
 (define-obsolete-variable-alias
-  'pyim-process-async-delay 'pyim-process-get-candidates-delay "5.0")
+  'pyim-process-async-delay 'pyim-process-run-delay "5.0")
 
-(defcustom pyim-process-get-candidates-delay 0.5
+(defcustom pyim-process-run-delay 0.5
   "延迟多少秒开始延迟获取词条。"
   :type 'integer)
 
@@ -118,7 +118,7 @@ entered (nihaom) 的第一个候选词。
 这个变量主要用于全拼和双拼输入法的多音字矫正,其取值一般使用用户
 输入生成的 imobjs 转换得到,保留了用户原始输入的许多信息。")
 
-(defvar pyim-process-get-candidates-delay-timer nil
+(defvar pyim-process-run-delay-timer nil
   "异步处理 entered 时,使用的 timer.")
 
 (defvar pyim-process-self-insert-commands nil
@@ -287,8 +287,7 @@ entered (nihaom) 的第一个候选词。
     (setq pyim-candidates
           (or (delete-dups (pyim-candidates-create pyim-imobjs scheme))
               (list entered-to-translate)))
-    (pyim-process-get-candidates-async)
-    (pyim-process-get-candidates-delay)
+    (pyim-process-run-delay)
     ;; 自动上屏功能
     (let ((autoselector-results
            (mapcar (lambda (x)
@@ -346,45 +345,40 @@ entered (nihaom) 的第一个候选词。
        (t (setq pyim-candidate-position 1)
           (pyim-process-ui-refresh))))))
 
-(defun pyim-process-ui-refresh (&optional hightlight-current)
-  "刷新 pyim 相关 UI."
-  (run-hook-with-args 'pyim-process-ui-refresh-hook hightlight-current))
-
-(defun pyim-process-self-insert-command-p (cmd)
-  "测试 CMD 是否是一个 pyim self insert command."
-  (member cmd pyim-process-self-insert-commands))
-
-(defun pyim-process-get-candidates-delay ()
+(defun pyim-process-run-delay ()
   "延迟获取候选词条。
 
 当用户选择词条时,如果停顿时间超过某个阈值,就激活延迟候选词获取
 流程,不同的输入法对延迟获取候选词流程定义也可能不同,比如:全拼
 输入法目前的延迟获取候选词流程是搜索当前 buffer 获取词条。而 rime
 的延迟获取候选词流程是获取所有的词条。"
-  (pyim-process-get-candidates-delay-timer-reset)
-  (setq pyim-process-get-candidates-delay-timer
+  (pyim-process-run-delay-timer-reset)
+  (setq pyim-process-run-delay-timer
         (run-with-timer
-         pyim-process-get-candidates-delay
-         nil #'pyim-process-get-candidates-delay-1)))
+         pyim-process-run-delay
+         nil #'pyim-process-run-delay-1)))
+
+(defun pyim-process-run-delay-1 ()
+  "Function used by `pyim-process-run-delay-timer'"
+  (pyim-process-handle-candidates-async)
+  (pyim-process-handle-candidates-limit-time))
 
-(defun pyim-process-get-candidates-delay-1 ()
-  "Function used by `pyim-process-get-candidates-delay-timer'"
+(defun pyim-process-handle-candidates-limit-time ()
+  "使用限时的方式获取候选词。"
   (let* ((scheme (pyim-scheme-current))
          (words (delete-dups
-                 (pyim-candidates-create-delay
+                 (pyim-candidates-create-limit-time
                   pyim-imobjs scheme
                   pyim-candidates))))
     (when words
       (setq pyim-candidates words)
       (pyim-process-ui-refresh))))
 
-(defun pyim-process-get-candidates-delay-timer-reset ()
-  "Reset `pyim-process-get-candidates-delay-timer'."
-  (when pyim-process-get-candidates-delay-timer
-    (cancel-timer pyim-process-get-candidates-delay-timer)
-    (setq pyim-process-get-candidates-delay-timer nil)))
+(defun pyim-process-ui-refresh (&optional hightlight-current)
+  "刷新 pyim 相关 UI."
+  (run-hook-with-args 'pyim-process-ui-refresh-hook hightlight-current))
 
-(defun pyim-process-get-candidates-async ()
+(defun pyim-process-handle-candidates-async ()
   "使用异步的方式获取候选词条词条。"
   (let ((buffer (current-buffer)))
     (pyim-candidates-create-async
@@ -401,6 +395,16 @@ entered (nihaom) 的第一个候选词。
                     ,@(cdr pyim-candidates))))
            (pyim-process-ui-refresh)))))))
 
+(defun pyim-process-run-delay-timer-reset ()
+  "Reset `pyim-process-run-delay-timer'."
+  (when pyim-process-run-delay-timer
+    (cancel-timer pyim-process-run-delay-timer)
+    (setq pyim-process-run-delay-timer nil)))
+
+(defun pyim-process-self-insert-command-p (cmd)
+  "测试 CMD 是否是一个 pyim self insert command."
+  (member cmd pyim-process-self-insert-commands))
+
 (defun pyim-process-get-candidates ()
   pyim-candidates)
 
@@ -704,7 +708,7 @@ BUG:拼音无法有效地处理多音字。"
   (setq pyim-process-force-input-chinese nil)
   (setq pyim-candidates nil)
   (setq pyim-candidates-last nil)
-  (pyim-process-get-candidates-delay-timer-reset)
+  (pyim-process-run-delay-timer-reset)
   (pyim-process-ui-hide))
 
 (defun pyim-process-ui-hide ()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]