Skip to content

Commit 7eaa018

Browse files
author
7kharov
committed
feat(persistence): add persistence policy with max retry && final callback
feat(persistence): add persistence policy with max retry && final callback
1 parent 8eb02ad commit 7eaa018

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lib/electrum_client.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class ElectrumClient extends Client {
99
this.timeLastCall = 0;
1010
}
1111

12-
initElectrum(electrumConfig) {
12+
initElectrum(electrumConfig, persistencePolicy = { maxRetry: 1000, callback: null }) {
13+
this.persistencePolicy = persistencePolicy;
1314
this.electrumConfig = electrumConfig;
1415
this.timeLastCall = 0;
1516
return this
@@ -20,18 +21,13 @@ class ElectrumClient extends Client {
2021

2122
// Override parent
2223
request(method, params) {
23-
console.log(`Call ${method} with ${JSON.stringify(params)}`);
2424
this.timeLastCall = new Date().getTime();
2525
const parentPromise = super.request(method, params);
2626
return parentPromise
2727
.then((response) => {
2828
this.keepAlive();
2929
return response;
3030
})
31-
.catch((error) => {
32-
console.log("Electrum_client catch:", error);
33-
return Promise.reject(error);
34-
})
3531
;
3632
}
3733

@@ -45,7 +41,14 @@ class ElectrumClient extends Client {
4541
];
4642
list.forEach(event => this.subscribe.removeAllListeners(event));
4743
setTimeout(() => {
48-
this.reconnect();
44+
if (this.persistencePolicy != null && this.persistencePolicy.maxRetry > 0) {
45+
this.reconnect();
46+
this.persistencePolicy.maxRetry -= 1;
47+
} else if (this.persistencePolicy != null && this.persistencePolicy.callback != null) {
48+
this.persistencePolicy.callback();
49+
} else if (this.persistencePolicy == null) {
50+
this.reconnect();
51+
}
4952
}, 10000);
5053
}
5154

0 commit comments

Comments
 (0)