@ls-stack/utils 3.52.0 → 3.53.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/dist/timers.cjs CHANGED
@@ -23,9 +23,24 @@ __export(timers_exports, {
23
23
  createDebouncedTimeout: () => createDebouncedTimeout,
24
24
  createInterval: () => createInterval,
25
25
  createTimeout: () => createTimeout,
26
- createWaitUntil: () => createWaitUntil
26
+ createWaitUntil: () => createWaitUntil,
27
+ waitFor: () => waitFor
27
28
  });
28
29
  module.exports = __toCommonJS(timers_exports);
30
+ var import_t_result = require("t-result");
31
+
32
+ // src/promiseUtils.ts
33
+ function defer() {
34
+ let resolve;
35
+ let reject;
36
+ const promise = new Promise((_resolve, _reject) => {
37
+ resolve = _resolve;
38
+ reject = _reject;
39
+ });
40
+ return { resolve, reject, promise };
41
+ }
42
+
43
+ // src/timers.ts
29
44
  function createTimeout(ms, callback) {
30
45
  const timeoutId = setTimeout(callback, ms);
31
46
  let isCleaned = false;
@@ -84,10 +99,75 @@ function createWaitUntil({
84
99
  cleanCheckTimeout?.();
85
100
  };
86
101
  }
102
+ async function waitFor(condition, { polling, timeout }) {
103
+ const { promise, resolve } = defer();
104
+ let timeoutId = null;
105
+ let intervalId = null;
106
+ let rafId = null;
107
+ let isResolved = false;
108
+ function cleanup() {
109
+ if (timeoutId) {
110
+ clearTimeout(timeoutId);
111
+ timeoutId = null;
112
+ }
113
+ if (intervalId) {
114
+ clearInterval(intervalId);
115
+ intervalId = null;
116
+ }
117
+ if (rafId && typeof cancelAnimationFrame !== "undefined") {
118
+ cancelAnimationFrame(rafId);
119
+ rafId = null;
120
+ }
121
+ }
122
+ function resolveWith(result) {
123
+ if (isResolved) return;
124
+ isResolved = true;
125
+ cleanup();
126
+ resolve(result);
127
+ }
128
+ function checkCondition() {
129
+ try {
130
+ if (condition()) {
131
+ resolveWith(import_t_result.Result.ok(void 0));
132
+ return true;
133
+ }
134
+ return false;
135
+ } catch (error) {
136
+ resolveWith(import_t_result.Result.err(new Error(`Condition check failed: ${error}`)));
137
+ return true;
138
+ }
139
+ }
140
+ if (checkCondition()) {
141
+ return promise;
142
+ }
143
+ timeoutId = setTimeout(() => {
144
+ resolveWith(import_t_result.Result.err(new Error(`Timeout of ${timeout}ms exceeded while waiting for condition`)));
145
+ }, timeout);
146
+ if (polling === "raf") {
147
+ let rafCheck2 = function() {
148
+ if (isResolved) return;
149
+ if (!checkCondition()) {
150
+ rafId = requestAnimationFrame(rafCheck2);
151
+ }
152
+ };
153
+ var rafCheck = rafCheck2;
154
+ if (typeof requestAnimationFrame === "undefined") {
155
+ resolveWith(import_t_result.Result.err(new Error("requestAnimationFrame is not available in this environment")));
156
+ return promise;
157
+ }
158
+ rafId = requestAnimationFrame(rafCheck2);
159
+ } else {
160
+ intervalId = setInterval(() => {
161
+ checkCondition();
162
+ }, polling);
163
+ }
164
+ return promise;
165
+ }
87
166
  // Annotate the CommonJS export names for ESM import in node:
88
167
  0 && (module.exports = {
89
168
  createDebouncedTimeout,
90
169
  createInterval,
91
170
  createTimeout,
92
- createWaitUntil
171
+ createWaitUntil,
172
+ waitFor
93
173
  });
package/dist/timers.d.cts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Result } from 't-result';
2
+
1
3
  type CleanupTimer = () => void;
2
4
  /**
3
5
  * Creates a timeout with automatic cleanup capability.
@@ -111,5 +113,9 @@ declare function createWaitUntil<T extends NonNullable<unknown>>({ condition, ma
111
113
  callback: (value: T) => void;
112
114
  checkIntervalMs?: number;
113
115
  }): CleanupTimer;
116
+ declare function waitFor(condition: () => boolean, { polling, timeout }: {
117
+ polling: number | 'raf';
118
+ timeout: number;
119
+ }): Promise<Result<void, Error>>;
114
120
 
115
- export { type CleanupTimer, createDebouncedTimeout, createInterval, createTimeout, createWaitUntil };
121
+ export { type CleanupTimer, createDebouncedTimeout, createInterval, createTimeout, createWaitUntil, waitFor };
package/dist/timers.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Result } from 't-result';
2
+
1
3
  type CleanupTimer = () => void;
2
4
  /**
3
5
  * Creates a timeout with automatic cleanup capability.
@@ -111,5 +113,9 @@ declare function createWaitUntil<T extends NonNullable<unknown>>({ condition, ma
111
113
  callback: (value: T) => void;
112
114
  checkIntervalMs?: number;
113
115
  }): CleanupTimer;
116
+ declare function waitFor(condition: () => boolean, { polling, timeout }: {
117
+ polling: number | 'raf';
118
+ timeout: number;
119
+ }): Promise<Result<void, Error>>;
114
120
 
115
- export { type CleanupTimer, createDebouncedTimeout, createInterval, createTimeout, createWaitUntil };
121
+ export { type CleanupTimer, createDebouncedTimeout, createInterval, createTimeout, createWaitUntil, waitFor };
package/dist/timers.js CHANGED
@@ -1,4 +1,9 @@
1
+ import {
2
+ defer
3
+ } from "./chunk-DFXNVEH6.js";
4
+
1
5
  // src/timers.ts
6
+ import { Result } from "t-result";
2
7
  function createTimeout(ms, callback) {
3
8
  const timeoutId = setTimeout(callback, ms);
4
9
  let isCleaned = false;
@@ -57,9 +62,74 @@ function createWaitUntil({
57
62
  cleanCheckTimeout?.();
58
63
  };
59
64
  }
65
+ async function waitFor(condition, { polling, timeout }) {
66
+ const { promise, resolve } = defer();
67
+ let timeoutId = null;
68
+ let intervalId = null;
69
+ let rafId = null;
70
+ let isResolved = false;
71
+ function cleanup() {
72
+ if (timeoutId) {
73
+ clearTimeout(timeoutId);
74
+ timeoutId = null;
75
+ }
76
+ if (intervalId) {
77
+ clearInterval(intervalId);
78
+ intervalId = null;
79
+ }
80
+ if (rafId && typeof cancelAnimationFrame !== "undefined") {
81
+ cancelAnimationFrame(rafId);
82
+ rafId = null;
83
+ }
84
+ }
85
+ function resolveWith(result) {
86
+ if (isResolved) return;
87
+ isResolved = true;
88
+ cleanup();
89
+ resolve(result);
90
+ }
91
+ function checkCondition() {
92
+ try {
93
+ if (condition()) {
94
+ resolveWith(Result.ok(void 0));
95
+ return true;
96
+ }
97
+ return false;
98
+ } catch (error) {
99
+ resolveWith(Result.err(new Error(`Condition check failed: ${error}`)));
100
+ return true;
101
+ }
102
+ }
103
+ if (checkCondition()) {
104
+ return promise;
105
+ }
106
+ timeoutId = setTimeout(() => {
107
+ resolveWith(Result.err(new Error(`Timeout of ${timeout}ms exceeded while waiting for condition`)));
108
+ }, timeout);
109
+ if (polling === "raf") {
110
+ let rafCheck2 = function() {
111
+ if (isResolved) return;
112
+ if (!checkCondition()) {
113
+ rafId = requestAnimationFrame(rafCheck2);
114
+ }
115
+ };
116
+ var rafCheck = rafCheck2;
117
+ if (typeof requestAnimationFrame === "undefined") {
118
+ resolveWith(Result.err(new Error("requestAnimationFrame is not available in this environment")));
119
+ return promise;
120
+ }
121
+ rafId = requestAnimationFrame(rafCheck2);
122
+ } else {
123
+ intervalId = setInterval(() => {
124
+ checkCondition();
125
+ }, polling);
126
+ }
127
+ return promise;
128
+ }
60
129
  export {
61
130
  createDebouncedTimeout,
62
131
  createInterval,
63
132
  createTimeout,
64
- createWaitUntil
133
+ createWaitUntil,
134
+ waitFor
65
135
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Universal TypeScript utilities for browser and Node.js",
4
- "version": "3.52.0",
4
+ "version": "3.53.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist",