<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>265096</bug_id>
          
          <creation_ts>2023-11-18 16:49:27 -0800</creation_ts>
          <short_desc>Provide ability to convert one type of NativePromise into another</short_desc>
          <delta_ts>2023-11-19 21:29:01 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Web Template Framework</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>DUPLICATE</resolution>
          <dup_id>264028</dup_id>
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=264028</see_also>
    
    <see_also>https://bugs.webkit.org/show_bug.cgi?id=265102</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Jean-Yves Avenard [:jya]">jean-yves.avenard</reporter>
          <assigned_to name="Jean-Yves Avenard [:jya]">jean-yves.avenard</assigned_to>
          <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1993711</commentid>
    <comment_count>0</comment_count>
    <who name="Jean-Yves Avenard [:jya]">jean-yves.avenard</who>
    <bug_when>2023-11-18 16:49:27 -0800</bug_when>
    <thetext>The problem was mentioned in https://github.com/WebKit/WebKit/pull/20652#discussion_r1397622606

Where we did the following:
```
Ref&lt;MediaPromise&gt; SourceBufferPrivate::removeCodedFrames(const MediaTime&amp; start, const MediaTime&amp; end, const MediaTime&amp; currentTime)
{
    MediaPromise::Producer producer;
    Ref promise = producer.promise();

    m_currentSourceBufferOperation = m_currentSourceBufferOperation-&gt;whenSettled(RunLoop::current(), [weakThis = WeakPtr { *this }, this, start, end, currentTime](auto&amp;&amp; result) mutable {
        if (!weakThis || !result)
            return MediaPromise::createAndReject(!result ? result.error() : PlatformMediaError::BufferRemoved);
        return removeCodedFramesInternal(start, end, currentTime);
    })-&gt;whenSettled(RunLoop::current(), [producer = WTFMove(producer)](auto&amp;&amp; result) mutable {
        producer.settle(result);
        return MediaPromise::createAndSettle(WTFMove(result));
    });
    return promise;
}
```

 m_currentSourceBufferOperation is a MediaPromise which is exclusive; you can only perform a single then/whenSettled on it. Exclusive promise are useful as it allows for move semantics and avoid needless copies of the resolved/rejected value.

So in the code above, we can&apos;t return m_currentSourceBufferOperation as it would mean you run then/whenSettled on it twice, which would assert.

One solution above would be to
1- make MediaPromise (NativePromise&lt;void, PlatformMediaError&gt;) a non-exclusive promise or
2- make m_currentSourceBufferOperation be a NonExclusive promise and allows to convert a non exclusive promise into an exclusive one.

Doing 1- would require changing all the MediaPromise use points to no longer expect a r-valued result (most are using `(auto&amp;&amp; result)` as their prototype
2- allows for less disruptive code and allow for rather more elegant code. It would also be a bit more efficient as you no longer require to dispatch a task

So the above could be written:
```
    m_currentSourceBufferOperation = m_currentSourceBufferOperation-&gt;whenSettled(RunLoop::current(), [weakThis = WeakPtr { *this }, this, start, end, currentTime](auto&amp;&amp; result) mutable {
        if (!weakThis || !result)
            return OperationPromise::createAndReject(!result ? result.error() : PlatformMediaError::BufferRemoved);
        return static_cast&lt;Ref&lt;OperationPromise&gt;&gt;(removeCodedFramesInternal(start, end, currentTime).get());
    });
    return m_currentSourceBufferOperation.get();
```
where m_currentSourceBufferOperation.get is now a non-exclusive promise.

Adopting solution 2, also gets us close to achieving bug 264028</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1993712</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2023-11-18 16:49:41 -0800</bug_when>
    <thetext>&lt;rdar://problem/118610126&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1993713</commentid>
    <comment_count>2</comment_count>
    <who name="Jean-Yves Avenard [:jya]">jean-yves.avenard</who>
    <bug_when>2023-11-18 17:06:53 -0800</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/20713</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1993739</commentid>
    <comment_count>3</comment_count>
    <who name="Jean-Yves Avenard [:jya]">jean-yves.avenard</who>
    <bug_when>2023-11-19 03:28:02 -0800</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/20721</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1993815</commentid>
    <comment_count>4</comment_count>
    <who name="Jean-Yves Avenard [:jya]">jean-yves.avenard</who>
    <bug_when>2023-11-19 21:29:01 -0800</bug_when>
    <thetext>Combined changes with bug 264028

*** This bug has been marked as a duplicate of bug 264028 ***</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>