Async XMLHttpRequest and class variables

G

gabriel.landais

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.processXMLnode = processXMLnode;
this.processxmlDoc = processxmlDoc;
this.processrequest = processrequest;

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

function processxmlDoc(xmlDoc) {
var markers =
xmlDoc.documentElement.getElementsByTagName("data");
mydataarray = [];

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

// PROCESS mydataarray
}

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

request.open("GET", url, async);

if (async) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
processxmlDoc(request.responseXML);
}
}
request.send(null);
}
else
{
request.send(null);
processxmlDoc(request.responseXML);
}
}

}

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
 
T

Thomas 'PointedEars' Lahn

I'm currently building a parser class in JS

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 FunctionObjectReference()').
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(xmlDoc) {
var markers =
xmlDoc.documentElement.getElementsByTagName("data");
mydataarray = [];

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

// PROCESS mydataarray
}

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

request.open("GET", url, async);

if (async) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
processxmlDoc(request.responseXML);
}
}
request.send(null);
}
else
{
request.send(null);
processxmlDoc(request.responseXML);
}
}

}

I'm not a JS expert, does processxmlDoc will be executed from the
same thread (or execution context) every time?


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.readyState equals 4 (success) when the `readystatechange' event
occurs. If, on the other hand, the value of `async' is a false-value,
processxmlDoc() is always called [after request.send(null)].
Does processxmlDoc is called only when nothing else is executing?

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.
Otherwise, how can I use a safe array?

It is already safe as it is.


PointedEars
 
G

gabriel.landais

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!
I'm currently building a parser class in JS

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 FunctionObjectReference()').
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(xmlDoc) {
var markers =
xmlDoc.documentElement.getElementsByTagName("data");
mydataarray = [];

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

// PROCESS mydataarray
}

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

request.open("GET", url, async);

if (async) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
processxmlDoc(request.responseXML);
}
}
request.send(null);
}
else
{
request.send(null);
processxmlDoc(request.responseXML);
}
}

}

I'm not a JS expert, does processxmlDoc will be executed from the
same thread (or execution context) every time?


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.readyState equals 4 (success) when the `readystatechange' event
occurs. If, on the other hand, the value of `async' is a false-value,
processxmlDoc() is always called [after request.send(null)].
Does processxmlDoc is called only when nothing else is executing?

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.
Otherwise, how can I use a safe array?

It is already safe as it is.


PointedEars
 
T

Thomas 'PointedEars' Lahn

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 ;)

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
Understood for the single thread context.

At least.
[Full quote]

Do not top-post on Usenet.

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


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
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.

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>
 
G

gabriel.landais

Thomas said:
Now that is real nonsense.

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

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

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

function processxmlDoc(xmlDoc) {
var markers =
xmlDoc.documentElement.getElementsByTagName("data");
mydataarray = [];

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

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

request.open("GET", url, async);

if (async) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
processxmlDoc(request.responseXML);
}
}
request.send(null);
}
else
{
request.send(null);
processxmlDoc(request.responseXML);
}
}

Thanks. I still believe that "prototype" thingy is oversized for my
problem.
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
 
T

Thomas 'PointedEars' Lahn

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

[...]

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.
Thanks. I still believe that "prototype" thingy is oversized for my
problem.

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.processXMLnode = 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.

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

this.geturlproperties = function()
{
// ...
}

instead of

this.geturlproperties = geturlproperties;

function geturlproperties()
{
// ...
}

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

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.asp>

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top