Skip to content

Commit

Permalink
Spec the last() operator (#133)
Browse files Browse the repository at this point in the history
* Spec the `last()` operator

* Account for observables that only emit null values

---------

Co-authored-by: Ben Lesh <[email protected]>
  • Loading branch information
domfarolino and benlesh authored May 3, 2024
1 parent 8895adc commit e6ee22b
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions spec.bs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ interface Observable {
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> 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 @@ -1289,6 +1290,51 @@ 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>last(|options|)</code></dfn> method steps are:

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

1. If |options|'s {{SubscribeOptions/signal}} is not null:

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

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

1. Return |p|.

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

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

1. Let |lastValue| be an {{any}}-or-null, initially null.

1. Let |hasLastValue| be a [=boolean=], initially false.

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

: [=internal observer/next steps=]
:: 1. Set |hasLastValue| to true.

1. Set |lastValue| to the passed in <var ignore>value</var>.

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

: [=internal observer/complete steps=]
:: 1. If |hasLastValue| is true, [=resolve=] |p| with |lastValue|.

1. Otherwise, [=reject=] |p| with a [=new=] {{RangeError}}.

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

1. Return |p|.
</div>

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

Expand Down

0 comments on commit e6ee22b

Please sign in to comment.