Adding call/answer in under 35 lines of Dart code

This is a follow up to Seaside in under 100 lines of Dart code. Over the past few days, I’ve added some more examples and a bunch of new features — such as the ability for sessions to expire — to the GitHub Repository.

The most interesting change is another key-defining feature of Seaside: the ability to create a flow of components in normal code. While I agree with critics that this is rarely useful in web applications, it is technically an interesting challenge to implement in many programming language; and I think with Dart we can get quite close.

Imagine we want to ask the user for two numbers, and then present the sum. This is very easy to do on the command-line or with a desktop application, but on the web it turns out to be quite involved. Essentially I’d like to write:

call-answer-cc.png

And with call_answer.dart we can. I’ve added ...

  • a typed mixin CanAnswer<T> that can be added to components so they can answer values of type T;
  • an abstract component class Task that can be used to describe the flow by defining a run method; and
  • a bunch of helper methods to call other components, and to answer results back to their caller.

This works out of the box. Unfortunately Dart Futures are not resumable, and the back button causes a server exception. Dart supports the creation of custom Future classes, maybe there is a way to tweak the system by using a custom implementation instead?

In any case, easy enough I can use continuation-passing style that works as expected: both for splitting a session into multiple paths and with the back button:

call-answer-cps.png

Posted by Lukas Renggli at 28 July 2020, 5:51 pm with tags dart, seaside, web, app, continuation link