From b524671b38133a13b192df0e3b907923fc31a519 Mon Sep 17 00:00:00 2001 From: cliffhall Date: Tue, 5 Mar 2024 12:08:59 -0500 Subject: [PATCH] * Fix issue where the setOnComplete method needed to use an arrow function calling this.nextCommand, rather than passing a reference to this.nextCommand, which is a prototype and does not have access to instance data * Improve nullish checking of this.onComplete * Add index.js to build folder that is copied to the bin folder at build time and is the entry point to the project * Added missing instance property declaration onComplete to AsyncMacroCommand.js --- bin/index.js | 6 ++++++ bin/puremvc-async-command.js | 9 +++++---- bin/puremvc-async-command.min.js | 2 +- package.json | 4 ++-- src/AsyncMacroCommand.js | 9 +++++---- 5 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 bin/index.js diff --git a/bin/index.js b/bin/index.js new file mode 100644 index 0000000..789fa29 --- /dev/null +++ b/bin/index.js @@ -0,0 +1,6 @@ +import {AsyncCommand, AsyncMacroCommand} from './puremvc-async-command.js'; + +export { + AsyncCommand, + AsyncMacroCommand +} \ No newline at end of file diff --git a/bin/puremvc-async-command.js b/bin/puremvc-async-command.js index 1361830..88ffe36 100644 --- a/bin/puremvc-async-command.js +++ b/bin/puremvc-async-command.js @@ -161,24 +161,25 @@ class AsyncMacroCommand extends puremvc.Notifier */ nextCommand() { - if (this.subCommands.length > 0) + if (this.subCommands?.length > 0) { const factory = this.subCommands.shift(); const instance = factory(); let isAsync = ( instance?.isAsync === true ); - if (isAsync) instance.setOnComplete( this.nextCommand ); + if (isAsync) instance.setOnComplete( () => this.nextCommand() ); instance.initializeNotifier( this.multitonKey ); instance.execute( this.note ); if (!isAsync) this.nextCommand(); } else { - if( this.onComplete !== null ) this.onComplete(); - this.note = null; + if( this?.onComplete ) this.onComplete(); + this.note = null; this.onComplete = null; } } note; // Notification subCommands; // Array of command subcommand factories + onComplete; // Optional function to call when the AsyncMacro completes isAsync = true; // simplest workaround to lack of interfaces } diff --git a/bin/puremvc-async-command.min.js b/bin/puremvc-async-command.min.js index edbe8cb..f3ee136 100644 --- a/bin/puremvc-async-command.min.js +++ b/bin/puremvc-async-command.min.js @@ -1 +1 @@ -import{puremvc as e}from"@puremvc/puremvc-js-multicore-framework";class t extends e.SimpleCommand{setOnComplete(e){this.onComplete=e}commandComplete(){this.onComplete()}onComplete;isAsync=!0}class s extends e.Notifier{constructor(){super(),this.subCommands=[],this.initializeAsyncMacroCommand()}initializeAsyncMacroCommand(){}addSubCommand(e){this.subCommands.push(e)}setOnComplete(e){this.onComplete=e}execute(e){this.note=e,this.nextCommand()}nextCommand(){if(this.subCommands.length>0){const e=this.subCommands.shift()();let t=!0===e?.isAsync;t&&e.setOnComplete(this.nextCommand),e.initializeNotifier(this.multitonKey),e.execute(this.note),t||this.nextCommand()}else null!==this.onComplete&&this.onComplete(),this.note=null,this.onComplete=null}note;subCommands;isAsync=!0}export{t as AsyncCommand,s as AsyncMacroCommand}; +import{puremvc as e}from"@puremvc/puremvc-js-multicore-framework";class t extends e.SimpleCommand{setOnComplete(e){this.onComplete=e}commandComplete(){this.onComplete()}onComplete;isAsync=!0}class s extends e.Notifier{constructor(){super(),this.subCommands=[],this.initializeAsyncMacroCommand()}initializeAsyncMacroCommand(){}addSubCommand(e){this.subCommands.push(e)}setOnComplete(e){this.onComplete=e}execute(e){this.note=e,this.nextCommand()}nextCommand(){if(this.subCommands?.length>0){const e=this.subCommands.shift()();let t=!0===e?.isAsync;t&&e.setOnComplete((()=>this.nextCommand())),e.initializeNotifier(this.multitonKey),e.execute(this.note),t||this.nextCommand()}else this?.onComplete&&this.onComplete(),this.note=null,this.onComplete=null}note;subCommands;onComplete;isAsync=!0}export{t as AsyncCommand,s as AsyncMacroCommand}; diff --git a/package.json b/package.json index 5c969f1..984dd37 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@puremvc/puremvc-js-util-async-command", - "version": "1.0.2", + "version": "1.0.5", "description": "PureMVC JS MultiCore Async Command Utility", "main": "bin/index.js", "type": "module", @@ -9,7 +9,7 @@ "url": "https://github.com/PureMVC/puremvc-js-util-async-command.git" }, "scripts": { - "build": "npm run clean && npm run build:lib", + "build": "npm run clean && npm run build:lib && cp build/index.js bin/", "build:lib": "rollup -c build/rollup.conf.mjs", "build:doc": "jsdoc -c build/jsdoc.json", "clean": "rm -rf bin", diff --git a/src/AsyncMacroCommand.js b/src/AsyncMacroCommand.js index 4e4eb24..1568a40 100644 --- a/src/AsyncMacroCommand.js +++ b/src/AsyncMacroCommand.js @@ -118,23 +118,24 @@ export class AsyncMacroCommand extends puremvc.Notifier */ nextCommand() { - if (this.subCommands.length > 0) + if (this.subCommands?.length > 0) { const factory = this.subCommands.shift(); const instance = factory(); let isAsync = ( instance?.isAsync === true ); - if (isAsync) instance.setOnComplete( this.nextCommand ); + if (isAsync) instance.setOnComplete( () => this.nextCommand() ); instance.initializeNotifier( this.multitonKey ); instance.execute( this.note ); if (!isAsync) this.nextCommand(); } else { - if( this.onComplete !== null ) this.onComplete(); - this.note = null; + if( this?.onComplete ) this.onComplete(); + this.note = null; this.onComplete = null; } } note; // Notification subCommands; // Array of command subcommand factories + onComplete; // Optional function to call when the AsyncMacro completes isAsync = true; // simplest workaround to lack of interfaces } \ No newline at end of file