Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add MSU.Class.Event class #321

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions msu/classes/event.nut
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
::MSU.Class.Event <- class
{
__Functions = null;

constructor()
{
this.__Functions = [];
}

function register( _env, _function )
{
// ::MSU.requireFunction(_function);
foreach (i, funcInfo in this.__Functions)
{
if (funcInfo[1] == _function)
{
return;
}
}

this.__Functions.push([_env, _function]);
}

function unregister( _function )
{
// ::MSU.requireFunction(_function);
foreach (i, funcInfo in this.__Functions)
{
if (funcInfo[1] == _function)
{
this.__Functions.remove[i];
return;
}
}

throw ::MSU.Exception.KeyNotFound(_function);
}

function invoke( _argsArray = null )
{
if (_argsArray == null) _argsArray = [null];
else _argsArray.insert(0, null);

foreach (funcInfo in this.__Functions)
{
_argsArray[0] = funcInfo[0];
funcInfo[1].acall(_argsArray);
}
}

// Will iterate over every registered function and check _conditionFunction
// if _conditionFunction returns false, stops iteration immediately
Comment on lines +51 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels to me like this should perhaps be based on the actual functions that are being called,ie if they return false then stop or something along those lines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, those functions can be anything. They don't necessarily have to be Boolean returning functions. This conditionalInvoke is more along the lines of replicating the isAlive feature of skill_container events where the iteration stops if the actor is no longer alive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning false to stop an event from continuing feels quite natural, it's how it's done in JS event bubbling for example. Using some otherwise decoupled thing to check for a state is weird in contrast.

function conditionalInvoke( _conditionFunction, _argsArray = null )
{
if (_argsArray == null) _argsArray = [null];
else _argsArray.insert(0, null);

foreach (funcInfo in this.__Functions)
{
if (_conditionFunction() == false)
return;

_argsArray[0] = funcInfo[0];
funcInfo[1].acall(_argsArray);
}
}

function clear()
{
this.__Functions.clear();
}
}