All files / src/internal/views/read-only read-only-cursor.ts

100% Statements 18/18
100% Branches 2/2
100% Functions 14/14
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                                                  156x 156x 156x       120x       167x 167x       102x                                   148x 148x       127x       123x       1x       3x       2x       1x       53x       54x       52x      
// Cursors of read-only collections: each wraps the source collection's own cursor, so it moves,
// fails fast or follows live changes exactly as the source's does, and refuses every change before
// looking at its state.
 
import type { Cursor } from '../../../interfaces/collection'
import type { ListCursor } from '../../../interfaces/list'
import { refuse } from './refuse'
import { hideSource, SOURCE } from './source-key'
 
/**
 * A forward cursor over a source cursor; with `reveal`, each element is passed through it.
 *
 * @internal
 */
export class ReadOnlyCursor<E> implements Cursor<E> {
  declare readonly [SOURCE]: Cursor<E>
  private readonly where: string
  private readonly reveal: ((element: E) => E) | undefined
 
  /**
   * @param source - The source collection's cursor.
   * @param where - The name refusals start with: `<Tag>.cursor`.
   * @param reveal - Maps each element before it is returned.
   */
  constructor(source: Cursor<E>, where: string, reveal?: (element: E) => E) {
    hideSource(this, source)
    this.where = where
    this.reveal = reveal
  }
 
  hasNext(): boolean {
    return this[SOURCE].hasNext()
  }
 
  next(): E {
    const element = this[SOURCE].next()
    return this.reveal === undefined ? element : this.reveal(element)
  }
 
  remove(): void {
    throw refuse(`${this.where}.remove`)
  }
}
 
/**
 * A list cursor over a source list cursor.
 *
 * @internal
 */
export class ReadOnlyListCursor<E> implements ListCursor<E> {
  declare readonly [SOURCE]: ListCursor<E>
  private readonly where: string
 
  /**
   * @param source - The source list's cursor.
   * @param where - The name refusals start with: `<Tag>.cursor` or `<Tag>.listCursor`.
   */
  constructor(source: ListCursor<E>, where: string) {
    hideSource(this, source)
    this.where = where
  }
 
  hasNext(): boolean {
    return this[SOURCE].hasNext()
  }
 
  next(): E {
    return this[SOURCE].next()
  }
 
  hasPrevious(): boolean {
    return this[SOURCE].hasPrevious()
  }
 
  previous(): E {
    return this[SOURCE].previous()
  }
 
  nextIndex(): number {
    return this[SOURCE].nextIndex()
  }
 
  previousIndex(): number {
    return this[SOURCE].previousIndex()
  }
 
  remove(): void {
    throw refuse(`${this.where}.remove`)
  }
 
  set(_element: E): void {
    throw refuse(`${this.where}.set`)
  }
 
  insert(_element: E): void {
    throw refuse(`${this.where}.insert`)
  }
}