1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "ReadableStreamTee.h"
11 #include "mozilla/HoldDropJSObjects.h"
12 #include "mozilla/dom/ReadableStream.h"
13 #include "mozilla/dom/Promise.h"
15 namespace mozilla::dom
{
17 using namespace streams_abstract
;
19 NS_IMPL_CYCLE_COLLECTION_WITH_JS_MEMBERS(TeeState
,
20 (mStream
, mReader
, mBranch1
, mBranch2
,
23 NS_IMPL_CYCLE_COLLECTING_ADDREF(TeeState
)
24 NS_IMPL_CYCLE_COLLECTING_RELEASE(TeeState
)
25 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TeeState
)
26 NS_INTERFACE_MAP_ENTRY(nsISupports
)
29 TeeState::TeeState(ReadableStream
* aStream
, bool aCloneForBranch2
)
31 mReason1(JS::NullValue()),
32 mReason2(JS::NullValue()),
33 mCloneForBranch2(aCloneForBranch2
) {
34 mozilla::HoldJSObjects(this);
35 MOZ_RELEASE_ASSERT(!aCloneForBranch2
,
36 "cloneForBranch2 path is not implemented.");
39 already_AddRefed
<TeeState
> TeeState::Create(ReadableStream
* aStream
,
40 bool aCloneForBranch2
,
42 RefPtr
<TeeState
> teeState
= new TeeState(aStream
, aCloneForBranch2
);
44 RefPtr
<ReadableStreamDefaultReader
> reader
=
45 AcquireReadableStreamDefaultReader(teeState
->GetStream(), aRv
);
49 teeState
->SetReader(reader
);
51 RefPtr
<Promise
> promise
=
52 Promise::CreateInfallible(teeState
->GetStream()->GetParentObject());
53 teeState
->SetCancelPromise(promise
);
55 return teeState
.forget();
58 // https://streams.spec.whatwg.org/#abstract-opdef-readablestreamdefaulttee
59 // Pull Algorithm Steps:
60 void TeeState::PullCallback(JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
62 // Step 13.1: If reading is true,
64 // Step 13.1.1: Set readAgain to true.
67 // Step 13.1.2: Return a promise resolved with undefined.
68 // (The caller will create it if necessary)
72 // Step 13.2: Set reading to true.
75 // Step 13.3: Let readRequest be a read request with the following items:
76 RefPtr
<ReadRequest
> readRequest
=
77 new ReadableStreamDefaultTeeReadRequest(this);
79 // Step 13.4: Perform ! ReadableStreamDefaultReaderRead(reader, readRequest).
80 RefPtr
<ReadableStreamGenericReader
> reader
= GetReader();
81 ReadableStreamDefaultReaderRead(aCx
, reader
, readRequest
, aRv
);
83 // Step 13.5: Return a promise resolved with undefined.
84 // (The caller will create it if necessary)
87 } // namespace mozilla::dom