Skip to content

Commit

Permalink
Spec the first() operator (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
domfarolino authored May 5, 2024
1 parent 5ef367b commit 7a4c941
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion spec.bs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ interface Observable {
Promise<sequence<any>> toArray(optional SubscribeOptions options = {});
Promise<undefined> forEach(Visitor callback, optional SubscribeOptions options = {});
Promise<boolean> every(Predicate predicate, optional SubscribeOptions options = {});
// Maybe? Promise<any> first(optional SubscribeOptions options = {});
Promise<any> first(optional SubscribeOptions options = {});
Promise<any> last(optional SubscribeOptions options = {});
Promise<any> find(Predicate predicate, optional SubscribeOptions options = {});
Promise<boolean> some(Predicate predicate, optional SubscribeOptions options = {});
Promise<any> reduce(Reducer reducer, optional any initialValue, optional SubscribeOptions options = {});
Expand Down Expand Up @@ -1293,6 +1294,63 @@ For now, see [https://github.com/wicg/observable#operators](https://github.com/w
1. Return |p|.
</div>

<div algorithm>
The <dfn for=Observable method><code>first(|options|)</code></dfn> method steps are:

1. Let |p| [=a new promise=].

1. Let |controller| be a [=new=] {{AbortController}}.

1. Let |internal options| be a new {{SubscribeOptions}} whose {{SubscribeOptions/signal}} is the
result of [=creating a dependent abort signal=] from the list «|controller|'s
[=AbortController/signal=], |options|'s {{SubscribeOptions/signal}} if non-null», using
{{AbortSignal}}, and the [=current realm=].

1. If |internal options|'s {{SubscribeOptions/signal}} is [=AbortSignal/aborted=], then:

1. [=Reject=] |p| with |internal options|'s {{SubscribeOptions/signal}}'s
[=AbortSignal/abort reason=].

1. Return |p|.

1. [=AbortSignal/add|Add the following abort algorithm=] to |internal options|'s
{{SubscribeOptions/signal}}:

1. [=Reject=] |p| with |internal options|'s {{SubscribeOptions/signal}}'s [=AbortSignal/abort
reason=].

1. Let |internal observer| be a new [=internal observer=], initialized as follows:

: [=internal observer/next steps=]
:: 1. [=Resolve=] |p| with the passed in <var ignore>value</var>.

1. [=AbortController/Signal abort=] |controller|.

: [=internal observer/error steps=]
:: [=Reject=] |p| with the passed in <var ignore>error</var>.

: [=internal observer/complete steps=]
:: [=Resolve=] |p| with {{undefined}}.

Note: This is only reached when the source {{Observable}} completes *before* it emits a
single value; in this case, resolving with {{undefined}} is harmless but makes it
difficult to distinguish between the first value trule being {{undefined}} and premature
completion. See <a href=https://github.com/WICG/observable/issues/132>#132</a> for
discussion on this.

1. <a for=Observable lt="subscribe to an Observable">Subscribe</a> to [=this=] given |internal
observer| and |internal options|.

1. Return |p|.

</div>

<div algorithm>
The <dfn for=Observable method><code>last(|options|)</code></dfn> method steps are:

1. <span class=XXX>TODO: Spec this and use |options|.</span>
</div>

<div algorithm>
The <dfn for=Observable method><code>find(|predicate|, |options|)</code></dfn> method steps are:

Expand Down

0 comments on commit 7a4c941

Please sign in to comment.