tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/170-best-practice-looping-command-interruptRobotlegs: Discussion 2018-10-18T16:35:12Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-05-20T07:28:05Z2010-05-20T07:31:00ZBest practice looping Command / Interrupt<div><p>Maybe it would make it easier if you would put all the uploading
logic in a <code>datamodel</code>. Then you use the
<code>command</code> to set the array of items in the
<code>datamodel</code> and then tell the model to start uploading.
The <code>datamodel</code> can keep track of the files in progress
and the ones that are done.<br>
For canceling you can use another <code>command</code>, or the same
one with an empty array or null and some logic to check that
null-ness, that will tell the model to cancel all loading that is
in progress. I would put some logic in that <code>command</code> to
check the state of the model</p>
<p>Or is the above what you mean with:<br></p>
<blockquote>
<p>I dont want to map a singleton</p>
</blockquote></div>jiritag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-05-20T07:34:55Z2010-05-20T07:34:55ZBest practice looping Command / Interrupt<div><p>This sounds to me like a targeted use case for the Finite
StateMachine:</p>
<p><a href=
"http://github.com/joelhooks/robotlegs-utilities-StateMachine">http://github.com/joelhooks/robotlegs-utilities-StateMachine</a></p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-05-20T15:47:07Z2010-05-20T15:47:07ZBest practice looping Command / Interrupt<div><p>thanks for the replies.<br>
@jiri, putting that loop in a model could work, but then that model
will be dependent on the service layer. (the file is being uploaded
against a server side script). Also, i dont need the FileReference
objects once i am done uploading them,</p>
<p>@Joel. ill take a look.. Does this mean i have to rewrite my RL
app to use this utilitiy? Can i have mulitple small FSMs in one
app? Can i have a standart RL app, and use the FSM utility (or
others) in a single place?</p>
<p>thanks</p></div>Raed Atouitag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-05-20T15:50:32Z2010-05-20T15:50:32ZBest practice looping Command / Interrupt<div><p>Ya, the FSM can be used anywhere (even outside of a RL app, it
just needs an IEventDispatcher). I doubt it would take an app
rewrite.</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-05-20T16:48:20Z2010-05-20T16:48:20ZBest practice looping Command / Interrupt<div><p>cool. I studied both of your examples, the chat and the
bootstrap one, and its very cool.<br>
It seems that i can integrate multiple FSMs into one app easily. I
guess for each FSM i can create a file with the constants and the
state descriptors, i just have to be careful about the values for
the constants like START so no 2 FSM get triggered by the same
start event<br></p></div>Raed Atouitag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-05-20T16:52:48Z2010-05-20T16:52:48ZBest practice looping Command / Interrupt<div><p>Ya, in that case I would tag their strings
"event/MyFSM/description" - that way you will be all good.</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-05-20T22:31:41Z2010-05-20T22:31:41ZBest practice looping Command / Interrupt<div><p>i started working with the FSM utility and its very cool.<br>
So, my FSM consists of 3 states. The command that creates the FSM
gets an array as payload.<br>
State1: Start (entry)<br>
State2: upload file<br>
State3: insert record in db<br>
State4: check how many files are remanning, if more files are left,
go to State2, else EXIT</p>
<p>How can i maintain the payload in memory while this is looping,
and how can this be interrupted by an outside event like STOP?</p>
<p>thanks</p></div>Raed Atouitag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-05-20T23:39:54Z2010-05-27T13:35:46ZBest practice looping Command / Interrupt<div><p>The FSM StateEvent has a data property, couldn't you use this to
keep track of the array of files?</p>
<p>But you also need to keep track of the FSM itself.. Maybe make
an UploadQueueModel to hold reference to all this?</p>
<p>Regarding the stopping...<br>
You could define a new state called Cancel and transition to it
with an Action called STOP. Make tha transition available on all
states (though probably it would only trigger on state 2 o 3).<br>
So when you arrive at the Cancel state, you could fire an event to
trigger a CancelCommand, which would in turn stop the upload
process by destroying the FSM.</p></div>Jonitag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-05-21T05:40:55Z2010-05-21T05:40:55ZBest practice looping Command / Interrupt<div><p>yeah, the StatEvent can hold data.<br>
The problem is not having the cancel state, but getting into it
from an outside event.</p></div>Raed Atouitag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-05-21T19:55:02Z2010-05-27T13:35:47ZBest practice looping Command / Interrupt<div><p>I see...<br>
How about the UploadQueueModel idea?<br>
A Cancel event could ask the model if there's a current upload
queue, and in that case, call a method on the model to destroy
it.</p></div>Jonitag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-05-21T20:05:51Z2010-05-21T20:05:51ZBest practice looping Command / Interrupt<div><p>yeah, i think that seems to be the last option. Since i cant
have a reference to a looping command (commands are supposed to be
short lived), a singleton behavior would solve this issue.</p></div>Raed Atouitag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-06-27T19:13:03Z2010-06-27T19:13:03ZBest practice looping Command / Interrupt<div><p>It occured to me that the FSM util could be modified to include
a new StateEvent.DISPOSE.<br>
The FSM would listen for this event to dispose itself (removing all
listeners for the eventDispatcher) and could optionally send
another event to inform the framework that it has been disposed
(this other event could be StateEvent.DISPOSED for example).</p></div>Jonitag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-06-27T20:05:56Z2010-06-27T20:05:56ZBest practice looping Command / Interrupt<div><p>I like that idea Joni.</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-06-28T01:57:39Z2010-06-28T01:57:39ZBest practice looping Command / Interrupt<div><p>Gonna give it a try forking the github repo, I'll post back
soon!</p></div>Jonitag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-06-28T02:15:40Z2010-06-28T02:15:40ZBest practice looping Command / Interrupt<div><p>Be sure to update unit tests with any new functionality as well.
;)</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-06-28T02:55:02Z2010-06-28T02:55:22ZBest practice looping Command / Interrupt<div><p>cool. good to hear that this has been picked up. Just to
clarify, the dispose event can come from outside? How would this be
mapped? Would be mapped like Mediators map events from the
framework ?<br>
thank you Joni :)</p></div>Raed Atouitag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-06-28T03:31:48Z2010-06-28T03:31:48ZBest practice looping Command / Interrupt<div><p>Your welcome!<br>
You wouldn't have to map anything directly to this dispose
event.<br>
The StateMachine has a reference to the framework's
eventDispatcher. So, anywhere within the application, you could
dispatch a StateEvent.DISPOSE to destroy the StateMachine. Pretty
much the same way you can dispatch a StateEvent.ACTION to trigger a
transition.</p>
<p>I was thinking that a good hook to catch the destruction of the
StateMachine would be listening for another event, for example:
StateEvent.DISPOSED (this one is past tense).<br>
This event would be dispatched by the StateMachine when it get's
destroyed, and you could map a command to this event and do
something when this happens.</p></div>Jonitag:robotlegs.tenderapp.com,2009-10-18:Comment/17346472010-06-28T13:42:39Z2010-06-28T13:44:43ZBest practice looping Command / Interrupt<div><p>Ok, here it goes!<br>
<a href=
"http://github.com/honi/robotlegs-utilities-StateMachine">http://github.com/honi/robotlegs-utilities-StateMachine</a></p>
<p>You can dispose the StateMachine by sending a StateEvent.DISPOSE
or you could also call stateMachine.dispose() if you have a
reference to it.</p>
<p>Any advice on the tests?</p></div>Joni