Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Run stream errors through the onError function (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
Morley Zhi authored Aug 9, 2019
1 parent 71c0c7e commit 4a66717
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stellar/wallet-sdk",
"version": "0.0.4-rc.1",
"version": "0.0.4-rc.2",
"description": "Libraries to help you write Stellar-enabled wallets in Javascript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
19 changes: 19 additions & 0 deletions src/data/DataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ interface CallbacksObject {
accountDetails?: () => void;
}

interface ErrorHandlersObject {
accountDetails?: (error: any) => void;
}

export class DataProvider {
private accountKey: string;
private serverUrl: string;
private unfundedWatcherTimeout: any;

private effectStreamEnder?: () => void;
private callbacks: CallbacksObject;
private errorHandlers: ErrorHandlersObject;

constructor(params: DataProviderParams) {
const accountKey = isAccount(params.accountOrKey)
Expand All @@ -65,6 +70,7 @@ export class DataProvider {
}

this.callbacks = {};
this.errorHandlers = {};
this.serverUrl = params.serverUrl;
this.accountKey = accountKey;
this.unfundedWatcherTimeout = null;
Expand Down Expand Up @@ -195,6 +201,7 @@ export class DataProvider {
.then(onMessage)
.catch(onError);
}, 2000);
this.errorHandlers.accountDetails = onError;

this._startEffectWatcher().catch((err) => {
onError(err);
Expand All @@ -219,6 +226,7 @@ export class DataProvider {
}

delete this.callbacks.accountDetails;
delete this.errorHandlers.accountDetails;
};
}

Expand Down Expand Up @@ -318,6 +326,17 @@ export class DataProvider {
}
}
},
onerror: (e) => {
// run error handlers
const errorHandlers = Object.values(this.errorHandlers).filter(
(errorHandler) => !!errorHandler,
);
if (errorHandlers.length) {
errorHandlers.forEach((errorHandler) => {
errorHandler(e);
});
}
},
});

return Promise.resolve({});
Expand Down

0 comments on commit 4a66717

Please sign in to comment.