@zelgadis87/utils-core 5.4.5 → 5.4.7
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 +37 -18
- package/.rollup/index.cjs.map +1 -1
- package/.rollup/index.d.ts +26 -16
- package/.rollup/index.mjs +37 -18
- package/.rollup/index.mjs.map +1 -1
- package/.rollup/tsconfig.tsbuildinfo +1 -1
- package/CHANGELOG.md +12 -0
- package/package.json +2 -1
- package/src/async/Semaphore.ts +25 -3
- package/src/upgrade/DataUpgraderBuilder.ts +25 -26
- package/src/utils/css.ts +1 -1
- package/src/utils/operations.ts +1 -1
package/.rollup/index.d.ts
CHANGED
|
@@ -1020,7 +1020,7 @@ type TCssSelectorDeclarationRulesDictionary = {
|
|
|
1020
1020
|
[selector: string]: TCssDeclarationRulesDictionary | TCssSelectorDeclarationRulesDictionary;
|
|
1021
1021
|
};
|
|
1022
1022
|
declare function cssDeclarationRulesDictionaryToCss(syleDeclarationRulesForSelectorsProduceable: TProduceable<TCssSelectorDeclarationRulesDictionary>, indent?: number): string;
|
|
1023
|
-
declare function transformCssDictionary(dict: TCssSelectorDeclarationRulesDictionary, transformer: (key: TCssGenericDeclarationKey, value: string) => string): TCssSelectorDeclarationRulesDictionary;
|
|
1023
|
+
declare function transformCssDictionary(dict: TCssDeclarationRulesDictionary | TCssSelectorDeclarationRulesDictionary, transformer: (key: TCssGenericDeclarationKey, value: string) => string): TCssSelectorDeclarationRulesDictionary;
|
|
1024
1024
|
type TProduceable<T> = T | TProducer<T>;
|
|
1025
1025
|
|
|
1026
1026
|
/**
|
|
@@ -1108,7 +1108,7 @@ type TOperationAggregateFlattenedError<E> = E extends OperationAggregateError<in
|
|
|
1108
1108
|
* @param results - Array of operation results to combine
|
|
1109
1109
|
* @returns A single operation with either all data or all aggregated errors (flattened)
|
|
1110
1110
|
*/
|
|
1111
|
-
declare function combine<T, E = Error>(results: TReadableArray<TOperation<T, E
|
|
1111
|
+
declare function combine<T, E = Error>(results: TReadableArray<TOperation<T, E | OperationAggregateError<E>>>): TOperation<T[], OperationAggregateError<TOperationAggregateFlattenedError<E>>>;
|
|
1112
1112
|
declare const Operation: {
|
|
1113
1113
|
ok: <const T>(data: T) => {
|
|
1114
1114
|
readonly success: true;
|
|
@@ -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;
|
|
@@ -1623,12 +1631,12 @@ type TAddTransition<T extends readonly TTransitionRecord[], From extends number,
|
|
|
1623
1631
|
*
|
|
1624
1632
|
* // Step 2: Build the upgrader with type-safe transitions
|
|
1625
1633
|
* const upgrader = DataUpgraderBuilder.start<V1>()
|
|
1626
|
-
* .addTransition<
|
|
1634
|
+
* .addTransition<V2>(2, async (data) => ({
|
|
1627
1635
|
* ...data,
|
|
1628
1636
|
* age: 0,
|
|
1629
1637
|
* $version: 2
|
|
1630
1638
|
* }))
|
|
1631
|
-
* .addTransition<
|
|
1639
|
+
* .addTransition<V3>(3, async (data) => ({
|
|
1632
1640
|
* ...data,
|
|
1633
1641
|
* email: "",
|
|
1634
1642
|
* $version: 3
|
|
@@ -1640,7 +1648,7 @@ type TAddTransition<T extends readonly TTransitionRecord[], From extends number,
|
|
|
1640
1648
|
* email: "",
|
|
1641
1649
|
* $version: 3
|
|
1642
1650
|
* }))
|
|
1643
|
-
* .build
|
|
1651
|
+
* .build(3);
|
|
1644
1652
|
*
|
|
1645
1653
|
* // Step 3: Use the upgrader
|
|
1646
1654
|
* async function loadData(json: string): Promise<V3> {
|
|
@@ -1654,21 +1662,22 @@ type TAddTransition<T extends readonly TTransitionRecord[], From extends number,
|
|
|
1654
1662
|
* const v3 = await loadData('{"name":"Carol","age":30,"email":"c@example.com","$version":3}'); // → V3
|
|
1655
1663
|
* ```
|
|
1656
1664
|
*/
|
|
1657
|
-
declare class DataUpgraderBuilder<VLowest extends TUpgradable, VUnion extends TUpgradable = VLowest, TTransitions extends readonly TTransitionRecord[] = readonly []> {
|
|
1665
|
+
declare class DataUpgraderBuilder<VLowest extends TUpgradable, VUnion extends TUpgradable = VLowest, VLatest extends TUpgradable = VLowest, TTransitions extends readonly TTransitionRecord[] = readonly []> {
|
|
1666
|
+
private latestVersion;
|
|
1658
1667
|
private transitions;
|
|
1659
1668
|
private constructor();
|
|
1660
1669
|
/** Starts building a new DataUpgrader from the lowest version. */
|
|
1661
|
-
static start<V1 extends TUpgradable>(): DataUpgraderBuilder<V1, V1, readonly []>;
|
|
1670
|
+
static start<V1 extends TUpgradable>(): DataUpgraderBuilder<V1, V1, V1, readonly []>;
|
|
1662
1671
|
/**
|
|
1663
|
-
* Adds a sequential transition from the current version to
|
|
1664
|
-
*
|
|
1672
|
+
* Adds a sequential transition from the current latest version to a new version.
|
|
1673
|
+
* Only the new version type needs to be specified.
|
|
1665
1674
|
*
|
|
1666
1675
|
* @example
|
|
1667
1676
|
* ```typescript
|
|
1668
|
-
* builder.addTransition<
|
|
1677
|
+
* builder.addTransition<V2>(2, async (d) => ({ ...d, extra: 0, $version: 2 }))
|
|
1669
1678
|
* ```
|
|
1670
1679
|
*/
|
|
1671
|
-
addTransition<VNext extends TUpgradable,
|
|
1680
|
+
addTransition<VNext extends TUpgradable, NewUnion extends TUpgradable = VUnion | VNext, LatestNumber extends number = VLatest["$version"], NextNumber extends number = VNext["$version"], NewTransitions extends readonly TTransitionRecord[] = TAddTransition<TTransitions, LatestNumber, NextNumber>>(toVersion: NextNumber, apply: (data: VLatest) => Promise<VNext> | VNext): DataUpgraderBuilder<VLowest, NewUnion, VNext, NewTransitions>;
|
|
1672
1681
|
/**
|
|
1673
1682
|
* Adds a shortcut transition between arbitrary versions.
|
|
1674
1683
|
* Both versions must already be in the accumulated union type.
|
|
@@ -1678,17 +1687,18 @@ declare class DataUpgraderBuilder<VLowest extends TUpgradable, VUnion extends TU
|
|
|
1678
1687
|
* builder.addShortcut<V1, V3>(1, 3, async (d) => ({ ...d, extra: 0, flag: false, $version: 3 }))
|
|
1679
1688
|
* ```
|
|
1680
1689
|
*/
|
|
1681
|
-
addShortcut<VFrom extends TUpgradable & VUnion, VTo extends TUpgradable & VUnion, From extends number = VFrom["$version"], To extends number = VTo["$version"], NewTransitions extends readonly TTransitionRecord[] = TAddTransition<TTransitions, From, To>>(fromVersion: From, toVersion: To, apply: (data: VFrom) => Promise<VTo> | VTo): DataUpgraderBuilder<VLowest, VUnion, NewTransitions>;
|
|
1690
|
+
addShortcut<VFrom extends TUpgradable & VUnion, VTo extends TUpgradable & VUnion, From extends number = VFrom["$version"], To extends number = VTo["$version"], NewTransitions extends readonly TTransitionRecord[] = TAddTransition<TTransitions, From, To>>(fromVersion: From, toVersion: To, apply: (data: VFrom) => Promise<VTo> | VTo): DataUpgraderBuilder<VLowest, VUnion, VLatest, NewTransitions>;
|
|
1682
1691
|
/**
|
|
1683
|
-
* Builds the DataUpgrader with the
|
|
1684
|
-
* The latest version must be in the accumulated union type.
|
|
1692
|
+
* Builds the DataUpgrader with the current latest version.
|
|
1685
1693
|
*
|
|
1686
1694
|
* @example
|
|
1687
1695
|
* ```typescript
|
|
1688
|
-
* const upgrader = builder.build
|
|
1696
|
+
* const upgrader = builder.build(3);
|
|
1689
1697
|
* ```
|
|
1690
1698
|
*/
|
|
1691
|
-
build
|
|
1699
|
+
build(latestVersion: VLatest["$version"]): DataUpgrader<VUnion, Extract<VUnion, {
|
|
1700
|
+
$version: VLatest["$version"];
|
|
1701
|
+
}>>;
|
|
1692
1702
|
/** @internal */
|
|
1693
1703
|
getTransitions(): Record<number, Record<number, {
|
|
1694
1704
|
from: number;
|
package/.rollup/index.mjs
CHANGED
|
@@ -151,9 +151,27 @@ class Semaphore {
|
|
|
151
151
|
this._inProgress -= 1;
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
|
-
async
|
|
155
|
-
|
|
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
|
}
|
|
@@ -3881,12 +3899,12 @@ function isUpgradable(obj) {
|
|
|
3881
3899
|
*
|
|
3882
3900
|
* // Step 2: Build the upgrader with type-safe transitions
|
|
3883
3901
|
* const upgrader = DataUpgraderBuilder.start<V1>()
|
|
3884
|
-
* .addTransition<
|
|
3902
|
+
* .addTransition<V2>(2, async (data) => ({
|
|
3885
3903
|
* ...data,
|
|
3886
3904
|
* age: 0,
|
|
3887
3905
|
* $version: 2
|
|
3888
3906
|
* }))
|
|
3889
|
-
* .addTransition<
|
|
3907
|
+
* .addTransition<V3>(3, async (data) => ({
|
|
3890
3908
|
* ...data,
|
|
3891
3909
|
* email: "",
|
|
3892
3910
|
* $version: 3
|
|
@@ -3898,7 +3916,7 @@ function isUpgradable(obj) {
|
|
|
3898
3916
|
* email: "",
|
|
3899
3917
|
* $version: 3
|
|
3900
3918
|
* }))
|
|
3901
|
-
* .build
|
|
3919
|
+
* .build(3);
|
|
3902
3920
|
*
|
|
3903
3921
|
* // Step 3: Use the upgrader
|
|
3904
3922
|
* async function loadData(json: string): Promise<V3> {
|
|
@@ -3913,32 +3931,34 @@ function isUpgradable(obj) {
|
|
|
3913
3931
|
* ```
|
|
3914
3932
|
*/
|
|
3915
3933
|
class DataUpgraderBuilder {
|
|
3934
|
+
latestVersion;
|
|
3916
3935
|
transitions;
|
|
3917
|
-
constructor(transitions = {}) {
|
|
3936
|
+
constructor(latestVersion, transitions = {}) {
|
|
3937
|
+
this.latestVersion = latestVersion;
|
|
3918
3938
|
this.transitions = transitions;
|
|
3919
3939
|
}
|
|
3920
3940
|
/** Starts building a new DataUpgrader from the lowest version. */
|
|
3921
3941
|
static start() {
|
|
3922
|
-
return new DataUpgraderBuilder();
|
|
3942
|
+
return new DataUpgraderBuilder(1);
|
|
3923
3943
|
}
|
|
3924
3944
|
/**
|
|
3925
|
-
* Adds a sequential transition from the current version to
|
|
3926
|
-
*
|
|
3945
|
+
* Adds a sequential transition from the current latest version to a new version.
|
|
3946
|
+
* Only the new version type needs to be specified.
|
|
3927
3947
|
*
|
|
3928
3948
|
* @example
|
|
3929
3949
|
* ```typescript
|
|
3930
|
-
* builder.addTransition<
|
|
3950
|
+
* builder.addTransition<V2>(2, async (d) => ({ ...d, extra: 0, $version: 2 }))
|
|
3931
3951
|
* ```
|
|
3932
3952
|
*/
|
|
3933
|
-
addTransition(
|
|
3953
|
+
addTransition(toVersion, apply) {
|
|
3934
3954
|
const newTransitions = {
|
|
3935
3955
|
...this.transitions,
|
|
3936
3956
|
[toVersion]: {
|
|
3937
3957
|
...(this.transitions[toVersion] ?? {}),
|
|
3938
|
-
[
|
|
3958
|
+
[this.latestVersion]: { from: this.latestVersion, to: toVersion, apply: async (d) => apply(d) }
|
|
3939
3959
|
}
|
|
3940
3960
|
};
|
|
3941
|
-
const builder = new DataUpgraderBuilder(newTransitions);
|
|
3961
|
+
const builder = new DataUpgraderBuilder(toVersion, newTransitions);
|
|
3942
3962
|
return builder;
|
|
3943
3963
|
}
|
|
3944
3964
|
/**
|
|
@@ -3958,16 +3978,15 @@ class DataUpgraderBuilder {
|
|
|
3958
3978
|
[fromVersion]: { from: fromVersion, to: toVersion, apply: async (d) => apply(d) }
|
|
3959
3979
|
}
|
|
3960
3980
|
};
|
|
3961
|
-
const builder = new DataUpgraderBuilder(newTransitions);
|
|
3981
|
+
const builder = new DataUpgraderBuilder(this.latestVersion, newTransitions);
|
|
3962
3982
|
return builder;
|
|
3963
3983
|
}
|
|
3964
3984
|
/**
|
|
3965
|
-
* Builds the DataUpgrader with the
|
|
3966
|
-
* The latest version must be in the accumulated union type.
|
|
3985
|
+
* Builds the DataUpgrader with the current latest version.
|
|
3967
3986
|
*
|
|
3968
3987
|
* @example
|
|
3969
3988
|
* ```typescript
|
|
3970
|
-
* const upgrader = builder.build
|
|
3989
|
+
* const upgrader = builder.build(3);
|
|
3971
3990
|
* ```
|
|
3972
3991
|
*/
|
|
3973
3992
|
build(latestVersion) {
|