Skip to content

Commit 4b0cc25

Browse files
committed
Added build apparatus and publish to npm
1 parent b69ae57 commit 4b0cc25

File tree

10 files changed

+667
-1652
lines changed

10 files changed

+667
-1652
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ With the `AsyncCommand` and `AsyncMacroCommand` you could dynamically create a p
1212
* [Historical Discussion](http://forums.puremvc.org/index.php?topic=831.0)
1313

1414
## Status
15-
Beta - [Version 1.0](https://github.com/PureMVC/puremvc-js-util-async-command/blob/master/VERSION)
15+
Beta - [Version 1.0.2](https://github.com/PureMVC/puremvc-js-util-async-command/blob/master/VERSION)
1616
* TODO: Tests and demos
1717

1818
## Platforms / Technologies

VERSION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ Release Date: 3/3/2024
44
Platform: JavaScript (Native)
55
Version: 1
66
Revision: 0
7-
Minor: 0
7+
Minor: 2
88
Authors: Copyright © 2008 Duncan Hall <duncan.hall@puremvc.org>
99
: Ported in 2024 by Cliff Hall <cliff.hall@puremvc.org>
1010
-------------------------------------------------------------------------
1111
1.0.0 Initial port
12-
12+
1.0.1 NPM package
13+
1.0.2 Update to latest PureMVC framework
1314

bin/puremvc-async-command.js

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

bin/puremvc-async-command.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)