event.tin 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #nop vim: set filetype=tt:;
  2. /*
  3. 本文件属于 PaoTin++ 的一部分
  4. ===========
  5. PaoTin++ © 2020~2023 的所有版权均由担子炮(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 %-30s %s} {事件/已注册的钩子} {类型} {模块} {说明/代码};
  59. #echo {%-20s %-5s %-30s %s} {@str.Repeat{20;-}} {----} {@str.Repeat{30;-}} {------------};
  60. #foreach {*gValidEvent[]} {event} {
  61. #local type {有参};
  62. #if { "$gValidEvent[$event][type]" == "{无参|}" } {
  63. #local type {无参};
  64. };
  65. #echo {%-20s %-5s %-30s %s}{$event} {$type}
  66. {@genModuleLink{$gValidEvent[$event][module];MOD}}
  67. {$gValidEvent[$event][desc]};
  68. #local hook {};
  69. #local count {0};
  70. #foreach {*gEventHandlers[$event][]} {hook} {
  71. #local len {1};
  72. #format len {%L} {$hook};
  73. #math len {16 - $len};
  74. #math count {$count + 1};
  75. #local lead {├};
  76. #if { $count == &gEventHandlers[$event][] } {
  77. #Local lead {╰};
  78. };
  79. #echo { $lead@str.Repeat{$len;─} %s %-30s %s}{$hook}
  80. {@genModuleLink{$gEventHandlers[$event][$hook][module];MOD}}
  81. {$gEventHandlers[$event][$hook][code]};
  82. };
  83. };
  84. #echo {%h};
  85. };
  86. #alias {event.Emit} {
  87. #local name {%1};
  88. #local pHook {%2};
  89. #local args {%3};
  90. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  91. errLog 事件名称不是合法的标识符名称。;
  92. #return;
  93. };
  94. #if { "$gValidEvent[$name]" == "" } {
  95. errLog 未定义的事件名称: $name;
  96. #return;
  97. };
  98. #if { &gEventHandlers[$name][] <= 0 } {
  99. #return;
  100. };
  101. #local hook {};
  102. #foreach {*gEventHandlers[$name][]} {hook} {
  103. #local options {$gEventHandlers[$name][$hook][options]};
  104. #local code {$gEventHandlers[$name][$hook][code]};
  105. #nop 如果发射事件时指定了 pHook,则只唤醒指定的 hook,注意这里的 pHook 支持通配符;
  106. #if { "$pHook" != "" && "$hook" != "$pHook" } {
  107. #return;
  108. };
  109. #if { "$options[justOnce]" == "true" } {
  110. #unvar {gEventHandlers[$name][$hook]};
  111. };
  112. #if { "$args" == "" || "$gValidEvent[$name][type]" == "无参" } {
  113. $code;
  114. };
  115. #else {
  116. $code {$args};
  117. };
  118. };
  119. };
  120. #alias {event.Handle} {
  121. #local name {%1};
  122. #local hook {%2};
  123. #local module {%3};
  124. #local code {%4};
  125. #if { "$name" == "" || "$hook" == "" || "$module" == "" || "$code" == "" } {
  126. #return;
  127. };
  128. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  129. errLog 事件名称不是合法的标识符名称。;
  130. #return;
  131. };
  132. #var {gEventHandlers[$name][$hook]} {
  133. {module}{$module}
  134. {code}{$code}
  135. };
  136. };
  137. #alias {event.UnHandle} {
  138. #local name {%1};
  139. #local hook {%2};
  140. #if { "$name" == "" || "$hook" == "" } {
  141. #return;
  142. };
  143. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  144. errLog 事件名称不是合法的标识符名称。;
  145. #return;
  146. };
  147. #unvar {gEventHandlers[$name][$hook]};
  148. };
  149. #alias {event.HandleOnce} {
  150. #local name {%1};
  151. #local hook {%2};
  152. #local module {%3};
  153. #local code {%4};
  154. #if { "$name" == "" || "$hook" == "" || "$module" == "" || "$code" == "" } {
  155. #return;
  156. };
  157. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  158. errLog 事件名称不是合法的标识符名称。;
  159. #return;
  160. };
  161. #var {gEventHandlers[$name][$hook]} {
  162. {options}{{justOnce}{true}}
  163. {module}{$module}
  164. {code}{$code}
  165. };
  166. };