Idea
There's already an Event system in ManiaScript.
There's already the notion of Custom Events to communicate between CUiLayer / Main script.
How cool would it be to allow a Manialink page (CMlScript context) or a ManiaApp to dispatch a custom event to itself, retrievable in PendingEvents. It would allow a very simple event-based flow for developing maniascript applications.
We are already using a ManiaScript implementation of this pattern. While it works okay and provides a nice proof of concept for the pattern itelf, there are some drawbacks:
* Dependency requirement
* Code requirement to init the lib, run the loop, etc.
* Weird code because of Event representation as an array
Poor man's implementation
Here is our current implementation:
Code: Select all
declare Text[][][] G_Event_PendingEvents;
declare Text[][][] G_Event_UpcomingEvents;
Void Event_Init() {
}
Void Event_DispatchCustomEvent(Text _Type, Text[] _Data) {
if(_Data.count > 0)
{
Debug_Log("[Event] "^_Type^" "^_Data[0]);
}
else
{
Debug_Log("[Event] "^_Type);
}
G_Event_UpcomingEvents.add([[_Type], _Data]);
}
Void Event_DispatchCustomEvent(Text _Type) {
Event_DispatchCustomEvent(_Type, Text[]);
}
Text[][][] Event_PendingEvents() {
return G_Event_PendingEvents;
}
Void Event_Yield() {
G_Event_PendingEvents = G_Event_UpcomingEvents;
if(G_Event_UpcomingEvents.count > 0) {
G_Event_UpcomingEvents = Text[][][];
}
}
And here's an example usage with our HTTP library. It dispatches events (HTTP.Success or HTTP.Error) whenever a request finishes.
Somewhere you fire an async HTTP request:
Code: Select all
declare Ident G_HomeRequestId;
G_HomeRequestId = HTTP_GetAsync("http://example.com/some_xml_webservice");
Code: Select all
foreach(Event in Event_PendingEvents())
{
switch(Event[0][0]) {
case "HTTP.Success": {
if(Event[1][0] == ""^G_HomeRequestId) {
declare CXmlDocument Xml <=> Xml.Create(Event[1][1]);
// So something with the response...
}
}
case "HTTP.Error": {
if(Event[1][0] == ""^G_HomeRequestId) {
// So something in case of error...
}
}
}
