ReadableStreamSource.h [plain text]
#pragma once
#if ENABLE(READABLE_STREAM_API)
#include "JSDOMPromise.h"
#include "ReadableStreamDefaultController.h"
#include <wtf/Optional.h>
namespace WebCore {
class ReadableStreamSource : public RefCounted<ReadableStreamSource> {
public:
virtual ~ReadableStreamSource() { }
void start(ReadableStreamDefaultController&&, DOMPromise<void>&&);
void pull(DOMPromise<void>&&);
void cancel(JSC::JSValue);
bool isPulling() const { return !!m_promise; }
protected:
ReadableStreamDefaultController& controller() { return m_controller.value(); }
const ReadableStreamDefaultController& controller() const { return m_controller.value(); }
void startFinished();
void pullFinished();
void cancelFinished();
void clean();
virtual void setActive() = 0;
virtual void setInactive() = 0;
virtual void doStart() = 0;
virtual void doPull() = 0;
virtual void doCancel() = 0;
private:
std::optional<DOMPromise<void>> m_promise;
std::optional<ReadableStreamDefaultController> m_controller;
};
inline void ReadableStreamSource::start(ReadableStreamDefaultController&& controller, DOMPromise<void>&& promise)
{
ASSERT(!m_promise);
m_promise = WTFMove(promise);
m_controller = WTFMove(controller);
setActive();
doStart();
}
inline void ReadableStreamSource::pull(DOMPromise<void>&& promise)
{
ASSERT(!m_promise);
ASSERT(m_controller);
m_promise = WTFMove(promise);
setActive();
doPull();
}
inline void ReadableStreamSource::startFinished()
{
ASSERT(m_promise);
std::exchange(m_promise, std::nullopt).value().resolve();
setInactive();
}
inline void ReadableStreamSource::pullFinished()
{
ASSERT(m_promise);
std::exchange(m_promise, std::nullopt).value().resolve();
setInactive();
}
inline void ReadableStreamSource::cancel(JSC::JSValue)
{
clean();
doCancel();
}
inline void ReadableStreamSource::clean()
{
if (m_promise) {
m_promise = std::nullopt;
setInactive();
}
}
}
#endif // ENABLE(READABLE_STREAM_API)