event.tin 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #function {__event_init__} {
  2. #var gEventHandlers {};
  3. #var gValidEvent {};
  4. };
  5. #function {__xtt_event_name_is_valid__} {
  6. #local name {%1};
  7. #if { "$name" == "{[_a-zA-Z]([./_a-zA-Z0-9-]*[./_a-zA-Z0-9])?}" } {
  8. #return {true};
  9. };
  10. #return {false};
  11. };
  12. #alias {event.define} {
  13. #local name {%1};
  14. #local type {%2};
  15. #local module {%3};
  16. #local desc {%4};
  17. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  18. errLog 事件名称不是合法的标识符名称。;
  19. #return;
  20. };
  21. #if { "$type" == "" } {
  22. #local type {无参};
  23. };
  24. #if { "$type" != "{有参|无参}" } {
  25. errLog 事件类型只能是「有参」和「无参」两者之一,省略表示「无参」。;
  26. #return;
  27. };
  28. #var {gValidEvent[$name]} {
  29. {type}{$type}
  30. {module}{$module}
  31. {desc}{$desc}
  32. };
  33. };
  34. #alias {event.list} {
  35. #local event {};
  36. #if { &gValidEvent[] <= 0 } {
  37. infoLog 尚未定义任何事件。;
  38. #return;
  39. };
  40. #echo {%h} { 已经定义的事件列表 };
  41. #echo {%-20s %-5s %-20s %s} {事件/已注册的钩子} {类型} {模块} {说明/代码};
  42. #echo {%-20s %-5s %-20s %s} {--------------------} {----} {----------} {------------};
  43. #foreach {*gValidEvent[]} {event} {
  44. #local type {有参};
  45. #if { "$gValidEvent[$event][type]" == "{无参|}" } {
  46. #local type {无参};
  47. };
  48. #echo {%-20s %-5s %-20s %s} {$event} {$type} {$gValidEvent[$event][module]} {$gValidEvent[$event][desc]};
  49. #local hook {};
  50. #local count {0};
  51. #foreach {*gEventHandlers[$event][]} {hook} {
  52. #local len {1};
  53. #format len {%L} {$hook};
  54. #math len {16 - $len};
  55. #math count {$count + 1};
  56. #local lead {├};
  57. #if { $count == &gEventHandlers[$event][] } {
  58. #Local lead {╰};
  59. };
  60. #echo { $lead@repeat{$len;─} %s %-20s %s}{$hook}
  61. {$gEventHandlers[$event][$hook][module]}
  62. {$gEventHandlers[$event][$hook][code]};
  63. };
  64. };
  65. #echo {%h};
  66. };
  67. #alias {event.emit} {
  68. #local name {%1};
  69. #local pHook {%2};
  70. #local args {%3};
  71. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  72. errLog 事件名称不是合法的标识符名称。;
  73. #return;
  74. };
  75. #if { "$gValidEvent[$name]" == "" } {
  76. errLog 未定义的事件名称: $name;
  77. #return;
  78. };
  79. #if { &gEventHandlers[$name][] <= 0 } {
  80. #return;
  81. };
  82. #local hook {};
  83. #foreach {*gEventHandlers[$name][]} {hook} {
  84. #local options {$gEventHandlers[$name][$hook][options]};
  85. #local code {$gEventHandlers[$name][$hook][code]};
  86. #nop 如果发射事件时指定了 pHook,则只唤醒指定的 hook,注意这里的 pHook 支持通配符;
  87. #if { "$pHook" != "" && "$hook" != "$pHook" } {
  88. #return;
  89. };
  90. #if { "$options[justOnce]" == "true" } {
  91. #unvar {gEventHandlers[$name][$hook]};
  92. };
  93. #if { "$args" == "" || "$gValidEvent[$name][type]" == "无参" } {
  94. $code;
  95. };
  96. #else {
  97. $code {$args};
  98. };
  99. };
  100. };
  101. #alias {event.handle} {
  102. #local name {%1};
  103. #local hook {%2};
  104. #local module {%3};
  105. #local code {%4};
  106. #if { "$name" == "" || "$hook" == "" || "$module" == "" || "$code" == "" } {
  107. #return;
  108. };
  109. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  110. errLog 事件名称不是合法的标识符名称。;
  111. #return;
  112. };
  113. #var {gEventHandlers[$name][$hook]} {
  114. {module}{$module}
  115. {code}{$code}
  116. };
  117. };
  118. #alias {event.unhandle} {
  119. #local name {%1};
  120. #local hook {%2};
  121. #if { "$name" == "" || "$hook" == "" } {
  122. #return;
  123. };
  124. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  125. errLog 事件名称不是合法的标识符名称。;
  126. #return;
  127. };
  128. #unvar {gEventHandlers[$name][$hook]};
  129. };
  130. #alias {event.handleOnce} {
  131. #local name {%1};
  132. #local hook {%2};
  133. #local module {%3};
  134. #local code {%4};
  135. #if { "$name" == "" || "$hook" == "" || "$module" == "" || "$code" == "" } {
  136. #return;
  137. };
  138. #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
  139. errLog 事件名称不是合法的标识符名称。;
  140. #return;
  141. };
  142. #var {gEventHandlers[$name][$hook]} {
  143. {options}{{justOnce}{true}}
  144. {module}{$module}
  145. {code}{$code}
  146. };
  147. };