@ipsme/msgenv-mqtt 0.1.7 → 0.2.4

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.
@@ -13003,7 +13003,7 @@ var auth = {};
13003
13003
 
13004
13004
  var shared = {};
13005
13005
 
13006
- var version = "5.14.1";
13006
+ var version = "5.15.1";
13007
13007
  var require$$0$2 = {
13008
13008
  version: version};
13009
13009
 
@@ -31290,6 +31290,7 @@ function requireMqtt () {
31290
31290
  exports$1.Client = client_1.default;
31291
31291
  __exportStar(requireClient(), exports$1);
31292
31292
  __exportStar(requireShared(), exports$1);
31293
+ __exportStar(requireValidations(), exports$1);
31293
31294
  var ack_1 = requireAck();
31294
31295
  Object.defineProperty(exports$1, "ReasonCodes", { enumerable: true, get: function () { return ack_1.ReasonCodes; } });
31295
31296
 
@@ -31380,6 +31381,7 @@ var cfg_ = (function () {
31380
31381
  const kstr_TOPIC = 'IPSME';
31381
31382
  class MsgEnvSingleton {
31382
31383
  static _instance = null;
31384
+ _disposed = false;
31383
31385
  client;
31384
31386
  constructor() {
31385
31387
  this.client = mqtt.connect({
@@ -31393,13 +31395,36 @@ class MsgEnvSingleton {
31393
31395
  this.client.on('connect', function () {
31394
31396
  // console.log('MQTT Connected');
31395
31397
  });
31398
+ this.client.on('close', () => {
31399
+ if (!this._disposed) {
31400
+ logr_.log(l_.CONNECTIONS, () => ['MQTT client disconnected unexpectedly']);
31401
+ }
31402
+ });
31396
31403
  }
31397
- static getInstance() {
31404
+ static get_instance() {
31398
31405
  if (!MsgEnvSingleton._instance) {
31399
31406
  MsgEnvSingleton._instance = new MsgEnvSingleton();
31400
31407
  }
31401
31408
  return MsgEnvSingleton._instance;
31402
31409
  }
31410
+ dispose(callback) {
31411
+ if (this._disposed) {
31412
+ callback?.();
31413
+ return;
31414
+ }
31415
+ this._disposed = true;
31416
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: disposing MQTT client']);
31417
+ // End the client connection gracefully
31418
+ this.client.end(true, {}, (err) => {
31419
+ if (err) {
31420
+ console.error('Error during MQTT client shutdown:', err);
31421
+ callback?.(err);
31422
+ return;
31423
+ }
31424
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: MQTT client cleanly disconnected']);
31425
+ callback?.();
31426
+ });
31427
+ }
31403
31428
  }
31404
31429
  // Normally you can specify an object to filter on when subscribing, but in electron that is missing
31405
31430
  //
@@ -31411,7 +31436,7 @@ function subscribe_(handler) {
31411
31436
  // LOGR_.log(l_.REFL, cfg_.prefix +'MsgEnv: onNotification: ', userInfo.msg);
31412
31437
  // this(userInfo.msg);
31413
31438
  // }.bind(handler));
31414
- let instance = MsgEnvSingleton.getInstance();
31439
+ let instance = MsgEnvSingleton.get_instance();
31415
31440
  handler.subscription_ID = function (topic, message) {
31416
31441
  handler(message.toString());
31417
31442
  };
@@ -31422,7 +31447,7 @@ function subscribe_(handler) {
31422
31447
  function unsubscribe_(handler) {
31423
31448
  logr_.log(l_.CONNECTIONS, () => [cfg_.prefix + 'MsgEnv: unsubscribe']);
31424
31449
  // systemPreferences.unsubscribeNotification(handler.subscription_ID);
31425
- let instance = MsgEnvSingleton.getInstance();
31450
+ let instance = MsgEnvSingleton.get_instance();
31426
31451
  instance.client.removeListener('message', handler.subscription_ID);
31427
31452
  instance.client.unsubscribe(kstr_TOPIC);
31428
31453
  delete handler.subscription_ID;
@@ -31432,11 +31457,21 @@ function unsubscribe_(handler) {
31432
31457
  function publish_(msg) {
31433
31458
  logr_.log(l_.REFLECTION, () => [cfg_.prefix + 'MsgEnv: postNotification: ', msg]);
31434
31459
  // systemPreferences.postNotification(cfg_.channel, { "msg" : msg }, true);
31435
- let instance = MsgEnvSingleton.getInstance();
31460
+ let instance = MsgEnvSingleton.get_instance();
31436
31461
  instance.client.publish(kstr_TOPIC, msg);
31437
31462
  }
31463
+ function dispose_(callback) {
31464
+ if (!MsgEnvSingleton._instance) {
31465
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: dispose called but no instance exists']);
31466
+ callback?.();
31467
+ return;
31468
+ }
31469
+ MsgEnvSingleton._instance.dispose(callback);
31470
+ MsgEnvSingleton._instance = null; // Allow re-creation if needed later
31471
+ }
31438
31472
 
31439
31473
  exports.config = cfg_;
31474
+ exports.dispose = dispose_;
31440
31475
  exports.logr = logr_;
31441
31476
  exports.publish = publish_;
31442
31477
  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 };
@@ -13001,7 +13001,7 @@ var auth = {};
13001
13001
 
13002
13002
  var shared = {};
13003
13003
 
13004
- var version = "5.14.1";
13004
+ var version = "5.15.1";
13005
13005
  var require$$0$2 = {
13006
13006
  version: version};
13007
13007
 
@@ -31288,6 +31288,7 @@ function requireMqtt () {
31288
31288
  exports$1.Client = client_1.default;
31289
31289
  __exportStar(requireClient(), exports$1);
31290
31290
  __exportStar(requireShared(), exports$1);
31291
+ __exportStar(requireValidations(), exports$1);
31291
31292
  var ack_1 = requireAck();
31292
31293
  Object.defineProperty(exports$1, "ReasonCodes", { enumerable: true, get: function () { return ack_1.ReasonCodes; } });
31293
31294
 
@@ -31378,6 +31379,7 @@ var cfg_ = (function () {
31378
31379
  const kstr_TOPIC = 'IPSME';
31379
31380
  class MsgEnvSingleton {
31380
31381
  static _instance = null;
31382
+ _disposed = false;
31381
31383
  client;
31382
31384
  constructor() {
31383
31385
  this.client = mqtt.connect({
@@ -31391,13 +31393,36 @@ class MsgEnvSingleton {
31391
31393
  this.client.on('connect', function () {
31392
31394
  // console.log('MQTT Connected');
31393
31395
  });
31396
+ this.client.on('close', () => {
31397
+ if (!this._disposed) {
31398
+ logr_.log(l_.CONNECTIONS, () => ['MQTT client disconnected unexpectedly']);
31399
+ }
31400
+ });
31394
31401
  }
31395
- static getInstance() {
31402
+ static get_instance() {
31396
31403
  if (!MsgEnvSingleton._instance) {
31397
31404
  MsgEnvSingleton._instance = new MsgEnvSingleton();
31398
31405
  }
31399
31406
  return MsgEnvSingleton._instance;
31400
31407
  }
31408
+ dispose(callback) {
31409
+ if (this._disposed) {
31410
+ callback?.();
31411
+ return;
31412
+ }
31413
+ this._disposed = true;
31414
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: disposing MQTT client']);
31415
+ // End the client connection gracefully
31416
+ this.client.end(true, {}, (err) => {
31417
+ if (err) {
31418
+ console.error('Error during MQTT client shutdown:', err);
31419
+ callback?.(err);
31420
+ return;
31421
+ }
31422
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: MQTT client cleanly disconnected']);
31423
+ callback?.();
31424
+ });
31425
+ }
31401
31426
  }
31402
31427
  // Normally you can specify an object to filter on when subscribing, but in electron that is missing
31403
31428
  //
@@ -31409,7 +31434,7 @@ function subscribe_(handler) {
31409
31434
  // LOGR_.log(l_.REFL, cfg_.prefix +'MsgEnv: onNotification: ', userInfo.msg);
31410
31435
  // this(userInfo.msg);
31411
31436
  // }.bind(handler));
31412
- let instance = MsgEnvSingleton.getInstance();
31437
+ let instance = MsgEnvSingleton.get_instance();
31413
31438
  handler.subscription_ID = function (topic, message) {
31414
31439
  handler(message.toString());
31415
31440
  };
@@ -31420,7 +31445,7 @@ function subscribe_(handler) {
31420
31445
  function unsubscribe_(handler) {
31421
31446
  logr_.log(l_.CONNECTIONS, () => [cfg_.prefix + 'MsgEnv: unsubscribe']);
31422
31447
  // systemPreferences.unsubscribeNotification(handler.subscription_ID);
31423
- let instance = MsgEnvSingleton.getInstance();
31448
+ let instance = MsgEnvSingleton.get_instance();
31424
31449
  instance.client.removeListener('message', handler.subscription_ID);
31425
31450
  instance.client.unsubscribe(kstr_TOPIC);
31426
31451
  delete handler.subscription_ID;
@@ -31430,8 +31455,17 @@ function unsubscribe_(handler) {
31430
31455
  function publish_(msg) {
31431
31456
  logr_.log(l_.REFLECTION, () => [cfg_.prefix + 'MsgEnv: postNotification: ', msg]);
31432
31457
  // systemPreferences.postNotification(cfg_.channel, { "msg" : msg }, true);
31433
- let instance = MsgEnvSingleton.getInstance();
31458
+ let instance = MsgEnvSingleton.get_instance();
31434
31459
  instance.client.publish(kstr_TOPIC, msg);
31435
31460
  }
31461
+ function dispose_(callback) {
31462
+ if (!MsgEnvSingleton._instance) {
31463
+ logr_.log(l_.CONNECTIONS, () => ['MsgEnv: dispose called but no instance exists']);
31464
+ callback?.();
31465
+ return;
31466
+ }
31467
+ MsgEnvSingleton._instance.dispose(callback);
31468
+ MsgEnvSingleton._instance = null; // Allow re-creation if needed later
31469
+ }
31436
31470
 
31437
- export { cfg_ as config, logr_ as logr, publish_ as publish, subscribe_ as subscribe, unsubscribe_ as unsubscribe };
31471
+ 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.4",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
@@ -28,18 +28,18 @@
28
28
  },
29
29
  "homepage": "https://github.com/IPSME/npm-msgenv-MQTT#readme",
30
30
  "dependencies": {
31
- "@knev/bitlogr": "^3.1.3",
32
- "mqtt": "^5.14.1"
31
+ "@knev/bitlogr": "^3.2.2",
32
+ "mqtt": "^5.15.1"
33
33
  },
34
34
  "devDependencies": {
35
- "@rollup/plugin-commonjs": "^29.0.0",
35
+ "@rollup/plugin-commonjs": "^29.0.2",
36
36
  "@rollup/plugin-json": "^6.1.0",
37
37
  "@rollup/plugin-node-resolve": "^16.0.3",
38
38
  "@rollup/plugin-typescript": "^12.3.0",
39
- "@types/node": "^25.0.9",
39
+ "@types/node": "^25.5.0",
40
40
  "del-cli": "^7.0.0",
41
- "rollup": "^4.55.3",
41
+ "rollup": "^4.60.0",
42
42
  "tslib": "^2.8.1",
43
- "typescript": "^5.9.3"
43
+ "typescript": "^6.0.2"
44
44
  }
45
45
  }
@@ -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();
package/tsconfig.json CHANGED
@@ -28,7 +28,7 @@
28
28
  /* Modules */
29
29
  "module": "ESNext", /* Specify what module code is generated. */
30
30
  "rootDir": "./src", /* Specify the root folder within your source files. */
31
- "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
31
+ "moduleResolution": "bundler", /* Specify how TypeScript looks up a file from a given module specifier. */
32
32
  // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
33
33
  // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
34
34
  // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */