@zelgadis87/utils-core 5.4.6 → 5.5.0-beta.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.
@@ -1369,7 +1369,15 @@ declare class Semaphore {
1369
1369
  constructor(availableSlots: number);
1370
1370
  private _awaitSlot;
1371
1371
  private _releaseSlot;
1372
+ submit<T>(fn: TAsyncProducer<T> | TProducer<T>, cooldown?: TimeDuration): Promise<T>;
1373
+ /** @deprecated[2026.04.01]: Use {@link submit} instead. */
1372
1374
  execute<T>(fn: TAsyncProducer<T> | TProducer<T>, cooldown?: TimeDuration): Promise<T>;
1375
+ /** Called when a task is added to the queue or immediately starts. Override in subclasses. */
1376
+ protected onTaskAdded(): void;
1377
+ /** Called when a task starts executing (acquires a slot). Override in subclasses. */
1378
+ protected onTaskStarted(): void;
1379
+ /** Called when a task completes and releases its slot. Override in subclasses. */
1380
+ protected onTaskCompleted(): void;
1373
1381
  get availableSlots(): number;
1374
1382
  get queueSize(): number;
1375
1383
  get inProgressSize(): number;
@@ -1423,13 +1431,60 @@ declare class LazyAsync<T> {
1423
1431
  static of<T>(generator: () => Promise<T>): LazyAsync<T>;
1424
1432
  }
1425
1433
 
1434
+ /**
1435
+ * A dictionary that lazily initializes values on first access.
1436
+ *
1437
+ * Values are generated by a user-provided factory function and cached for subsequent lookups.
1438
+ * Each key is initialized at most once — repeated calls to `getOrCreate` return the cached value
1439
+ * without invoking the generator again.
1440
+ *
1441
+ * @typeParam K - Key type (string, number, or symbol).
1442
+ * @typeParam T - The type of values produced by the generator.
1443
+ *
1444
+ * @example
1445
+ * ```ts
1446
+ * const dict = LazyDictionary.of( ( key: string ) => key.toUpperCase() );
1447
+ * dict.getOrCreate( 'hello' ); // → 'HELLO'
1448
+ * dict.getOrCreate( 'hello' ); // → 'HELLO' (cached, generator not called again)
1449
+ * dict.hasKey( 'hello' ); // → true
1450
+ * dict.getEntries(); // → [ [ 'hello', 'HELLO' ] ]
1451
+ * ```
1452
+ */
1426
1453
  declare class LazyDictionary<K extends string | number | symbol, T> {
1427
1454
  private readonly _generator;
1428
1455
  private readonly _dictionary;
1429
1456
  private constructor();
1457
+ /**
1458
+ * Creates a new {@link LazyDictionary} instance.
1459
+ * @param generator - Factory function that produces a value for a given key.
1460
+ */
1430
1461
  static of<K extends string | number | symbol, T>(generator: TFunction<K, T>): LazyDictionary<K, T>;
1431
- getOrCreate(key: K): T;
1462
+ /**
1463
+ * Returns the cached value for `key`, generating and caching it if this is the first access.
1464
+ * The generator is invoked at most once per key.
1465
+ * @param key - The key to look up or generate.
1466
+ * @returns The value associated with `key`.
1467
+ */ getOrCreate(key: K): T;
1468
+ /**
1469
+ * Returns the cached value for `key`, or throws if the key has not been generated yet.
1470
+ * Unlike {@link getOrCreate}, this method does **not** invoke the generator.
1471
+ * @param key - The key to look up.
1472
+ * @param errorMessage - Optional message for the thrown Error.
1473
+ * @throws {Error} If the key has not been generated yet.
1474
+ * @returns The value associated with `key`.
1475
+ */
1432
1476
  getOrThrow(key: K, errorMessage?: "Value not initialized"): T;
1477
+ /**
1478
+ * Returns whether a value has been generated for the given key.
1479
+ * @param key - The key to check.
1480
+ */
1481
+ hasKey(key: K): boolean;
1482
+ /**
1483
+ * Returns all key-value pairs that have been generated so far.
1484
+ * Does not invoke the generator for any key.
1485
+ * @returns An array of `[key, value]` tuples.
1486
+ */
1487
+ getEntries(): [K, T][];
1433
1488
  }
1434
1489
 
1435
1490
  /**
package/.rollup/index.mjs CHANGED
@@ -151,9 +151,27 @@ class Semaphore {
151
151
  this._inProgress -= 1;
152
152
  }
153
153
  }
154
- async execute(fn, cooldown = TimeDuration.ZERO) {
155
- return this._awaitSlot().then(fn).finally(() => { void cooldown.delay(() => this._releaseSlot()); });
156
- }
154
+ async submit(fn, cooldown = TimeDuration.ZERO) {
155
+ this.onTaskAdded();
156
+ await this._awaitSlot();
157
+ this.onTaskStarted();
158
+ const [result, error] = await withTryCatchAsync(async () => fn());
159
+ this.onTaskCompleted();
160
+ void cooldown.delay(() => this._releaseSlot());
161
+ if (error)
162
+ throw error;
163
+ return result;
164
+ }
165
+ /** @deprecated[2026.04.01]: Use {@link submit} instead. */
166
+ execute(fn, cooldown = TimeDuration.ZERO) {
167
+ return this.submit(fn, cooldown);
168
+ }
169
+ /** Called when a task is added to the queue or immediately starts. Override in subclasses. */
170
+ onTaskAdded() { }
171
+ /** Called when a task starts executing (acquires a slot). Override in subclasses. */
172
+ onTaskStarted() { }
173
+ /** Called when a task completes and releases its slot. Override in subclasses. */
174
+ onTaskCompleted() { }
157
175
  get availableSlots() {
158
176
  return this._availableSlots;
159
177
  }
@@ -1653,16 +1671,44 @@ class LazyAsync {
1653
1671
  }
1654
1672
  }
1655
1673
 
1674
+ /**
1675
+ * A dictionary that lazily initializes values on first access.
1676
+ *
1677
+ * Values are generated by a user-provided factory function and cached for subsequent lookups.
1678
+ * Each key is initialized at most once — repeated calls to `getOrCreate` return the cached value
1679
+ * without invoking the generator again.
1680
+ *
1681
+ * @typeParam K - Key type (string, number, or symbol).
1682
+ * @typeParam T - The type of values produced by the generator.
1683
+ *
1684
+ * @example
1685
+ * ```ts
1686
+ * const dict = LazyDictionary.of( ( key: string ) => key.toUpperCase() );
1687
+ * dict.getOrCreate( 'hello' ); // → 'HELLO'
1688
+ * dict.getOrCreate( 'hello' ); // → 'HELLO' (cached, generator not called again)
1689
+ * dict.hasKey( 'hello' ); // → true
1690
+ * dict.getEntries(); // → [ [ 'hello', 'HELLO' ] ]
1691
+ * ```
1692
+ */
1656
1693
  class LazyDictionary {
1657
1694
  _generator;
1658
1695
  _dictionary = {};
1659
1696
  constructor(_generator) {
1660
1697
  this._generator = _generator;
1661
1698
  }
1699
+ /**
1700
+ * Creates a new {@link LazyDictionary} instance.
1701
+ * @param generator - Factory function that produces a value for a given key.
1702
+ */
1662
1703
  static of(generator) {
1663
1704
  return new LazyDictionary(generator);
1664
1705
  }
1665
- getOrCreate(key) {
1706
+ /**
1707
+ * Returns the cached value for `key`, generating and caching it if this is the first access.
1708
+ * The generator is invoked at most once per key.
1709
+ * @param key - The key to look up or generate.
1710
+ * @returns The value associated with `key`.
1711
+ */ getOrCreate(key) {
1666
1712
  if (key in this._dictionary) {
1667
1713
  return this._dictionary[key];
1668
1714
  }
@@ -1670,12 +1716,35 @@ class LazyDictionary {
1670
1716
  this._dictionary[key] = value;
1671
1717
  return value;
1672
1718
  }
1719
+ /**
1720
+ * Returns the cached value for `key`, or throws if the key has not been generated yet.
1721
+ * Unlike {@link getOrCreate}, this method does **not** invoke the generator.
1722
+ * @param key - The key to look up.
1723
+ * @param errorMessage - Optional message for the thrown Error.
1724
+ * @throws {Error} If the key has not been generated yet.
1725
+ * @returns The value associated with `key`.
1726
+ */
1673
1727
  getOrThrow(key, errorMessage) {
1674
1728
  if (key in this._dictionary) {
1675
1729
  return this._dictionary[key];
1676
1730
  }
1677
1731
  throw new Error(errorMessage);
1678
1732
  }
1733
+ /**
1734
+ * Returns whether a value has been generated for the given key.
1735
+ * @param key - The key to check.
1736
+ */
1737
+ hasKey(key) {
1738
+ return key in this._dictionary;
1739
+ }
1740
+ /**
1741
+ * Returns all key-value pairs that have been generated so far.
1742
+ * Does not invoke the generator for any key.
1743
+ * @returns An array of `[key, value]` tuples.
1744
+ */
1745
+ getEntries() {
1746
+ return Object.entries(this._dictionary);
1747
+ }
1679
1748
  }
1680
1749
 
1681
1750
  class TimeUnit {