Nothing Special   »   [go: up one dir, main page]

Skip to content

TActionManager

Ivan Semenkov edited this page Oct 30, 2021 · 1 revision

Table of contents

About

TActionManager is advanced version of TEventManager mechanism for subscribe and fire events.

uses
  advutils.action;
  
type
  TActionManager = class

TActionState

Values enumerates possible TAction states and action subscribe filters.

uses
  advutils.action;
  
type
  TActionState = (

ACTION_RUN

Subscribe filter for action run.

    ACTION_RUN,

ACTION_FREEZE

Subscribe filter for freeze action.

    ACTION_FREEZE,

ACTION_UNFREEZE

Subscribe filter for unfreeze action.

    ACTION_UNFREEZE,

ACTION_DELETE

Subscribe filter for delete action.

    ACTION_DELETE,

ACTION_ANY

Subscribe filter for all action state changes.

    ACTION_ANY
  );

TRunStrategy

The namespace collected action run strategies.

TStrategy

Base class for all action run strategies.

uses
  advutils.action;
  
type
  TRunStrategy = class
    type
      TStrategy = class
  
  end;
After

Method calls after action run and will return current action state.

function After : TActionState; virtual;

TNormal

Run strategy for normally runs action.

uses
  advutils.action;
  
type
  TRunStrategy = class
  type
    TNormal = class(TStrategy)
    
  end;

TOnce

Run strategy which describe to run action only once.

uses
  advutils.action;
  
type
  TRunStrategy = class
  type
    TOnce = class
    
  end;

TTimes

Run strategy which describe to run action concrete times number and would delete it.

uses
  advutils.action;
  
type
  TRunAction = class
  type
    TTimes = class
    
  end;
Create

Times number to runs can setup by constructor.

constructor Create(ATimes : Cardinal);

Create custom strategy

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;

TFreezeStrategy

The namespace collected action freeze strategies.

TStrategy

Base class for all action freeze strategies.

uses
  advutils.action;

type
  TFreezeStrategy = class
  type
    TStrategy = class
    
  end;
Freeze

Method to freeze action.

procedure Freeze;
UnFreeze

Method to unfreeze action.

procedure UnFreeze;
FreezeReset

Method to warranty unfreeze action.

procedure FreezeReset;
IsFreeze

Return true if action is freeze.

function IsFreeze : Boolean;

TNone

Freeze strategy which describe that this action cannot be freezed.

uses
  advutils.action;
  
type
  TFreezeStrategy = class
  type
    TNone = class
    
  end;

TNormal

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;

TTimes

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;

TActionID

Every action has unique identifier using which can subscribe or fire it.

type
  TActionID = type Cardinal;

TAction

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

Create new action possible by its constructor.

constructor Create (ActionID : TActionID; ARunStrategy : TRunStrategy.TStrategy; 
  AFreezeStrategy : TFreezeStrategy.TStrategy);

Run

Do run action.

function Run (AData : TAnyValue) : TActionState;

Action returns its new state.

Freeze

Freeze action if possible.

procedure Freeze;

UnFreeze

Unfreeze action.

procedure UnFreeze;

FreezeReset

Warranty unfreeze action.

procedure FreezeReset;

IsFreeze

Return action freeze state.

function IsFreeze : Boolean;

RunAction

Abstract procedure in which action does job. Needs override in child classes.

protected
  procedure RunAction (AData : TAnyValue); virtual; abstract;

ID

Return action id value.

property ID : TActionID;

TActionManager

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

TActionCallback

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.

Register

All actions before runs must be register.

procedure Register (Action : TAction);
Example
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

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.

Example
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.

Example
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;

Freeze

Method freeze action by action id.

procedure Freeze (ActionID : TActionID);
Example
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;

UnFreeze

Method unfreeze action by action id.

procedure UnFreeze (ActionID : TActionID);
Example
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;

FreezeReset

Method varranty unfreeze action by action id.

procedure FreezeReset (ActionID : TActionID);
Example
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

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.

Example
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;