-
Notifications
You must be signed in to change notification settings - Fork 16
/
spec.bs
1848 lines (1306 loc) · 76.2 KB
/
spec.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class="metadata">
Title: Observable
Shortname: observable
Repository: WICG/observable
Inline Github Issues: true
Group: WICG
Status: CG-DRAFT
Level: 1
URL: https://wicg.github.io/observable/
Boilerplate: omit conformance, omit feedback-header
Editor: Dominic Farolino, Google https://www.google.com/, [email protected], https://domfarolino.com
Abstract: The Observable API provides a composable, ergonomic way of handling an asynchronous stream of events
!Participate: <a href="https://github.com/WICG/observable">GitHub WICG/observable</a> (<a href="https://github.com/WICG/observable/issues/new">new issue</a>, <a href="https://github.com/WICG/observable/issues?state=open">open issues</a>)
!Commits: <a href="https://github.com/WICG/observable/commits/master/spec.bs">GitHub spec.bs commits</a>
Complain About: accidental-2119 yes, missing-example-ids yes
Indent: 2
Default Biblio Status: current
Markup Shorthands: markdown yes
Assume Explicit For: yes
WPT Display: open
</pre>
<pre class="link-defaults">
</pre>
<pre class="anchors">
urlPrefix: https://tc39.es/ecma262/#; spec: ECMASCRIPT
type: dfn
text: current realm
urlPrefix: https://dom.spec.whatwg.org; spec: DOM
type: dfn
for: event listener
text: type; url: event-listener-type
text: capture; url: event-listener-capture
text: passive; url: event-listener-passive
text: once; url: event-listener-once
text: signal; url: event-listener-signal
for: AbortSignal
text: dependent signals; url: abortsignal-dependent-signals
text: signal abort; url:abortsignal-signal-abort
text: abort reason; url:abortsignal-abort-reason
</pre>
<style>
/* Put nice boxes around each algorithm. */
[data-algorithm]:not(.heading) {
padding: .5em;
border: thin solid #ddd; border-radius: .5em;
margin: .5em calc(-0.5em - 1px);
}
[data-algorithm]:not(.heading) > :first-child {
margin-top: 0;
}
[data-algorithm]:not(.heading) > :last-child {
margin-bottom: 0;
}
[data-algorithm] [data-algorithm] {
margin: 1em 0;
}
.selected-text-file-an-issue {
position: fixed;
bottom: 0;
right: 0;
background: rgba(255, 255, 255, 0.8);
font-size: smaller;
padding: 4px 10px;
z-index: 4;
}
dfn var {
font-style: italic;
}
table {
margin: 1em 0;
}
/* WHATWG-style <hr>s, instead of WICG-style. Specific selector is necessary to override WICG styles. */
:not(.head) > :not(.head) + hr {
display: block;
background: none;
border: none;
padding: 0;
margin: 3em 0;
height: auto;
}
:not(.head) > :not(.head) + hr::before {
content: none;
}
/* WHATWG-style element definition class */
.element {
background: #EEFFEE;
}
dt {
margin-top: 12px;
}
dl, dd {
padding-left: .5em;
}
/* domintro from https://resources.whatwg.org/standard.css */
.domintro {
position: relative;
color: green;
background: #DDFFDD;
margin: 2.5em 0 2em 0;
padding: 1.5em 1em 0.5em 2em;
}
.domintro dt, .domintro dt * {
color: black;
font-size: inherit;
}
.domintro dd {
margin: 0.5em 0 1em 2em; padding: 0;
}
.domintro dd p {
margin: 0.5em 0;
}
.domintro::before {
content: 'For web developers (non-normative)';
background: green;
color: white;
padding: 0.15em 0.25em;
font-style: normal;
position: absolute;
top: -0.8em;
left: -0.8em;
}
/* .XXX from https://resources.whatwg.org/standard.css */
.XXX {
color: #D50606;
background: white;
border: solid #D50606;
}
</style>
<script src="https://resources.whatwg.org/file-issue.js" async></script>
<h2 id=introduction>Introduction</h2>
*This section is non-normative.*
<h2 id=core-infrastructure>Core infrastructure</h2>
<h3 id=subscriber-api>The {{Subscriber}} interface</h3>
<xmp class=idl>
[Exposed=*]
interface Subscriber {
undefined next(any value);
undefined error(any error);
undefined complete();
undefined addTeardown(VoidFunction teardown);
// True after the Subscriber is created, up until either
// complete()/error() are invoked, or the subscriber unsubscribes. Inside
// complete()/error(), this attribute is true.
readonly attribute boolean active;
readonly attribute AbortSignal signal;
};
</xmp>
Each {{Subscriber}} has a <dfn for=Subscriber>next algorithm</dfn>, which is a [=internal
observer/next steps=].
Each {{Subscriber}} has a <dfn for=Subscriber>error algorithm</dfn>, which is an [=internal
observer/error steps=].
Each {{Subscriber}} has a <dfn for=Subscriber>complete algorithm</dfn>, which is a [=internal
observer/complete steps=].
Each {{Subscriber}} has a <dfn for=Subscriber>teardown callbacks</dfn>, which is a [=list=] of
{{VoidFunction}}s, initially empty.
Each {{Subscriber}} has a <dfn for=Subscriber>subscription controller</dfn>, which is an
{{AbortController}}.
Each {{Subscriber}} has a <dfn for=Subscriber>active</dfn> boolean, initially true.
Note: This is a bookkeeping variable to ensure that a {{Subscriber}} never calls any of the
callbacks it owns after it has been [=close a subscription|closed=].
The <dfn attribute for=Subscriber><code>active</code></dfn> getter steps are to return [=this=]'s
[=Subscriber/active=] boolean.
The <dfn attribute for=Subscriber><code>signal</code></dfn> getter steps are to return [=this=]'s
[=Subscriber/subscription controller=]'s [=AbortController/signal=].
<div algorithm>
The <dfn for=Subscriber method><code>next(|value|)</code></dfn> method steps are:
1. If [=this=]'s [=Subscriber/active=] is false, then return.
1. If [=this=]'s [=relevant global object=] is a {{Window}} object, and its [=associated
Document=] is not [=Document/fully active=], then return.
1. Run [=this=]'s [=Subscriber/next algorithm=] given |value|.
[=Assert=]: No <a spec=webidl lt="an exception was thrown">exception was thrown</a>.
<div class=note>
<p>Note: No exception can be thrown here because in the case where [=Subscriber/next
algorithm=] is just a wrapper around a script-provided callback, the <a
href=#process-observer>process observer</a> steps take care to wrap these callbacks in
logic that, when invoking them, catches any exceptions, and reports them to the global.</p>
<p>When the [=Subscriber/next algorithm=] is a spec algorithm, those steps take care to not
throw any exceptions outside of itself, to appease this assert.</p>
</div>
</div>
<div algorithm>
The <dfn for=Subscriber method><code>error(|error|)</code></dfn> method steps are:
1. If [=this=]'s [=Subscriber/active=] is false, [=report the exception=] |error|, then return.
1. If [=this=]'s [=relevant global object=] is a {{Window}} object, and its [=associated
Document=] is not [=Document/fully active=], then return.
1. [=close a subscription|Close=] [=this=].
1. Run [=this=]'s [=Subscriber/error algorithm=] given |error|.
[=Assert=]: No <a spec=webidl lt="an exception was thrown">exception was thrown</a>.
Note: See the documentation in {{Subscriber/next()}} for details on why this is true.
</div>
<div algorithm>
The <dfn for=Subscriber method><code>complete()</code></dfn> method steps are:
1. If [=this=]'s [=Subscriber/active=] is false, then return.
1. If [=this=]'s [=relevant global object=] is a {{Window}} object, and its [=associated
Document=] is not [=Document/fully active=], then return.
1. [=close a subscription|Close=] [=this=].
1. Run [=this=]'s [=Subscriber/complete algorithm=].
[=Assert=]: No <a spec=webidl lt="an exception was thrown">exception was thrown</a>.
Note: See the documentation in {{Subscriber/next()}} for details on why this is true.
</div>
<div algorithm>
The <dfn for=Subscriber method><code>addTeardown(|teardown|)</code></dfn> method steps are:
1. If [=this=]'s [=relevant global object=] is a {{Window}} object, and its [=associated
Document=] is not [=Document/fully active=], then return.
1. If [=this=]'s [=Subscriber/active=] is true, then [=list/append=] |teardown| to [=this=]'s
[=Subscriber/teardown callbacks=] list.
1. Otherwise, [=invoke=] |teardown|.
If <a spec=webidl lt="an exception was thrown">an exception |E| was thrown</a>, then [=report
the exception=] |E|.
</div>
<div algorithm>
To <dfn>close a subscription</dfn> given a {{Subscriber}} |subscriber|, and
an optional {{any}} |reason|, run these steps:
1. If |subscriber|'s [=Subscriber/active=] is false, then return.
<div class=note>
<p>This guards against re-entrant invocation, which can happen in the "producer-initiated"
unsubscription case. Consider the following example:</p>
<div class=example id=re-entrant-close>
<pre highlight=js>
const outerController = new AbortController();
const observable = new Observable(subscriber => {
subscriber.addTeardown(() => {
// 2.) This teardown executes inside the "Close" algorithm, while it's
// running. Aborting the downstream signal run its abort algorithms,
// one of which is the currently-running "Close" algorithm.
outerController.abort();
});
// 1.) This immediately invokes the "Close" algorithm, which
// sets subscriber.active to false.
subscriber.complete();
});
observable.subscribe({}, {signal: outerController.signal});
</pre>
</div>
</div>
1. Set |subscriber|'s [=Subscriber/active=] boolean to false.
1. [=AbortSignal/Signal abort=] |subscriber|'s [=Subscriber/subscription controller=]
with |reason|, if it is given.
1. [=list/For each=] |teardown| of |subscriber|'s [=Subscriber/teardown callbacks=] sorted in
reverse insertion order:
1. If |subscriber|'s [=relevant global object=] is a {{Window}} object, and its [=associated
Document=] is not [=Document/fully active=], then abort these steps.
Note: This step runs repeatedly because each |teardown| could result in the above
{{Document}} becoming inactive.
1. [=Invoke=] |teardown|.
If <a spec=webidl lt="an exception was thrown">an exception |E| was thrown</a>, then [=report
the exception=] |E|.
</div>
<h3 id=observable-api>The {{Observable}} interface</h3>
<xmp class=idl>
// SubscribeCallback is where the Observable "creator's" code lives. It's
// called when subscribe() is called, to set up a new subscription.
callback SubscribeCallback = undefined (Subscriber subscriber);
callback ObservableSubscriptionCallback = undefined (any value);
dictionary SubscriptionObserver {
ObservableSubscriptionCallback next;
ObservableSubscriptionCallback error;
VoidFunction complete;
};
callback ObservableInspectorAbortHandler = undefined (any value);
dictionary ObservableInspector {
ObservableSubscriptionCallback next;
ObservableSubscriptionCallback error;
VoidFunction complete;
VoidFunction subscribe;
ObservableInspectorAbortHandler abort;
};
typedef (ObservableSubscriptionCallback or SubscriptionObserver) ObserverUnion;
typedef (ObservableSubscriptionCallback or ObservableInspector) ObservableInspectorUnion;
dictionary SubscribeOptions {
AbortSignal signal;
};
callback Predicate = boolean (any value, unsigned long long index);
callback Reducer = any (any accumulator, any currentValue, unsigned long long index);
callback Mapper = any (any value, unsigned long long index);
// Differs from Mapper only in return type, since this callback is exclusively
// used to visit each element in a sequence, not transform it.
callback Visitor = undefined (any value, unsigned long long index);
// This callback returns an `any` that must convert into an `Observable`, via
// the `Observable` conversion semantics.
callback CatchCallback = any (any value);
[Exposed=*]
interface Observable {
constructor(SubscribeCallback callback);
undefined subscribe(optional ObserverUnion observer = {}, optional SubscribeOptions options = {});
// Constructs a native Observable from value if it's any of the following:
// - Observable
// - AsyncIterable
// - Iterable
// - Promise
static Observable from(any value);
// Observable-returning operators. See "Operators" section in the spec.
//
// takeUntil() can consume promises, iterables, async iterables, and other
// observables.
Observable takeUntil(any notifier);
Observable map(Mapper mapper);
Observable filter(Predicate predicate);
Observable take(unsigned long long amount);
Observable drop(unsigned long long amount);
Observable flatMap(Mapper mapper);
Observable switchMap(Mapper mapper);
Observable inspect(optional ObservableInspectorUnion inspectorUnion = {});
Observable catch(CatchCallback callback);
Observable finally(VoidFunction callback);
// Promise-returning operators.
Promise<sequence<any>> toArray(optional SubscribeOptions options = {});
Promise<undefined> forEach(Visitor callback, optional SubscribeOptions options = {});
Promise<boolean> every(Predicate predicate, 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 = {});
};
</xmp>
Each {{Observable}} has a <dfn for=Observable>subscribe callback</dfn>, which is a
{{SubscribeCallback}} or a set of steps that take in a {{Subscriber}}.
Note: The "union" of these types is to support both {{Observable}}s created by JavaScript (that are
always constructed with a {{SubscribeCallback}}), and natively-constructed {{Observable}} objects
(whose [=Observable/subscribe callback=] could be an arbitrary set of native steps, not a JavaScript
callback). The return value of {{EventTarget/when()}} is an example of the latter.
<div algorithm>
The <dfn for=Observable constructor lt="Observable(callback)"><code>new
Observable(|callback|)</code></dfn> constructor steps are:
1. Set [=this=]'s [=Observable/subscribe callback=] to |callback|.
Note: This callback will get invoked later when {{Observable/subscribe()}} is called.
</div>
<div algorithm>
The <dfn for=Observable method><code>subscribe(|observer|, |options|)</code></dfn> method steps
are:
1. <a for=Observable lt="subscribe to an Observable">Subscribe</a> to [=this=] given |observer|
and |options|.
</div>
<h4 id=observable-supporting-concepts>Supporting concepts</h4>
<div algorithm>
The <dfn>default error algorithm</dfn> is an algorithm that takes an {{any}} |error|, and runs
these steps:
1. [=Report the exception=] |error|.
Note: We pull this default out separately so that every place in this specification that natively
<a for=Observable lt="subscribe to an Observable">subscribes</a> to an {{Observable}} (i.e.,
subscribes from spec prose, not going through the {{Observable/subscribe()}} method) doesn't have
to redundantly define these steps.
</div>
An <dfn>internal observer</dfn> is a [=struct=] with the following [=struct/items=]:
<dl dfn-for="internal observer">
: <dfn>next steps</dfn>
:: An algorithm that takes a single parameter of type {{any}}. Initially, these steps do nothing.
: <dfn>error steps</dfn>
:: An algorithm that takes a single parameter of type {{any}}. Initially, the [=default error
algorithm=].
: <dfn>complete steps</dfn>
:: An algorithm with no parameters. Initially, these steps do nothing.
</dl>
<div class=note>
<p>The [=internal observer=] [=struct=] is used to mirror the {{SubscriptionObserver/next}},
{{SubscriptionObserver/error}}, and {{SubscriptionObserver/complete}} [=callback functions=]. For
any {{Observable}} that is subscribed by JavaScript via the {{Observable/subscribe()}} method,
these algorithm "steps" will just be a wrapper around [=invoking=] the corresponding
{{SubscriptionObserver/next}}, {{SubscriptionObserver/error}}, and
{{SubscriptionObserver/complete}} [=callback functions=] provided by script.</p>
<p>But when internal spec prose (not user script) <a for=Observable lt="subscribe to an
Observable">subscribes</a> to an {{Observable}}, these "steps" are arbitrary spec algorithms that
are not provided via an {{ObserverUnion}} packed with Web IDL [=callback functions=]. See the
[[#promise-returning-operators]] that make use of this, for example.</p>
</div>
<div algorithm>
To <dfn for=Observable>subscribe to an {{Observable}}</dfn> given an
{{ObserverUnion}}-or-[=internal observer=] |observer|, and a {{SubscribeOptions}} |options|, run
these steps:
Note: We split this algorithm out from the Web IDL {{Observable/subscribe()}} method, so that
spec prose can <a for=Observable lt="subscribe to an Observable">subscribe</a> to an
{{Observable}} without going through the Web IDL bindings. See <a
href=https://github.com/w3c/IntersectionObserver/issues/464>w3c/IntersectionObserver#464</a> for
similar context, where "internal" prose <span class=allow-2119>must</span> not go through Web IDL
bindings on objects whose properties could be mutated by JavaScript. See
[[#promise-returning-operators]] for usage of this.
1. If [=this=]'s [=relevant global object=] is a {{Window}} object, and its [=associated
Document=] is not [=Document/fully active=], then return.
1. Let |internal observer| be a new [=internal observer=].
1. Process |observer| as follows:
<ol id=process-observer>
<li>
<dl class="switch">
<dt>If |observer| is an {{ObservableSubscriptionCallback}}</dt>
<dd>Set |internal observer|'s [=internal observer/next steps=] to these steps that take
an {{any}} |value|:
1. [=Invoke=] |observer| with |value|.
If <a spec=webidl lt="an exception was thrown">an exception |E| was thrown</a>,
then [=report the exception=] |E|.
</dd>
<dt>If |observer| is a {{SubscriptionObserver}}</dt>
<dd>
1. If |observer|'s {{SubscriptionObserver/next}} [=map/exists=], then set
|internal observer|'s [=internal observer/next steps=] to these steps that take an
{{any}} |value|:
1. [=Invoke=] |observer|'s {{SubscriptionObserver/next}} with |value|.
If <a spec=webidl lt="an exception was thrown">an exception |E| was thrown</a>,
then [=report the exception=] |E|.
1. If |observer|'s {{SubscriptionObserver/error}} [=map/exists=], then set
|internal observer|'s [=internal observer/error steps=] to these steps that take
an {{any}} |error|:
1. [=Invoke=] |observer|'s {{SubscriptionObserver/error}} with |error|.
If <a spec=webidl lt="an exception was thrown">an exception |E| was thrown</a>,
then [=report the exception=] |E|.
1. If |observer|'s {{SubscriptionObserver/complete}} [=map/exists=], then set
|internal observer|'s [=internal observer/complete steps=] to these steps:
1. [=Invoke=] |observer|'s {{SubscriptionObserver/complete}}.
If <a spec=webidl lt="an exception was thrown">an exception |E| was thrown</a>,
then [=report the exception=] |E|.
</dd>
<dt>If |observer| is an [=internal observer=]</dt>
<dd>Set |internal observer| to |observer|.</dd>
</dl>
</li>
</ol>
1. [=Assert=]: |internal observer|'s [=internal observer/error steps=] is either the [=default
error algorithm=], or an algorithm that [=invokes=] the provided {{SubscriptionObserver/error}}
[=callback function=].
1. Let |subscriber| be a [=new=] {{Subscriber}}, initialized as:
: [=Subscriber/next algorithm=]
:: |internal observer|'s [=internal observer/next steps=]
: [=Subscriber/error algorithm=]
:: |internal observer|'s [=internal observer/error steps=]
: [=Subscriber/complete algorithm=]
:: |internal observer|'s [=internal observer/complete steps=]
1. If |options|'s {{SubscribeOptions/signal}} [=map/exists=], then:
1. If |options|'s {{SubscribeOptions/signal}} is [=AbortSignal/aborted=], then [=close a
subscription|close=] |subscriber| given |options|'s {{SubscribeOptions/signal}}
[=AbortSignal/abort reason=].
1. Otherwise, [=AbortSignal/add|add the following abort algorithm=] to |options|'s
{{SubscribeOptions/signal}}:
1. [=close a subscription|Close=] |subscriber| with |options|'s
{{SubscribeOptions/signal}} [=AbortSignal/abort reason=].
1. If [=this=]'s [=Observable/subscribe callback=] is a {{SubscribeCallback}}, [=invoke=] it
with |subscriber|.
If <a spec=webidl lt="an exception was thrown">an exception |E| was thrown</a>, call
|subscriber|'s {{Subscriber/error()}} method with |E|.
1. Otherwise, run the steps given by [=this=]'s [=Observable/subscribe callback=], given
|subscriber|.
</div>
<wpt>
/dom/observable/tentative/observable-constructor.any.js
/dom/observable/tentative/observable-constructor.window.js
</wpt>
<h3 id=operators>Operators</h3>
For now, see [https://github.com/wicg/observable#operators](https://github.com/wicg/observable#operators).
<h4 id=observable-from>{{Observable/from()}}</h4>
<p class=XXX>Spec the exact semantics of {{Observable/from()}} conversion.</p>
<h4 id=observable-returning-operators>{{Observable}}-returning operators</h4>
<div algorithm>
The <dfn for=Observable method><code>takeUntil(|notifier|)</code></dfn> method steps are:
1. Let |sourceObservable| be [=this=].
1. Let |observable| be a [=new=] {{Observable}} whose [=Observable/subscribe callback=] is an
algorithm that takes a {{Subscriber}} |subscriber| and does the following:
<div class=note>
Note that this method involves <a for=Observable lt="subscribe to an
Observable">Subscribing</a> to two {{Observable}}s: (1) |notifier|, and (2)
|sourceObservable|. We "unsubscribe" from **both** of them in the following situations:
1. |notifier| starts emitting values (either "next" or "error"). In this case, we
unsubscribe from |notifier| since we got all we need from it, and no longer need it
to keep producing values. We also unsubscribe from |sourceObservable|, because it
no longer needs to produce values that get plumbed through this method's returned
|observable|, because we're manually ending the subscription to |observable|, since
|notifier| finally produced a value.
1. |sourceObservable| either {{Subscriber/error()}}s or {{Subscriber/complete()}}s
itself. In this case, we unsubscribe from |notifier| since we no longer need to
listen for values it emits in order to determine when |observable| can stop
mirroring values from |sourceObservable| (since |sourceObservable| ran to
completion by itself). Unsubscribing from |sourceObservable| isn't necessary, since
its subscription has been exhausted by itself.
</div>
1. Let |notifierObserver| be a new [=internal observer=], initialized as follows:
: [=internal observer/next steps=]
:: Run |subscriber|'s {{Subscriber/complete()}} method.
Note: This will "unsubscribe" from |sourceObservable|, if it has been subscribed to by
this point. This is because |sourceObservable| is subscribed to with the "outer"
|subscriber|'s [=Subscriber/subscription controller=]'s [=AbortController/signal=] as
an input signal, and that signal will get [=AbortSignal/signal abort|aborted=] when the
"outer" |subscriber|'s {{Subscriber/complete()}} is called above (and below).
: [=internal observer/error steps=]
:: Run |subscriber|'s {{Subscriber/complete()}} method.
Note: We do not specify [=internal observer/complete steps=], because if the |notifier|
{{Observable}} completes itself, we do not need to complete the |subscriber| associated
with the |observable| returned from this method. Rather, the |observable| will continue to
mirror |sourceObservable| uninterrupted.
1. Let |options| be a new {{SubscribeOptions}} whose {{SubscribeOptions/signal}} is
|subscriber|'s [=Subscriber/subscription controller=]'s [=AbortController/signal=].
1. <a for=Observable lt="subscribe to an Observable">Subscribe</a> to |notifier| given
|notifierObserver| and |options|.
1. If |subscriber|'s [=Subscriber/active=] is false, then return.
Note: This means that |sourceObservable|'s [=Observable/subscribe callback=] will not even
get invoked once, if |notifier| synchronously emits a value. If |notifier| only
"completes" synchronously though (without emitting a "next" or "error" value), then
|subscriber|'s [=Subscriber/active=] will still be true, and we proceed to subscribe to
|sourceObservable|, which |observable| will mirror uninterrupted.
1. Let |sourceObserver| be a new [=internal observer=], initialized as follows:
: [=internal observer/next steps=]
:: Run |subscriber|'s {{Subscriber/next()}} method, given the passed in <var
ignore>value</var>.
: [=internal observer/error steps=]
:: Run |subscriber|'s {{Subscriber/error()}} method, given the passed in <var
ignore>error</var>.
: [=internal observer/complete steps=]
:: Run |subscriber|'s {{Subscriber/complete()}} method.
Note: |sourceObserver| is mostly a pass-through, mirroring everything that
|sourceObservable| emits, with the exception of having the ability to unsubscribe from the
|notifier| {{Observable}} in the case where |sourceObservable| is exhausted before
|notifier| emits anything.
1. <a for=Observable lt="subscribe to an Observable">Subscribe</a> to |sourceObservable|
given |sourceObserver| and |options|.
1. Return |observable|.
<wpt>
/dom/observable/tentative/observable-takeUntil.any.js
/dom/observable/tentative/observable-takeUntil.window.js
</wpt>
</div>
<div algorithm>
The <dfn for=Observable method><code>map(|mapper|)</code></dfn> method steps are:
1. Let |sourceObservable| be [=this=].
1. Let |observable| be a [=new=] {{Observable}} whose [=Observable/subscribe callback=] is an
algorithm that takes a {{Subscriber}} |subscriber| and does the following:
1. Let |idx| be an {{unsigned long long}}, initially 0.
1. Let |sourceObserver| be a new [=internal observer=], initialized as follows:
: [=internal observer/next steps=]
:: 1. [=Invoke=] |mapper| with the passed in <var ignore>value</var>, and |idx|, and let
|mappedValue| be the returned value.
If <a spec=webidl lt="an exception was thrown">an exception |E| was thrown</a>,
then run |subscriber|'s {{Subscriber/error()}} method, given |E|, and abort these
steps.
1. Increment |idx|.
1. Run |subscriber|'s {{Subscriber/next()}} method, given |mappedValue|.
: [=internal observer/error steps=]
:: Run |subscriber|'s {{Subscriber/error()}} method, given the passed in <var
ignore>error</var>.
: [=internal observer/complete steps=]
:: Run |subscriber|'s {{Subscriber/complete()}} method.
1. Let |options| be a new {{SubscribeOptions}} whose {{SubscribeOptions/signal}} is
|subscriber|'s [=Subscriber/subscription controller=]'s [=AbortController/signal=].
1. <a for=Observable lt="subscribe to an Observable">Subscribe</a> to |sourceObservable|
given |sourceObserver| and |options|.
1. Return |observable|.
<wpt>
/dom/observable/tentative/observable-map.any.js
/dom/observable/tentative/observable-map.window.js
</wpt>
</div>
<div algorithm>
The <dfn for=Observable method><code>filter(|predicate|)</code></dfn> method steps are:
1. Let |sourceObservable| be [=this=].
1. Let |observable| be a [=new=] {{Observable}} whose [=Observable/subscribe callback=] is an
algorithm that takes a {{Subscriber}} |subscriber| and does the following:
1. Let |idx| be an {{unsigned long long}}, initially 0.
1. Let |sourceObserver| be a new [=internal observer=], initialized as follows:
: [=internal observer/next steps=]
:: 1. [=Invoke=] |predicate| with the passed in |value| and |idx|, and let |matches| be
the returned value.
If <a spec=webidl lt="an exception was thrown">an exception |E| was thrown</a>,
then run |subscriber|'s {{Subscriber/error()}} method, given |E|, and abort these
steps.
1. Set |idx| to |idx| + 1.
1. If |matches| is true, then run |subscriber|'s {{Subscriber/next()}} method, given
|value|.
: [=internal observer/error steps=]
:: Run |subscriber|'s {{Subscriber/error()}} method, given the passed in <var
ignore>error</var>.
: [=internal observer/complete steps=]
:: Run |subscriber|'s {{Subscriber/complete()}} method.
1. Let |options| be a new {{SubscribeOptions}} whose {{SubscribeOptions/signal}} is
|subscriber|'s [=Subscriber/subscription controller=]'s [=AbortController/signal=].
1. <a for=Observable lt="subscribe to an Observable">Subscribe</a> to |sourceObservable|
given |sourceObserver| and |options|.
1. Return |observable|.
<wpt>
/dom/observable/tentative/observable-filter.any.js
</wpt>
</div>
<div algorithm>
The <dfn for=Observable method><code>take(|amount|)</code></dfn> method steps are:
1. Let |sourceObservable| be [=this=].
1. Let |observable| be a [=new=] {{Observable}} whose [=Observable/subscribe callback=] is an
algorithm that takes a {{Subscriber}} |subscriber| and does the following:
1. Let |remaining| be |amount|.
1. If |remaining| is 0, then run |subscriber|'s {{Subscriber/complete()}} method and abort
these steps.
1. Let |sourceObserver| be a new [=internal observer=], initialized as follows:
: [=internal observer/next steps=]
:: 1. Run |subscriber|'s {{Subscriber/next()}} method with the passed in <var
ignore>value</var>.
1. Decrement |remaining|.
1. If |remaining| is 0, then run |subscriber|'s {{Subscriber/complete()}} method.
: [=internal observer/error steps=]
:: Run |subscriber|'s {{Subscriber/error()}} method, given the passed in <var
ignore>error</var>.
: [=internal observer/complete steps=]
:: Run |subscriber|'s {{Subscriber/complete()}} method.
1. Let |options| be a new {{SubscribeOptions}} whose {{SubscribeOptions/signal}} is
|subscriber|'s [=Subscriber/subscription controller=]'s [=AbortController/signal=].
1. <a for=Observable lt="subscribe to an Observable">Subscribe</a> to |sourceObservable|
given |sourceObserver| and |options|.
1. Return |observable|.
<wpt>
/dom/observable/tentative/observable-take.any.js
</wpt>
</div>
<div algorithm>
The <dfn for=Observable method><code>drop(|amount|)</code></dfn> method steps are:
1. Let |sourceObservable| be [=this=].
1. Let |observable| be a [=new=] {{Observable}} whose [=Observable/subscribe callback=] is an
algorithm that takes a {{Subscriber}} |subscriber| and does the following:
1. Let |remaining| be |amount|.
1. Let |sourceObserver| be a new [=internal observer=], initialized as follows:
: [=internal observer/next steps=]
:: 1. If |remaining| is > 0, then decrement |remaining| and abort these steps.
1. [=Assert=]: |remaining| is 0.
1. Run |subscriber|'s {{Subscriber/next()}} method with the passed in <var
ignore>value</var>.
: [=internal observer/error steps=]
:: Run |subscriber|'s {{Subscriber/error()}} method, given the passed in <var
ignore>error</var>.
: [=internal observer/complete steps=]
:: Run |subscriber|'s {{Subscriber/complete()}} method.
1. Let |options| be a new {{SubscribeOptions}} whose {{SubscribeOptions/signal}} is
|subscriber|'s [=Subscriber/subscription controller=]'s [=AbortController/signal=].
1. <a for=Observable lt="subscribe to an Observable">Subscribe</a> to |sourceObservable|
given |sourceObserver| and |options|.
1. Return |observable|.
<wpt>
/dom/observable/tentative/observable-drop.any.js
</wpt>
</div>
<div algorithm>
The <dfn for=Observable method><code>flatMap(|mapper|)</code></dfn> method steps are:
1. Let |sourceObservable| be [=this=].
1. Let |observable| be a [=new=] {{Observable}} whose [=Observable/subscribe callback=] is an
algorithm that takes a {{Subscriber}} |subscriber| and does the following:
1. Let |idx| be an {{unsigned long long}}, initially 0.
1. Let |outerSubscriptionHasCompleted| to a [=boolean=], initially false.
1. Let |queue| be a new [=list=] of {{any}} values, initially empty.
Note: This |queue| is used to store any {{Observable}}s emitted by |sourceObservable|,
while |observable| is currently subscribed to an {{Observable}} emitted earlier by
|sourceObservable| that has not yet been exhausted.
1. Let |activeInnerSubscription| be a [=boolean=], initially false.
1. Let |sourceObserver| be a new [=internal observer=], initialized as follows:
: [=internal observer/next steps=]
:: 1. If |activeInnerSubscription| is true, then:
1. [=list/Append=] |value| to |queue|.
Note: This |value| will eventually be processed once the {{Observable}} that is
currently subscribed-to (as indicated by |activeInnerSubscription|) is exhausted.
1. Otherwise:
1. Set |activeInnerSubscription| to true.
1. Run the [=flatmap process next value steps=] with |value|, |subscriber|,
|mapper|, and <b>references</b> to all of the following: |queue|,
|activeInnerSubscription|, |outerSubscriptionHasCompleted|, and |idx|.
<div class=note>
<p>Note: This [=flatmap process next value steps=] will subscribe to the
{{Observable}} derived from |value| (if one such can be derived) and keep
processing values from it until its subscription becomes inactive (either by
error or completion). If this "inner" {{Observable}} completes, then the
processing steps will recursively invoke themselves with the next {{any}} in
|queue|.</p>
<p>If no such value [=list/exists=], then the processing steps will terminate,
<b>unsetting</b> |activeInnerSubscription|, so that future values emitted from
|sourceObservable| are processed correctly.</p>
</div>
: [=internal observer/error steps=]
:: Run |subscriber|'s {{Subscriber/error()}} method, given the passed in <var
ignore>error</var>.
: [=internal observer/complete steps=]
:: 1. Set |outerSubscriptionHasCompleted| to true.
Note: If |activeInnerSubscription| is true, then the below step will *not* complete
|subscriber|. In that case, the [=flatmap process next value steps=] will be
responsible for completing |subscriber| when |queue| is [=list/empty=], after the
"inner" subscription becomes inactive.
1. If |activeInnerSubscription| is false and |queue| is [=list/empty=], run
|subscriber|'s {{Subscriber/complete()}} method.
1. Let |options| be a new {{SubscribeOptions}} whose {{SubscribeOptions/signal}} is
|subscriber|'s [=Subscriber/subscription controller=]'s [=AbortController/signal=].
1. <a for=Observable lt="subscribe to an Observable">Subscribe</a> to |sourceObservable|
given |sourceObserver| and |options|.
1. Return |observable|.
</div>
<div algorithm>
The <dfn>flatmap process next value steps</dfn>, given an {{any}} |value|, a {{Subscriber}}
|subscriber|, a {{Mapper}} |mapper|, and <b>references</b> to all of the following: a [=list=] of
{{any}} values |queue|, a [=boolean=] |activeInnerSubscription|, a [=boolean=]
|outerSubscriptionHasCompleted|, and an {{unsigned long long}} |idx|:
1. Let |mappedResult| be the result of [=invoking=] |mapper| with |value| and |idx|.
If <a spec=webidl lt="an exception was thrown">an exception |E| was thrown</a>, then run
|subscriber|'s {{Subscriber/error()}} method, given |E|, and abort these steps.
1. Set |idx| to |idx| + 1.
1. Let |innerObservable| be the result of calling {{Observable/from()}} with |mappedResult|.
If <a spec=webidl lt="an exception was thrown">an exception |E| was thrown</a>, then run
|subscriber|'s {{Subscriber/error()}} method, given |E|, and abort these steps.
Issue: We shouldn't invoke {{Observable/from()}} directly. Rather, we should
call some internal algorithm that passes-back the exceptions for us to handle
properly here, since we want to pipe them to |subscriber|.
1. Let |innerObserver| be a new [=internal observer=], initialized as follows:
: [=internal observer/next steps=]
:: Run |subscriber|'s {{Subscriber/next()}} method, given the passed in |value|.
: [=internal observer/error steps=]
:: Run |subscriber|'s {{Subscriber/error()}} method, given the passed in <var
ignore>error</var>.
: [=internal observer/complete steps=]
:: 1. If |queue| is not empty, then:
1. Let |nextValue| be the first item in |queue|; [=list/remove=] remove this item from
|queue|.
1. Run [=flatmap process next value steps=] given |nextValue|, |subscriber|, |mapper|,
and <b>references</b> to |queue| and |activeInnerSubscription|.
1. Otherwise:
1. Set |activeInnerSubscription| to false.
Note: Because |activeInnerSubscription| is a reference, this has the effect of
ensuring that all subsequent values emitted from the "outer" {{Observable}} (called
<var ignore>sourceObservable</var>.
1. If |outerSubscriptionHasCompleted| is true, run |subscriber|'s
{{Subscriber/complete()}} method.
Note: This means the "outer" {{Observable}} has already completed, but did not
proceed to complete |subscriber| yet because there was at least one more pending
"inner" {{Observable}} (i.e., |innerObservable|) that had already been queued and
had not yet completed. Until right now!
1. Let |innerOptions| be a new {{SubscribeOptions}} whose {{SubscribeOptions/signal}} is
|subscriber|'s [=Subscriber/subscription controller=]'s [=AbortController/signal=].
1. <a for=Observable lt="subscribe to an Observable">Subscribe</a> to |innerObservable| given
|innerObserver| and |innerOptions|.
</div>
<div algorithm>
The <dfn for=Observable method><code>switchMap(|mapper|)</code></dfn> method steps are:
1. Let |sourceObservable| be [=this=].
1. Let |observable| be a [=new=] {{Observable}} whose [=Observable/subscribe callback=] is an
algorithm that takes a {{Subscriber}} |subscriber| and does the following:
1. Let |idx| be an {{unsigned long long}}, initially 0.
1. Let |outerSubscriptionHasCompleted| be a [=boolean=], initially false.
1. Let |activeInnerAbortController| be an {{AbortController}}-or-null, initially null.
Note: This {{AbortController}} is assigned to a new {{AbortController}} only by this
algorithm's <a href=#switchmap-next-steps>next steps</a> (below), and only assigned to
null by the [=switchmap process next value steps=], when the "inner" {{Observable}} either
completes or errors. This variable is used as a marker for whether there is currently an