]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java
(refs #7607) Fixed handling of SCLContext in asynchronous requests
[simantics/platform.git] / bundles / org.simantics.scl.db / src / org / simantics / scl / db / SCLFunctions.java
index da88b72c063ab2bb2862156f1f39afe5058839c9..59923933f6107f3e8f0d25e928c9b2fdc7229f1e 100644 (file)
@@ -53,25 +53,19 @@ public class SCLFunctions {
     }
     
     public static void asyncRead(final Function f) throws DatabaseException {
-        final SCLContext context = SCLContext.getCurrent();
-        Object graph = context.get(GRAPH);
-        if (graph != null) {
-            f.apply(Tuple0.INSTANCE);
-        } else {
-            Simantics.getSession().asyncRequest(new ReadRequest() {
-                @Override
-                public void run(ReadGraph graph) throws DatabaseException {
-                    SCLContext.push(context);
-                    ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
-                    try {
-                        f.apply(Tuple0.INSTANCE);
-                    } finally {
-                        context.put(GRAPH, oldGraph);
-                        SCLContext.pop();
-                    }
+        final SCLContext context = SCLContext.createDerivedContext();
+        Simantics.getSession().asyncRequest(new ReadRequest() {
+            @Override
+            public void run(ReadGraph graph) throws DatabaseException {
+                SCLContext.push(context);
+                context.put(GRAPH, graph);
+                try {
+                    f.apply(Tuple0.INSTANCE);
+                } finally {
+                    SCLContext.pop();
                 }
-            });
-        }
+            }
+        });
     }
     
     public static <T> T syncRead(final Function f) throws DatabaseException {
@@ -97,25 +91,19 @@ public class SCLFunctions {
     }
     
     public static void asyncWrite(final Function f) throws DatabaseException {
-        final SCLContext context = SCLContext.getCurrent();
-        Object graph = context.get(GRAPH);
-        if (graph != null) {
-            f.apply(Tuple0.INSTANCE);
-        } else {
-            Simantics.getSession().asyncRequest(new WriteRequest() {
-                @Override
-                public void perform(WriteGraph graph) throws DatabaseException {
-                    SCLContext.push(context);
-                    ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
-                    try {
-                        f.apply(Tuple0.INSTANCE);
-                    } finally {
-                        context.put(GRAPH, oldGraph);
-                        SCLContext.pop();
-                    }
+        SCLContext context = SCLContext.createDerivedContext();
+        Simantics.getSession().asyncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                SCLContext.push(context);
+                context.put(GRAPH, graph);
+                try {
+                    f.apply(Tuple0.INSTANCE);
+                } finally {
+                    SCLContext.pop();
                 }
-            });
-        }
+            }
+        });
     }
     
     public static <T> T syncWrite(final Function f) throws DatabaseException {