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 16f7f4614742e98f7f0d7d9cea9bf6cdcca20b47
Author: Jaroslav Tulach <[email protected]>
AuthorDate: Thu Feb 14 05:49:56 2019 +0100

    Caching needs to be done per presenter
---
 .../java/org/netbeans/html/ko4j/CacheObjs.java     | 50 ++++++++++++++++++++--
 .../main/java/org/netbeans/html/ko4j/Knockout.java | 11 ++---
 2 files changed, 52 insertions(+), 9 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
index af7e9f6..6311ff2 100644
--- a/ko4j/src/main/java/org/netbeans/html/ko4j/CacheObjs.java
+++ b/ko4j/src/main/java/org/netbeans/html/ko4j/CacheObjs.java
@@ -19,14 +19,56 @@
 
 package org.netbeans.html.ko4j;
 
-final class CacheObjs {
-    private static final CacheObjs C = new CacheObjs();
+import java.lang.ref.WeakReference;
+import org.netbeans.html.boot.spi.Fn;
 
+final class CacheObjs extends WeakReference<Fn.Presenter> {
+    /* both @GuardedBy CacheObjs.class */
+    private static CacheObjs list;
+    private CacheObjs next;
+
+    /* both @GuardedBy presenter single threaded access */
     private Object[] jsObjects;
     private int jsIndex;
 
-    static CacheObjs find() {
-        return C;
+    private CacheObjs(CacheObjs next, Fn.Presenter p) {
+        super(p);
+        this.next = next;
+    }
+
+    static synchronized CacheObjs find(Fn.Presenter key) {
+        if (list == null) {
+            return list = new CacheObjs(null, key);
+        }
+
+        Fn.Presenter p;
+        for (;;) {
+            p = list.get();
+            if (p != null) {
+                break;
+            }
+            list = list.next;
+        }
+
+        if (p == key) {
+            return list;
+        }
+
+        CacheObjs prev = list;
+        CacheObjs now = list.next;
+
+        while (now != null) {
+            p = now.get();
+            if (p == null) {
+                prev.next = now;
+            }
+            if (p == key) {
+                return now;
+            }
+            prev = now;
+            now = now.next;
+        }
+        return prev.next = new CacheObjs(null, key);
     }
 
     Object getJSObject() {
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 ac91bcc..cc41bb8 100644
--- a/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java
+++ b/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java
@@ -79,15 +79,16 @@ final class Knockout  {
     }
 
     final Object js() {
-        Object js = objs.get(Fn.activePresenter());
+        final Fn.Presenter c = Fn.activePresenter();
+        Object js = objs.get(c);
         if (js == null) {
-            js = initObjs(copyFrom);
-            objs.put(Fn.activePresenter(), js);
+            js = initObjs(c, copyFrom);
+            objs.put(c, js);
         }
         return js;
     }
 
-    private Object initObjs(Object copyFrom) {
+    private Object initObjs(Fn.Presenter p, Object copyFrom) {
         String[] propNames = new String[props.length];
         Number[] propInfo = new Number[props.length];
         Object[] propValues = new Object[props.length];
@@ -107,7 +108,7 @@ final class Knockout  {
         for (int i = 0; i < funcNames.length; i++) {
             funcNames[i] = funcs[i].getFunctionName();
         }
-        Object ret = CacheObjs.find().getJSObject();
+        Object ret = CacheObjs.find(p).getJSObject();
         wrapModel(this,ret, copyFrom, propNames, propInfo, propValues, 
funcNames);
         return ret;
     }


---------------------------------------------------------------------
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