Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Async Eco #11

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Async Eco #11

wants to merge 10 commits into from

Conversation

bigeasy
Copy link

@bigeasy bigeasy commented May 4, 2011

First, notes from the discussion on the CoffeeScript mailing list, taken from discussion earlier today on IRC.

I've decided to make async a compiler option. If the option is all, all helper blocks are called asynchronously. If it is off, the function is identical to the current Eco generated functions. This is because the helper author and the compiler invoker are almost certainly the same person, and they will know if async is necessary or not.

When async is on, the generated function takes a callback. If it is off, the generated function returns its result as it always has.

This leads to the following programming problems with async Eco:

  • Async is not needed, but async is on ~ If the generated function is called without a callback, an exception is raised. If it is called as an async function, there is no real problem here.
  • Async is needed, but async is off ~ The template output will most likely be missing chunks of expected output.

I've come to the following decisions regarding the questions at the end of that post:

Maybe there's a way for the helper to say that it is async, instead of having to remember that in the code? (Too much complexity defeats the use case.) ~ Currently, I'm tucking an exclamation point into the directive to indicate that the call should be async. This is fiddly. The async implementation is supposed to be transparent to the template author. When I'm writing templates, I don't want to think about concurrency. The use case given for async Eco is a template system for people used to writing themes with a a template language. Telling them they forgot the ! will only lead to a painful attempt to explain to them what it is for. Thus, I've decided that there will be no extensions to the Eco directives.

This decision addresses the other question as well.

Async blocks in code and no callback? You get "[ Function ]" in your blocks. Could easily detect this condition, but how to report the error? Exception? Or let it slide? ~ Instead of async being a property that is applied per block helper, it is applied to all helper blocks in a template using a complier option. This means that if the option is not present, there is no effect at all on the existing Eco templates. This means that if you generate an async method, the async method can assert that it got a callback.

 * Altered the scanner to report the block nature of code at as part of
   "beginCode". This meant moving the "beginCode" invocation to after the code
   scan, but did not change the preprocessor. It did change the scanner tests,
   however, since they now detect a truncated template before they report
   "beginCode". I changed the test inputs, but not their meaning.
 * All tests are passing again.
 * This ends the modifications necessary to the scanner, preprocessors and
   compiler. The only changes now are to the helper methods in the generated
   code.
 * Passing the error (now only ever `null`) as the first parameter to the
   async callback.
 * Updated `eco` function to take addtional boolean parameter to indicate an
   asyc compile.
 * Updated `render` to accept an option third callback function, which will
   trigger an async compile.
 * Catching exceptions thrown by the helpers when run asynchronously.
 * Forwarding caught exceptions to the callback method.
 * Safety is moot on blocks, since only inline printed code is escaped.
@bigeasy
Copy link
Author

bigeasy commented May 4, 2011

I'd begun to develop with Async Eco, took a break, and now I'm back. This code seems to be stabilizing. I'll be working with it more in the new few weeks.

In the mean time, here's my current development. I've added tests for the async code. Current users of Eco are not effected by these changes. Their code will run as it always has. All existing tests pass. Asynchronicity is enabled with a compiler flag. The resulting template expects a callback as second argument. The callback receives an idomatic (error, output) as its result.

I had to change the scanner slightly to report the block nature of a code scan to the beginCode method of the preprocessor. This effected only the scanner and not the processors. Details can be found in d60bbb3.

I made a slight change to the indent helper seen in d60bbb3 to remove the indent from the first line, making it easier to add optional blocks to the JavaScript function building heredoc.

Note that all existing compiler test fixtures are unchanged. The compiler output for non-async code after this patch is applyed is identical.

Probably need to patch the documentation as well. Seeking feedback on progress so far. Please checkout and run tests and tell me what you think.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants