Merge pull request #2720 from urbit/la-channel-err

channel.js: added way to handle event source errors more smoothly, and implemented in chat
This commit is contained in:
ixv 2020-04-14 11:40:02 -07:00 committed by GitHub
commit e5cf4fb813
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 12 deletions

View File

@ -1,5 +1,17 @@
class Channel {
constructor() {
this.init();
this.deleteOnUnload();
// a way to handle channel errors
//
//
this.onChannelError = (err) => {
console.error('event source error: ', err);
};
}
init() {
// unique identifier: current time and random number
//
this.uid =
@ -40,8 +52,10 @@ class Channel {
// disconnect function may be called exactly once.
//
this.outstandingSubscriptions = new Map();
}
this.deleteOnUnload();
setOnChannelError(onError = (err) => {}) {
this.onChannelError = onError;
}
deleteOnUnload() {
@ -196,8 +210,9 @@ class Channel {
}
this.eventSource.onerror = e => {
console.error("eventSource error:", e);
this.delete();
this.init();
this.onChannelError(e);
}
}

View File

@ -9,7 +9,20 @@ import { LocalReducer } from '/reducers/local.js';
class Store {
constructor() {
this.state = {
this.state = this.initialState();
this.initialReducer = new InitialReducer();
this.permissionUpdateReducer = new PermissionUpdateReducer();
this.contactUpdateReducer = new ContactUpdateReducer();
this.chatUpdateReducer = new ChatUpdateReducer();
this.inviteUpdateReducer = new InviteUpdateReducer();
this.metadataReducer = new MetadataReducer();
this.localReducer = new LocalReducer();
this.setState = () => {};
}
initialState() {
return {
inbox: {},
chatSynced: {},
contacts: {},
@ -24,15 +37,6 @@ class Store {
pendingMessages: new Map([]),
chatInitialized: false
};
this.initialReducer = new InitialReducer();
this.permissionUpdateReducer = new PermissionUpdateReducer();
this.contactUpdateReducer = new ContactUpdateReducer();
this.chatUpdateReducer = new ChatUpdateReducer();
this.inviteUpdateReducer = new InviteUpdateReducer();
this.metadataReducer = new MetadataReducer();
this.localReducer = new LocalReducer();
this.setState = () => {};
}
setStateHandler(setState) {
@ -42,6 +46,11 @@ class Store {
handleEvent(data) {
let json = data.data;
if ('clear' in json && json.clear) {
this.setState(this.initialState());
return;
}
console.log(json);
this.initialReducer.reduce(json, this.state);
this.permissionUpdateReducer.reduce(json, this.state);

View File

@ -13,11 +13,24 @@ export class Subscription {
start() {
if (api.authTokens) {
this.firstRoundSubscription();
window.urb.setOnChannelError(this.onChannelError.bind(this));
} else {
console.error("~~~ ERROR: Must set api.authTokens before operation ~~~");
}
}
onChannelError(err) {
console.error('event source error: ', err);
console.log('initiating new channel');
this.firstRoundSubscriptionComplete = false;
setTimeout(2000, () => {
store.handleEvent({
data: { clear : true}
});
this.start();
});
}
subscribe(path, app) {
api.bind(path, 'PUT', api.authTokens.ship, app,
this.handleEvent.bind(this),