From e6ee22b49c8e35f82733e188bae69ae7e7efb563 Mon Sep 17 00:00:00 2001 From: Dominic Farolino Date: Fri, 3 May 2024 16:23:22 -0400 Subject: [PATCH] Spec the `last()` operator (#133) * Spec the `last()` operator * Account for observables that only emit null values --------- Co-authored-by: Ben Lesh --- spec.bs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/spec.bs b/spec.bs index 51ead03..216fc1a 100644 --- a/spec.bs +++ b/spec.bs @@ -374,6 +374,7 @@ interface Observable { Promise forEach(Visitor callback, optional SubscribeOptions options = {}); Promise every(Predicate predicate, optional SubscribeOptions options = {}); // Maybe? Promise first(optional SubscribeOptions options = {}); + Promise last(optional SubscribeOptions options = {}); Promise find(Predicate predicate, optional SubscribeOptions options = {}); Promise some(Predicate predicate, optional SubscribeOptions options = {}); Promise reduce(Reducer reducer, optional any initialValue, optional SubscribeOptions options = {}); @@ -1289,6 +1290,51 @@ For now, see [https://github.com/wicg/observable#operators](https://github.com/w 1. Return |p|. +
+ The last(|options|) 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 value. + + : [=internal observer/error steps=] + :: [=Reject=] |p| with the passed in error. + + : [=internal observer/complete steps=] + :: 1. If |hasLastValue| is true, [=resolve=] |p| with |lastValue|. + + 1. Otherwise, [=reject=] |p| with a [=new=] {{RangeError}}. + + 1. Subscribe to [=this=] given |observer| + and |options|. + + 1. Return |p|. +
+
The find(|predicate|, |options|) method steps are: