Async XMLHttpRequest and class variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gabriel.landais@gmail.com

    Async XMLHttpRequest and class variables

    Hi,
    I'm currently building a parser class in JS and I have a question
    about variables. I retrieve XML data and then process it. After that, I
    process a result array (mydataarray). It looks like this :

    function MyClass()
    {

    var mydataarray = [];

    this.processXML node = processXMLnode;
    this.processxml Doc = processxmlDoc;
    this.processreq uest = processrequest;

    function processXMLnode( marker) {
    // ADD SOMETHING TO mydataarray
    }

    function processxmlDoc(x mlDoc) {
    var markers =
    xmlDoc.document Element.getElem entsByTagName(" data");
    mydataarray = [];

    for (var i = 0; i < markers.length; i++) {
    processXMLnode( markers[i]);
    }

    // PROCESS mydataarray
    }

    function processrequest( url, async) {
    var request = XMLHttpRequest. create();

    request.open("G ET", url, async);

    if (async) {
    request.onready statechange = function() {
    if (request.readyS tate == 4) {
    processxmlDoc(r equest.response XML);
    }
    }
    request.send(nu ll);
    }
    else
    {
    request.send(nu ll);
    processxmlDoc(r equest.response XML);
    }
    }

    }

    I'm not a JS expert, does processxmlDoc will be executed from the same
    thread (or execution context) every time? Does processxmlDoc is called
    only when nothing else is executing? Otherwise, how can I use a safe
    array?

    Cheers
    Gabriel

  • Thomas 'PointedEars' Lahn

    #2
    Re: Async XMLHttpRequest and class variables

    gabriel.landais @gmail.com wrote:
    [color=blue]
    > I'm currently building a parser class in JS[/color]

    The below is not a class declaration, instead a function declaration
    and probably a constructor. You are not using any of the class-based
    OOP features of the latest JavaScript/JScript/ECMAScript versions/editions,
    instead you are using inner functions and probably the prototype-based
    OOP features (with `new FunctionObjectR eference()').
    [color=blue]
    > and I have a question about variables. I retrieve XML data and then
    > process it. After that, I process a result array (mydataarray). It
    > looks like this :
    >
    > function MyClass()
    > {
    > [...]
    >
    > function processxmlDoc(x mlDoc) {
    > var markers =
    > xmlDoc.document Element.getElem entsByTagName(" data");
    > mydataarray = [];
    >
    > for (var i = 0; i < markers.length; i++) {
    > processXMLnode( markers[i]);
    > }
    >
    > // PROCESS mydataarray
    > }
    >
    > function processrequest( url, async) {
    > var request = XMLHttpRequest. create();
    >
    > request.open("G ET", url, async);
    >
    > if (async) {
    > request.onready statechange = function() {
    > if (request.readyS tate == 4) {
    > processxmlDoc(r equest.response XML);
    > }
    > }
    > request.send(nu ll);
    > }
    > else
    > {
    > request.send(nu ll);
    > processxmlDoc(r equest.response XML);
    > }
    > }
    >
    > }
    >
    > I'm not a JS expert, does processxmlDoc will be executed from the
    > same thread (or execution context) every time?[/color]

    No. The use of `async' here implies a value that can be type-converted
    to boolean. Therefore, if the value of `async' is a true-value, the
    event listener is assigned. It it, processxmlDoc() is called iff
    request.readySt ate equals 4 (success) when the `readystatechan ge' event
    occurs. If, on the other hand, the value of `async' is a false-value,
    processxmlDoc() is always called [after request.send(nu ll)].
    [color=blue]
    > Does processxmlDoc is called only when nothing else is executing?[/color]

    Yes, the respective programming languages are single-threaded.
    The inevitable delay in processing is mitigated, but not completely
    compensated by the use of event handlers for host objects.
    [color=blue]
    > Otherwise, how can I use a safe array?[/color]

    It is already safe as it is.


    PointedEars

    Comment

    • gabriel.landais@gmail.com

      #3
      Re: Async XMLHttpRequest and class variables

      I understand what I've done, hopefully ;) In fact I've created that
      pseudo class to access inner functions to be able to do something like
      :

      function a() {
      b();
      }

      function b() {
      c();
      }

      function c() {
      a();
      }

      With some "if" of course ;)

      Understood for the single thread context.
      Cheers!

      Thomas 'PointedEars' Lahn wrote:[color=blue]
      > gabriel.landais @gmail.com wrote:
      >[color=green]
      > > I'm currently building a parser class in JS[/color]
      >
      > The below is not a class declaration, instead a function declaration
      > and probably a constructor. You are not using any of the class-based
      > OOP features of the latest JavaScript/JScript/ECMAScript versions/editions,
      > instead you are using inner functions and probably the prototype-based
      > OOP features (with `new FunctionObjectR eference()').
      >[color=green]
      > > and I have a question about variables. I retrieve XML data and then
      > > process it. After that, I process a result array (mydataarray). It
      > > looks like this :
      > >
      > > function MyClass()
      > > {
      > > [...]
      > >
      > > function processxmlDoc(x mlDoc) {
      > > var markers =
      > > xmlDoc.document Element.getElem entsByTagName(" data");
      > > mydataarray = [];
      > >
      > > for (var i = 0; i < markers.length; i++) {
      > > processXMLnode( markers[i]);
      > > }
      > >
      > > // PROCESS mydataarray
      > > }
      > >
      > > function processrequest( url, async) {
      > > var request = XMLHttpRequest. create();
      > >
      > > request.open("G ET", url, async);
      > >
      > > if (async) {
      > > request.onready statechange = function() {
      > > if (request.readyS tate == 4) {
      > > processxmlDoc(r equest.response XML);
      > > }
      > > }
      > > request.send(nu ll);
      > > }
      > > else
      > > {
      > > request.send(nu ll);
      > > processxmlDoc(r equest.response XML);
      > > }
      > > }
      > >
      > > }
      > >
      > > I'm not a JS expert, does processxmlDoc will be executed from the
      > > same thread (or execution context) every time?[/color]
      >
      > No. The use of `async' here implies a value that can be type-converted
      > to boolean. Therefore, if the value of `async' is a true-value, the
      > event listener is assigned. It it, processxmlDoc() is called iff
      > request.readySt ate equals 4 (success) when the `readystatechan ge' event
      > occurs. If, on the other hand, the value of `async' is a false-value,
      > processxmlDoc() is always called [after request.send(nu ll)].
      >[color=green]
      > > Does processxmlDoc is called only when nothing else is executing?[/color]
      >
      > Yes, the respective programming languages are single-threaded.
      > The inevitable delay in processing is mitigated, but not completely
      > compensated by the use of event handlers for host objects.
      >[color=green]
      > > Otherwise, how can I use a safe array?[/color]
      >
      > It is already safe as it is.
      >
      >
      > PointedEars[/color]

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Async XMLHttpRequest and class variables

        gabriel.landais @gmail.com wrote:
        [color=blue]
        > I understand what I've done, hopefully ;) In fact I've created that
        > pseudo class to access inner functions to be able to do something like
        > :
        >
        > function a() {
        > b();
        > }
        >
        > function b() {
        > c();
        > }
        >
        > function c() {
        > a();
        > }
        >
        > With some "if" of course ;)[/color]

        Now that is real nonsense.

        Say I call a(), it calls b(), which calls c(), which calls a(), ...
        Say I call b(), it calls c(), which calls a(), which calls b(), ...
        Say I call c(), it calls a(), which calls b(), which calls c(), ...

        And there is no inner function whatsoever.

        If the above is instead but a bad example and you assumed that you
        would be able to call b.c() as in

        function b()
        {
        function c()
        {
        // ...
        }
        }

        then this is not entirely true. What would be used then is a _JavaScript_
        (Mozilla/2+) extension to ECMAScript, it is not available in JScript (IE)
        or Opera, for example.

        One of many correct ways to implement a public method is

        function Foo()
        {
        this.bar = function()
        {
        // ...
        }
        }

        var a = new Foo();
        a.bar();

        See
        <URL:http://developer.mozil la.org/en/docs/Core_JavaScript _1.5_Guide#Deta ils_of_the_Obje ct_Model>
        [color=blue]
        > Understood for the single thread context.[/color]

        At least.
        [color=blue]
        > [Full quote][/color]

        Do not top-post on Usenet.

        <URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1P ost>


        PointedEars

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          [FYI] Calling inner functions from outer context (was: Async XMLHttpRequest and class variables)

          Thomas 'PointedEars' Lahn wrote:
          [color=blue]
          > If [...] you assumed that you would be able to call b.c() as in
          >
          > function b()
          > {
          > function c()
          > {
          > // ...
          > }
          > }
          >
          > then this is not entirely true. What would be used then is a _JavaScript_
          > (Mozilla/2+) extension to ECMAScript, it is not available in JScript (IE)
          > or Opera, for example.[/color]

          Example: function x() { function y() { alert("42"); } }; x.y() // 42 or not

          It turns out that this feature is no longer available in JavaScript 1.6
          as implemented in Firefox 1.5 (Mozilla/5.0 rv:1.8), while it still is in
          JavaScript 1.5 as implemented in Mozilla/5.0 rv:1.7.12. The Support
          Matrix[1] is growing larger every day :)


          PointedEars
          ___________
          [1] <URL:http://PointedEars.de/scripts/js-version-info>

          Comment

          • gabriel.landais@gmail.com

            #6
            Re: Async XMLHttpRequest and class variables

            Thomas 'PointedEars' Lahn wrote:[color=blue]
            > gabriel.landais @gmail.com wrote:
            >[color=green]
            > > I understand what I've done, hopefully ;) In fact I've created that
            > > pseudo class to access inner functions to be able to do something like
            > > :
            > >
            > > function a() {
            > > b();
            > > }
            > >
            > > function b() {
            > > c();
            > > }
            > >
            > > function c() {
            > > a();
            > > }
            > >
            > > With some "if" of course ;)[/color]
            >
            > Now that is real nonsense.[/color]

            Ok my scheme was really simplified here, should I put :

            function doSomething() {
            processrequest( 'backtrack.php? button='+this.s omething, true);
            }

            function processXMLnode( marker) {
            element = createNewButton (marker);
            element.onclick = doSomething();
            }

            function processxmlDoc(x mlDoc) {
            var markers =
            xmlDoc.document Element.getElem entsByTagName(" data");
            mydataarray = [];

            for (var i = 0; i < markers.length; i++) {
            processXMLnode( markers[i]);
            }
            }

            function processrequest( url, async) {
            var request = XMLHttpRequest. create();

            request.open("G ET", url, async);

            if (async) {
            request.onready statechange = function() {
            if (request.readyS tate == 4) {
            processxmlDoc(r equest.response XML);
            }
            }
            request.send(nu ll);
            }
            else
            {
            request.send(nu ll);
            processxmlDoc(r equest.response XML);
            }
            }
            [color=blue]
            >
            > See
            > <URL:http://developer.mozil la.org/en/docs/Core_JavaScript _1.5_Guide#Deta ils_of_the_Obje ct_Model>
            >[/color]

            Thanks. I still believe that "prototype" thingy is oversized for my
            problem.
            [color=blue]
            > Do not top-post on Usenet.
            >
            > <URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1P ost>
            >[/color]
            Sorry about that!

            My very first draft is there :
            http://gabriel.landais.org/testGM/map.inc.js . It doesn't work very
            well with IE :) Thanks to your site, I hope I will be able to
            understand why!

            Cheers

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Async XMLHttpRequest and class variables

              gabriel.landais @gmail.com wrote:
              [color=blue]
              > Ok my scheme was really simplified here, should I put :
              >
              > [...][/color]

              Looks OK, but there are no inner functions whatsoever left :)

              Or did you mean that you used that inside a function in order
              to avoid spoiling the global namespace? Then I'll agree.
              [color=blue][color=green]
              >> See
              >>[/color][/color]
              <URL:http://developer.mozil la.org/en/docs/Core_JavaScript _1.5_Guide#Deta ils_of_the_Obje ct_Model>[color=blue][color=green]
              >>[/color]
              >
              > Thanks. I still believe that "prototype" thingy is oversized for my
              > problem.[/color]

              If you create only one object with calling new MapClass(), then yes.
              If not, you should consider the prototype since inheriting it from the
              prototype as in

              function Map()
              {
              // ..
              }

              Map.prototype.p rocessXMLnode = function()
              {
              // ...
              }

              prevents each Map object to have a method (Function object) of its own
              (initially) which will save you heap; on the other hand, it removes the
              possible advantage of closures to define methods specific to an object
              on initialization.
              [color=blue]
              > My very first draft is there :
              > http://gabriel.landais.org/testGM/map.inc.js .[/color]

              Ahh, that approach is OK then. A bit old-fashioned IMHO, as function
              expressions allow for

              this.geturlprop erties = function()
              {
              // ...
              }

              instead of

              this.geturlprop erties = geturlpropertie s;

              function geturlpropertie s()
              {
              // ...
              }

              since JavaScript 1.3/JScript 2.0?/ECMAScript 3.
              [color=blue]
              > It doesn't work very well with IE :) Thanks to your site, I hope I
              > will be able to understand why![/color]

              Hmmm ... getAttribute() is often buggy and may not be necessary.

              For responseXML to work in IE (with MSXML), you have to make sure
              that the response is served at least with Content-Type: text/xml.

              <URL:http://msdn.microsoft. com/library/en-us/xmlsdk/html/ab1b76cf-7dd9-46b5-b782-cc04823df117.as p>

              Apart from that, I do not see syntax or semantical errors, but
              I may have overlooked some, especially those that may be in the
              constructors you call that are not defined in map.inc.js.


              HTH

              PointedEars

              Comment

              Working...