Execute callback when smoosh has completed or between commands #13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request addresses issue #12. It adds support for a
done
command that can execute a callback. It is intended to be used like this:Note that the
done
command can be added multiple times between other commands as well:The command could be named
callback
or something else besidesdone
if you prefer. I've updated the README.md file to include documentation as well.Implementation Details
I went through everything that smoosh does and discovered it uses sync methods for almost everything. This includes using readFileSync and writeFileSync. All of the tools it depends on are used in synchronous ways, including uglifyjs, jshint, gzip, rimraf, and sqwish. This is what enables the commands to be chained like they are.
This means, for instance, that when
build
is executed, everything inrun
has already completed. So whendone
is executed, we know that all of the step prior to it have already finished.However, there are two dependencies that were causing problems, and that is
asciimo
andgzip
. This is because they runs asynchronously. All theasciimo
dependency does is create the SMOOSH banner, which really isn't critical to the operation of Smoosh.The smoosh output was being rendered to the console in a manner that I think could be problematic. Inside of
config()
, the smoosh banner was created asynchronously. Once it was finished being created, then all of the output from smoosh was flushed to console.out. But what if the chained commands haven't finished running yet and have not added all the messages to the output queue? It seems to me like there could be a race condition here. If the generating of the smoosh banner took less time than the running, building, and anlayzing, I'm thinking that some of the output wouldn't have been displayed.The solution I went with was to add
_write.flush()
just before each command returns, so each command is responsible for flushing it's own output. I also removed the use of asciimo since the async nature of it would have made it much more difficult to add a callback feature to smoosh. Instead, the smoosh banner is simply hard coded.There is still one minor flaw in this solution. The
gzip
problem isn't as easy to solve without using something like async method queues[1] by @ded. Fortunately,gzip
is only used inanalyze
, which no other commands rely on. It does mean that the gzip output from theanalyze
command may be displayed after a callback has been run. For now, the fix I've proposed will functionally work, but some output might be displayed late.To keep asciimo or to get
analyze
with gzip working perfectly, then it will be much more complex to support asynchronous commands with callbacks while using chaining. At that point I'd recommend using @ded's solution. But it seems to me like overkill just to be able to use asciimo.[1] http://www.dustindiaz.com/async-method-queues