| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #nop vim: set filetype=tt:;
- /*
- 本文件属于 PaoTin++ 的一部分
- ===========
- PaoTin++ © 2020~2022 的所有版权均由担子炮(dzp <danzipao@gmail.com>) 享有并保留一切法律权利
- 你可以在遵照 GPLv3 协议的基础之上使用、修改及重新分发本程序。
- ===========
- */
- #var lib_event[META] {
- {NAME} {事件驱动编程框架}
- {DESC} {提供基本的事件驱动编程 API,允许用户定义、发射、订阅事件}
- {AUTHOR} {担子炮}
- {NOTE} {本文件属于 PaoTin++ 的一部分}
- };
- #func {lib_event.Init} {
- #class data/lib/event open;
- #var gEventHandlers {};
- #var gValidEvent {};
- #class data/lib/event close;
- #return true;
- };
- #func {__xtt_event_name_is_valid__} {
- #local name {%1};
- #if { "$name" == "{[_a-zA-Z]([./_a-zA-Z0-9-]*[./_a-zA-Z0-9])?}" } {
- #return {true};
- };
-
- #return {false};
- };
- #alias {event.Define} {
- #local name {%1};
- #local type {%2};
- #local module {%3};
- #local desc {%4};
- #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
- errLog 事件名称不是合法的标识符名称。;
- #return;
- };
- #if { "$type" == "" } {
- #local type {无参};
- };
- #if { "$type" != "{有参|无参}" } {
- errLog 事件类型只能是「有参」和「无参」两者之一,省略表示「无参」。;
- #return;
- };
- #var {gValidEvent[$name]} {
- {type}{$type}
- {module}{$module}
- {desc}{$desc}
- };
- };
- #alias {event.List} {
- #local event {};
- #if { &gValidEvent[] <= 0 } {
- infoLog 尚未定义任何事件。;
- #return;
- };
- #echo {%h} { 已经定义的事件列表 };
- #echo {%-20s %-5s %-20s %s} {事件/已注册的钩子} {类型} {模块} {说明/代码};
- #echo {%-20s %-5s %-20s %s} {--------------------} {----} {----------} {------------};
- #foreach {*gValidEvent[]} {event} {
- #local type {有参};
- #if { "$gValidEvent[$event][type]" == "{无参|}" } {
- #local type {无参};
- };
- #echo {%-20s %-5s %-20s %s} {$event} {$type} {$gValidEvent[$event][module]} {$gValidEvent[$event][desc]};
- #local hook {};
- #local count {0};
- #foreach {*gEventHandlers[$event][]} {hook} {
- #local len {1};
- #format len {%L} {$hook};
- #math len {16 - $len};
- #math count {$count + 1};
- #local lead {├};
- #if { $count == &gEventHandlers[$event][] } {
- #Local lead {╰};
- };
- #echo { $lead@repeat{$len;─} %s %-20s %s}{$hook}
- {$gEventHandlers[$event][$hook][module]}
- {$gEventHandlers[$event][$hook][code]};
- };
- };
- #echo {%h};
- };
- #alias {event.Emit} {
- #local name {%1};
- #local pHook {%2};
- #local args {%3};
- #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
- errLog 事件名称不是合法的标识符名称。;
- #return;
- };
- #if { "$gValidEvent[$name]" == "" } {
- errLog 未定义的事件名称: $name;
- #return;
- };
- #if { &gEventHandlers[$name][] <= 0 } {
- #return;
- };
- #local hook {};
- #foreach {*gEventHandlers[$name][]} {hook} {
- #local options {$gEventHandlers[$name][$hook][options]};
- #local code {$gEventHandlers[$name][$hook][code]};
- #nop 如果发射事件时指定了 pHook,则只唤醒指定的 hook,注意这里的 pHook 支持通配符;
- #if { "$pHook" != "" && "$hook" != "$pHook" } {
- #return;
- };
- #if { "$options[justOnce]" == "true" } {
- #unvar {gEventHandlers[$name][$hook]};
- };
- #if { "$args" == "" || "$gValidEvent[$name][type]" == "无参" } {
- $code;
- };
- #else {
- $code {$args};
- };
- };
- };
- #alias {event.Handle} {
- #local name {%1};
- #local hook {%2};
- #local module {%3};
- #local code {%4};
- #if { "$name" == "" || "$hook" == "" || "$module" == "" || "$code" == "" } {
- #return;
- };
- #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
- errLog 事件名称不是合法的标识符名称。;
- #return;
- };
- #var {gEventHandlers[$name][$hook]} {
- {module}{$module}
- {code}{$code}
- };
- };
- #alias {event.UnHandle} {
- #local name {%1};
- #local hook {%2};
- #if { "$name" == "" || "$hook" == "" } {
- #return;
- };
- #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
- errLog 事件名称不是合法的标识符名称。;
- #return;
- };
- #unvar {gEventHandlers[$name][$hook]};
- };
- #alias {event.HandleOnce} {
- #local name {%1};
- #local hook {%2};
- #local module {%3};
- #local code {%4};
- #if { "$name" == "" || "$hook" == "" || "$module" == "" || "$code" == "" } {
- #return;
- };
- #if { "@__xtt_event_name_is_valid__{{$name}}" != "true" } {
- errLog 事件名称不是合法的标识符名称。;
- #return;
- };
- #var {gEventHandlers[$name][$hook]} {
- {options}{{justOnce}{true}}
- {module}{$module}
- {code}{$code}
- };
- };
|