From a0932c219866b12f884da9184694ab1bf87cfaa3 Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Sun, 21 Jul 2024 03:41:10 +0200 Subject: [PATCH] automatically reconnect to previous address --- dist/index.js | 13 +++++++++++++ src/Model/connectionModel.ts | 23 ++++++++++++++++++++++- src/Model/storageModel.ts | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/dist/index.js b/dist/index.js index bf853567..0221fdb7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -347,6 +347,7 @@ var storageKeys = { // connection socketAddress: [DATA_VERSION, "connection", "socket-address"], + reconnectAddress: [DATA_VERSION, "connection", "reconnect-address"], outbox: [DATA_VERSION, "connection", "outbox"], // settings username: [DATA_VERSION, "settings", "user-name"], @@ -1504,8 +1505,11 @@ connect = (address) => { this.udn.connect(address); }; + a; disconnect = () => { this.udn.disconnect(); + const reconnectAddressPath = storageKeys.reconnectAddress; + this.storageModel.remove(reconnectAddressPath); }; handleMessage = (data) => { this.messageHandler(data); @@ -1525,6 +1529,7 @@ this.sendSubscriptionRequest(); }; sendSubscriptionRequest = () => { + if (this.isConnected == false) return; for (const channel of this.channelsToSubscribe) { this.udn.subscribe(channel); } @@ -1579,6 +1584,9 @@ storeAddress = (address) => { const addressPath = this.getAddressPath(address); this.storageModel.store(addressPath, ""); + const reconnectAddressPath = storageKeys.reconnectAddress; + this.storageModel.store(reconnectAddressPath, address); + this.storageModel.print(); }; removeAddress = (address) => { const addressPath = this.getAddressPath(address); @@ -1608,6 +1616,11 @@ this.udn.ondisconnect = () => { this.handleConnectionChange(); }; + const reconnectAddressPath = storageKeys.reconnectAddress; + const reconnectAddress = storageModel2.restore(reconnectAddressPath); + if (reconnectAddress != null) { + this.connect(reconnectAddress); + } } }; diff --git a/src/Model/connectionModel.ts b/src/Model/connectionModel.ts index 5ae255b6..1bef9178 100644 --- a/src/Model/connectionModel.ts +++ b/src/Model/connectionModel.ts @@ -29,9 +29,13 @@ export default class ConnectionModel { connect = (address: string): void => { this.udn.connect(address); }; - + a; disconnect = (): void => { this.udn.disconnect(); + + // do not reconnect + const reconnectAddressPath: string[] = storageKeys.reconnectAddress; + this.storageModel.remove(reconnectAddressPath); }; handleMessage = (data: Message): void => { @@ -41,6 +45,7 @@ export default class ConnectionModel { handleConnectionChange = (): void => { console.log("connection status:", this.isConnected, this.address); this.connectionChangeHandler(); + if (this.isConnected == false) return; if (!this.address) return; @@ -56,6 +61,8 @@ export default class ConnectionModel { }; sendSubscriptionRequest = (): void => { + if (this.isConnected == false) return; + for (const channel of this.channelsToSubscribe) { this.udn.subscribe(channel); } @@ -122,8 +129,14 @@ export default class ConnectionModel { }; storeAddress = (address: string): void => { + // history const addressPath = this.getAddressPath(address); this.storageModel.store(addressPath, ""); + + // reconnect + const reconnectAddressPath: string[] = storageKeys.reconnectAddress; + this.storageModel.store(reconnectAddressPath, address); + this.storageModel.print(); }; removeAddress = (address: string): void => { @@ -161,5 +174,13 @@ export default class ConnectionModel { this.udn.ondisconnect = () => { this.handleConnectionChange(); }; + + // reconnect + const reconnectAddressPath: string[] = storageKeys.reconnectAddress; + const reconnectAddress: string | null = + storageModel.restore(reconnectAddressPath); + if (reconnectAddress != null) { + this.connect(reconnectAddress); + } } } diff --git a/src/Model/storageModel.ts b/src/Model/storageModel.ts index 091714e5..368b3a73 100644 --- a/src/Model/storageModel.ts +++ b/src/Model/storageModel.ts @@ -131,6 +131,7 @@ export type StorageEntry = { [key: string]: StorageEntry }; export const storageKeys = { // connection socketAddress: [DATA_VERSION, "connection", "socket-address"], + reconnectAddress: [DATA_VERSION, "connection", "reconnect-address"], outbox: [DATA_VERSION, "connection", "outbox"], // settings