event.tin 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #nop vim: set filetype=tt:;
  2. /*
  3. 本文件属于 PaoTin++ 的一部分
  4. ===========
  5. PaoTin++ © 2020~2022 的所有版权均由担子炮(dzp <danzipao@gmail.com>) 享有并保留一切法律权利
  6. 你可以在遵照 GPLv3 协议的基础之上使用、修改及重新分发本程序。
  7. ===========
  8. */
  9. #var lib_event[META] {
  10. {NAME} {事件驱动编程框架}
  11. {DESC} {提供基本的事件驱动编程 API,允许用户定义、发射、订阅事件}
  12. {AUTHOR} {担子炮}
  13. {NOTE} {本文件属于 PaoTin++ 的一部分}
  14. };
  15. #func {lib_event.Init} {
  16. #class data/lib/event open;
  17. #var gEventHandlers {};
  18. #var gValidEvent {};
  19. #class data/lib/event close;
  20. #return true;
  21. };
  22. #func {__xtt_event_name_is_valid__} {
  23. #local name {%1};
  24. #if { "$name" == "{[_a-zA-Z]([./_a-zA-Z0-9-]*[./_a-zA-Z0-9])?}" } {
  25. #return {true};
  26. };
  27. #return {false};
  28. };
  29. #alias {event.Define} {
  30. #local name {%1};
  31. #local type {%2};
  32. #local module {%3};
  33. #local desc {%4};
  34. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  35. errLog 事件名称不是合法的标识符名称。;
  36. #return;
  37. };
  38. #if { "$type" == "" } {
  39. #local type {无参};
  40. };
  41. #if { "$type" != "{有参|无参}" } {
  42. errLog 事件类型只能是「有参」和「无参」两者之一,省略表示「无参」。;
  43. #return;
  44. };
  45. #var {gValidEvent[$name]} {
  46. {type}{$type}
  47. {module}{$module}
  48. {desc}{$desc}
  49. };
  50. };
  51. #alias {event.List} {
  52. #local event {};
  53. #if { &gValidEvent[] <= 0 } {
  54. infoLog 尚未定义任何事件。;
  55. #return;
  56. };
  57. #echo {%h} { 已经定义的事件列表 };
  58. #echo {%-20s %-5s %-20s %s} {事件/已注册的钩子} {类型} {模块} {说明/代码};
  59. #echo {%-20s %-5s %-20s %s} {--------------------} {----} {----------} {------------};
  60. #foreach {*gValidEvent[]} {event} {
  61. #local type {有参};
  62. #if { "$gValidEvent[$event][type]" == "{无参|}" } {
  63. #local type {无参};
  64. };
  65. #echo {%-20s %-5s %-20s %s} {$event} {$type} {$gValidEvent[$event][module]} {$gValidEvent[$event][desc]};
  66. #local hook {};
  67. #local count {0};
  68. #foreach {*gEventHandlers[$event][]} {hook} {
  69. #local len {1};
  70. #format len {%L} {$hook};
  71. #math len {16 - $len};
  72. #math count {$count + 1};
  73. #local lead {├};
  74. #if { $count == &gEventHandlers[$event][] } {
  75. #Local lead {╰};
  76. };
  77. #echo { $lead@repeat{$len;─} %s %-20s %s}{$hook}
  78. {$gEventHandlers[$event][$hook][module]}
  79. {$gEventHandlers[$event][$hook][code]};
  80. };
  81. };
  82. #echo {%h};
  83. };
  84. #alias {event.Emit} {
  85. #local name {%1};
  86. #local pHook {%2};
  87. #local args {%3};
  88. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  89. errLog 事件名称不是合法的标识符名称。;
  90. #return;
  91. };
  92. #if { "$gValidEvent[$name]" == "" } {
  93. errLog 未定义的事件名称: $name;
  94. #return;
  95. };
  96. #if { &gEventHandlers[$name][] <= 0 } {
  97. #return;
  98. };
  99. #local hook {};
  100. #foreach {*gEventHandlers[$name][]} {hook} {
  101. #local options {$gEventHandlers[$name][$hook][options]};
  102. #local code {$gEventHandlers[$name][$hook][code]};
  103. #nop 如果发射事件时指定了 pHook,则只唤醒指定的 hook,注意这里的 pHook 支持通配符;
  104. #if { "$pHook" != "" && "$hook" != "$pHook" } {
  105. #return;
  106. };
  107. #if { "$options[justOnce]" == "true" } {
  108. #unvar {gEventHandlers[$name][$hook]};
  109. };
  110. #if { "$args" == "" || "$gValidEvent[$name][type]" == "无参" } {
  111. $code;
  112. };
  113. #else {
  114. $code {$args};
  115. };
  116. };
  117. };
  118. #alias {event.Handle} {
  119. #local name {%1};
  120. #local hook {%2};
  121. #local module {%3};
  122. #local code {%4};
  123. #if { "$name" == "" || "$hook" == "" || "$module" == "" || "$code" == "" } {
  124. #return;
  125. };
  126. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  127. errLog 事件名称不是合法的标识符名称。;
  128. #return;
  129. };
  130. #var {gEventHandlers[$name][$hook]} {
  131. {module}{$module}
  132. {code}{$code}
  133. };
  134. };
  135. #alias {event.UnHandle} {
  136. #local name {%1};
  137. #local hook {%2};
  138. #if { "$name" == "" || "$hook" == "" } {
  139. #return;
  140. };
  141. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  142. errLog 事件名称不是合法的标识符名称。;
  143. #return;
  144. };
  145. #unvar {gEventHandlers[$name][$hook]};
  146. };
  147. #alias {event.HandleOnce} {
  148. #local name {%1};
  149. #local hook {%2};
  150. #local module {%3};
  151. #local code {%4};
  152. #if { "$name" == "" || "$hook" == "" || "$module" == "" || "$code" == "" } {
  153. #return;
  154. };
  155. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  156. errLog 事件名称不是合法的标识符名称。;
  157. #return;
  158. };
  159. #var {gEventHandlers[$name][$hook]} {
  160. {options}{{justOnce}{true}}
  161. {module}{$module}
  162. {code}{$code}
  163. };
  164. };