@ipsme/msgenv-mqtt 0.1.7 → 0.2.1

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.
@@ -31380,6 +31380,7 @@ var cfg_ = (function () {
31380
31380
  const kstr_TOPIC = 'IPSME';
31381
31381
  class MsgEnvSingleton {
31382
31382
  static _instance = null;
31383
+ _disposed = false;
31383
31384
  client;
31384
31385
  constructor() {
31385
31386
  this.client = mqtt.connect({
@@ -31393,13 +31394,36 @@ class MsgEnvSingleton {
31393
31394
  this.client.on('connect', function () {
31394
31395
  // console.log('MQTT Connected');
31395
31396
  });
31397
+ this.client.on('close', () => {
31398
+ if (!this._disposed) {
31399
+ logr_.log(l_.CONNECTIONS, () => ['MQTT client disconnected unexpectedly']);
31400
+ }
31401
+ });
31396
31402
  }
31397
- static getInstance() {
31403
+ static get_instance() {
31398
31404
  if (!MsgEnvSingleton._instance) {
31399
31405
  MsgEnvSingleton._instance = new MsgEnvSingleton();
31400
31406
  }
31401
31407
  return MsgEnvSingleton._instance;
31402
31408
  }
31409
+ dispose(callback) {
31410
+ if (this._disposed) {
31411
+ callback?.();
31412
+ return;
31413
+ }
31414
+ this._disposed = true;
31415
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: disposing MQTT client']);
31416
+ // End the client connection gracefully
31417
+ this.client.end(true, {}, (err) => {
31418
+ if (err) {
31419
+ console.error('Error during MQTT client shutdown:', err);
31420
+ callback?.(err);
31421
+ return;
31422
+ }
31423
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: MQTT client cleanly disconnected']);
31424
+ callback?.();
31425
+ });
31426
+ }
31403
31427
  }
31404
31428
  // Normally you can specify an object to filter on when subscribing, but in electron that is missing
31405
31429
  //
@@ -31411,7 +31435,7 @@ function subscribe_(handler) {
31411
31435
  // LOGR_.log(l_.REFL, cfg_.prefix +'MsgEnv: onNotification: ', userInfo.msg);
31412
31436
  // this(userInfo.msg);
31413
31437
  // }.bind(handler));
31414
- let instance = MsgEnvSingleton.getInstance();
31438
+ let instance = MsgEnvSingleton.get_instance();
31415
31439
  handler.subscription_ID = function (topic, message) {
31416
31440
  handler(message.toString());
31417
31441
  };
@@ -31422,7 +31446,7 @@ function subscribe_(handler) {
31422
31446
  function unsubscribe_(handler) {
31423
31447
  logr_.log(l_.CONNECTIONS, () => [cfg_.prefix + 'MsgEnv: unsubscribe']);
31424
31448
  // systemPreferences.unsubscribeNotification(handler.subscription_ID);
31425
- let instance = MsgEnvSingleton.getInstance();
31449
+ let instance = MsgEnvSingleton.get_instance();
31426
31450
  instance.client.removeListener('message', handler.subscription_ID);
31427
31451
  instance.client.unsubscribe(kstr_TOPIC);
31428
31452
  delete handler.subscription_ID;
@@ -31432,11 +31456,21 @@ function unsubscribe_(handler) {
31432
31456
  function publish_(msg) {
31433
31457
  logr_.log(l_.REFLECTION, () => [cfg_.prefix + 'MsgEnv: postNotification: ', msg]);
31434
31458
  // systemPreferences.postNotification(cfg_.channel, { "msg" : msg }, true);
31435
- let instance = MsgEnvSingleton.getInstance();
31459
+ let instance = MsgEnvSingleton.get_instance();
31436
31460
  instance.client.publish(kstr_TOPIC, msg);
31437
31461
  }
31462
+ function dispose_(callback) {
31463
+ if (!MsgEnvSingleton._instance) {
31464
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: dispose called but no instance exists']);
31465
+ callback?.();
31466
+ return;
31467
+ }
31468
+ MsgEnvSingleton._instance.dispose(callback);
31469
+ MsgEnvSingleton._instance = null; // Allow re-creation if needed later
31470
+ }
31438
31471
 
31439
31472
  exports.config = cfg_;
31473
+ exports.dispose = dispose_;
31440
31474
  exports.logr = logr_;
31441
31475
  exports.publish = publish_;
31442
31476
  exports.subscribe = subscribe_;
@@ -11,4 +11,5 @@ declare var cfg_: {
11
11
  declare function subscribe_(handler: any): void;
12
12
  declare function unsubscribe_(handler: any): void;
13
13
  declare function publish_(msg: any): void;
14
- export { cfg_ as config, subscribe_ as subscribe, unsubscribe_ as unsubscribe, publish_ as publish, logr_ as logr };
14
+ declare function dispose_(callback?: (err?: Error) => void): void;
15
+ export { cfg_ as config, subscribe_ as subscribe, unsubscribe_ as unsubscribe, publish_ as publish, dispose_ as dispose, logr_ as logr };
@@ -31378,6 +31378,7 @@ var cfg_ = (function () {
31378
31378
  const kstr_TOPIC = 'IPSME';
31379
31379
  class MsgEnvSingleton {
31380
31380
  static _instance = null;
31381
+ _disposed = false;
31381
31382
  client;
31382
31383
  constructor() {
31383
31384
  this.client = mqtt.connect({
@@ -31391,13 +31392,36 @@ class MsgEnvSingleton {
31391
31392
  this.client.on('connect', function () {
31392
31393
  // console.log('MQTT Connected');
31393
31394
  });
31395
+ this.client.on('close', () => {
31396
+ if (!this._disposed) {
31397
+ logr_.log(l_.CONNECTIONS, () => ['MQTT client disconnected unexpectedly']);
31398
+ }
31399
+ });
31394
31400
  }
31395
- static getInstance() {
31401
+ static get_instance() {
31396
31402
  if (!MsgEnvSingleton._instance) {
31397
31403
  MsgEnvSingleton._instance = new MsgEnvSingleton();
31398
31404
  }
31399
31405
  return MsgEnvSingleton._instance;
31400
31406
  }
31407
+ dispose(callback) {
31408
+ if (this._disposed) {
31409
+ callback?.();
31410
+ return;
31411
+ }
31412
+ this._disposed = true;
31413
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: disposing MQTT client']);
31414
+ // End the client connection gracefully
31415
+ this.client.end(true, {}, (err) => {
31416
+ if (err) {
31417
+ console.error('Error during MQTT client shutdown:', err);
31418
+ callback?.(err);
31419
+ return;
31420
+ }
31421
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: MQTT client cleanly disconnected']);
31422
+ callback?.();
31423
+ });
31424
+ }
31401
31425
  }
31402
31426
  // Normally you can specify an object to filter on when subscribing, but in electron that is missing
31403
31427
  //
@@ -31409,7 +31433,7 @@ function subscribe_(handler) {
31409
31433
  // LOGR_.log(l_.REFL, cfg_.prefix +'MsgEnv: onNotification: ', userInfo.msg);
31410
31434
  // this(userInfo.msg);
31411
31435
  // }.bind(handler));
31412
- let instance = MsgEnvSingleton.getInstance();
31436
+ let instance = MsgEnvSingleton.get_instance();
31413
31437
  handler.subscription_ID = function (topic, message) {
31414
31438
  handler(message.toString());
31415
31439
  };
@@ -31420,7 +31444,7 @@ function subscribe_(handler) {
31420
31444
  function unsubscribe_(handler) {
31421
31445
  logr_.log(l_.CONNECTIONS, () => [cfg_.prefix + 'MsgEnv: unsubscribe']);
31422
31446
  // systemPreferences.unsubscribeNotification(handler.subscription_ID);
31423
- let instance = MsgEnvSingleton.getInstance();
31447
+ let instance = MsgEnvSingleton.get_instance();
31424
31448
  instance.client.removeListener('message', handler.subscription_ID);
31425
31449
  instance.client.unsubscribe(kstr_TOPIC);
31426
31450
  delete handler.subscription_ID;
@@ -31430,8 +31454,17 @@ function unsubscribe_(handler) {
31430
31454
  function publish_(msg) {
31431
31455
  logr_.log(l_.REFLECTION, () => [cfg_.prefix + 'MsgEnv: postNotification: ', msg]);
31432
31456
  // systemPreferences.postNotification(cfg_.channel, { "msg" : msg }, true);
31433
- let instance = MsgEnvSingleton.getInstance();
31457
+ let instance = MsgEnvSingleton.get_instance();
31434
31458
  instance.client.publish(kstr_TOPIC, msg);
31435
31459
  }
31460
+ function dispose_(callback) {
31461
+ if (!MsgEnvSingleton._instance) {
31462
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: dispose called but no instance exists']);
31463
+ callback?.();
31464
+ return;
31465
+ }
31466
+ MsgEnvSingleton._instance.dispose(callback);
31467
+ MsgEnvSingleton._instance = null; // Allow re-creation if needed later
31468
+ }
31436
31469
 
31437
- export { cfg_ as config, logr_ as logr, publish_ as publish, subscribe_ as subscribe, unsubscribe_ as unsubscribe };
31470
+ export { cfg_ as config, dispose_ as dispose, logr_ as logr, publish_ as publish, subscribe_ as subscribe, unsubscribe_ as unsubscribe };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ipsme/msgenv-mqtt",
3
- "version": "0.1.7",
3
+ "version": "0.2.1",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
@@ -46,7 +46,8 @@ var cfg_= (function() {
46
46
  const kstr_TOPIC = 'IPSME';
47
47
 
48
48
  class MsgEnvSingleton {
49
- private static _instance: MsgEnvSingleton | null = null;
49
+ static _instance: MsgEnvSingleton | null = null;
50
+ private _disposed: boolean = false;
50
51
 
51
52
  public readonly client: MqttClient;
52
53
 
@@ -64,14 +65,44 @@ class MsgEnvSingleton {
64
65
  this.client.on('connect', function () {
65
66
  // console.log('MQTT Connected');
66
67
  });
68
+
69
+ this.client.on('close', () => {
70
+ if (!this._disposed) {
71
+ logr_.log(l_.CONNECTIONS, () => ['MQTT client disconnected unexpectedly']);
72
+ }
73
+ });
67
74
  }
68
75
 
69
- public static getInstance() : MsgEnvSingleton {
76
+ public static get_instance() : MsgEnvSingleton {
70
77
  if (! MsgEnvSingleton._instance) {
71
78
  MsgEnvSingleton._instance = new MsgEnvSingleton();
72
79
  }
73
80
  return MsgEnvSingleton._instance;
74
81
  }
82
+
83
+ public dispose(callback?: (err?: Error) => void): void {
84
+ if (this._disposed) {
85
+ callback?.();
86
+ return;
87
+ }
88
+
89
+ this._disposed = true;
90
+
91
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: disposing MQTT client']);
92
+
93
+ // End the client connection gracefully
94
+ this.client.end(true, {}, (err) => {
95
+ if (err) {
96
+ console.error('Error during MQTT client shutdown:', err);
97
+ callback?.(err);
98
+ return;
99
+ }
100
+
101
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: MQTT client cleanly disconnected']);
102
+ callback?.();
103
+ });
104
+ }
105
+
75
106
  }
76
107
 
77
108
  // Normally you can specify an object to filter on when subscribing, but in electron that is missing
@@ -85,7 +116,7 @@ function subscribe_(handler) {
85
116
  // this(userInfo.msg);
86
117
  // }.bind(handler));
87
118
 
88
- let instance = MsgEnvSingleton.getInstance();
119
+ let instance = MsgEnvSingleton.get_instance();
89
120
  handler.subscription_ID = function (topic, message) {
90
121
  handler(message.toString());
91
122
  };
@@ -98,7 +129,7 @@ function subscribe_(handler) {
98
129
  function unsubscribe_(handler) {
99
130
  logr_.log(l_.CONNECTIONS, () => [cfg_.prefix +'MsgEnv: unsubscribe'] );
100
131
  // systemPreferences.unsubscribeNotification(handler.subscription_ID);
101
- let instance = MsgEnvSingleton.getInstance();
132
+ let instance = MsgEnvSingleton.get_instance();
102
133
  instance.client.removeListener('message', handler.subscription_ID);
103
134
  instance.client.unsubscribe(kstr_TOPIC);
104
135
  delete handler.subscription_ID;
@@ -109,10 +140,21 @@ function unsubscribe_(handler) {
109
140
  function publish_(msg) {
110
141
  logr_.log(l_.REFLECTION, () => [cfg_.prefix +'MsgEnv: postNotification: ', msg] );
111
142
  // systemPreferences.postNotification(cfg_.channel, { "msg" : msg }, true);
112
- let instance = MsgEnvSingleton.getInstance();
143
+ let instance = MsgEnvSingleton.get_instance();
113
144
  instance.client.publish(kstr_TOPIC, msg);
114
145
  }
115
146
 
147
+ function dispose_(callback ?: (err?: Error) => void): void {
148
+ if (! MsgEnvSingleton._instance) {
149
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: dispose called but no instance exists']);
150
+ callback?.();
151
+ return;
152
+ }
153
+
154
+ MsgEnvSingleton._instance.dispose(callback);
155
+ MsgEnvSingleton._instance = null; // Allow re-creation if needed later
156
+ }
157
+
116
158
  //-------------------------------------------------------------------------------------------------
117
159
 
118
160
  export {
@@ -120,5 +162,6 @@ export {
120
162
  subscribe_ as subscribe,
121
163
  unsubscribe_ as unsubscribe,
122
164
  publish_ as publish,
165
+ dispose_ as dispose,
123
166
  logr_ as logr
124
167
  }
@@ -27,6 +27,10 @@ async function demo() {
27
27
 
28
28
  IPSME_MsgEnv_OS.subscribe( handler_ );
29
29
  IPSME_MsgEnv_OS.publish('BOOYAH 3.o');
30
+
31
+ await sleep(2000);
32
+
33
+ IPSME_MsgEnv_OS.dispose();
30
34
  }
31
35
 
32
36
  demo();