@effect-atom/atom 0.1.14 → 0.1.16

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/src/Result.ts CHANGED
@@ -288,12 +288,28 @@ export const failWithPrevious = <A, E>(
288
288
  * @since 1.0.0
289
289
  * @category constructors
290
290
  */
291
- export const waiting = <R extends Result<any, any>>(self: R): R => {
291
+ export const waiting = <R extends Result<any, any>>(self: R, options?: {
292
+ readonly touch?: boolean | undefined
293
+ }): R => {
292
294
  if (self.waiting) {
293
- return self
295
+ return options?.touch ? touch(self) : self
294
296
  }
295
297
  const result = Object.assign(Object.create(ResultProto), self)
296
298
  result.waiting = true
299
+ if (options?.touch && isSuccess(result)) {
300
+ ;(result as any).timestamp = Date.now()
301
+ }
302
+ return result
303
+ }
304
+
305
+ /**
306
+ * @since 1.0.0
307
+ * @category combinators
308
+ */
309
+ export const touch = <A extends Result<any, any>>(result: A): A => {
310
+ if (isSuccess(result)) {
311
+ return success(result.value, { waiting: result.waiting }) as A
312
+ }
297
313
  return result
298
314
  }
299
315
 
@@ -477,16 +477,23 @@ function childrenAreActive(children: Array<Node<any>>): boolean {
477
477
  if (children.length === 0) {
478
478
  return false
479
479
  }
480
- for (let i = 0, len = children.length; i < len; i++) {
481
- const child = children[i]
482
- if (!child.atom.lazy || child.listeners.length > 0) {
483
- return true
484
- }
485
- }
486
- for (let i = 0, len = children.length; i < len; i++) {
487
- if (childrenAreActive(children[i].children)) {
488
- return true
480
+ let current: Array<Node<any>> | undefined = children
481
+ let stack: Array<Array<Node<any>>> | undefined
482
+ let stackIndex = 0
483
+ while (current !== undefined) {
484
+ for (let i = 0, len = current.length; i < len; i++) {
485
+ const child = current[i]
486
+ if (!child.atom.lazy || child.listeners.length > 0) {
487
+ return true
488
+ } else if (child.children.length > 0) {
489
+ if (stack === undefined) {
490
+ stack = [child.children]
491
+ } else {
492
+ stack.push(child.children)
493
+ }
494
+ }
489
495
  }
496
+ current = stack?.[stackIndex++]
490
497
  }
491
498
  return false
492
499
  }