concurrency issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Smith

    concurrency issues

    The Situation:

    I've got a frameset that loads two other pages in frames. Frame A
    creates an object, and the FrameB needs to invoke a method on that
    object.

    The Problem:

    How does Frame B find out when Frame A has finished creating the object?
    If Frame A hasn't finished creating the object yet, I want Frame B to
    wait, and invoke the method as soon as the object has been created. I
    can find out IF the object has been created (test the variable for
    undefined or null), but if it hasn't been created yet, I can't find a
    way to wait and send the notification when it has.

    I've looked around, but JavaScript doesn't seem to provide the
    synchronization constructs that I'd use to solve this problem in other
    languages.

    I have control over all three pages (the frameset, and both frames), and
    I suspect the solution is going to have something to do with code in the
    frameset, since neither frame can deterministical ly interact with the
    other... but how does it get done? Without some way to manage
    concurrency, I don't see a way to let the two share state.

    --

    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
  • Chris Smith

    #2
    Re: concurrency issues

    Andrew Urquhart wrote:[color=blue][color=green]
    > > The Problem:
    > >
    > > How does Frame B find out when Frame A has finished creating the object?
    > > If Frame A hasn't finished creating the object yet, I want Frame B to
    > > wait, and invoke the method as soon as the object has been created. I
    > > can find out IF the object has been created (test the variable for
    > > undefined or null), but if it hasn't been created yet, I can't find a way
    > > to wait and send the notification when it has.[/color]
    >
    > Hi Chris, good - a nice clear description.
    >
    > In frame B:
    > In a timed loop within Frame B, poll frame A every 200 milliseconds or so
    > to see if the object exists (actually poll each stage down the DOM tree to
    > catch if the frame exists etc, before directly testing for the object).
    > Only when you find the object do you leave the polling loop and invoke the
    > rest of your code. Note: You might want to put a limit on how long you poll
    > before you give up and err. You should also check that the method is
    > available on your object, rather than just assuming that once the object
    > exists that the method will as well. Something similar I think to some old
    > code I once wrote: http://www.andrewu.co.uk/clj/checkframesloaded/[/color]

    Does the code at the end of this message look like what you were
    suggesting? Do you see any problems with it?

    Incidentally, you wrote:[color=blue]
    > Only when you find the object do you leave the polling loop and invoke the
    > rest of your code.[/color]

    I assume by "the rest of your code", you mean the method I need to
    invoke on the other frame? I certainly want the main frame to be
    operational before the other frame finishes loading.
    [color=blue]
    > You should also check that the method is
    > available on your object, rather than just assuming that once the object
    > exists that the method will as well.[/color]

    Okay, I've done so... but is there any reason this would ever happen?
    I'm assigning the methods to the object in the constructor, and I would
    think that the constructor would have to completely finish before the
    reference returned from the object creation expression. I'm not very
    familiar with JavaScript, and I'll do it the careful way until I know
    for sure...

    Anyhow, here's the code...

    <script type="text/javascript">
    var topDone = false;
    var bottomDone = false;
    var intervalKey = window.setInter val(function()
    {
    if (!topDone && parent && parent.topFrame
    && parent.topFrame .menu && parent.topFrame .menu.showPage)
    {
    parent.topFrame .menu.showPage( '<%= pageName %>');
    topDone = true;
    }

    if (!bottomDone && parent && parent.bottomFr ame
    && parent.bottomFr ame.menu && parent.bottomFr ame.menu.showPa ge)
    {
    parent.bottomFr ame.menu.showPa ge('<%= pageName %>');
    bottomDone = true;
    }

    if (topDone && bottomDone)
    {
    window.clearInt erval(intervalK ey);
    }
    }, 200);
    </script>

    I'm a bit bothered by polling, even with the delay... but I guess it
    can't be helped, then.

    --

    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation

    Comment

    • Andrew Urquhart

      #3
      Re: concurrency issues

      On Mon, 28 Jul 2003 14:53:45 -0600, Chris Smith <cdsmith@twu.ne t> wrote:
      [color=blue][color=green]
      >> Something similar I think to some old code I once wrote:
      >> http://www.andrewu.co.uk/clj/checkframesloaded/[/color]
      >
      > Does the code at the end of this message look like what you were
      > suggesting?[/color]

      Yes, I think so.
      [color=blue]
      > Do you see any problems with it?[/color]

      I see you're working backwards up the DOM tree from your current context,
      which is probably safer than what I would have done - it means that your
      script should still work if your site is viewed in someone else's frame!
      [color=blue]
      > Incidentally, you wrote:[color=green]
      >> Only when you find the object do you leave the polling loop and invoke
      >> the rest of your code.[/color]
      >
      > I assume by "the rest of your code", you mean the method I need to invoke
      > on the other frame? I certainly want the main frame to be operational
      > before the other frame finishes loading.[/color]

      I was assuming that the call itself to the method living in frame A would
      be just a part of additional script in frame B. If all frame B wants to do
      is invoke the object method in frame A once it's available, then yes. Not
      sure what the implications are of your last sentence there as I don't know
      what frame you're referencing when you say "main" frame.
      [color=blue][color=green]
      >> You should also check that the method is available on your object,
      >> rather than just assuming that once the object exists that the method
      >> will as well.[/color]
      >
      > Okay, I've done so... but is there any reason this would ever happen?[/color]

      I didn't know how you were setting your object methods, so assumed the
      worst case scenario. You could have created the object and then added
      methods to it later on, in which case your script had a very small chance
      of failing since the object could exist before the method did! Although it
      could be considered pedantic, personally I consider it just good practice.

      <trim>
      [color=blue]
      > I'm a bit bothered by polling, even with the delay... but I guess it
      > can't be helped, then.[/color]

      OK, but given the nature of the medium I don't think you can do much else
      (at least not that is cross-browser, although you could always find a non-
      framed solution!) Reduce the polling delay if needs be (but not too small,
      the granularity on some platform combos can be of the order of 100ms if I
      remember rightly). Also bear in mind that if this is running in an open web
      environment I'd be surprised if anyone noticed an extra 100-200ms when it
      might take ten times longer to download the page, and don't forget the
      average additional wait as a result of this script is half this value!

      Cheers,
      --
      Andrew Urquhart
      - http://www.andrewu.co.uk/clj.asp
      - FAQ for comp.lang.javas cript by Jim Ley at http://jibbering.com/faq
      - Archive at http://groups.google.com/groups?grou...ang.javascript

      Comment

      Working...