Dart HTML HttpRequest Global Handler

228 views
Skip to first unread message

Giovanni Candido da Silva

unread,
Mar 18, 2013, 9:02:18 AM3/18/13
to mi...@dartlang.org
Hi,
I want to perform the follow use case:

Attach event listeners for global exception handler and global request handlers. I want simple perform user friend messages for all no handled error that the server could throw. 

Do this for every request is not (even a little) scalable.

I was thinking if the HttpRequest have some way of doing this. But I'm not sure.

I will try to create a wrapper class  that do a facade on the HttpRequest, I have some designs in mind:

First approach:

Create a AjaxSingleton class with methods for create requests, this methods delegate the work for HttpRequest but attach listeners for the HttpEvents and dispatch them globally, a client listen the events on AjaxSingleton to perform global feedback. I'm not sure how I can perform the same API of HttpRequest returning Futures, because Future have only relationship with the caller and I not see how I can return Future and dispatch the same event.

Second approach:

Extends the HttpRequest and create a factory constructor. Before construct the HttpRequest I attach the listener for a function that dispatch the event back in a broadcast Stream.

The two approaches share the same problem: A third part library will not use my class, and I will not able to listen the requests of this library.

Also I'm new to dart and I don't understand well the Stream API, I have knowledge about the observer patter by Gang of Four, but the Stream is a little more complicated. If you know some tutorial about Streams this can help-me understand better my problem.

Thank you.

Pete Blois

unread,
Mar 18, 2013, 1:10:47 PM3/18/13
to mi...@dartlang.org
There is bug 5958 which is tracking a global unhandled error handler. It covers all unhandled errors (not just HttpRequest errors), but you should be able to do a filter for just HttpRequest errors.

In general though, I believe that HttpRequests should be expected to fail and that the errors should be gracefully handled by the callers. Networks are flaky. A form submission should try again, then tell the user that they appear to be offline (there's also an offline event on Window!).

I think that a library which does a good job of handling offline/online, retrying, prioritization of requests, and cancelation of pending requests would be very useful.

Giovanni Candido da Silva

unread,
Mar 18, 2013, 4:06:52 PM3/18/13
to mi...@dartlang.org
I do this using the first approach. Current I listen for the onLoadEnd event but after a can wrap all events.

Here is my implementation with some simple test case

import 'lib/Ajax.dart';
import 'lib/Teste.dart';
main(){
  Ajax.getInstance().onLoadEnd.listen(showOnLoad);
  HttpRequest request = Ajax.getInstance().request;
  request..open('GET', "../data.json")
        ..onLoadEnd.listen(showData)
        ..send();
  print('Send Another Ajax Request');
  Teste.sendAjax();



}
showData(data){
  print(data.target.response);
}
showOnLoad(event){
  print('On show Global called, do user feedback');
}

library ajax;
import 'Ajax.dart';
class Teste {
  static sendAjax(){
    HttpRequest request = Ajax.getInstance().request;
    Ajax.getInstance().onLoadEnd.listen(onLoadEnd2);
    request..open('GET', "../data.json")
    ..send();
  }
  static onLoadEnd2(HttpRequestProgressEvent event){
    print('''Received event globally another time, this can be other use case,
    like inspect for security constrants send by server''');
  }
}


library ajax;
import 'dart:html';
import 'dart:async';

class Ajax {
  static Ajax _instance;
  StreamController _onLoadEndStreamController = new StreamController.broadcast();
  /**
   * Singleton Factory
   */
  factory Ajax(){
     return getInstance();
  }
  /**
   * Ajax is Singleton so, the constructor is private
   */
  Ajax._private(){

  }
  static Ajax getInstance(){
    if(_instance == null){
      _instance = new Ajax._private();
    }
    return _instance;
  }
  /**
   * Return a request that always have one listen to forward globally
   * Dispatches the event for the world. All the events that this class support have to be added here
   */
  HttpRequest _createRequest(){
    HttpRequest request = new HttpRequest();
    request.onLoadEnd.listen(_onLoadEndHandler);
    return request;
  }

  /**
   * Dispatches the event for the world
   */
  _onLoadEndHandler(event){
    _onLoadEndStreamController.add(event);
  }
  /**
   * Gets for the request and onLoadEnd Stream
   */
  Stream<HttpRequestProgressEvent> get onLoadEnd => _onLoadEndStreamController.stream;
  HttpRequest get request => _createRequest();
}




Cassio Tavares

unread,
Mar 19, 2013, 12:21:15 AM3/19/13
to mi...@dartlang.org
Hi Giovanni,

I don't know if it is a good idea set all handlers to the same event.Have you seen that request has OnError?

I am from Brazil too and I'd like to talk about Dart projects with you. Can I have your personal email?

Regards,

Giovanni Candido da Silva

unread,
Mar 19, 2013, 8:12:49 AM3/19/13
to mi...@dartlang.org
The handlers will not be set for the same event. I only post a prof of concept. The others events will be every one that the HttpRequest Suports, they are:

  • Abort
  • Load
  • LoadEnd
  • LoadStart
  • Progress
I do another implementation were Ajax implements HttpRequest and return a instance of HttpRequest with global handlers attached, and I use static functions for global listeners and the normal functions for caller listeners. The two implementations works, but this one the caller can treat Ajax Class as HttpRequest class.

I'm testing...

My email is giovann...@me.com
You can add me on Gtalk as giov...@atende.info
And we can speak good portuguese :-)

Giovanni Candido da Silva

unread,
Mar 19, 2013, 8:14:09 AM3/19/13
to mi...@dartlang.org
O forgot the event Error


Em terça-feira, 19 de março de 2013 01h21min15s UTC-3, Cassio Tavares escreveu:

Giovanni Candido da Silva

unread,
Mar 22, 2013, 4:55:18 PM3/22/13
to mi...@dartlang.org
I open a feature request on http://code.google.com/p/dart/issues/detail?id=9381


Em segunda-feira, 18 de março de 2013 10h02min18s UTC-3, Giovanni Candido da Silva escreveu:
Reply all
Reply to author
Forward
0 new messages