This is an automated email from the ASF dual-hosted git repository.

jtulach pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans-html4j.git

commit 8c5f0bbf05983cda70a2b9a8575f09b67b06c2fd
Author: Jaroslav Tulach <[email protected]>
AuthorDate: Tue Feb 12 19:23:34 2019 +0100

    Create the objs in constructor
---
 .../java/org/netbeans/html/ko4j/CacheObjs.java     | 46 ++++++++++++++++++++++
 .../main/java/org/netbeans/html/ko4j/KOTech.java   | 45 +--------------------
 .../main/java/org/netbeans/html/ko4j/Knockout.java | 31 +++++++++++++--
 3 files changed, 75 insertions(+), 47 deletions(-)

diff --git a/ko4j/src/main/java/org/netbeans/html/ko4j/CacheObjs.java 
b/ko4j/src/main/java/org/netbeans/html/ko4j/CacheObjs.java
new file mode 100644
index 0000000..af7e9f6
--- /dev/null
+++ b/ko4j/src/main/java/org/netbeans/html/ko4j/CacheObjs.java
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.netbeans.html.ko4j;
+
+final class CacheObjs {
+    private static final CacheObjs C = new CacheObjs();
+
+    private Object[] jsObjects;
+    private int jsIndex;
+
+    static CacheObjs find() {
+        return C;
+    }
+
+    Object getJSObject() {
+        int len = 64;
+        if (jsObjects != null && jsIndex < (len = jsObjects.length)) {
+            Object ret = jsObjects[jsIndex];
+            jsObjects[jsIndex] = null;
+            jsIndex++;
+            return ret;
+        }
+        jsObjects = Knockout.allocJS(len * 2);
+        jsIndex = 1;
+        Object ret = jsObjects[0];
+        jsObjects[0] = null;
+        return ret;
+    }
+}
diff --git a/ko4j/src/main/java/org/netbeans/html/ko4j/KOTech.java 
b/ko4j/src/main/java/org/netbeans/html/ko4j/KOTech.java
index 32f0ea9..04f9928 100644
--- a/ko4j/src/main/java/org/netbeans/html/ko4j/KOTech.java
+++ b/ko4j/src/main/java/org/netbeans/html/ko4j/KOTech.java
@@ -36,9 +36,6 @@ import org.netbeans.html.json.spi.Technology;
 final class KOTech
 implements Technology.BatchCopy<Knockout>, Technology.ValueMutated<Knockout>,
 Technology.ApplyId<Knockout>, Technology.ToJavaScript<Knockout> {
-    private Object[] jsObjects;
-    private int jsIndex;
-
     public KOTech() {
     }
     
@@ -48,53 +45,13 @@ Technology.ApplyId<Knockout>, 
Technology.ToJavaScript<Knockout> {
     }
 
     final Knockout createKO(Object model, Object copyFrom, PropertyBinding[] 
propArr, FunctionBinding[] funcArr, Knockout[] ko) {
-        String[] propNames = new String[propArr.length];
-        Number[] propInfo = new Number[propArr.length];
-        Object[] propValues = new Object[propArr.length];
-        for (int i = 0; i < propNames.length; i++) {
-            propNames[i] = propArr[i].getPropertyName();
-            int info =
-                (propArr[i].isReadOnly() ? 1 : 0) +
-                (propArr[i].isConstant()? 2 : 0);
-            propInfo[i] = info;
-            Object value = propArr[i].getValue();
-            if (value instanceof Enum) {
-                value = value.toString();
-            }
-            propValues[i] = value;
-        }
-        String[] funcNames = new String[funcArr.length];
-        for (int i = 0; i < funcNames.length; i++) {
-            funcNames[i] = funcArr[i].getFunctionName();
-        }
-        Object ret = getJSObject();
-        Knockout newKO = new Knockout(model, ret, propArr, funcArr);
+        Knockout newKO = new Knockout(model, copyFrom, propArr, funcArr);
         if (ko != null) {
             ko[0] = newKO;
         }
-        Knockout.wrapModel(
-            newKO,
-            ret, copyFrom,
-            propNames, propInfo, propValues, funcNames
-        );
         return newKO;
     }
     
-    private Object getJSObject() {
-        int len = 64;
-        if (jsObjects != null && jsIndex < (len = jsObjects.length)) {
-            Object ret = jsObjects[jsIndex];
-            jsObjects[jsIndex] = null;
-            jsIndex++;
-            return ret;
-        }
-        jsObjects = Knockout.allocJS(len * 2);
-        jsIndex = 1;
-        Object ret = jsObjects[0];
-        jsObjects[0] = null;
-        return ret;
-    }
-    
     @Override
     public Knockout wrapModel(Object model) {
         throw new UnsupportedOperationException();
diff --git a/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java 
b/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java
index ae52c39..7efd4e3 100644
--- a/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java
+++ b/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java
@@ -58,9 +58,8 @@ final class Knockout  {
     private Object js;
     private Object strong;
 
-    public Knockout(Object model, Object js, PropertyBinding[] props, 
FunctionBinding[] funcs) {
+    public Knockout(Object model, Object copyFrom, PropertyBinding[] props, 
FunctionBinding[] funcs) {
         this.strong = model;
-        this.js = js;
         this.props = new PropertyBinding[props.length];
         for (int i = 0; i < props.length; i++) {
             this.props[i] = props[i].weak();
@@ -69,12 +68,38 @@ final class Knockout  {
         for (int i = 0; i < funcs.length; i++) {
             this.funcs[i] = funcs[i].weak();
         }
+        this.js = initObjs(copyFrom);
     }
 
     final Object js() {
         return js;
     }
 
+    private Object initObjs(Object copyFrom) {
+        String[] propNames = new String[props.length];
+        Number[] propInfo = new Number[props.length];
+        Object[] propValues = new Object[props.length];
+        for (int i = 0; i < propNames.length; i++) {
+            propNames[i] = props[i].getPropertyName();
+            int info
+                    = (props[i].isReadOnly() ? 1 : 0)
+                    + (props[i].isConstant() ? 2 : 0);
+            propInfo[i] = info;
+            Object value = props[i].getValue();
+            if (value instanceof Enum) {
+                value = value.toString();
+            }
+            propValues[i] = value;
+        }
+        String[] funcNames = new String[funcs.length];
+        for (int i = 0; i < funcNames.length; i++) {
+            funcNames[i] = funcs[i].getFunctionName();
+        }
+        Object ret = CacheObjs.find().getJSObject();
+        wrapModel(this,ret, copyFrom, propNames, propInfo, propValues, 
funcNames);
+        return ret;
+    }
+
     static void cleanUp() {
         for (;;) {
             Knockout ko = null;
@@ -228,7 +253,7 @@ final class Knockout  {
         + "  koExpose(i, funcNames[i]);\n"
         + "}\n"
         )
-    static native void wrapModel(
+    private static native void wrapModel(
         Knockout thiz,
         Object ret, Object copyFrom,
         String[] propNames, Number[] propInfo,


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to