flush() presenters when their last instance is removed from the stack async
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Thu, 06 Mar 2014 23:45:57 +0100
branchasync
changeset 58255688d308ce7
parent 581 0e33d61d89cc
child 586 213efcfc5a0c
flush() presenters when their last instance is removed from the stack
boot/src/main/java/org/netbeans/html/boot/impl/FnContext.java
boot/src/test/java/org/netbeans/html/boot/impl/FnTest.java
     1.1 --- a/boot/src/main/java/org/netbeans/html/boot/impl/FnContext.java	Thu Mar 06 21:53:38 2014 +0100
     1.2 +++ b/boot/src/main/java/org/netbeans/html/boot/impl/FnContext.java	Thu Mar 06 23:45:57 2014 +0100
     1.3 @@ -43,6 +43,7 @@
     1.4  package org.netbeans.html.boot.impl;
     1.5  
     1.6  import java.io.Closeable;
     1.7 +import java.io.Flushable;
     1.8  import java.io.IOException;
     1.9  import java.util.logging.Logger;
    1.10  import org.apidesign.html.boot.spi.Fn;
    1.11 @@ -53,10 +54,17 @@
    1.12   */
    1.13  public final class FnContext implements Closeable {
    1.14      private static final Logger LOG = Logger.getLogger(FnContext.class.getName());
    1.15 +    private static final FnContext DUMMY;
    1.16 +    static {
    1.17 +        DUMMY = new FnContext(null, null);
    1.18 +        DUMMY.prev = DUMMY;
    1.19 +    }
    1.20  
    1.21      private Object prev;
    1.22 -    private FnContext(Fn.Presenter p) {
    1.23 -        this.prev = p;
    1.24 +    private final Fn.Presenter current;
    1.25 +    private FnContext(Fn.Presenter prevP, Fn.Presenter newP) {
    1.26 +        this.current = newP;
    1.27 +        this.prev = prevP;
    1.28      }
    1.29  
    1.30      @Override
    1.31 @@ -64,6 +72,9 @@
    1.32          if (prev != this) {
    1.33              currentPresenter((Fn.Presenter)prev);
    1.34              prev = this;
    1.35 +            if (current instanceof Flushable) {
    1.36 +                ((Flushable)current).flush();
    1.37 +            }
    1.38          }
    1.39      }
    1.40  /*
    1.41 @@ -75,7 +86,11 @@
    1.42      }
    1.43  */
    1.44      public static Closeable activate(Fn.Presenter newP) {
    1.45 -        return new FnContext(currentPresenter(newP));
    1.46 +        final Fn.Presenter oldP = currentPresenter(newP);
    1.47 +        if (oldP == newP) {
    1.48 +            return DUMMY;
    1.49 +        }
    1.50 +        return new FnContext(oldP, newP);
    1.51      }
    1.52      
    1.53      
     2.1 --- a/boot/src/test/java/org/netbeans/html/boot/impl/FnTest.java	Thu Mar 06 21:53:38 2014 +0100
     2.2 +++ b/boot/src/test/java/org/netbeans/html/boot/impl/FnTest.java	Thu Mar 06 23:45:57 2014 +0100
     2.3 @@ -43,6 +43,8 @@
     2.4  package org.netbeans.html.boot.impl;
     2.5  
     2.6  import java.io.Closeable;
     2.7 +import java.io.Flushable;
     2.8 +import java.io.IOException;
     2.9  import java.io.Reader;
    2.10  import java.net.URL;
    2.11  import java.net.URLClassLoader;
    2.12 @@ -55,8 +57,10 @@
    2.13  import javax.script.ScriptEngineManager;
    2.14  import javax.script.ScriptException;
    2.15  import org.apidesign.html.boot.spi.Fn;
    2.16 +import static org.testng.Assert.assertEquals;
    2.17  import org.testng.annotations.BeforeClass;
    2.18  import org.testng.annotations.BeforeMethod;
    2.19 +import org.testng.annotations.Test;
    2.20  
    2.21  /**
    2.22   *
    2.23 @@ -141,6 +145,40 @@
    2.24          methodClass = loader.loadClass(JsMethods.class.getName());
    2.25          close.close();
    2.26      }
    2.27 +    
    2.28 +    @Test public void flushingPresenter() throws IOException {
    2.29 +        class FP implements Fn.Presenter, Flushable {
    2.30 +            int flush;
    2.31 +
    2.32 +            @Override
    2.33 +            public Fn defineFn(String code, String... names) {
    2.34 +                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.35 +            }
    2.36 +
    2.37 +            @Override
    2.38 +            public void displayPage(URL page, Runnable onPageLoad) {
    2.39 +                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.40 +            }
    2.41 +
    2.42 +            @Override
    2.43 +            public void loadScript(Reader code) throws Exception {
    2.44 +                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.45 +            }
    2.46 +
    2.47 +            @Override
    2.48 +            public void flush() throws IOException {
    2.49 +                flush++;
    2.50 +            }
    2.51 +        }
    2.52 +        
    2.53 +        FP p = new FP();
    2.54 +        Closeable c1 = Fn.activate(p);
    2.55 +        Closeable c2 = Fn.activate(p);
    2.56 +        c2.close();
    2.57 +        assertEquals(p.flush, 0, "No flush yet");
    2.58 +        c1.close();
    2.59 +        assertEquals(p.flush, 1, "Now flushed");
    2.60 +    }
    2.61  
    2.62      @BeforeMethod public void initPresenter() {
    2.63          FnContext.currentPresenter(presenter);