MacroCommand

open class MacroCommand : Notifier, ICommand

A base ICommand implementation that executes other ICommands.

A MacroCommand maintains an list of ICommand Class references called SubCommands.

When execute is called, the MacroCommand retrieves ICommands by executing closures and then calls execute on each of its SubCommands turn. Each SubCommand will be passed a reference to the original INotification that was passed to the MacroCommand‘s execute method.

Unlike SimpleCommand, your subclass should not override execute, but instead, should override the initializeMacroCommand method, calling addSubCommand once for each SubCommand to be executed.

@see org.puremvc.swift.core.controller.Controller Controller

@see org.puremvc.swift.patterns.observer.Notification Notification

@see org.puremvc.swift.patterns.command.SimpleCommand SimpleCommand

  • Constructor.

    You should not need to define a constructor, instead, override the initializeMacroCommand method.

    If your subclass does define a constructor, be sure to call super().

    Declaration

    Swift

    public override init()
  • Initialize the MacroCommand.

    In your subclass, override this method to initialize the MacroCommand‘s SubCommand list with closure references like this:

    // Initialize MyMacroCommand
    public func addSubCommand(closure: () -> ICommand) {
        addSubCommand( { FirstCommand() } );
        addSubCommand( { SecondCommand() } );
        addSubCommand( ) { ThirdCommand() }; //or by using a trailing closure
    }
    

    Note that SubCommands may be any closure returning ICommand implementor, MacroCommands or SimpleCommands are both acceptable.

    Declaration

    Swift

    open func initializeMacroCommand()
  • Add a SubCommand.

    The SubCommands will be called in First In/First Out (FIFO) order.

    Declaration

    Swift

    open func addSubCommand(_ factory: @escaping () -> ICommand)

    Parameters

    closure

    reference that returns ICommand.

  • Execute this MacroCommand‘s SubCommands.

    The SubCommands will be called in First In/First Out (FIFO) order.

    Declaration

    Swift

    public final func execute(_ notification: INotification)

    Parameters

    notification

    the INotification object to be passsed to each SubCommand.