| 612 | ) |
| 613 | } |
| 614 | static changes(args: unknown) { |
| 615 | return Stream.fromChannel( |
| 616 | Channel.fromTransform((_, scope) => |
| 617 | Effect.uninterruptible( |
| 618 | withCache((cache, context) => |
| 619 | Effect.suspend(() => { |
| 620 | // Its first value would never come. |
| 621 | if (cache.offServer(def)) return Effect.interrupt |
| 622 | type Item = AsyncResult.AsyncResult<any, any> |
| 623 | const pending: Array<Item> = [] |
| 624 | let failed: Cause.Cause<any> | undefined |
| 625 | let resume: |
| 626 | | (( |
| 627 | effect: Effect.Effect<readonly [Item, ...Array<Item>], any> |
| 628 | ) => void) |
| 629 | | undefined |
| 630 | let last: Item | undefined |
| 631 | const wake = () => { |
| 632 | if (resume === undefined) return |
| 633 | const r = resume |
| 634 | if (pending.length > 0) { |
| 635 | resume = undefined |
| 636 | r( |
| 637 | Effect.succeed( |
| 638 | pending.splice(0) as [Item, ...Array<Item>] |
| 639 | ) |
| 640 | ) |
| 641 | } else if (failed !== undefined) { |
| 642 | resume = undefined |
| 643 | r(Effect.failCause(failed)) |
| 644 | } |
| 645 | } |
| 646 | const offer = (result: Item): void => { |
| 647 | if (failed !== undefined) return |
| 648 | if ( |
| 649 | last !== undefined && queryCore.sameResult(result, last) |
| 650 | ) return |
| 651 | last = result |
| 652 | pending.push(result) |
| 653 | wake() |
| 654 | } |
| 655 | const handle = cache.watch( |
| 656 | def, |
| 657 | args, |
| 658 | fetcherOf(context, Base), |
| 659 | offer, |
| 660 | (cause) => { |
| 661 | if (failed !== undefined) return |
| 662 | failed = cause |
| 663 | wake() |
| 664 | } |
| 665 | ) |
| 666 | const current = handle.current() |
| 667 | if (current._tag !== "Initial") offer(current) |
| 668 | const pull = Effect.callback< |
| 669 | readonly [Item, ...Array<Item>], |
| 670 | any |
| 671 | >((r) => { |