Skip to content

Commit

Permalink
Return ScriptPromise from TransformStreamTransformer
Browse files Browse the repository at this point in the history
Previously the Transform() and Flush() methods of
TransformStreamTransformer returned void. This meant that there was no
way for them to wait for asynchronous work to finish.

Change the function signatures to return ScriptPromise. Transform() or
Flush() won't be called again until the returned promise is settled.

Returning v8::Local<v8::Promise> would have been slightly more
efficient, but ScriptPromise was used for consistency with
UnderlyingSinkBase and UnderlyingSourceBase.

Add tests that Transform() and Flush() promises are actually waited for.

BUG=962837

Change-Id: I2e79c6a79a457fd021eee17eca4f1502c3aa521f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1865735
Reviewed-by: Yutaka Hirano <yhirano@chromium.org>
Commit-Queue: Adam Rice <ricea@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709861}
  • Loading branch information
ricea authored and Commit Bot committed Oct 28, 2019
1 parent 9d5550f commit 18def51
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "third_party/blink/renderer/core/streams/transform_stream_native.h"

#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/core/streams/miscellaneous_operations.h"
Expand Down Expand Up @@ -66,19 +67,20 @@ class TransformStreamNative::FlushAlgorithm final : public StreamAlgorithm {
ExceptionState exception_state(script_state->GetIsolate(),
ExceptionState::kUnknownContext, "", "");
ControllerInterface controller_interface(script_state, controller_);
ScriptPromise promise;
{
// This is needed because the realm of the transformer can be different
// from the realm of the transform stream.
ScriptState::Scope scope(transformer_->GetScriptState());
transformer_->Flush(&controller_interface, exception_state);
promise = transformer_->Flush(&controller_interface, exception_state);
}
if (exception_state.HadException()) {
auto exception = exception_state.GetException();
exception_state.ClearException();
return PromiseReject(script_state, exception);
}

return PromiseResolveWithUndefined(script_state);
return promise.V8Value().As<v8::Promise>();
}

// SetController() must be called before Run() is.
Expand Down Expand Up @@ -112,19 +114,21 @@ class TransformStreamNative::TransformAlgorithm final : public StreamAlgorithm {
ExceptionState exception_state(script_state->GetIsolate(),
ExceptionState::kUnknownContext, "", "");
ControllerInterface controller_interface(script_state, controller_);
ScriptPromise promise;
{
// This is needed because the realm of the transformer can be different
// from the realm of the transform stream.
ScriptState::Scope scope(transformer_->GetScriptState());
transformer_->Transform(argv[0], &controller_interface, exception_state);
promise = transformer_->Transform(argv[0], &controller_interface,
exception_state);
}
if (exception_state.HadException()) {
auto exception = exception_state.GetException();
exception_state.ClearException();
return PromiseReject(script_state, exception);
}

return PromiseResolveWithUndefined(script_state);
return promise.V8Value().As<v8::Promise>();
}

// SetController() must be called before Run() is.
Expand Down
Loading

0 comments on commit 18def51

Please sign in to comment.