Bug 1321744. Part 2 - add the ability of promise chaining to Then(). draft
authorJW Wang <jwwang@mozilla.com>
Tue, 13 Dec 2016 17:07:03 +0800
changeset 448941 76542594d44cdadc0b35f4d3d3335b1cf2a58192
parent 448940 a8c1c34728687bfd3d9e653b40ff688f4c0e384f
child 448942 48620f5a7823ce3c73dcb7de1d157d60b4fe92c4
push id38491
push userjwwang@mozilla.com
push dateTue, 13 Dec 2016 09:13:24 +0000
bugs1321744
milestone53.0a1
Bug 1321744. Part 2 - add the ability of promise chaining to Then(). MozReview-Commit-ID: IaDgoWcmFRO
xpcom/threads/MozPromise.h
--- a/xpcom/threads/MozPromise.h
+++ b/xpcom/threads/MozPromise.h
@@ -673,16 +673,38 @@ private:
     // Allow passing Then() to MozPromiseRequestHolder::Begin().
     operator RefPtr<Request>()
     {
       RefPtr<ThenValueBase> thenValue = mThenValue.forget();
       mReceiver->ThenInternal(mResponseThread, thenValue, mCallSite);
       return thenValue.forget();
     }
 
+    // Allow RefPtr<MozPromise> p = somePromise->Then();
+    //       p->Then(thread1, ...);
+    //       p->Then(thread2, ...);
+    operator RefPtr<MozPromise>()
+    {
+      RefPtr<ThenValueBase> thenValue = mThenValue.forget();
+      // mCompletionPromise must be created before ThenInternal() to avoid race.
+      RefPtr<MozPromise> p = new MozPromise::Private(
+        "<completion promise>", true /* aIsCompletionPromise */);
+      thenValue->mCompletionPromise = p;
+      // Note ThenInternal() might nullify mCompletionPromise before return.
+      // So we need to return p instead of mCompletionPromise.
+      mReceiver->ThenInternal(mResponseThread, thenValue, mCallSite);
+      return p;
+    }
+
+    // Allow calling ->Then() again for more promise chaining.
+    RefPtr<MozPromise> operator->()
+    {
+      return *this;
+    }
+
   private:
     AbstractThread* mResponseThread;
     const char* mCallSite;
     RefPtr<ThenValueBase> mThenValue;
     MozPromise* mReceiver;
   };
 
   public: