Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In chipper-startup, prevent further attempts after success #771

Open
samreid opened this issue Jul 12, 2019 · 5 comments
Open

In chipper-startup, prevent further attempts after success #771

samreid opened this issue Jul 12, 2019 · 5 comments

Comments

@samreid
Copy link
Member

samreid commented Jul 12, 2019

From #764 I said:

(6) It looks like there is a little interference between the setTimeout and the addEventListener( 'resize'. For instance, if attemptStart is initially false, then a timeout is scheduled for 1000ms later, then resize triggers at 500ms, then attemptStart is called later unnecessarily. I know at that point it becomes a no-op, but it seems asymmetrical to not have the events types cancel each other. Not sure how much more complicated this would make it though. Something like:

  // We can call this anytime to attempt to start things (if they haven't been started already).
  function attemptStart() {
    if ( !started && isReady() ) {
      started = true;
      timeoutStillRunning && clearTimeout();
      windowListenerStillRunning && removeWindowListener();

      phet.chipper.runRequireJS();
    }
  }

For instance, with this patch:

Index: templates/chipper-startup.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- templates/chipper-startup.js	(revision 0e1cf7b7f51776bca20379e777bc36200b9b3757)
+++ templates/chipper-startup.js	(date 1562348254000)
@@ -62,7 +62,10 @@
 
   // We can call this anytime to attempt to start things (if they haven't been started already).
   function attemptStart() {
+    console.log( 'attempt start' );
     if ( !started && isReady() ) {
+
+      console.log( 'started' );
       started = true;
 
       phet.chipper.runRequireJS();
@@ -71,6 +74,7 @@
 
   // Attempt to start based on timeouts (in case other methods don't work). This will call attemptStart() at least once.
   ( function timeoutListener() {
+    console.log( 'timeoutListener' );
     attemptStart();
 
     if ( !started ) {
@@ -82,10 +86,12 @@
 
     // Attempt to start on window resizes
     window.addEventListener( 'resize', function resizeListener() {
+      console.log( 'resize' );
       attemptStart();
 
       if ( started ) {
         window.removeEventListener( 'resize', resizeListener );
+        console.log( 'resize.started' );
       }
     } );
   }

I saw this console.log trace:

timeoutListener
attempt start
timeoutListener
attempt start
timeoutListener
attempt start
timeoutListener
attempt start
timeoutListener
attempt start
resize
attempt start
started
resize.started
timeoutListener
attempt start

Notice that even after resize and started, there is still another attempt to start.

@samreid
Copy link
Member Author

samreid commented Jul 12, 2019

@jonathanolson replied:

It seems like more effort to independently track the state of all listeners to be able to clear them. Is there an advantage?

@samreid
Copy link
Member Author

samreid commented Jul 12, 2019

On slack, I said:

I also think it would be nice to remove listeners on a fine-grained basis so we don’t end up with the pattern where “attempt start” is called again after it is already started.

Would this be prohibitively expensive to implement?

@jonathanolson
Copy link
Contributor

Would this be prohibitively expensive to implement?

It's not prohibitive, but I find the current approach cleaner and easier to maintain. What is an advantage to the other approach, as it would have more code and more moving parts?

@samreid
Copy link
Member Author

samreid commented Jul 27, 2019

Leaving self-assigned to implement the fine-grained pattern for comparison. Not sure when I'll get to this, so let me know if timing becomes an issue.

@samreid
Copy link
Member Author

samreid commented Mar 17, 2020

This is not a priority for me at the moment, unassigning.

@samreid samreid removed their assignment Mar 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants