Dart Documentationclean_ajax.client

clean_ajax.client library

A library for efficient server-client communication that guarantees order of requests and responses.

Examples

Following example demonstrate simple usage of connection in browser. It is guaranteed that the first request finishes before the second.

 import "package:clean_ajax/client_browser.dart";

 var connection = createHttpConnection("http://www.example.com/api/",
                                       new Duration(milliseconds: 100));

 connection.send(() => new ClientRequest("user/get", {"name": "John"}))
 .then((user) {
   showUserInfo(user);
 });

 connection.send(() => new ClientRequest("user/get", {"name": "Peter"}))
 .then((user) {
   showUserInfo(user);
 });

It can be handy to send some requests periodically, for example to check the state of the email inbox.

 import "package:clean_ajax/client_browser.dart";

 var connection = createHttpConnection("http://www.example.com/api/",
                                       new Duration(milliseconds: 100));

 connection.sendPeriodically(() => new ClientRequest("inbox/get", {}))
 .listen((inbox) {
   updateInbox(inbox);
 });

Periodical requests are canceled simply by canceling the subscription to results.

 import "package:clean_ajax/client_browser.dart";

 var connection = createHttpConnection("http://www.example.com/api/",
                                       new Duration(milliseconds: 100));

 var subscription = connection.sendPeriodically(
     () => new ClientRequest("inbox/get", {})
 ).listen((inbox) {
   updateInbox(inbox);
 });

 // Stop sending requests when the inbox is closed by the user.
 onInboxClose(() => subscription.cancel());

Reusal of code on server is encouraged by LoopBackTransport layer, that works on the server.

 import "package:clean_ajax/client_backend.dart";

 var connection = createLoopBackConnection(requestHandler);

 connection.send(() => new ClientRequest("user/get", {"name": "John"}))
 .then((user) {
   showUserInfo(user);
 });

 connection.send(() => new ClientRequest("user/get", {"name": "Peter"}))
 .then((user) {
   showUserInfo(user);
 });

However, sendPeriodically is not supported by LoopBackTransport, it is not error to call it on the server, however it gets send only first time and later times when normal send is triggered.

Exports

Properties

final logger #

final logger = new Logger('clean_ajax')

Abstract Classes

Classes

Typedefs

Exceptions