@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 CHANGED
@@ -153,9 +153,27 @@ class Semaphore {
153
153
  this._inProgress -= 1;
154
154
  }
155
155
  }
156
- async execute(fn, cooldown = TimeDuration.ZERO) {
157
- return this._awaitSlot().then(fn).finally(() => { void cooldown.delay(() => this._releaseSlot()); });
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
  }
@@ -3883,12 +3901,12 @@ function isUpgradable(obj) {
3883
3901
  *
3884
3902
  * // Step 2: Build the upgrader with type-safe transitions
3885
3903
  * const upgrader = DataUpgraderBuilder.start<V1>()
3886
- * .addTransition<V1, V2>(1, 2, async (data) => ({
3904
+ * .addTransition<V2>(2, async (data) => ({
3887
3905
  * ...data,
3888
3906
  * age: 0,
3889
3907
  * $version: 2
3890
3908
  * }))
3891
- * .addTransition<V2, V3>(2, 3, async (data) => ({
3909
+ * .addTransition<V3>(3, async (data) => ({
3892
3910
  * ...data,
3893
3911
  * email: "",
3894
3912
  * $version: 3
@@ -3900,7 +3918,7 @@ function isUpgradable(obj) {
3900
3918
  * email: "",
3901
3919
  * $version: 3
3902
3920
  * }))
3903
- * .build<V3>(3);
3921
+ * .build(3);
3904
3922
  *
3905
3923
  * // Step 3: Use the upgrader
3906
3924
  * async function loadData(json: string): Promise<V3> {
@@ -3915,32 +3933,34 @@ function isUpgradable(obj) {
3915
3933
  * ```
3916
3934
  */
3917
3935
  class DataUpgraderBuilder {
3936
+ latestVersion;
3918
3937
  transitions;
3919
- constructor(transitions = {}) {
3938
+ constructor(latestVersion, transitions = {}) {
3939
+ this.latestVersion = latestVersion;
3920
3940
  this.transitions = transitions;
3921
3941
  }
3922
3942
  /** Starts building a new DataUpgrader from the lowest version. */
3923
3943
  static start() {
3924
- return new DataUpgraderBuilder();
3944
+ return new DataUpgraderBuilder(1);
3925
3945
  }
3926
3946
  /**
3927
- * Adds a sequential transition from the current version to the next version.
3928
- * Prevents backward transitions and duplicates at compile-time.
3947
+ * Adds a sequential transition from the current latest version to a new version.
3948
+ * Only the new version type needs to be specified.
3929
3949
  *
3930
3950
  * @example
3931
3951
  * ```typescript
3932
- * builder.addTransition<V1, V2>(1, 2, async (d) => ({ ...d, extra: 0, $version: 2 }))
3952
+ * builder.addTransition<V2>(2, async (d) => ({ ...d, extra: 0, $version: 2 }))
3933
3953
  * ```
3934
3954
  */
3935
- addTransition(fromVersion, toVersion, apply) {
3955
+ addTransition(toVersion, apply) {
3936
3956
  const newTransitions = {
3937
3957
  ...this.transitions,
3938
3958
  [toVersion]: {
3939
3959
  ...(this.transitions[toVersion] ?? {}),
3940
- [fromVersion]: { from: fromVersion, to: toVersion, apply: async (d) => apply(d) }
3960
+ [this.latestVersion]: { from: this.latestVersion, to: toVersion, apply: async (d) => apply(d) }
3941
3961
  }
3942
3962
  };
3943
- const builder = new DataUpgraderBuilder(newTransitions);
3963
+ const builder = new DataUpgraderBuilder(toVersion, newTransitions);
3944
3964
  return builder;
3945
3965
  }
3946
3966
  /**
@@ -3960,16 +3980,15 @@ class DataUpgraderBuilder {
3960
3980
  [fromVersion]: { from: fromVersion, to: toVersion, apply: async (d) => apply(d) }
3961
3981
  }
3962
3982
  };
3963
- const builder = new DataUpgraderBuilder(newTransitions);
3983
+ const builder = new DataUpgraderBuilder(this.latestVersion, newTransitions);
3964
3984
  return builder;
3965
3985
  }
3966
3986
  /**
3967
- * Builds the DataUpgrader with the specified latest version.
3968
- * The latest version must be in the accumulated union type.
3987
+ * Builds the DataUpgrader with the current latest version.
3969
3988
  *
3970
3989
  * @example
3971
3990
  * ```typescript
3972
- * const upgrader = builder.build<V3>(3);
3991
+ * const upgrader = builder.build(3);
3973
3992
  * ```
3974
3993
  */
3975
3994
  build(latestVersion) {