Makes progress on https://github.com/WICG/observable/issues/64. Simply adds the catch operator to the README. This is one of the most widely used operators. It's particularly useful in preventing inner subscriptions from terminating outer subscriptions in cases like long-polling or events that kick off other event streams.
For example, let's say someone creates an observable from a web socket:
The above stream will terminate the socket under the following error conditions:
The socket closes "dirty", often due to losing network.
The socket experiences another error
The message data fails JSON.parse.
Ideally, in the first two cases, we can reconnect the socket. To do this, we can use catch:
// A utility for timeout
function timeout(ms) {
return new Observable(subscriber => {
const id = setTimeout(() => {
subscriber.next();
subscrber.complete();
}, ms);
subscriber.addTeardown(() => clearTimeout(id));
});
}
// This is now a socket that will retry if it fails.
const socketStream = socket('wss://someendpoint')
.catch(err => {
// log the error
console.error(err);
if (err.type === 'close' && !err.wasClean) {
if (!navigator.onLine) {
// Wait until we're online and reconnect
return window.on('online')
.take(1)
.flatMap(() => socketStream)
}
}
// Wait for 1 second and try again
return timeout(1000)
.flatMap(() => socketStream);
});
socketStream
.do(e => {
if (e.type === 'open') {
const socket = e.target;
socket.send('start');
}
})
.filter(e => e.type === 'message')
.map(e => {
// These sorts of errors are generally going to be
// resolved the traditional way.
try {
return JSON.parse(e.data)
} catch (err) {
console.error(err);
return { parseError: err }
}
})
.subscribe(console.log);
This seems to add back all of the IDL to the README file. Maybe this means to instead just add a couple lines to the README, and a few more to the spec?
Makes progress on https://github.com/WICG/observable/issues/64. Simply adds the
catch
operator to theREADME
. This is one of the most widely used operators. It's particularly useful in preventing inner subscriptions from terminating outer subscriptions in cases like long-polling or events that kick off other event streams.For example, let's say someone creates an observable from a web socket:
The above stream will terminate the socket under the following error conditions:
JSON.parse
.Ideally, in the first two cases, we can reconnect the socket. To do this, we can use
catch
: