File: observable-catch.any.js

package info (click to toggle)
firefox 144.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,637,504 kB
  • sloc: cpp: 7,576,692; javascript: 6,430,831; ansic: 3,748,119; python: 1,398,978; xml: 628,810; asm: 438,679; java: 186,194; sh: 63,212; makefile: 19,159; objc: 13,086; perl: 12,986; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (255 lines) | stat: -rw-r--r-- 8,046 bytes parent folder | download | duplicates (13)
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
test(() => {
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.complete();
  });

  const caughtObservable = source.catch(() => {
    assert_unreached("catch() is not called");
  });

  const results = [];

  caughtObservable.subscribe({
    next: value => results.push(value),
    complete: () => results.push('complete')
  });

  assert_array_equals(results, [1, 2, 3, 'complete']);
}, "catch(): Returns an Observable that is a pass-through for next()/complete()");

test(() => {
  let sourceError = new Error("from the source");
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.error(sourceError);
  });

  const caughtObservable = source.catch(error => {
    assert_equals(error, sourceError);
    return new Observable(subscriber => {
      subscriber.next(3);
      subscriber.complete();
    });
  });

  const results = [];

  caughtObservable.subscribe({
    next: value => results.push(value),
    complete: () => results.push("complete"),
  });

  assert_array_equals(results, [1, 2, 3, 'complete']);
}, "catch(): Handle errors from source and flatten to a new Observable");

test(() => {
  const sourceError = new Error("from the source");
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.error(sourceError);
  });

  const catchCallbackError = new Error("from the catch callback");
  const caughtObservable = source.catch(error => {
    assert_equals(error, sourceError);
    throw catchCallbackError;
  });

  const results = [];

  caughtObservable.subscribe({
    next: value => results.push(value),
    error: error => {
      results.push(error);
    },
    complete: () => results.push('complete'),
  });

  assert_array_equals(results, [1, 2, catchCallbackError]);
}, "catch(): Errors thrown in the catch() callback are sent to the consumer's error handler");

test(() => {
  // A common use case is logging and keeping the stream alive.
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.complete();
  });

  const flatteningError = new Error("from the flattening operation");
  function errorsOnTwo(value) {
    return new Observable(subscriber => {
      if (value === 2) {
        subscriber.error(flatteningError);
      } else {
        subscriber.next(value);
        subscriber.complete();
      }
    });
  }

  const results = [];

  source.flatMap(value => errorsOnTwo(value)
    .catch(error => {
      results.push(error);
      // This empty array converts to an Observable which automatically
      // completes.
      return [];
    })
  ).subscribe({
    next: value => results.push(value),
    complete: () => results.push("complete")
  });

  assert_array_equals(results, [1, flatteningError, 3, "complete"]);
}, "catch(): CatchHandler can return an empty iterable");

promise_test(async () => {
  const sourceError = new Error("from the source");
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.error(sourceError);
  });

  const caughtObservable = source.catch(error => {
    assert_equals(error, sourceError);
    return Promise.resolve(error.message);
  });

  const results = await caughtObservable.toArray();

  assert_array_equals(results, [1, 2, "from the source"]);
}, "catch(): CatchHandler can return a Promise");

promise_test(async () => {
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.error(new Error('from the source'));
  });

  const caughtObservable = source.catch(async function* (error) {
    assert_true(error instanceof Error);
    assert_equals(error.message, 'from the source');
    yield 3;
  });

  const results = await caughtObservable.toArray();

  assert_array_equals(results, [1, 2, 3], 'catch(): should handle returning an observable');
}, 'catch(): should handle returning an async iterable');

test(() => {
  const sourceError = new Error("from the source");
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.error(sourceError);
  });

  const caughtObservable = source.catch(error => {
    assert_equals(error, sourceError);
    // Primitive values like this are not convertible to an Observable, via the
    // `from()` semantics.
    return 3;
  });

  const results = [];

  caughtObservable.subscribe({
    next: value => results.push(value),
    error: error => {
      assert_true(error instanceof TypeError);
      results.push("TypeError");
    },
    complete: () => results.push("complete"),
  });

  assert_array_equals(results, [1, 2, "TypeError"]);
}, "catch(): CatchHandler emits an error if the value returned is not " +
   "convertible to an Observable");

test(() => {
  const source = new Observable(subscriber => {
    susbcriber.error(new Error("from the source"));
  });

  const results = [];

  const innerSubscriptionError = new Error("CatchHandler subscription error");
  const catchObservable = source.catch(() => {
    results.push('CatchHandler invoked');
    return new Observable(subscriber => {
      throw innerSubscriptionError;
    });
  });

  catchObservable.subscribe({
    error: e => {
      results.push(e);
    }
  });

  assert_array_equals(results, ['CatchHandler invoked', innerSubscriptionError]);
}, "catch(): CatchHandler returns an Observable that throws immediately on " +
   "subscription");

// This test asserts that the relationship between (a) the AbortSignal passed
// into `subscribe()` and (b) the AbortSignal associated with the Observable
// returned from `catch()`'s CatchHandler is not a "dependent" relationship.
// This is important because Observables have moved away from the "dependent
// abort signal" infrastructure in https://github.com/WICG/observable/pull/154,
// and this test asserts so.
//
// Here are all of the associated Observables and signals in this test:
// 1. Raw outer signal passed into `subscribe()`
// 2. catchObservable's inner Subscriber's signal
//    a. Per the above PR, and Subscriber's initialization logic [1], this
//       signal is set to abort in response to (1)'s abort algorithms. This
//       means its "abort" event gets fired before (1)'s.
// 3. Inner CatchHandler-returned Observable's Subscriber's signal
//    a. Also per [1], this is set to abort in response to (2)'s abort
//       algorithms, since we subscribe to this "inner Observable" with (2)'s
//       signal as the `SubscribeOptions#signal`.
//
// (1), (2), and (3) above all form an abort chain:
// (1) --> (2) --> (3)
//
// …such that when (1) aborts, its abort algorithms immediately abort (2),
// whose abort algorithms immediately abort (3). Finally on the way back up the
// chain, (3)'s `abort` event is fired, (2)'s `abort` event is fired, and then
// (1)'s `abort` event is fired. This ordering of abort events is what this test
// ensures.
//
// [1]: https://wicg.github.io/observable/#ref-for-abortsignal-add
test(() => {
  const results = [];
  const source = new Observable(subscriber =>
      susbcriber.error(new Error("from the source")));

  const catchObservable = source.catch(() => {
    return new Observable(subscriber => {
      subscriber.addTeardown(() => results.push('inner teardown'));
      subscriber.signal.addEventListener('abort',
          e => results.push('inner signal abort'));

      // No values or completion. We'll just wait for the subscriber to abort
      // its subscription.
    });
  });

  const ac = new AbortController();
  ac.signal.addEventListener('abort', e => results.push('outer signal abort'));
  catchObservable.subscribe({}, {signal: ac.signal});
  ac.abort();

  assert_array_equals(results, ['inner signal abort', 'inner teardown', 'outer signal abort']);
}, "catch(): Abort order between outer AbortSignal and inner CatchHandler subscriber's AbortSignal");