-
Notifications
You must be signed in to change notification settings - Fork 3
TActionManager
TActionManager is advanced version of TEventManager mechanism for subscribe and fire events.
uses
advutils.action;
type
TActionManager = class
Values enumerates possible TAction states and action subscribe filters.
uses
advutils.action;
type
TActionState = (
Subscribe filter for action run.
ACTION_RUN,
Subscribe filter for freeze action.
ACTION_FREEZE,
Subscribe filter for unfreeze action.
ACTION_UNFREEZE,
Subscribe filter for delete action.
ACTION_DELETE,
Subscribe filter for all action state changes.
ACTION_ANY
);
The namespace collected action run strategies.
Base class for all action run strategies.
uses
advutils.action;
type
TRunStrategy = class
type
TStrategy = class
end;
Method calls after action run and will return current action state.
function After : TActionState; virtual;
Run strategy for normally runs action.
uses
advutils.action;
type
TRunStrategy = class
type
TNormal = class(TStrategy)
end;
Run strategy which describe to run action only once.
uses
advutils.action;
type
TRunStrategy = class
type
TOnce = class
end;
Run strategy which describe to run action concrete times number and would delete it.
uses
advutils.action;
type
TRunAction = class
type
TTimes = class
end;
Times number to runs can setup by constructor.
constructor Create(ATimes : Cardinal);
All run strategies must extended from TStrategy.
uses
advutils.action;
type
TCustomRunStrategy = class(TRunStrategy.TStrategy)
public
function After : TActionState; override;
begin
{ ... do something ... }
Result := ACTION_RUN;
end;
end;
The namespace collected action freeze strategies.
Base class for all action freeze strategies.
uses
advutils.action;
type
TFreezeStrategy = class
type
TStrategy = class
end;
Method to freeze action.
procedure Freeze;
Method to unfreeze action.
procedure UnFreeze;
Method to warranty unfreeze action.
procedure FreezeReset;
Return true if action is freeze.
function IsFreeze : Boolean;
Freeze strategy which describe that this action cannot be freezed.
uses
advutils.action;
type
TFreezeStrategy = class
type
TNone = class
end;
Freeze strategy which describe that this action freezes normally. Multiple times freeze do not have any effects.
uses
advutils.action;
type
TFreezeStrategy = class
type
TNormal = class
end;
Freeze strategy which describe that this action can be freeze. Multiple times freeze increases counter and will need the same times number to call unfreeze method.
uses
advutils.action;
type
TFreezeStrategy = class
type
TTimes = class
end;
Every action has unique identifier using which can subscribe or fire it.
type
TActionID = type Cardinal;
Action presents by class that encapsulates run and freeze strategies and can do job. TAction is a base abstract class for all custom actions.
Create new action possible by its constructor.
constructor Create (ActionID : TActionID; ARunStrategy : TRunStrategy.TStrategy;
AFreezeStrategy : TFreezeStrategy.TStrategy);
Do run action.
function Run (AData : TAnyValue) : TActionState;
Action returns its new state.
Freeze action if possible.
procedure Freeze;
Unfreeze action.
procedure UnFreeze;
Warranty unfreeze action.
procedure FreezeReset;
Return action freeze state.
function IsFreeze : Boolean;
Abstract procedure in which action does job. Needs override in child classes.
protected
procedure RunAction (AData : TAnyValue); virtual; abstract;
Return action id value.
property ID : TActionID;
Class register actions and provide methods to operated them by action id. Action manager also have mechanism to subscribe to different actions and can runs callback only when changes concrete state.
uses
advutils.action;
type
TActionManager = class
Type for action subscribe procedures.
uses
advutils.action;
type
TActionManager = class
type
TActionCallback = procedure (AData : TAnyValue; AdditionalData : TAnyValue) of object;
AData is a value which send to callback from action run method. AdditionalData saved on subscribe method and send it on action run to callback procedure.
All actions before runs must be register.
procedure Register (Action : TAction);
uses
advutils.action, utils.any;
type
TCustomAction = class(TAction)
protected
procedure RunAction (AData : TAnyValue); override;
begin
{ ... do something ... }
end;
end;
var
manager : TActionManager;
action : TCustomAction;
begin
manager := TActionManager.Create;
action := TCustomAction.Create(0, TRunStrategy.TNormal.Create,
TFreezeStrategy.TNormal.Create);
manager.Register(action);
FreeAndNil(manager);
end;
Run action can by call run method. Function return True if action was runs.
function Run (ActionID : TActionID; AData : TAnyValue = nil) : Boolean;
AData is a TAny value which sent to action run method.
uses
advutils.action, utils.any;
type
TCustomAction = class(TAction)
protected
procedure RunAction (AData : TAnyValue); override;
begin
{ ... do something ... }
end;
end;
var
manager : TActionManager;
action : TCustomAction;
begin
manager := TActionManager.Create;
action := TCustomAction.Create(0, TRunStrategy.TNormal.Create,
TFreezeStrategy.TNormal.Create);
manager.Register(action);
manager.Run(0, TIntegerValue.Create(-1));
FreeAndNil(manager);
end;
Run method has overloaded form which can register action before runs.
function Run (Action : TAction; AData : TAnyValue = nil) : Boolean;
AData is a TAny value which sent to action run method.
uses
advutils.action, utils.any;
type
TCustomAction = class(TAction)
protected
procedure RunAction (AData : TAnyValue); override;
begin
{ ... do something ... }
end;
end;
var
manager : TActionManager;
action : TCustomAction;
begin
manager := TActionManager.Create;
action := TCustomAction.Create(0, TRunStrategy.TNormal.Create,
TFreezeStrategy.TNormal.Create);
manager.Run(action, TIntegerValue.Create(-1));
FreeAndNil(manager);
end;
Method freeze action by action id.
procedure Freeze (ActionID : TActionID);
uses
advutils.action, utils.any;
type
TCustomAction = class(TAction)
protected
procedure RunAction (AData : TAnyValue); override;
begin
{ ... do something ... }
end;
end;
var
manager : TActionManager;
action : TCustomAction;
begin
manager := TActionManager.Create;
action := TCustomAction.Create(0, TRunStrategy.TNormal.Create,
TFreezeStrategy.TNormal.Create);
manager.Register(action);
manager.Freeze(0);
FreeAndNil(manager);
end;
Method unfreeze action by action id.
procedure UnFreeze (ActionID : TActionID);
uses
advutils.action, utils.any;
type
TCustomAction = class(TAction)
protected
procedure RunAction (AData : TAnyValue); override;
begin
{ ... do something ... }
end;
end;
var
manager : TActionManager;
action : TCustomAction;
begin
manager := TActionManager.Create;
action := TCustomAction.Create(0, TRunStrategy.TNormal.Create,
TFreezeStrategy.TNormal.Create);
manager.Register(action);
manager.Freeze(0);
manager.UnFreeze(0);
FreeAndNil(manager);
end;
Method varranty unfreeze action by action id.
procedure FreezeReset (ActionID : TActionID);
uses
advutils.action, utils.any;
type
TCustomAction = class(TAction)
protected
procedure RunAction (AData : TAnyValue); override;
begin
{ ... do something ... }
end;
end;
var
manager : TActionManager;
action : TCustomAction;
begin
manager := TActionManager.Create;
action := TCustomAction.Create(0, TRunStrategy.TNormal.Create,
TFreezeStrategy.TNormal.Create);
manager.Register(action);
manager.Freeze(0);
manager.FreezeReset(0);
FreeAndNil(manager);
end;
Subscribe for action can by subscribe procedure.
procedure Subscribe (ActionID : TActionID; AState : TActionState;
ACallback : TActionCallback; AdditionalData : TAnyValue = nil);
AState is action state to which needs run callback. Use ACTION_ANY to subscribe to all action state changes.
AdditionalValue is TAny value which sent into subscribe callback function on run.
Multiple callbacks can be subscribed to each action.
uses
advutils.action, utils.any;
type
TCustomAction = class(TAction)
protected
procedure RunAction (AData : TAnyValue); override;
begin
{ ... do something ... }
end;
end;
procedure Callback (AData : TAnyValue; AdditionalData : TAnyValue);
begin
writeln('It''s work!');
end;
var
manager : TActionManager;
action : TCustomAction;
begin
manager := TActionManager.Create;
action := TCustomAction.Create(0, TRunStrategy.TNormal.Create,
TFreezeStrategy.TNormal.Create);
manager.Register(action);
manager.Subscribe(0, ACTION_RUN, {$IFDEF FPC}@{$ENDIF}Callback, TIntegerValue.Create(-1));
FreeAndNil(manager);
end;