Reviewers: rchandia, rjrjr,

Description:
Suppress redundant PERSIST updates.
Issue 5674.
Patch by: bobv
Review by: rchandia


Please review this at http://gwt-code-reviews.appspot.com/1196801/show

Affected files:
M user/src/com/google/gwt/requestfactory/shared/impl/AbstractRequestContext.java
  M user/test/com/google/gwt/requestfactory/client/FindServiceTest.java


Index: user/src/com/google/gwt/requestfactory/shared/impl/AbstractRequestContext.java
===================================================================
--- user/src/com/google/gwt/requestfactory/shared/impl/AbstractRequestContext.java (revision 9365) +++ user/src/com/google/gwt/requestfactory/shared/impl/AbstractRequestContext.java (working copy)
@@ -114,7 +114,7 @@
   }

   private static final String PARENT_OBJECT = "parentObject";
-
+ private final Set<SimpleProxyId<?>> createdids = new HashSet<SimpleProxyId<?>>(); private final List<AbstractRequest<?>> invocations = new ArrayList<AbstractRequest<?>>();
   private boolean locked;
   private final AbstractRequestFactory requestFactory;
@@ -148,8 +148,9 @@
   public <T extends BaseProxy> T create(Class<T> clazz) {
     checkLocked();

-    AutoBean<T> created = requestFactory.createProxy(clazz,
-        requestFactory.allocateId(clazz));
+    SimpleProxyId<T> id = requestFactory.allocateId(clazz);
+    createdids.add(id);
+    AutoBean<T> created = requestFactory.createProxy(clazz, id);
     return takeOwnership(created);
   }

@@ -791,7 +792,8 @@
       SimpleProxyId<?> id = getId(op);
       if (id.isEphemeral()) {
         processReturnOperation(id, op);
-      } else if (id.wasEphemeral()) {
+      } else if (id.wasEphemeral() && createdids.contains(id)) {
+        // Only send a PERSIST if this RequestContext created the id.
         processReturnOperation(id, op, WriteOperation.PERSIST,
             WriteOperation.UPDATE);
       } else {
Index: user/test/com/google/gwt/requestfactory/client/FindServiceTest.java
===================================================================
--- user/test/com/google/gwt/requestfactory/client/FindServiceTest.java (revision 9365) +++ user/test/com/google/gwt/requestfactory/client/FindServiceTest.java (working copy)
@@ -23,6 +23,7 @@
 import com.google.gwt.requestfactory.shared.SimpleBarProxy;
 import com.google.gwt.requestfactory.shared.SimpleBarRequest;
 import com.google.gwt.requestfactory.shared.SimpleFooProxy;
+import com.google.gwt.requestfactory.shared.SimpleFooRequest;
 import com.google.gwt.requestfactory.shared.SimpleRequestFactory;

 /**
@@ -131,6 +132,38 @@
               @Override
               public void onSuccess(SimpleBarProxy returnedProxy) {
                 assertEquals(stableId, returnedProxy.stableId());
+                finishTestAndReset();
+              }
+            });
+          }
+        });
+  }
+
+  public void testFetchsAfterCreateDontUpdate() {
+    final int[] count = {0};
+ final HandlerRegistration registration = EntityProxyChange.registerForProxyType(
+        req.getEventBus(), SimpleFooProxy.class,
+        new EntityProxyChange.Handler<SimpleFooProxy>() {
+ public void onProxyChange(EntityProxyChange<SimpleFooProxy> event) {
+            System.out.println(event.getWriteOperation());
+            count[0]++;
+          }
+        });
+    delayTestFinish(TEST_DELAY);
+    SimpleFooRequest context = req.simpleFooRequest();
+    SimpleFooProxy proxy = context.create(SimpleFooProxy.class);
+    context.persistAndReturnSelf().using(proxy).fire(
+        new Receiver<SimpleFooProxy>() {
+          @Override
+          public void onSuccess(SimpleFooProxy response) {
+            // Persist and Update events
+            assertEquals(2, count[0]);
+ req.find(response.stableId()).fire(new Receiver<SimpleFooProxy>() {
+              @Override
+              public void onSuccess(SimpleFooProxy response) {
+                // No new events
+                assertEquals(2, count[0]);
+                registration.removeHandler();
                 finishTestAndReset();
               }
             });


--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to