@theshelf/connection 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ConnectionManager.d.ts +2 -3
- package/dist/ConnectionManager.js +20 -20
- package/package.json +4 -3
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import type Logger from '
|
|
1
|
+
import type Logger from '../../logging/core/dist/index.js';
|
|
2
2
|
import type { State } from './definitions/constants.js';
|
|
3
3
|
import type { Connectable } from './definitions/interfaces.js';
|
|
4
4
|
type Configuration = {
|
|
5
5
|
readonly name: string;
|
|
6
6
|
readonly connectable: Connectable;
|
|
7
|
-
readonly logger: Logger;
|
|
8
7
|
readonly monitoringTimeout?: number;
|
|
9
8
|
};
|
|
10
9
|
export default class ConnectionManager {
|
|
11
10
|
#private;
|
|
12
|
-
constructor(configuration: Configuration);
|
|
11
|
+
constructor(configuration: Configuration, logger?: Logger);
|
|
13
12
|
get name(): string;
|
|
14
13
|
get state(): State;
|
|
15
14
|
connect(): Promise<void>;
|
|
@@ -3,27 +3,29 @@ const DEFAULT_MONITORING_TIMEOUT = 3000;
|
|
|
3
3
|
export default class ConnectionManager {
|
|
4
4
|
#name;
|
|
5
5
|
#connectable;
|
|
6
|
-
#logger;
|
|
7
6
|
#timeoutDuration;
|
|
7
|
+
#logger;
|
|
8
|
+
#logPrefix;
|
|
8
9
|
#state = States.DISCONNECTED;
|
|
9
10
|
#monitorTimeout;
|
|
10
11
|
#connectPromise;
|
|
11
12
|
#disconnectPromise;
|
|
12
|
-
constructor(configuration) {
|
|
13
|
+
constructor(configuration, logger) {
|
|
13
14
|
this.#name = configuration.name;
|
|
14
15
|
this.#connectable = configuration.connectable;
|
|
15
|
-
this.#logger = configuration.logger;
|
|
16
16
|
this.#timeoutDuration = configuration.monitoringTimeout ?? DEFAULT_MONITORING_TIMEOUT;
|
|
17
|
+
this.#logger = logger?.for(ConnectionManager.name);
|
|
18
|
+
this.#logPrefix = `${this.#name} ->`;
|
|
17
19
|
}
|
|
18
20
|
get name() { return this.#name; }
|
|
19
21
|
get state() { return this.#state; }
|
|
20
22
|
async connect() {
|
|
21
23
|
if (this.#connectPromise !== undefined) {
|
|
22
|
-
this.#logger
|
|
24
|
+
this.#logger?.warn(this.#logPrefix, 'connect already in progress');
|
|
23
25
|
return this.#connectPromise;
|
|
24
26
|
}
|
|
25
27
|
if (this.#state !== States.DISCONNECTED) {
|
|
26
|
-
this.#logger
|
|
28
|
+
this.#logger?.warn(this.#logPrefix, 'connect in invalid state');
|
|
27
29
|
return;
|
|
28
30
|
}
|
|
29
31
|
await this.#connect();
|
|
@@ -31,11 +33,11 @@ export default class ConnectionManager {
|
|
|
31
33
|
}
|
|
32
34
|
async disconnect() {
|
|
33
35
|
if (this.#disconnectPromise !== undefined) {
|
|
34
|
-
this.#logger
|
|
36
|
+
this.#logger?.warn(this.#logPrefix, 'disconnect already in progress');
|
|
35
37
|
return this.#disconnectPromise;
|
|
36
38
|
}
|
|
37
39
|
if (this.#state !== States.CONNECTED) {
|
|
38
|
-
this.#logger
|
|
40
|
+
this.#logger?.warn(this.#logPrefix, 'disconnect in invalid state');
|
|
39
41
|
return;
|
|
40
42
|
}
|
|
41
43
|
this.#stopMonitoring();
|
|
@@ -47,11 +49,11 @@ export default class ConnectionManager {
|
|
|
47
49
|
this.#connectPromise = this.#connectable.connect();
|
|
48
50
|
await this.#connectPromise;
|
|
49
51
|
this.#state = States.CONNECTED;
|
|
50
|
-
this.#logger
|
|
52
|
+
this.#logger?.info(this.#logPrefix, 'connected successfully');
|
|
51
53
|
}
|
|
52
54
|
catch (error) {
|
|
53
55
|
this.#state = States.DISCONNECTED;
|
|
54
|
-
this.#logger
|
|
56
|
+
this.#logger?.error(this.#logPrefix, 'connection failure', error);
|
|
55
57
|
// The error isn't re-thrown to make it non-blocking, and let the monitoring do its work.
|
|
56
58
|
}
|
|
57
59
|
finally {
|
|
@@ -64,11 +66,11 @@ export default class ConnectionManager {
|
|
|
64
66
|
this.#disconnectPromise = this.#connectable.disconnect();
|
|
65
67
|
await this.#disconnectPromise;
|
|
66
68
|
this.#state = States.DISCONNECTED;
|
|
67
|
-
this.#logger
|
|
69
|
+
this.#logger?.info(this.#logPrefix, 'disconnected successfully');
|
|
68
70
|
}
|
|
69
71
|
catch (error) {
|
|
70
72
|
this.#state = States.CONNECTED;
|
|
71
|
-
this.#logger
|
|
73
|
+
this.#logger?.error(this.#logPrefix, 'disconnection failure', error);
|
|
72
74
|
throw error;
|
|
73
75
|
}
|
|
74
76
|
finally {
|
|
@@ -77,40 +79,38 @@ export default class ConnectionManager {
|
|
|
77
79
|
}
|
|
78
80
|
#startMonitoring() {
|
|
79
81
|
if (this.#monitorTimeout !== undefined) {
|
|
80
|
-
this.#logger
|
|
82
|
+
this.#logger?.warn(this.#logPrefix, 'monitoring already started');
|
|
81
83
|
return;
|
|
82
84
|
}
|
|
83
85
|
this.#scheduleMonitoring();
|
|
84
|
-
this.#logger
|
|
86
|
+
this.#logger?.info(this.#logPrefix, 'monitoring started');
|
|
85
87
|
}
|
|
86
88
|
#scheduleMonitoring() {
|
|
87
89
|
this.#monitorTimeout = setTimeout(async () => {
|
|
88
90
|
await this.#monitorConnection();
|
|
89
91
|
this.#scheduleMonitoring();
|
|
90
92
|
}, this.#timeoutDuration);
|
|
93
|
+
this.#monitorTimeout.unref();
|
|
91
94
|
}
|
|
92
95
|
#stopMonitoring() {
|
|
93
96
|
if (this.#monitorTimeout === undefined) {
|
|
94
|
-
this.#logger
|
|
97
|
+
this.#logger?.warn(this.#logPrefix, 'monitoring already stopped');
|
|
95
98
|
return;
|
|
96
99
|
}
|
|
97
100
|
clearTimeout(this.#monitorTimeout);
|
|
98
101
|
this.#monitorTimeout = undefined;
|
|
99
|
-
this.#logger
|
|
102
|
+
this.#logger?.info(this.#logPrefix, 'monitoring stopped');
|
|
100
103
|
}
|
|
101
104
|
async #monitorConnection() {
|
|
102
|
-
this.#logger
|
|
105
|
+
this.#logger?.debug(this.#logPrefix, 'monitoring connection');
|
|
103
106
|
if (this.#connectable.connected) {
|
|
104
107
|
return;
|
|
105
108
|
}
|
|
106
109
|
if (this.#connectPromise !== undefined) {
|
|
107
110
|
return this.#connectPromise;
|
|
108
111
|
}
|
|
109
|
-
this.#logger
|
|
112
|
+
this.#logger?.warn(this.#logPrefix, 'connection lost');
|
|
110
113
|
this.#state = States.DISCONNECTED;
|
|
111
114
|
return this.#connect();
|
|
112
115
|
}
|
|
113
|
-
#createLogMessage(message) {
|
|
114
|
-
return `[CONNECTION][${this.#name}] ${message}`;
|
|
115
|
-
}
|
|
116
116
|
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theshelf/connection",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
7
7
|
"url": "git+https://github.com/MaskingTechnology/theshelf.git"
|
|
8
8
|
},
|
|
9
|
+
"license": "MIT",
|
|
9
10
|
"scripts": {
|
|
10
11
|
"build": "tsc",
|
|
11
12
|
"clean": "rimraf dist",
|
|
@@ -19,9 +20,9 @@
|
|
|
19
20
|
"README.md",
|
|
20
21
|
"dist"
|
|
21
22
|
],
|
|
22
|
-
"types": "dist/index.d.ts",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
23
24
|
"exports": "./dist/index.js",
|
|
24
25
|
"peerDependencies": {
|
|
25
|
-
"@theshelf/logging": "^0.
|
|
26
|
+
"@theshelf/logging": "^0.4.0"
|
|
26
27
|
}
|
|
27
28
|
}
|