|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +exports.AsyncMacroCommand = void 0; |
| 4 | +const AsyncCommand_1 = require("./AsyncCommand"); |
| 5 | +const puremvc_typescript_multicore_framework_1 = require("@puremvc/puremvc-typescript-multicore-framework"); |
| 6 | +/** |
| 7 | + * A base <code>ICommand</code> implementation that executes other |
| 8 | + * <code>ICommand</code>s asynchronously. |
| 9 | + * |
| 10 | + * <P> |
| 11 | + * An <code>AsyncMacroCommand</code> maintains a list of |
| 12 | + * <code>ICommand</code> Class references called <i>SubCommands</i>.</P> |
| 13 | + * |
| 14 | + * <P> |
| 15 | + * When <code>execute</code> is called, the <code>AsyncMacroCommand</code> |
| 16 | + * caches a reference to the <code>INotification</code> and calls |
| 17 | + * <code>nextCommand</code>.</P> |
| 18 | + * |
| 19 | + * <P> |
| 20 | + * If there are still <i>SubCommands</i>'s to be executed, |
| 21 | + * the <code>nextCommand</code> method instantiates and calls <code>execute</code> |
| 22 | + * on each of its <i>SubCommands</i> in turn. Each <i>SubCommand</i> will be passed |
| 23 | + * a reference to the original <code>INotification</code> that was passed to the |
| 24 | + * <code>AsyncMacroCommand</code>'s <code>execute</code> method. If the |
| 25 | + * <i>SubCommand</i> to execute is an <code>IAsyncCommand</code>, the |
| 26 | + * next <i>SubCommand</i> will not be executed until the previous |
| 27 | + * <code>IAsyncCommand</code> has called its <i>commandComplete</i> method.</P> |
| 28 | + * |
| 29 | + * <P> |
| 30 | + * Unlike <code>AsyncCommand</code> and <code>SimpleCommand</code>, your subclass |
| 31 | + * should not override <code>execute</code>, but instead, should |
| 32 | + * override the <code>initializeAsyncMacroCommand</code> method, |
| 33 | + * calling <code>addSubCommand</code> once for each <i>SubCommand</i> |
| 34 | + * to be executed.</P> |
| 35 | + * |
| 36 | + * @see AsyncCommand |
| 37 | + */ |
| 38 | +class AsyncMacroCommand extends puremvc_typescript_multicore_framework_1.Notifier { |
| 39 | + /** |
| 40 | + * Constructor. |
| 41 | + * |
| 42 | + * <P> |
| 43 | + * You should not need to define a constructor, |
| 44 | + * instead, override the <code>initializeAsyncMacroCommand</code> |
| 45 | + * method.</P> |
| 46 | + * |
| 47 | + * <P> |
| 48 | + * If your subclass does define a constructor, be |
| 49 | + * sure to call <code>super()</code>.</P> |
| 50 | + */ |
| 51 | + constructor() { |
| 52 | + super(); |
| 53 | + this.note = null; |
| 54 | + this.onComplete = null; |
| 55 | + this.subCommands = new Array(); |
| 56 | + this.note = null; |
| 57 | + this.onComplete = null; |
| 58 | + this.initializeAsyncMacroCommand(); |
| 59 | + } |
| 60 | + /** |
| 61 | + * Initialize the <code>AsyncMacroCommand</code>. |
| 62 | + * |
| 63 | + * <P> |
| 64 | + * In your subclass, override this method to |
| 65 | + * initialize the <code>AsyncMacroCommand</code>'s <i>SubCommand</i> |
| 66 | + * list with <code>ICommand</code> class references. |
| 67 | + * </P> |
| 68 | + * |
| 69 | + * <listing> |
| 70 | + * // Initialize MyMacroCommand |
| 71 | + * override protected function initializeAsyncMacroCommand() : void |
| 72 | + * { |
| 73 | + * addSubCommand( () => new FirstCommand() ); |
| 74 | + * addSubCommand( () => SecondCommand() ); |
| 75 | + * addSubCommand( () => ThirdCommand() ); |
| 76 | + * } |
| 77 | + * </listing> |
| 78 | + * |
| 79 | + * <P> |
| 80 | + * Note that <i>SubCommand</i>s may be any <code>ICommand</code> implementor, |
| 81 | + * <code>AsyncMacroCommand</code>s, <code>AsyncCommand</code>s, |
| 82 | + * <code>MacroCommand</code>s or <code>SimpleCommands</code> are all acceptable. |
| 83 | + */ |
| 84 | + initializeAsyncMacroCommand() { } |
| 85 | + /** |
| 86 | + * Add a <i>SubCommand</i>. |
| 87 | + * <P> |
| 88 | + * The <i>SubCommands</i> will be called in First In/First Out (FIFO) |
| 89 | + * order.</P> |
| 90 | + * |
| 91 | + * @param factory a factory that returns an instance that implements <code>ICommand</code>. |
| 92 | + */ |
| 93 | + addSubCommand(factory) { |
| 94 | + this.subCommands.push(factory); |
| 95 | + } |
| 96 | + /** |
| 97 | + * Registers the callback for a parent <code>AsyncMacroCommand</code>. |
| 98 | + * |
| 99 | + * @param value The <code>AsyncMacroCommand</code> method to call on completion |
| 100 | + */ |
| 101 | + setOnComplete(value) { |
| 102 | + this.onComplete = value; |
| 103 | + } |
| 104 | + /** |
| 105 | + * Starts execution of this <code>AsyncMacroCommand</code>'s <i>SubCommands</i>. |
| 106 | + * |
| 107 | + * <P> |
| 108 | + * The <i>SubCommands</i> will be called in First In/First Out (FIFO) order. |
| 109 | + * </P> |
| 110 | + * |
| 111 | + * @param notification the <code>INotification</code> object to be passsed to each <i>SubCommand</i>. |
| 112 | + */ |
| 113 | + execute(notification) { |
| 114 | + this.note = notification; |
| 115 | + this.nextCommand(); |
| 116 | + } |
| 117 | + /** |
| 118 | + * Execute this <code>AsyncMacroCommand</code>'s next <i>SubCommand</i>. |
| 119 | + * |
| 120 | + * <P> |
| 121 | + * If the next <i>SubCommand</i> is asynchronous, a callback is registered for |
| 122 | + * the command completion, else the next command is run.</P> |
| 123 | + */ |
| 124 | + nextCommand() { |
| 125 | + if (this.subCommands.length > 0) { |
| 126 | + const factory = this.subCommands.shift(); |
| 127 | + const commandInstance = factory(); |
| 128 | + const isAsync = commandInstance instanceof AsyncMacroCommand || |
| 129 | + commandInstance instanceof AsyncCommand_1.AsyncCommand; |
| 130 | + if (isAsync) { |
| 131 | + commandInstance.setOnComplete(this.nextCommand); |
| 132 | + } |
| 133 | + commandInstance.initializeNotifier(this.multitonKey); |
| 134 | + if (this.note) |
| 135 | + commandInstance.execute(this.note); |
| 136 | + if (!isAsync) |
| 137 | + this.nextCommand(); |
| 138 | + } |
| 139 | + else { |
| 140 | + if (this.onComplete !== null) |
| 141 | + this.onComplete(); |
| 142 | + this.note = null; |
| 143 | + this.onComplete = null; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +exports.AsyncMacroCommand = AsyncMacroCommand; |
0 commit comments