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

Review at http://gwt-code-reviews.appspot.com/1196801


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9372 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/requestfactory/shared/impl/AbstractRequestContext.java b/user/src/com/google/gwt/requestfactory/shared/impl/AbstractRequestContext.java
index 839f4ee..9124cea 100644
--- a/user/src/com/google/gwt/requestfactory/shared/impl/AbstractRequestContext.java
+++ b/user/src/com/google/gwt/requestfactory/shared/impl/AbstractRequestContext.java
@@ -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 {
diff --git a/user/test/com/google/gwt/requestfactory/client/FindServiceTest.java b/user/test/com/google/gwt/requestfactory/client/FindServiceTest.java
index 6285434..1967839 100644
--- a/user/test/com/google/gwt/requestfactory/client/FindServiceTest.java
+++ b/user/test/com/google/gwt/requestfactory/client/FindServiceTest.java
@@ -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;
 
 /**
@@ -138,6 +139,37 @@
         });
   }
 
+  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) {
+            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();
+              }
+            });
+          }
+        });
+  }
+
   /**
    * Demonstrates behavior when fetching an unpersisted id. The setup is
    * analagous to saving a future id into a cookie and then trying to fetch it