| 1708 | } |
| 1709 | |
| 1710 | class ReplayWindowImpl<A> implements ReplayWindow<A> { |
| 1711 | // cleared once exhausted, so the window does not retain later messages |
| 1712 | head: ReplayNode<A> | undefined |
| 1713 | index: number |
| 1714 | remaining: number |
| 1715 | constructor(readonly buffer: ReplayBuffer<A>) { |
| 1716 | this.index = buffer.index |
| 1717 | this.remaining = buffer.size |
| 1718 | this.head = this.remaining === 0 ? undefined : buffer.head |
| 1719 | } |
| 1720 | take(): A | undefined { |
| 1721 | if (this.remaining === 0) { |
| 1722 | return undefined |
| 1723 | } |
| 1724 | let node = this.head! |
| 1725 | while (this.index < this.buffer.index) { |
| 1726 | node = node.next! |
| 1727 | this.index++ |
| 1728 | } |
| 1729 | this.remaining-- |
| 1730 | this.head = this.remaining === 0 ? undefined : node.next! |
| 1731 | return node.value as A |
| 1732 | } |
| 1733 | takeN(n: number): Chunk.Chunk<A> { |
| 1734 | const items = new Array(Math.max(0, Math.min(n, this.remaining))) |
| 1735 | for (let i = 0; i < items.length; i++) { |
| 1736 | items[i] = this.take() |
| 1737 | } |
| 1738 | return Chunk.unsafeFromArray(items) |
| 1739 | } |
| 1740 | takeAll(): Chunk.Chunk<A> { |
| 1741 | return this.takeN(this.remaining) |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | const emptyReplayWindow: ReplayWindow<never> = { |
| 1746 | remaining: 0, |
nothing calls this directly
no outgoing calls
no test coverage detected