@ceeblue/web-utils 3.2.1 → 4.0.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.
@@ -302,7 +302,7 @@ type Params = {
302
302
  /**
303
303
  * Optional query to add into the generated url of connection
304
304
  */
305
- query?: Record<string, string>;
305
+ query?: URLSearchParams;
306
306
  };
307
307
  /**
308
308
  * Type of connection
@@ -349,6 +349,20 @@ declare namespace Connect {
349
349
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
350
350
  */
351
351
 
352
+ type FilterOnKey<T> = T extends 'once' | 'on' ? never : T extends `on${infer R}` ? R : never;
353
+ type CaseVariations<T extends string> = string extends T ? string : Lowercase<T> | Capitalize<T>;
354
+ /**
355
+ * Extract all event keys from a class.
356
+ * @example
357
+ * class Logger extends EventEmitter {
358
+ * onLog(log:string) { console.log(log); }
359
+ * onClick(log:string) { console.log(log); }
360
+ * }
361
+ * type LoggerEvents = EventKeys<Logger>; // "log" | "click"
362
+ */
363
+ type EventKeys<Keys> = keyof {
364
+ [K in keyof Keys as CaseVariations<FilterOnKey<K>>]: never;
365
+ };
352
366
  /**
353
367
  * A advanced EventEmitter which allows to declare event as natural function in the inheriting children class,
354
368
  * function must start by `on` prefix to be recognized as an event.
@@ -402,7 +416,7 @@ declare class EventEmitter extends Loggable {
402
416
  * @param event Subscriber Function
403
417
  * @param options.signal Optional `AbortSignal` to stop this or multiple subscriptions in same time
404
418
  */
405
- on(name: string, event: Function, options?: {
419
+ on(name: EventKeys<this>, event: Function, options?: {
406
420
  signal?: AbortSignal;
407
421
  }): void;
408
422
  /**
@@ -411,7 +425,7 @@ declare class EventEmitter extends Loggable {
411
425
  * @param event Subscriber Function
412
426
  * @param options.abortSignal Optional `AbortSignal` to stop this or multiple subscriptions in same time
413
427
  */
414
- once(name: string, event: Function, options?: {
428
+ once(name: EventKeys<this>, event: Function, options?: {
415
429
  signal?: AbortSignal;
416
430
  }): void;
417
431
  /**
@@ -419,7 +433,7 @@ declare class EventEmitter extends Loggable {
419
433
  * @param name Name of event without the 'on' prefix (ex: 'log' to 'onLog' event declared)
420
434
  * @param event Unsubscriber Function, must be the one passed to {@link on} or {@link once} subscription methods
421
435
  */
422
- off(name: string, event: Function): void;
436
+ off(name: EventKeys<this>, event: Function): boolean;
423
437
  private _event;
424
438
  }
425
439
 
@@ -783,10 +797,11 @@ declare function objectFrom(value: any, params: {
783
797
  }): any;
784
798
  /**
785
799
  * Returns entries from something iterable, such as a Map, Set, or Array
800
+ * If value is null it returns an empty array
786
801
  * @param value iterable input
787
802
  * @returns An javascript object
788
803
  */
789
- declare function objectEntries(value: any): [string, any][];
804
+ declare function objectEntries(value: any | null): [string, any][];
790
805
  /**
791
806
  * Converts various data types, such as objects, strings, exceptions, errors,
792
807
  * or numbers, into a string representation. Since it offers a more comprehensive format,
package/dist/web-utils.js CHANGED
@@ -258,10 +258,14 @@ function objectFrom(value, params) {
258
258
  }
259
259
  /**
260
260
  * Returns entries from something iterable, such as a Map, Set, or Array
261
+ * If value is null it returns an empty array
261
262
  * @param value iterable input
262
263
  * @returns An javascript object
263
264
  */
264
265
  function objectEntries(value) {
266
+ if (!value) {
267
+ return [];
268
+ }
265
269
  if (value.entries) {
266
270
  return value.entries();
267
271
  }
@@ -1222,8 +1226,8 @@ function buildURL(type, params, protocol = 'wss') {
1222
1226
  if (params.accessToken) {
1223
1227
  url.searchParams.set('id', params.accessToken);
1224
1228
  }
1225
- for (const key in params.query) {
1226
- url.searchParams.set(key, params.query[key]);
1229
+ for (const [key, value] of objectEntries(params.query)) {
1230
+ url.searchParams.set(key, value);
1227
1231
  }
1228
1232
  return url;
1229
1233
  }var Connect=/*#__PURE__*/Object.freeze({__proto__:null,get Type(){return Type},buildURL:buildURL,defineMediaExt:defineMediaExt});/**
@@ -1286,19 +1290,18 @@ class EventEmitter extends Loggable {
1286
1290
  if (name.length < 3 || !name.startsWith('on')) {
1287
1291
  continue;
1288
1292
  }
1289
- if (proto[name] instanceof Function) {
1293
+ let defaultEvent = proto[name];
1294
+ if (defaultEvent instanceof Function) {
1290
1295
  const events = new Set();
1291
1296
  this._events.set(name.substring(2).toLowerCase(), events);
1292
- let defaultEvent = proto[name];
1293
1297
  const raise = (...args) => {
1294
- // Call default event if not null (can happen in JS usage)
1295
- if (defaultEvent) {
1296
- defaultEvent.call(this, ...args);
1297
- }
1298
+ // Call default event if not undefined, can happen if assigned to null/undefined
1299
+ const result = defaultEvent ? defaultEvent.call(this, ...args) : undefined;
1298
1300
  // Call subscribers
1299
1301
  for (const event of events) {
1300
1302
  event(...args);
1301
1303
  }
1304
+ return result;
1302
1305
  };
1303
1306
  Object.defineProperties(this, {
1304
1307
  [name]: {
@@ -1322,8 +1325,8 @@ class EventEmitter extends Loggable {
1322
1325
  */
1323
1326
  on(name, event, options) {
1324
1327
  var _a;
1325
- if (!event) {
1326
- throw Error('event to subscribe cannot be null');
1328
+ if (typeof event !== 'function') {
1329
+ throw Error('event callback must be a function');
1327
1330
  }
1328
1331
  const events = this._event(name);
1329
1332
  events.add(event);
@@ -1337,8 +1340,8 @@ class EventEmitter extends Loggable {
1337
1340
  */
1338
1341
  once(name, event, options) {
1339
1342
  var _a;
1340
- if (!event) {
1341
- throw Error('event to subscribe cannot be null');
1343
+ if (typeof event !== 'function') {
1344
+ throw Error('event callback must be a function');
1342
1345
  }
1343
1346
  const events = this._event(name);
1344
1347
  events.add((...args) => {
@@ -1356,7 +1359,7 @@ class EventEmitter extends Loggable {
1356
1359
  if (!event) {
1357
1360
  throw Error('event to unsubscribe cannot be null');
1358
1361
  }
1359
- this._event(name).delete(event);
1362
+ return this._event(name).delete(event);
1360
1363
  }
1361
1364
  _event(name) {
1362
1365
  const events = this._events.get(name.toLowerCase());
@@ -2416,4 +2419,4 @@ class UIMetrics {
2416
2419
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
2417
2420
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
2418
2421
  */
2419
- const VERSION = '3.2.1';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EpochTime,EventEmitter,FixMap,Log,LogLevel,Loggable,NetAddress,Numbers,Queue,SDP,UIMetrics,Util,VERSION,WebSocketReliable,log};//# sourceMappingURL=web-utils.js.map
2422
+ const VERSION = '4.0.0';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EpochTime,EventEmitter,FixMap,Log,LogLevel,Loggable,NetAddress,Numbers,Queue,SDP,UIMetrics,Util,VERSION,WebSocketReliable,log};//# sourceMappingURL=web-utils.js.map