| 1708 | } |
| 1709 | |
| 1710 | class ReplayWindowImpl<A> implements ReplayWindow<A> { |
| 1711 | head: ReplayNode<A> |
| 1712 | index: number |
| 1713 | remaining: number |
| 1714 | constructor(readonly buffer: ReplayBuffer<A>) { |
| 1715 | this.index = buffer.index |
| 1716 | this.remaining = buffer.size |
| 1717 | this.head = buffer.head |
| 1718 | } |
| 1719 | fastForward() { |
| 1720 | while (this.index < this.buffer.index) { |
| 1721 | this.head = this.head.next! |
| 1722 | this.index++ |
| 1723 | } |
| 1724 | } |
| 1725 | take(): A | undefined { |
| 1726 | if (this.remaining === 0) { |
| 1727 | return undefined |
| 1728 | } else if (this.index < this.buffer.index) { |
| 1729 | this.fastForward() |
| 1730 | } |
| 1731 | this.remaining-- |
| 1732 | const value = this.head.value |
| 1733 | this.head = this.head.next! |
| 1734 | return value as A |
| 1735 | } |
| 1736 | takeN(n: number): Chunk.Chunk<A> { |
| 1737 | if (this.remaining === 0) { |
| 1738 | return Chunk.empty() |
| 1739 | } else if (this.index < this.buffer.index) { |
| 1740 | this.fastForward() |
| 1741 | } |
| 1742 | const len = Math.min(n, this.remaining) |
| 1743 | const items = new Array(len) |
| 1744 | for (let i = 0; i < len; i++) { |
| 1745 | const value = this.head.value as A |
| 1746 | this.head = this.head.next! |
| 1747 | items[i] = value |
| 1748 | } |
| 1749 | this.remaining -= len |
| 1750 | return Chunk.unsafeFromArray(items) |
| 1751 | } |
| 1752 | takeAll(): Chunk.Chunk<A> { |
| 1753 | return this.takeN(this.remaining) |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | const emptyReplayWindow: ReplayWindow<never> = { |
| 1758 | remaining: 0, |
nothing calls this directly
no outgoing calls
no test coverage detected