Since switchMap
may cut off (unsubscribe) an inner Observable if a new item comes in, we expect the following:
const items = from([1, 2])
const seen = []
const derived = items.pipe(
switchMap(item =>
concat(
of(`now: ${item}`),
of(`end: ${item}`).pipe(delay(10))
)
)
)
derived.subscribe(next => seen.push(next))
to result in seen
having:
['now: 1', 'now: 2', 'end: 2']
^ unsubscribed to 1 here, as planned!
The question is, is there a way to get notified that item 1 was canceled? The reason being, if cancellations are happening too frequently I'd like to know.
I've tried ways of modifying the inner Observables, but basically anything I try to do 'at the end' of them simply doesn't happen when unsubscribed. The Observer spec has next
, completed
, and error
, but nothing for "ended early", so I'm not sure what to try.
I'd like to be able to pass an additional function to switchMap, which will be invoked by the operator when an inner observable is canceled, and provided the argument.
const derived = items.pipe(
switchMap(item => concat(
of(`now: ${item}`),
of(`end: ${item}`).pipe(delay(10))
),
(item) => console.error(`${item} was cut off.`))
)