| 2976 | } |
| 2977 | |
| 2978 | class ReplayWindowImpl<A> implements PubSub.ReplayWindow<A> { |
| 2979 | readonly buffer: ReplayBuffer<A> |
| 2980 | readonly values: Array<A> |
| 2981 | index = 0 |
| 2982 | remaining: number |
| 2983 | slideIndex: number |
| 2984 | newestIndex = -1 |
| 2985 | |
| 2986 | constructor(buffer: ReplayBuffer<A>) { |
| 2987 | this.buffer = buffer |
| 2988 | this.remaining = buffer.size |
| 2989 | this.slideIndex = buffer.index |
| 2990 | this.values = new Array(this.remaining) |
| 2991 | let node = buffer.head |
| 2992 | for (let i = 0; i < this.remaining; i++) { |
| 2993 | this.values[i] = node.value as A |
| 2994 | this.newestIndex = node.index |
| 2995 | node = node.next! |
| 2996 | } |
| 2997 | } |
| 2998 | close(): void { |
| 2999 | this.values.length = 0 |
| 3000 | this.remaining = 0 |
| 3001 | } |
| 3002 | sync(): void { |
| 3003 | const slides = this.buffer.index - this.slideIndex |
| 3004 | if (slides === 0 || this.remaining === 0) { |
| 3005 | return |
| 3006 | } |
| 3007 | const count = Math.min(slides, this.buffer.capacity) |
| 3008 | const start = this.buffer.index - count |
| 3009 | for (let i = 0; i < count; i++) { |
| 3010 | const entry = this.buffer.slideValues[(start + i) % this.buffer.capacity] |
| 3011 | if (entry.index > this.newestIndex) { |
| 3012 | this.index = (this.index + 1) % this.values.length |
| 3013 | this.values[(this.index + this.remaining - 1) % this.values.length] = entry.value |
| 3014 | this.newestIndex = entry.index |
| 3015 | } |
| 3016 | } |
| 3017 | this.slideIndex = this.buffer.index |
| 3018 | } |
| 3019 | take(): A | undefined { |
| 3020 | if (this.remaining === 0) { |
| 3021 | return undefined |
| 3022 | } |
| 3023 | this.sync() |
| 3024 | const value = this.values[this.index] |
| 3025 | this.values[this.index] = AbsentValue as unknown as A |
| 3026 | this.index = (this.index + 1) % this.values.length |
| 3027 | this.remaining-- |
| 3028 | if (this.remaining === 0) { |
| 3029 | this.close() |
| 3030 | } |
| 3031 | return value as A |
| 3032 | } |
| 3033 | takeN(n: number): Array<A> { |
| 3034 | n = Count.normalize(n) |
| 3035 | const len = Math.min(n, this.remaining) |
nothing calls this directly
no outgoing calls
no test coverage detected