浏览代码

change some code naming

wangweimin 5 年之前
父节点
当前提交
014e164db8
共有 5 个文件被更改,包括 41 次插入41 次删除
  1. 28 28
      pywebio/html/js/form.js
  2. 3 3
      pywebio/io_ctrl.py
  3. 1 1
      pywebio/session/base.py
  4. 5 5
      pywebio/session/coroutinebased.py
  5. 4 4
      pywebio/session/threadbased.py

+ 28 - 28
pywebio/html/js/form.js

@@ -222,7 +222,7 @@
         var val = $(this_ele).val();
         WebIOSession_.send_message({
             event: "callback",
-            coro_id: callback_id,
+            task_id: callback_id,
             data: val
         });
     }
@@ -235,12 +235,12 @@
         this.webio_session = webio_session;
         this.container_elem = container_elem;
 
-        this.form_ctrls = new LRUMap(); // coro_id -> stack of FormGroupController
+        this.form_ctrls = new LRUMap(); // task_id -> stack of FormGroupController
 
-        // hide old_ctrls显示的表单,激活coro_id对应的表单
-        // 需要保证 coro_id 对应有表单
-        this._activate_form = function (coro_id, old_ctrl) {
-            var ctrls = this.form_ctrls.get_value(coro_id);
+        // hide old_ctrls显示的表单,激活 task_id 对应的表单
+        // 需要保证 task_id 对应有表单
+        this._activate_form = function (task_id, old_ctrl) {
+            var ctrls = this.form_ctrls.get_value(task_id);
             var ctrl = ctrls[ctrls.length - 1];
             if (ctrl === old_ctrl || old_ctrl === undefined) {
                 console.log('开:%s', ctrl.spec.label);
@@ -249,7 +249,7 @@
                         $('[auto_focus]').focus();
                 });
             }
-            this.form_ctrls.move_to_top(coro_id);
+            this.form_ctrls.move_to_top(task_id);
             var that = this;
             old_ctrl.element.hide(100, () => {
                 // ctrl.element.show(100);
@@ -285,33 +285,33 @@
         this.handle_message = function (msg) {
             var old_ctrls = this.form_ctrls.get_top();
             var old_ctrl = old_ctrls && old_ctrls[old_ctrls.length - 1];
-            var target_ctrls = this.form_ctrls.get_value(msg.coro_id);
+            var target_ctrls = this.form_ctrls.get_value(msg.task_id);
             if (target_ctrls === undefined) {
-                this.form_ctrls.push(msg.coro_id, []);
-                target_ctrls = this.form_ctrls.get_value(msg.coro_id);
+                this.form_ctrls.push(msg.task_id, []);
+                target_ctrls = this.form_ctrls.get_value(msg.task_id);
             }
 
             // 创建表单
             if (msg.command in make_set(['input', 'input_group'])) {
-                var ctrl = new FormController(this.webio_session, msg.coro_id, msg.spec);
+                var ctrl = new FormController(this.webio_session, msg.task_id, msg.spec);
                 target_ctrls.push(ctrl);
                 this.container_elem.append(ctrl.element);
-                this._activate_form(msg.coro_id, old_ctrl);
+                this._activate_form(msg.task_id, old_ctrl);
             } else if (msg.command in make_set(['update_input'])) {
                 // 更新表单
                 if (target_ctrls.length === 0) {
-                    return console.error('No form to current message. coro_id:%s', msg.coro_id);
+                    return console.error('No form to current message. task_id:%s', msg.task_id);
                 }
                 target_ctrls[target_ctrls.length - 1].dispatch_ctrl_message(msg.spec);
                 // 表单前置 removed
-                // this._activate_form(msg.coro_id, old_ctrl);
+                // this._activate_form(msg.task_id, old_ctrl);
             } else if (msg.command === 'destroy_form') {
                 if (target_ctrls.length === 0) {
-                    return console.error('No form to current message. coro_id:%s', msg.coro_id);
+                    return console.error('No form to current message. task_id:%s', msg.task_id);
                 }
                 var deleted = target_ctrls.pop();
                 if (target_ctrls.length === 0)
-                    this.form_ctrls.remove(msg.coro_id);
+                    this.form_ctrls.remove(msg.task_id);
 
                 // 销毁的是当前显示的form
                 if (old_ctrls === target_ctrls) {
@@ -342,9 +342,9 @@
     }
 
 
-    function FormController(webio_session, coro_id, spec) {
+    function FormController(webio_session, task_id, spec) {
         this.webio_session = webio_session;
-        this.coro_id = coro_id;
+        this.task_id = task_id;
         this.spec = spec;
 
         this.element = undefined;
@@ -386,7 +386,7 @@
                 var ctrl_cls = this.input_controllers[i];
                 // console.log(ctrl_cls, ctrl_cls.prototype.accept_input_types);
                 if (input_spec.type in make_set(ctrl_cls.prototype.accept_input_types)) {
-                    ctrl = new ctrl_cls(this.webio_session, this.coro_id, input_spec);
+                    ctrl = new ctrl_cls(this.webio_session, this.task_id, input_spec);
                     break;
                 }
             }
@@ -408,7 +408,7 @@
             });
             that.webio_session.send_message({
                 event: "from_submit",
-                coro_id: that.coro_id,
+                task_id: that.task_id,
                 data: data
             });
         });
@@ -423,9 +423,9 @@
     };
 
 
-    function FormItemController(webio_session, coro_id, spec) {
+    function FormItemController(webio_session, task_id, spec) {
         this.webio_session = webio_session;
-        this.coro_id = coro_id;
+        this.task_id = task_id;
         this.spec = spec;
         this.element = undefined;
 
@@ -434,7 +434,7 @@
             var this_elem = $(this);
             that.webio_session.send_message({
                 event: "input_event",
-                coro_id: that.coro_id,
+                task_id: that.task_id,
                 data: {
                     event_name: e.type.toLowerCase(),
                     name: that.spec.name,
@@ -478,7 +478,7 @@
     }
 
 
-    function CommonInputController(webio_session, coro_id, spec) {
+    function CommonInputController(webio_session, task_id, spec) {
         FormItemController.apply(this, arguments);
 
         this.create_element();
@@ -559,7 +559,7 @@
         return this.element.find('input,select').val();
     };
 
-    function TextareaInputController(webio_session, coro_id, spec) {
+    function TextareaInputController(webio_session, task_id, spec) {
         FormItemController.apply(this, arguments);
 
         this.create_element();
@@ -636,7 +636,7 @@
     };
 
 
-    function CheckboxRadioController(webio_session, coro_id, spec) {
+    function CheckboxRadioController(webio_session, task_id, spec) {
         FormItemController.apply(this, arguments);
 
         this.create_element();
@@ -715,7 +715,7 @@
         }
     };
 
-    function ButtonsController(webio_session, coro_id, spec) {
+    function ButtonsController(webio_session, task_id, spec) {
         FormItemController.apply(this, arguments);
 
         this.last_checked_value = null;  // 上次点击按钮的value
@@ -765,7 +765,7 @@
         return this.last_checked_value;
     };
 
-    function FileInputController(webio_session, coro_id, spec) {
+    function FileInputController(webio_session, task_id, spec) {
         FormItemController.apply(this, arguments);
         this.data_url_value = null;
         this.create_element();

+ 3 - 3
pywebio/io_ctrl.py

@@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
 
 
 def send_msg(cmd, spec=None):
-    msg = dict(command=cmd, spec=spec, coro_id=get_current_task_id())
+    msg = dict(command=cmd, spec=spec, task_id=get_current_task_id())
     get_current_session().send_task_command(msg)
 
 
@@ -145,5 +145,5 @@ def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_funcs):
 
 
 def output_register_callback(callback, **options):
-    coro_id = get_current_session().register_callback(callback, **options)
-    return coro_id
+    task_id = get_current_session().register_callback(callback, **options)
+    return task_id

+ 1 - 1
pywebio/session/base.py

@@ -57,7 +57,7 @@ class AbstractSession:
     def register_callback(self, callback, **options):
         """ 向Session注册一个回调函数,返回回调id
 
-        Session需要保证当收到前端发送的事件消息 ``{event: "callback",coro_id: 回调id, data:...}`` 时,
+        Session需要保证当收到前端发送的事件消息 ``{event: "callback",task_id: 回调id, data:...}`` 时,
         ``callback`` 回调函数被执行, 并传入事件消息中的 ``data`` 字段值作为参数
         """
         raise NotImplementedError

+ 5 - 5
pywebio/session/coroutinebased.py

@@ -54,15 +54,15 @@ class CoroutineBasedSession(AbstractSession):
 
     def __init__(self, coroutine_func, on_task_command=None, on_session_close=None):
         """
-        :param coro_func: 协程函数
-        :param on_coro_msg: 由协程内发给session的消息的处理函数
+        :param coroutine_func: 协程函数
+        :param on_task_command: 由协程内发给session的消息的处理函数
         :param on_session_close: 会话结束的处理函数。后端Backend在相应on_session_close时关闭连接时,需要保证会话内的所有消息都传送到了客户端
         """
         self._on_task_command = on_task_command or (lambda _: None)
         self._on_session_close = on_session_close or (lambda: None)
         self.unhandled_task_msgs = []
 
-        self.coros = {}  # coro_id -> coro
+        self.coros = {}  # coro_task_id -> coro
 
         self._closed = False
         self.inactive_coro_instances = []  # 待激活的协程实例列表
@@ -109,7 +109,7 @@ class CoroutineBasedSession(AbstractSession):
 
         :param dict event: 事件️消息
         """
-        coro_id = event['coro_id']
+        coro_id = event['task_id']
         coro = self.coros.get(coro_id)
         if not coro:
             logger.error('coro not found, coro_id:%s', coro_id)
@@ -179,7 +179,7 @@ class CoroutineBasedSession(AbstractSession):
                     try:
                         callback(event['data'])
                     except:
-                        AsyncCoroutineBasedSessionBasedSession.get_current_session().on_task_exception()
+                        CoroutineBasedSession.get_current_session().on_task_exception()
 
                 if coro is not None:
                     if mutex_mode:

+ 4 - 4
pywebio/session/threadbased.py

@@ -111,7 +111,7 @@ class ThreadBasedSession(AbstractSession):
 
         :param dict event: 事件️消息
         """
-        task_id = event['coro_id']
+        task_id = event['task_id']
         mq = self.event_mqs.get(task_id)
         if not mq and task_id in self.callbacks:
             mq = self.callback_mq
@@ -187,9 +187,9 @@ class ThreadBasedSession(AbstractSession):
             event = self.callback_mq.get()
             if event is None:  # 结束信号
                 break
-            callback_info = self.callbacks.get(event['coro_id'])
+            callback_info = self.callbacks.get(event['task_id'])
             if not callback_info:
-                logger.error("No callback for coro_id:%s", event['coro_id'])
+                logger.error("No callback for task_id:%s", event['task_id'])
                 return
             callback, mutex = callback_info
 
@@ -210,7 +210,7 @@ class ThreadBasedSession(AbstractSession):
     def register_callback(self, callback, serial_mode=False):
         """ 向Session注册一个回调函数,返回回调id
 
-        Session需要保证当收到前端发送的事件消息 ``{event: "callback",coro_id: 回调id, data:...}`` 时,
+        Session需要保证当收到前端发送的事件消息 ``{event: "callback",task_id: 回调id, data:...}`` 时,
         ``callback`` 回调函数被执行, 并传入事件消息中的 ``data`` 字段值作为参数
 
         :param bool serial_mode: 串行模式模式。若为 ``True`` ,则对于同一组件的点击事件,串行执行其回调函数