Changeset 194035 in webkit


Ignore:
Timestamp:
Dec 14, 2015 9:27:27 AM (8 years ago)
Author:
youenn.fablet@crf.canon.fr
Message:

[Streams API] Directly use @then as much as possible
https://bugs.webkit.org/show_bug.cgi?id=151631

Reviewed by Darin Adler.

Moved from @Promise.prototype.@then.@call(promise,...) to promise.@then.(...)
for promise objects that are not exposed to user scripts.

Updated promiseInvokeXX stream utility functions to ensure that returned promise always has a @then.
This allows improving the readability of code calling promiseInvokeXX functions.
Changed invokeOrNoop to promiseInvokeOrNoopNoCatch as invokeOrNoop
result is always wrapped as a promise using Promise.resolve.

No change in behavior.

  • Modules/streams/ReadableStream.js:

(initializeReadableStream):

  • Modules/streams/ReadableStreamInternals.js:

(teeReadableStream):
(teeReadableStreamBranch2CancelFunction):
(cancelReadableStream):

  • Modules/streams/StreamInternals.js:

(shieldingPromiseResolve): introduced this routine to ensure the returned promise has a @then property.
(promiseInvokeOrNoopNoCatch):
(promiseInvokeOrNoop):
(promiseInvokeOrFallbackOrNoop):

  • Modules/streams/WritableStream.js:

(initializeWritableStream):
(abort):

  • Modules/streams/WritableStreamInternals.js:

(callOrScheduleWritableStreamAdvanceQueue):

Location:
trunk/Source/WebCore
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r194033 r194035  
     12015-12-14  Youenn Fablet  <youenn.fablet@crf.canon.fr>
     2
     3        [Streams API] Directly use @then as much as possible
     4        https://bugs.webkit.org/show_bug.cgi?id=151631
     5
     6        Reviewed by Darin Adler.
     7
     8        Moved from @Promise.prototype.@then.@call(promise,...) to promise.@then.(...)
     9        for promise objects that are not exposed to user scripts.
     10
     11        Updated promiseInvokeXX stream utility functions to ensure that returned promise always has a @then.
     12        This allows improving the readability of code calling promiseInvokeXX functions.
     13        Changed invokeOrNoop to promiseInvokeOrNoopNoCatch as invokeOrNoop
     14        result is always wrapped as a promise using Promise.resolve.
     15
     16        No change in behavior.
     17
     18        * Modules/streams/ReadableStream.js:
     19        (initializeReadableStream):
     20        * Modules/streams/ReadableStreamInternals.js:
     21        (teeReadableStream):
     22        (teeReadableStreamBranch2CancelFunction):
     23        (cancelReadableStream):
     24        * Modules/streams/StreamInternals.js:
     25        (shieldingPromiseResolve): introduced this routine to ensure the returned promise has a @then property.
     26        (promiseInvokeOrNoopNoCatch):
     27        (promiseInvokeOrNoop):
     28        (promiseInvokeOrFallbackOrNoop):
     29        * Modules/streams/WritableStream.js:
     30        (initializeWritableStream):
     31        (abort):
     32        * Modules/streams/WritableStreamInternals.js:
     33        (callOrScheduleWritableStreamAdvanceQueue):
     34
    1352015-12-14  Xabier Rodriguez Calvar  <calvaris@igalia.com> and Youenn Fablet  <youenn.fablet@crf.canon.fr>
    236
  • trunk/Source/WebCore/Modules/streams/ReadableStream.js

    r193832 r194035  
    5656    this.@strategy = @validateAndNormalizeQueuingStrategy(strategy.size, strategy.highWaterMark);
    5757
    58     const result = @invokeOrNoop(underlyingSource, "start", [this.@controller]);
    59     @Promise.prototype.@then.@call(@Promise.@resolve(result), () => {
     58    @promiseInvokeOrNoopNoCatch(underlyingSource, "start", [this.@controller]).@then(() => {
    6059        this.@started = true;
    6160        @requestReadableStreamPull(this);
     
    105104    "use strict";
    106105
    107     // We are not shielding against methods and attributes of the reader and destination as those objects don't have to
    108     // be necessarily ReadableStreamReader and WritableStream.
     106    // FIXME: rewrite pipeTo so as to require to have 'this' as a ReadableStream and destination be a WritableStream.
     107    // See https://github.com/whatwg/streams/issues/407.
     108    // We should shield the pipeTo implementation at the same time.
    109109
    110110    const preventClose = @isObject(options) && !!options.preventClose;
  • trunk/Source/WebCore/Modules/streams/ReadableStreamInternals.js

    r193899 r194035  
    9797    });
    9898
    99     @Promise.prototype.@then.@call(reader.closed, undefined, function(e) {
     99    reader.@closedPromiseCapability.@promise.@then(undefined, function(e) {
    100100        if (teeState.closedOrErrored)
    101101            return;
     
    147147        teeState.reason1 = r;
    148148        if (teeState.canceled2) {
    149             @Promise.prototype.@then.@call(@cancelReadableStream(stream, [teeState.reason1, teeState.reason2]),
    150                                            teeState.cancelPromiseCapability.@resolve,
    151                                            teeState.cancelPromiseCapability.@reject);
     149            @cancelReadableStream(stream, [teeState.reason1, teeState.reason2]).@then(
     150                teeState.cancelPromiseCapability.@resolve,
     151                teeState.cancelPromiseCapability.@reject);
    152152        }
    153153        return teeState.cancelPromiseCapability.@promise;
     
    163163        teeState.reason2 = r;
    164164        if (teeState.canceled1) {
    165             @Promise.prototype.@then.@call(@cancelReadableStream(stream, [teeState.reason1, teeState.reason2]),
    166                                            teeState.cancelPromiseCapability.@resolve,
    167                                            teeState.cancelPromiseCapability.@reject);
     165            @cancelReadableStream(stream, [teeState.reason1, teeState.reason2]).@then(
     166                teeState.cancelPromiseCapability.@resolve,
     167                teeState.cancelPromiseCapability.@reject);
    168168        }
    169169        return teeState.cancelPromiseCapability.@promise;
     
    235235    stream.@pulling = true;
    236236
    237     const promise = @promiseInvokeOrNoop(stream.@underlyingSource, "pull", [stream.@controller]);
    238     @Promise.prototype.@then.@call(promise, function() {
     237    @promiseInvokeOrNoop(stream.@underlyingSource, "pull", [stream.@controller]).@then(function() {
    239238        stream.@pulling = false;
    240239        if (stream.@pullAgain) {
     
    274273    stream.@queue = @newQueue();
    275274    @finishClosingReadableStream(stream);
    276     return @Promise.prototype.@then.@call(@promiseInvokeOrNoop(stream.@underlyingSource, "cancel", [reason]), function() { });
     275    return @promiseInvokeOrNoop(stream.@underlyingSource, "cancel", [reason]).@then(function() { });
    277276}
    278277
  • trunk/Source/WebCore/Modules/streams/StreamInternals.js

    r193899 r194035  
    2828// @internal
    2929
    30 function invokeOrNoop(object, key, args)
     30function shieldingPromiseResolve(result)
     31{
     32    const promise = @Promise.@resolve(result);
     33    if (promise.@then === undefined)
     34        promise.@then = @Promise.prototype.@then;
     35    return promise;
     36}
     37
     38function promiseInvokeOrNoopNoCatch(object, key, args)
    3139{
    3240    "use strict";
    3341
    3442    const method = object[key];
    35     if (typeof method === "undefined")
    36         return;
    37     return method.@apply(object, args);
     43    if (method === undefined)
     44        return @Promise.@resolve();
     45    return @shieldingPromiseResolve(method.@apply(object, args));
    3846}
    3947
     
    4351
    4452    try {
    45         const method = object[key];
    46         if (typeof method === "undefined")
    47             return @Promise.@resolve();
    48         return @Promise.@resolve(method.@apply(object, args));
     53        return @promiseInvokeOrNoopNoCatch(object, key, args);
    4954    }
    5055    catch(error) {
     
    6065    try {
    6166        const method = object[key1];
    62         if (typeof method === "undefined")
    63             return @promiseInvokeOrNoop(object, key2, args2);
    64         return @Promise.@resolve(method.@apply(object, args1));
     67        if (method === undefined)
     68            return @promiseInvokeOrNoopNoCatch(object, key2, args2);
     69        return @shieldingPromiseResolve(method.@apply(object, args1));
    6570    }
    6671    catch(error) {
  • trunk/Source/WebCore/Modules/streams/WritableStream.js

    r192878 r194035  
    5757        @errorWritableStream(this, e);
    5858    };
    59     this.@startedPromise = @Promise.@resolve(@invokeOrNoop(underlyingSink, "start", [errorFunction]));
    60     @Promise.prototype.@then.@call(this.@startedPromise, () => {
     59    this.@startedPromise = @promiseInvokeOrNoopNoCatch(underlyingSink, "start", [errorFunction]);
     60    this.@startedPromise.@then(() => {
    6161        this.@started = true;
    6262        this.@startedPromise = undefined;
     
    8181    @errorWritableStream(this, reason);
    8282
    83     const sinkAbortPromise = @promiseInvokeOrFallbackOrNoop(this.@underlyingSink, "abort", [reason], "close", []);
    84 
    85     return @Promise.prototype.@then.@call(sinkAbortPromise, function() { });
     83    return @promiseInvokeOrFallbackOrNoop(this.@underlyingSink, "abort", [reason], "close", []).@then(function() { });
    8684}
    8785
  • trunk/Source/WebCore/Modules/streams/WritableStreamInternals.js

    r192878 r194035  
    7878
    7979    if (!stream.@started)
    80         @Promise.prototype.@then.@call(stream.@startedPromise, function() { @writableStreamAdvanceQueue(stream); });
     80        stream.@startedPromise.@then(function() { @writableStreamAdvanceQueue(stream); });
    8181    else
    8282        @writableStreamAdvanceQueue(stream);
     
    100100
    101101    stream.@writing = true;
    102     @Promise.prototype.@then.@call(@promiseInvokeOrNoop(stream.@underlyingSink, "write", [writeRecord.chunk]),
     102    @promiseInvokeOrNoop(stream.@underlyingSink, "write", [writeRecord.chunk]).@then(
    103103        function() {
    104104            if (stream.@state === @streamErrored)
     
    121121
    122122    @assert(stream.@state === @streamClosing);
    123     @Promise.prototype.@then.@call(@promiseInvokeOrNoop(stream.@underlyingSink, "close"),
     123    @promiseInvokeOrNoop(stream.@underlyingSink, "close").@then(
    124124        function() {
    125125            if (stream.@state === @streamErrored)
Note: See TracChangeset for help on using the changeset viewer.