All files / src/internal/views/read-only source-key.ts

100% Statements 10/10
100% Branches 4/4
100% Functions 4/4
100% Lines 9/9

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                      105x       30534x                               6644x 6644x 6644x       18715x 18715x 12315x       232x      
// The key under which a read-only view, cursor, entry or iterator keeps what it reads. It is not a
// registered symbol and it is not exported from the package, and the property is non-enumerable,
// so `Object.keys`, `JSON.stringify`, `for…in`, spread and `Object.assign` never show or copy the
// source. Reflection (`Object.getOwnPropertySymbols`) still sees it; the source is hidden, not
// protected. The property stays writable and configurable so that framework proxies, whose `get`
// trap may return a wrapped value, keep working.
 
import { defineProperty } from '../../intrinsics'
import { done, IteratorBase } from '../../iteration'
 
/** The key of a read-only wrapper's source. @internal */
export const SOURCE: unique symbol = /*#__PURE__*/ Symbol('source')
 
/** Stores `source` on `target` under {@link SOURCE}, non-enumerable. @internal */
export function hideSource(target: object, source: object): void {
  defineProperty(target, SOURCE, { value: source, writable: true, configurable: true })
}
 
/**
 * The iterator a read-only view returns: it walks the source's iterator, which it keeps under
 * {@link SOURCE}, so the source's iterator and the storage it walks stay out of reach. Each item
 * is passed through `reveal` when one is given. The live or fail-fast behaviour is the source
 * iterator's.
 *
 * @internal
 */
export class ReadOnlyIterator<E> extends IteratorBase implements IterableIterator<E> {
  declare readonly [SOURCE]: Iterator<E>
  private readonly reveal: ((item: E) => E) | undefined
 
  constructor(inner: Iterator<E>, reveal?: (item: E) => E) {
    super()
    hideSource(this, inner)
    this.reveal = reveal
  }
 
  next(): IteratorResult<E> {
    const result = this[SOURCE].next()
    if (result.done) return done()
    return this.reveal === undefined ? result : { done: false, value: this.reveal(result.value) }
  }
 
  [Symbol.iterator](): this {
    return this
  }
}