@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.
- package/.rollup/index.cjs +73 -4
- package/.rollup/index.cjs.map +1 -1
- package/.rollup/index.d.ts +56 -1
- package/.rollup/index.mjs +73 -4
- package/.rollup/index.mjs.map +1 -1
- package/.rollup/tsconfig.tsbuildinfo +1 -1
- package/CHANGELOG.md +12 -0
- package/package.json +1 -1
- package/src/async/Semaphore.ts +25 -3
- package/src/lazy/LazyDictionary.ts +54 -2
package/.rollup/index.cjs
CHANGED
|
@@ -153,9 +153,27 @@ class Semaphore {
|
|
|
153
153
|
this._inProgress -= 1;
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
|
-
async
|
|
157
|
-
|
|
158
|
-
|
|
156
|
+
async submit(fn, cooldown = TimeDuration.ZERO) {
|
|
157
|
+
this.onTaskAdded();
|
|
158
|
+
await this._awaitSlot();
|
|
159
|
+
this.onTaskStarted();
|
|
160
|
+
const [result, error] = await withTryCatchAsync(async () => fn());
|
|
161
|
+
this.onTaskCompleted();
|
|
162
|
+
void cooldown.delay(() => this._releaseSlot());
|
|
163
|
+
if (error)
|
|
164
|
+
throw error;
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
/** @deprecated[2026.04.01]: Use {@link submit} instead. */
|
|
168
|
+
execute(fn, cooldown = TimeDuration.ZERO) {
|
|
169
|
+
return this.submit(fn, cooldown);
|
|
170
|
+
}
|
|
171
|
+
/** Called when a task is added to the queue or immediately starts. Override in subclasses. */
|
|
172
|
+
onTaskAdded() { }
|
|
173
|
+
/** Called when a task starts executing (acquires a slot). Override in subclasses. */
|
|
174
|
+
onTaskStarted() { }
|
|
175
|
+
/** Called when a task completes and releases its slot. Override in subclasses. */
|
|
176
|
+
onTaskCompleted() { }
|
|
159
177
|
get availableSlots() {
|
|
160
178
|
return this._availableSlots;
|
|
161
179
|
}
|
|
@@ -1655,16 +1673,44 @@ class LazyAsync {
|
|
|
1655
1673
|
}
|
|
1656
1674
|
}
|
|
1657
1675
|
|
|
1676
|
+
/**
|
|
1677
|
+
* A dictionary that lazily initializes values on first access.
|
|
1678
|
+
*
|
|
1679
|
+
* Values are generated by a user-provided factory function and cached for subsequent lookups.
|
|
1680
|
+
* Each key is initialized at most once — repeated calls to `getOrCreate` return the cached value
|
|
1681
|
+
* without invoking the generator again.
|
|
1682
|
+
*
|
|
1683
|
+
* @typeParam K - Key type (string, number, or symbol).
|
|
1684
|
+
* @typeParam T - The type of values produced by the generator.
|
|
1685
|
+
*
|
|
1686
|
+
* @example
|
|
1687
|
+
* ```ts
|
|
1688
|
+
* const dict = LazyDictionary.of( ( key: string ) => key.toUpperCase() );
|
|
1689
|
+
* dict.getOrCreate( 'hello' ); // → 'HELLO'
|
|
1690
|
+
* dict.getOrCreate( 'hello' ); // → 'HELLO' (cached, generator not called again)
|
|
1691
|
+
* dict.hasKey( 'hello' ); // → true
|
|
1692
|
+
* dict.getEntries(); // → [ [ 'hello', 'HELLO' ] ]
|
|
1693
|
+
* ```
|
|
1694
|
+
*/
|
|
1658
1695
|
class LazyDictionary {
|
|
1659
1696
|
_generator;
|
|
1660
1697
|
_dictionary = {};
|
|
1661
1698
|
constructor(_generator) {
|
|
1662
1699
|
this._generator = _generator;
|
|
1663
1700
|
}
|
|
1701
|
+
/**
|
|
1702
|
+
* Creates a new {@link LazyDictionary} instance.
|
|
1703
|
+
* @param generator - Factory function that produces a value for a given key.
|
|
1704
|
+
*/
|
|
1664
1705
|
static of(generator) {
|
|
1665
1706
|
return new LazyDictionary(generator);
|
|
1666
1707
|
}
|
|
1667
|
-
|
|
1708
|
+
/**
|
|
1709
|
+
* Returns the cached value for `key`, generating and caching it if this is the first access.
|
|
1710
|
+
* The generator is invoked at most once per key.
|
|
1711
|
+
* @param key - The key to look up or generate.
|
|
1712
|
+
* @returns The value associated with `key`.
|
|
1713
|
+
*/ getOrCreate(key) {
|
|
1668
1714
|
if (key in this._dictionary) {
|
|
1669
1715
|
return this._dictionary[key];
|
|
1670
1716
|
}
|
|
@@ -1672,12 +1718,35 @@ class LazyDictionary {
|
|
|
1672
1718
|
this._dictionary[key] = value;
|
|
1673
1719
|
return value;
|
|
1674
1720
|
}
|
|
1721
|
+
/**
|
|
1722
|
+
* Returns the cached value for `key`, or throws if the key has not been generated yet.
|
|
1723
|
+
* Unlike {@link getOrCreate}, this method does **not** invoke the generator.
|
|
1724
|
+
* @param key - The key to look up.
|
|
1725
|
+
* @param errorMessage - Optional message for the thrown Error.
|
|
1726
|
+
* @throws {Error} If the key has not been generated yet.
|
|
1727
|
+
* @returns The value associated with `key`.
|
|
1728
|
+
*/
|
|
1675
1729
|
getOrThrow(key, errorMessage) {
|
|
1676
1730
|
if (key in this._dictionary) {
|
|
1677
1731
|
return this._dictionary[key];
|
|
1678
1732
|
}
|
|
1679
1733
|
throw new Error(errorMessage);
|
|
1680
1734
|
}
|
|
1735
|
+
/**
|
|
1736
|
+
* Returns whether a value has been generated for the given key.
|
|
1737
|
+
* @param key - The key to check.
|
|
1738
|
+
*/
|
|
1739
|
+
hasKey(key) {
|
|
1740
|
+
return key in this._dictionary;
|
|
1741
|
+
}
|
|
1742
|
+
/**
|
|
1743
|
+
* Returns all key-value pairs that have been generated so far.
|
|
1744
|
+
* Does not invoke the generator for any key.
|
|
1745
|
+
* @returns An array of `[key, value]` tuples.
|
|
1746
|
+
*/
|
|
1747
|
+
getEntries() {
|
|
1748
|
+
return Object.entries(this._dictionary);
|
|
1749
|
+
}
|
|
1681
1750
|
}
|
|
1682
1751
|
|
|
1683
1752
|
class TimeUnit {
|