event.tin 5.0 KB

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