@vielzeug/toolkit 1.0.14 → 1.1.2

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.
Files changed (70) hide show
  1. package/dist/async/attempt.cjs.map +1 -0
  2. package/dist/async/attempt.js.map +1 -0
  3. package/dist/async/defer.cjs +2 -0
  4. package/dist/async/defer.cjs.map +1 -0
  5. package/dist/async/defer.js +10 -0
  6. package/dist/async/defer.js.map +1 -0
  7. package/dist/async/delay.cjs.map +1 -0
  8. package/dist/async/delay.js.map +1 -0
  9. package/dist/async/parallel.cjs.map +1 -0
  10. package/dist/async/parallel.js.map +1 -0
  11. package/dist/async/pool.cjs +2 -0
  12. package/dist/async/pool.cjs.map +1 -0
  13. package/dist/async/pool.js +22 -0
  14. package/dist/async/pool.js.map +1 -0
  15. package/dist/async/predict.cjs.map +1 -0
  16. package/dist/async/predict.js.map +1 -0
  17. package/dist/async/queue.cjs +2 -0
  18. package/dist/async/queue.cjs.map +1 -0
  19. package/dist/async/queue.js +57 -0
  20. package/dist/async/queue.js.map +1 -0
  21. package/dist/async/race.cjs +2 -0
  22. package/dist/async/race.cjs.map +1 -0
  23. package/dist/async/race.js +8 -0
  24. package/dist/async/race.js.map +1 -0
  25. package/dist/async/retry.cjs.map +1 -0
  26. package/dist/{function → async}/retry.js +4 -4
  27. package/dist/async/retry.js.map +1 -0
  28. package/dist/async/sleep.cjs +2 -0
  29. package/dist/async/sleep.cjs.map +1 -0
  30. package/dist/{function → async}/sleep.js +1 -1
  31. package/dist/async/sleep.js.map +1 -0
  32. package/dist/async/waitFor.cjs +2 -0
  33. package/dist/async/waitFor.cjs.map +1 -0
  34. package/dist/async/waitFor.js +37 -0
  35. package/dist/async/waitFor.js.map +1 -0
  36. package/dist/index.cjs +1 -1
  37. package/dist/index.d.ts +175 -0
  38. package/dist/index.js +242 -230
  39. package/dist/index.js.map +1 -1
  40. package/dist/logit/dist/logit.cjs +1 -1
  41. package/dist/logit/dist/logit.cjs.map +1 -1
  42. package/dist/logit/dist/logit.js +147 -64
  43. package/dist/logit/dist/logit.js.map +1 -1
  44. package/dist/object/cache.cjs +2 -0
  45. package/dist/object/cache.cjs.map +1 -0
  46. package/dist/object/cache.js +63 -0
  47. package/dist/object/cache.js.map +1 -0
  48. package/package.json +1 -1
  49. package/dist/function/attempt.cjs.map +0 -1
  50. package/dist/function/attempt.js.map +0 -1
  51. package/dist/function/delay.cjs.map +0 -1
  52. package/dist/function/delay.js.map +0 -1
  53. package/dist/function/parallel.cjs.map +0 -1
  54. package/dist/function/parallel.js.map +0 -1
  55. package/dist/function/predict.cjs.map +0 -1
  56. package/dist/function/predict.js.map +0 -1
  57. package/dist/function/retry.cjs.map +0 -1
  58. package/dist/function/retry.js.map +0 -1
  59. package/dist/function/sleep.cjs +0 -2
  60. package/dist/function/sleep.cjs.map +0 -1
  61. package/dist/function/sleep.js.map +0 -1
  62. /package/dist/{function → async}/attempt.cjs +0 -0
  63. /package/dist/{function → async}/attempt.js +0 -0
  64. /package/dist/{function → async}/delay.cjs +0 -0
  65. /package/dist/{function → async}/delay.js +0 -0
  66. /package/dist/{function → async}/parallel.cjs +0 -0
  67. /package/dist/{function → async}/parallel.js +0 -0
  68. /package/dist/{function → async}/predict.cjs +0 -0
  69. /package/dist/{function → async}/predict.js +0 -0
  70. /package/dist/{function → async}/retry.cjs +0 -0
package/dist/index.d.ts CHANGED
@@ -260,6 +260,35 @@ export declare function average<T>(array: T[], callback?: (item: T) => number |
260
260
  */
261
261
  export declare function boil<T>(array: readonly T[], callback: (a: T, b: T) => T): T | undefined;
262
262
 
263
+ /**
264
+ * Creates a generic key-value cache with automatic garbage collection and observer support.
265
+ *
266
+ * @example
267
+ * ```ts
268
+ * const myCache = cache<string>();
269
+ * myCache.set(['user', 1], 'John Doe');
270
+ * const value = myCache.get(['user', 1]); // 'John Doe'
271
+ * myCache.scheduleGc(['user', 1], 5000); // Auto-delete after 5s
272
+ * ```
273
+ *
274
+ * @template T - The type of values stored in the cache.
275
+ *
276
+ * @returns A cache instance with get, set, delete, clear, and GC methods.
277
+ */
278
+ export declare function cache<T>(): {
279
+ clear: () => void;
280
+ delete: (key: readonly unknown[]) => boolean;
281
+ get: (key: readonly unknown[]) => T | undefined;
282
+ getMeta: (key: readonly unknown[]) => Record<string, unknown> | undefined;
283
+ getMetaByHash: (keyHash: string) => Record<string, unknown> | undefined;
284
+ hash: (key: readonly unknown[]) => string;
285
+ listMetaHashes: () => string[];
286
+ scheduleGc: (key: readonly unknown[], delayMs: number) => void;
287
+ set: (key: readonly unknown[], value: T) => void;
288
+ setMeta: (key: readonly unknown[], meta: Record<string, unknown>) => void;
289
+ size: () => number;
290
+ };
291
+
263
292
  /** biome-ignore-all lint/suspicious/noExplicitAny: - */
264
293
  export declare type Callback<T = any, R = any> = (value: T, index: number, array: T[]) => R;
265
294
 
@@ -519,6 +548,29 @@ declare type DeepMerge<T, U> = T extends Obj ? U extends Obj ? {
519
548
  [K in keyof T | keyof U]: K extends keyof T ? K extends keyof U ? DeepMerge<T[K], U[K]> : T[K] : K extends keyof U ? U[K] : never;
520
549
  } : U : U;
521
550
 
551
+ /**
552
+ * Creates a deferred promise with resolve and reject methods exposed.
553
+ * Useful for creating promises that are resolved/rejected externally.
554
+ *
555
+ * @example
556
+ * ```ts
557
+ * const deferred = defer<string>();
558
+ *
559
+ * setTimeout(() => {
560
+ * deferred.resolve('Done!');
561
+ * }, 1000);
562
+ *
563
+ * const result = await deferred.promise; // 'Done!'
564
+ * ```
565
+ *
566
+ * @returns Object with promise and resolve/reject methods
567
+ */
568
+ export declare function defer<T = void>(): {
569
+ promise: Promise<T>;
570
+ resolve: (value: T | PromiseLike<T>) => void;
571
+ reject: (reason?: unknown) => void;
572
+ };
573
+
522
574
  /**
523
575
  * Delays the execution of a function by a specified amount of time.
524
576
  *
@@ -1898,6 +1950,27 @@ export declare function pipe<T extends FnDynamic[]>(...fns: T): PipeReturn<T>;
1898
1950
 
1899
1951
  declare type PipeReturn<T extends FnDynamic[]> = (...args: FirstParameters<T>) => LastReturnType<T> extends Promise<any> ? Promise<Awaited<LastReturnType<T>>> : LastReturnType<T>;
1900
1952
 
1953
+ /**
1954
+ * Creates a promise pool that limits the number of concurrent promises.
1955
+ * Useful for rate limiting API calls or controlling resource usage.
1956
+ *
1957
+ * @example
1958
+ * ```ts
1959
+ * const requestPool = pool(3);
1960
+ *
1961
+ * const results = await Promise.all([
1962
+ * requestPool(() => fetch('/api/1')),
1963
+ * requestPool(() => fetch('/api/2')),
1964
+ * requestPool(() => fetch('/api/3')),
1965
+ * requestPool(() => fetch('/api/4')), // Will wait for one of the above to finish
1966
+ * ]);
1967
+ * ```
1968
+ *
1969
+ * @param limit - Maximum number of concurrent promises
1970
+ * @returns Function that accepts a promise-returning function and executes it when a slot is available
1971
+ */
1972
+ export declare function pool(limit: number): <T>(fn: () => Promise<T>) => Promise<T>;
1973
+
1901
1974
  export declare type Predicate<T> = (value: T, index: number, array: readonly T[]) => boolean;
1902
1975
 
1903
1976
  /**
@@ -1976,6 +2049,74 @@ declare type ProxyOptions<T> = {
1976
2049
  */
1977
2050
  export declare function prune<T>(value: T): T | undefined;
1978
2051
 
2052
+ /**
2053
+ * Creates a promise queue that processes promises sequentially with optional concurrency limit.
2054
+ *
2055
+ * @example
2056
+ * ```ts
2057
+ * const requestQueue = queue({ concurrency: 2 });
2058
+ *
2059
+ * requestQueue.add(() => fetch('/api/1'));
2060
+ * requestQueue.add(() => fetch('/api/2'));
2061
+ * requestQueue.add(() => fetch('/api/3'));
2062
+ *
2063
+ * await requestQueue.onIdle(); // Wait for all tasks to complete
2064
+ * ```
2065
+ *
2066
+ * @param options - Queue configuration
2067
+ * @param options.concurrency - Maximum number of concurrent promises (default: 1)
2068
+ * @returns Queue instance with add, onIdle, and clear methods
2069
+ */
2070
+ export declare function queue(options?: {
2071
+ concurrency?: number;
2072
+ }): {
2073
+ /**
2074
+ * Adds a promise-returning function to the queue
2075
+ */
2076
+ add: <T>(fn: () => Promise<T>) => Promise<T>;
2077
+ /**
2078
+ * Clears all pending tasks from the queue
2079
+ */
2080
+ clear: () => void;
2081
+ /**
2082
+ * Returns a promise that resolves when the queue becomes idle
2083
+ */
2084
+ onIdle: () => Promise<void>;
2085
+ /**
2086
+ * Returns the number of currently active promises
2087
+ */
2088
+ readonly pending: number;
2089
+ /**
2090
+ * Returns the current size of the queue
2091
+ */
2092
+ readonly size: number;
2093
+ };
2094
+
2095
+ /**
2096
+ * Race multiple promises but with a guaranteed minimum delay.
2097
+ * Useful for showing loading states for at least a minimum duration.
2098
+ *
2099
+ * @example
2100
+ * ```ts
2101
+ * // Show loading spinner for at least 500ms
2102
+ * const result = await race(
2103
+ * fetchData(),
2104
+ * 500
2105
+ * );
2106
+ *
2107
+ * // With multiple promises
2108
+ * const result = await race(
2109
+ * [fetch('/api/1'), fetch('/api/2')],
2110
+ * 1000
2111
+ * );
2112
+ * ```
2113
+ *
2114
+ * @param promises - Single promise or array of promises to race
2115
+ * @param minDelay - Minimum delay in milliseconds before resolving
2116
+ * @returns Promise that resolves with the first result after the minimum delay
2117
+ */
2118
+ export declare function race<T>(promises: Promise<T> | Promise<T>[], minDelay: number): Promise<T>;
2119
+
1979
2120
  /**
1980
2121
  * Generates a random integer between two values, inclusive.
1981
2122
  *
@@ -2621,6 +2762,40 @@ export declare function uuid(): string;
2621
2762
  */
2622
2763
  export declare function values<T extends Obj, K extends keyof T>(item: T): T[K][];
2623
2764
 
2765
+ /**
2766
+ * Waits for a condition to become true by polling.
2767
+ * Useful for waiting for DOM elements, API states, or other conditions.
2768
+ *
2769
+ * @example
2770
+ * ```ts
2771
+ * // Wait for an element to appear
2772
+ * await waitFor(() => document.querySelector('#myElement') !== null);
2773
+ *
2774
+ * // Wait for API to be ready
2775
+ * await waitFor(
2776
+ * async () => {
2777
+ * const res = await fetch('/api/health');
2778
+ * return res.ok;
2779
+ * },
2780
+ * { timeout: 30000, interval: 1000 }
2781
+ * );
2782
+ * ```
2783
+ *
2784
+ * @param condition - Function that returns true when condition is met
2785
+ * @param options - Configuration options
2786
+ * @param options.timeout - Maximum time to wait in ms (default: 5000)
2787
+ * @param options.interval - Polling interval in ms (default: 100)
2788
+ * @param options.signal - AbortSignal to cancel waiting
2789
+ * @returns Promise that resolves when condition becomes true
2790
+ * @throws {Error} If timeout is reached
2791
+ * @throws {DOMException} If aborted via signal
2792
+ */
2793
+ export declare function waitFor(condition: () => boolean | Promise<boolean>, options?: {
2794
+ timeout?: number;
2795
+ interval?: number;
2796
+ signal?: AbortSignal;
2797
+ }): Promise<void>;
2798
+
2624
2799
  /**
2625
2800
  * Creates a function that runs the provided callback in a web worker with specified dependencies.
2626
2801
  *