Array properties may be modified from background thread setters
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Tue, 08 Jul 2014 07:59:01 +0200
branchsetters
changeset 7193ce33991bd96
parent 718 70c2ca785e5a
child 720 caad0b28814d
Array properties may be modified from background thread
json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java
json/src/main/java/org/netbeans/html/json/impl/JSONList.java
     1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java	Tue Jul 08 07:56:25 2014 +0200
     1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java	Tue Jul 08 07:59:01 2014 +0200
     1.3 @@ -220,6 +220,45 @@
     1.4          }
     1.5      }
     1.6      
     1.7 +    @KOTest public void displayContentOfAsyncArray() throws Exception {
     1.8 +        if (js == null) {
     1.9 +            Utils.exposeHTML(KnockoutTest.class, 
    1.10 +                "<ul id='ul' data-bind='foreach: results'>\n"
    1.11 +                + "  <li data-bind='text: $data, click: $root.call'/>\n"
    1.12 +                + "</ul>\n"
    1.13 +            );
    1.14 +            js = Models.bind(new KnockoutModel(), newContext());
    1.15 +            js.getResults().add("Ahoj");
    1.16 +            js.applyBindings();
    1.17 +
    1.18 +            int cnt = Utils.countChildren(KnockoutTest.class, "ul");
    1.19 +            assert cnt == 1 : "One child, but was " + cnt;
    1.20 +            
    1.21 +            Timer t = new Timer("add to array");
    1.22 +            t.schedule(new TimerTask() {
    1.23 +                @Override
    1.24 +                public void run() {
    1.25 +                    js.getResults().add("Hi");
    1.26 +                }
    1.27 +            }, 1);
    1.28 +        }
    1.29 +
    1.30 +
    1.31 +        int cnt = Utils.countChildren(KnockoutTest.class, "ul");
    1.32 +        if (cnt != 2) {
    1.33 +            throw new InterruptedException();
    1.34 +        }
    1.35 +
    1.36 +        try {
    1.37 +            triggerChildClick("ul", 1);
    1.38 +
    1.39 +            assert 1 == js.getCallbackCount() : "One callback " + js.getCallbackCount();
    1.40 +            assert "Hi".equals(js.getName()) : "We got callback from 2nd child " + js.getName();
    1.41 +        } finally {
    1.42 +            Utils.exposeHTML(KnockoutTest.class, "");
    1.43 +        }
    1.44 +    }
    1.45 +    
    1.46      @KOTest public void displayContentOfComputedArray() throws Exception {
    1.47          Object exp = Utils.exposeHTML(KnockoutTest.class, 
    1.48              "<ul id='ul' data-bind='foreach: bothNames'>\n"
     2.1 --- a/json/src/main/java/org/netbeans/html/json/impl/JSONList.java	Tue Jul 08 07:56:25 2014 +0200
     2.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/JSONList.java	Tue Jul 08 07:59:01 2014 +0200
     2.3 @@ -174,16 +174,21 @@
     2.4      }
     2.5  
     2.6      private void notifyChange() {
     2.7 -        Bindings m = PropertyBindingAccessor.getBindings(proto, false);
     2.8 -        if (m != null) {
     2.9 -            m.valueHasMutated(name, null, null);
    2.10 -            for (String dependant : deps) {
    2.11 -                m.valueHasMutated(dependant, null, null);
    2.12 +        proto.getContext().execute(new Runnable() {
    2.13 +            @Override
    2.14 +            public void run() {
    2.15 +                Bindings m = PropertyBindingAccessor.getBindings(proto, false);
    2.16 +                if (m != null) {
    2.17 +                    m.valueHasMutated(name, null, null);
    2.18 +                    for (String dependant : deps) {
    2.19 +                        m.valueHasMutated(dependant, null, null);
    2.20 +                    }
    2.21 +                    if (index >= 0) {
    2.22 +                        PropertyBindingAccessor.notifyProtoChange(proto, index);
    2.23 +                    }
    2.24 +                }
    2.25              }
    2.26 -            if (index >= 0) {
    2.27 -                PropertyBindingAccessor.notifyProtoChange(proto, index);
    2.28 -            }
    2.29 -        }
    2.30 +        });
    2.31      }
    2.32  
    2.33      @Override