Hi T.J.,

thank you for your reply. You are right about the idle-flag, I already
renamed it :)
I followed your advice and moved up the "idle = false" thing, but it
does not work so well.
With a slow connection the code below works fine:


var MessageQueue = Class.create({
    initialize: function(url) {
        this.url = url;
        this.queue = [];
        this.busy = false;
    },
    add: function(message) {
        if (this.busy) this.queue.push(message)
        else this.push(message);
    },
    push: function(message) {
        this.busy = true;
        new Ajax.Request(this.url, {
            onSuccess: function() {
                alert(message);
                this.busy = false;
                if (this.queue.size() > 0) {
                    this.push("queued: " + this.queue.shift());
                }
            }.bind(this)
        });
    }
});

var mq = new MessageQueue("/ajax_html_echo/");
mq.add("First");
mq.add("Second");
mq.add("Third");


But when the backend is fast and you type the hell out of your
keyboard, it seems, that the part
this.push("queued: " + this.queue.shift());
is never reached. There is no output with "queued:"

My thought was, that when new messages arrive, while a request is in
progess, to store them until the request is finished. Then all
messages in the stack should be sent until the busy-flag is set to
false. Therefore I had it below the if-condition.

I saw that the guys at jQuery use a sentinel "inprogress" which they
unshift/shift to/from the array. But I don't know if that would be
helpful here.

It would be super great if you have the time, to have a second look at
it!

Thank you so much

On 13 Aug., 10:18, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> Hi,
>
> Your "idle" flag -- which you seem to use to mean "busy processing a
> request", which I found *really* confusing at first :-) -- is getting
> cleared inappropriately whenever you pull something from your queue. I
> don't know that that's the problem, but it's probably wrong. The error
> is in your onComplete function:
>
> if (this.queue.size() > 0) {
>     console.log("queue-size > 0, so enqueue next item");
>     this.push("queued: " + this.queue.shift());}
>
> this.idle = false; // <=== Error if `if` above was true
>
> If there's something in the queue, you call `push`, which sets the
> flag, starts a new async request, and returns. But then you clear the
> flag, so the next time `add` gets called, it'll think it should call
> `push` rather than `queue.push` even though there's an outstanding
> request.
>
> If you just move the `this.idle = false;` line above the `if`
> statement, it should sort out this issue. Whether it solves the
> problems you described I can't say, but I suspect it'll solve at least
> some of them.
>
> HTH,
> --
> T.J. Crowder
> Independent Software Consultant
> tj / crowder software / comwww.crowdersoftware.com
>
> On Aug 12, 7:55 pm, Moo <stuhlm...@fifty-nine.de> wrote:
>
>
>
> > Hi there,
>
> > I want to ensure, that messages are posted in the order, they come in
> > the stack. My tests at jsFiddle were okay, but with a quick backend
> > (where the requests are processed very fast) the order is crap.
> > Sometimes items are posted twice. Is there anybody who can help me
> > out? Thank you all!
>
> > var MessageQueue = Class.create({
> >         initialize: function(url) {
> >         this.url = url;
> >         this.queue = [];
> >         this.idle = false;
> >         },
> >         add: function(message) {
> >         if (this.idle) {
> >                 console.log("idle, so saved to queue: " + message);
> >                 this.queue.push(message);
> >         } else {
> >                 console.log("not idle, so pushed directly: " + message);
> >                 this.push(message);
> >         }
> >         },
> >         push: function(message) {
> >         this.idle = true;
> >         console.log("Ajax.Request will be called now!");
> >         new Ajax.Request(this.url + "&message=" +
> > encodeURIComponent(message), {
> >                 onComplete: function() {
> >                         console.log("successfully sent: " + message);
> >                 if (this.queue.size() > 0) {
> >                         console.log("queue-size > 0, so enqueue next item");
> >                         this.push("queued: " + this.queue.shift());
> >                 }
> >                 this.idle = false;
> >                 }.bind(this)
> >         });
> >         }
>
> > });- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to