|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var puremvcJsMulticoreFramework = require('@puremvc/puremvc-js-multicore-framework'); |
| 4 | + |
| 5 | +/* |
| 6 | + PureMVC MultiCore Utility for JS - AsyncCommand |
| 7 | + Copyright(c) 2008 Duncan Hall <duncan.hall@puremvc.org> |
| 8 | + 2024 Cliff Hall <cliff.hall@puremvc.org> |
| 9 | + Your reuse is governed by the Creative Commons Attribution 3.0 License |
| 10 | +*/ |
| 11 | + |
| 12 | +/** |
| 13 | + * A base IAsyncCommand implementation. |
| 14 | + * |
| 15 | + * Your subclass should override the `execute` |
| 16 | + * method where your business logic will handle the <code>Notification</code>. </P> |
| 17 | + * |
| 18 | + * @see AsyncMacroCommand |
| 19 | + */ |
| 20 | +class AsyncCommand extends puremvcJsMulticoreFramework.SimpleCommand { |
| 21 | + |
| 22 | + /** |
| 23 | + * Registers the callback for a parent <code>AsyncMacroCommand</code>. |
| 24 | + * |
| 25 | + * @param callback The <code>AsyncMacroCommand</code> method to call on completion |
| 26 | + */ |
| 27 | + setOnComplete ( callback ) |
| 28 | + { |
| 29 | + this.onComplete = callback; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Notify the parent AsyncMacroCommand that this command is complete. |
| 34 | + * |
| 35 | + * Call this method from your subclass to signify that your asynchronous |
| 36 | + * command has finished. |
| 37 | + */ |
| 38 | + commandComplete() |
| 39 | + { |
| 40 | + this.onComplete(); |
| 41 | + } |
| 42 | + |
| 43 | + onComplete; // the callback to invoke on command completion |
| 44 | + isAsync = true; // simplest workaround to lack of interfaces |
| 45 | +} |
| 46 | + |
| 47 | +/* |
| 48 | + PureMVC MultiCore Utility for JS - AsyncCommand |
| 49 | + Copyright(c) 2008 Duncan Hall <duncan.hall@puremvc.org> |
| 50 | + 2024 Cliff Hall <cliff.hall@puremvc.org> |
| 51 | + Your reuse is governed by the Creative Commons Attribution 3.0 License |
| 52 | +*/ |
| 53 | + |
| 54 | +/** |
| 55 | + * A base Command implementation that executes other |
| 56 | + * Commands asynchronously. |
| 57 | + |
| 58 | + * An AsyncMacroCommand maintains a list of |
| 59 | + * factories that create SubCommands. |
| 60 | + |
| 61 | + * When execute is called, the AsyncMacroCommand |
| 62 | + * caches a reference to the Notification and calls |
| 63 | + * nextCommand. |
| 64 | + |
| 65 | + * If there are still SubCommands to be executed, |
| 66 | + * the nextCommand method instantiates and calls |
| 67 | + * execute on each of its SubCommands in turn. |
| 68 | + * |
| 69 | + * Each SubCommand will be passed a reference to the |
| 70 | + * original Notification that was passed to the |
| 71 | + * AsyncMacroCommand's execute method. If the |
| 72 | + * SubCommand to execute is an IAsyncCommand, the |
| 73 | + * next SubCommand will not be executed until the |
| 74 | + * previousAsyncCommand has called its commandComplete |
| 75 | + * method. |
| 76 | + |
| 77 | + * Unlike AsyncCommand and SimpleCommand, your subclass |
| 78 | + * should not override execute, but instead, should |
| 79 | + * override the initializeAsyncMacroCommand method, |
| 80 | + * calling addSubCommand once for each SubCommand |
| 81 | + * to be executed. |
| 82 | + * |
| 83 | + * @see AsyncCommand |
| 84 | + */ |
| 85 | +class AsyncMacroCommand extends puremvcJsMulticoreFramework.Notifier |
| 86 | +{ |
| 87 | + |
| 88 | + /** |
| 89 | + * Constructor. |
| 90 | + |
| 91 | + * You should not need to define a constructor, |
| 92 | + * instead, override the initializeAsyncMacroCommand |
| 93 | + * method. |
| 94 | + |
| 95 | + * If your subclass does define a constructor, be |
| 96 | + * sure to call super(). |
| 97 | + */ |
| 98 | + constructor() |
| 99 | + { |
| 100 | + super(); |
| 101 | + this.subCommands = []; |
| 102 | + this.initializeAsyncMacroCommand(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Initialize the AsyncMacroCommand. |
| 107 | + * |
| 108 | + * In your subclass, override this method to |
| 109 | + * initialize the AsyncMacroCommand's SubCommand |
| 110 | + * list with factories like this: |
| 111 | + * |
| 112 | + * // Initialize MyMacroCommand |
| 113 | + * function initializeAsyncMacroCommand() |
| 114 | + * { |
| 115 | + * addSubCommand( () => new FirstCommand ); |
| 116 | + * addSubCommand( () => new SecondCommand ); |
| 117 | + * addSubCommand( () => new ThirdCommand ); |
| 118 | + * } |
| 119 | + * |
| 120 | + * SubCommands should extend one of the following |
| 121 | + * - AsyncMacroCommand |
| 122 | + * - AsyncCommand |
| 123 | + * - MacroCommand |
| 124 | + * - SimpleCommand |
| 125 | + */ |
| 126 | + initializeAsyncMacroCommand(){} |
| 127 | + |
| 128 | + /** |
| 129 | + * Add a SubCommand. |
| 130 | + * |
| 131 | + * The SubCommands will be called in First In/First Out (FIFO) |
| 132 | + * order. |
| 133 | + * |
| 134 | + * @param factory a function that instantiates the subcommand |
| 135 | + */ |
| 136 | + addSubCommand( factory ) { this.subCommands.push( factory );} |
| 137 | + |
| 138 | + /** |
| 139 | + * Registers the callback for a parent AsyncMacroCommand. |
| 140 | + * |
| 141 | + * @param callback The AsyncMacroCommand method to call on completion |
| 142 | + */ |
| 143 | + setOnComplete ( callback ) { this.onComplete = callback; } |
| 144 | + |
| 145 | + /** |
| 146 | + * Starts execution of this AsyncMacroCommand's SubCommands. |
| 147 | + * |
| 148 | + * The SubCommands will be called in First In/First Out (FIFO) order. |
| 149 | + * |
| 150 | + * @param notification the Notification object to be passed to each SubCommand. |
| 151 | + */ |
| 152 | + execute( notification ) |
| 153 | + { |
| 154 | + this.note = notification; |
| 155 | + this.nextCommand(); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Execute this AsyncMacroCommand's next SubCommand. |
| 160 | + * |
| 161 | + * If the next SubCommand is asynchronous, a callback is registered for |
| 162 | + * the command completion, else the next command is run. |
| 163 | + */ |
| 164 | + nextCommand() |
| 165 | + { |
| 166 | + if (this.subCommands?.length > 0) |
| 167 | + { |
| 168 | + const factory = this.subCommands.shift(); |
| 169 | + const instance = factory(); |
| 170 | + let isAsync = ( instance?.isAsync === true ); |
| 171 | + if (isAsync) instance.setOnComplete( () => this.nextCommand() ); |
| 172 | + instance.initializeNotifier( this.multitonKey ); |
| 173 | + instance.execute( this.note ); |
| 174 | + if (!isAsync) this.nextCommand(); |
| 175 | + } else { |
| 176 | + if( this?.onComplete ) this.onComplete(); |
| 177 | + this.note = null; |
| 178 | + this.onComplete = null; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + note; // Notification |
| 183 | + subCommands; // Array of command subcommand factories |
| 184 | + onComplete; // Optional function to call when the AsyncMacro completes |
| 185 | + isAsync = true; // simplest workaround to lack of interfaces |
| 186 | +} |
| 187 | + |
| 188 | +exports.AsyncCommand = AsyncCommand; |
| 189 | +exports.AsyncMacroCommand = AsyncMacroCommand; |
0 commit comments