All files select.ts

46.42% Statements 26/56
21.73% Branches 5/23
63.63% Functions 7/11
46.15% Lines 24/52

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 853x   4x     4x 105x 102x     102x             105x           6x 6x 6x 12x 12x 6x 3x                                                   105x 105x 129x 129x 129x 129x 99x                                   30x 30x   6x    
import { Channel, ReadAttempt, WriteAttempt, Attempt, Attempted, Thunk } from './channel.js'
 
export async function* select<Attempts extends Attempt[]>(
  ...attempts: Attempts
): AsyncGenerator<Attempted<Attempts[number]>> {
  while (true) {
    const result = await selectNext(...attempts)
    Iif (result.done) {
      break
    }
    yield result.value
  }
}
 
export async function selectNext<Attempts extends Attempt[]>(
  ...attempts: Attempts
): Promise<IteratorResult<Attempted<Attempts[number]>>> {
  return selectSync(attempts) ?? await selectAsync(attempts)
}
 
export function selectAsync<Attempts extends Attempt[]>(
  attempts: Attempts
): Promise<IteratorResult<Attempted<Attempts[number]>>> {
  return new Promise((resolve, reject) => {
    const undos: Thunk[] = []
    for (const attempt of attempts) {
      if (attempt instanceof Channel) {
        undos.push(attempt.pushRead(result => {
          undos.forEach(undo => undo())
          resolve(result as IteratorResult<Attempted<Attempts[number]>>)
        }))
      } else Eif (attempt instanceof WriteAttempt) {
        undos.push(attempt.channel.pushWrite({ value: attempt.value, enqueued: (err: unknown) => {
          undos.forEach(_ => _())
          Iif (err) {
            reject(err)
            return
          }
          resolve(attempt.perform(attempt.value) as IteratorResult<Attempted<Attempts[number]>>)
        }}))
      } else if (attempt instanceof ReadAttempt) {
        undos.push(attempt.channel.pushRead(result => {
          undos.forEach(undo => undo())
          resolve(attempt.perform(result) as IteratorResult<Attempted<Attempts[number]>>)
        }))
      } else {
        throw new Error('Invalid attempt.')
      }
    }
  })
}
 
export function selectSync<Attempts extends Attempt[]>(
  attempts: Attempts
): undefined | IteratorResult<Attempted<Attempts[number]>> {
  const n = attempts.length
  for (let i = 0; i < n; i++) {
    const j = i + Math.floor(Math.random() * (n - i))
    const attempt = attempts[j] as Attempts[number]
    if (attempt instanceof Channel) {
      if (attempt.pendingWrites > 0) {
        return { value: attempt.consumeWrite() }
      }
    } else Eif (attempt instanceof WriteAttempt) {
      if (attempt.channel.cap === 0 && attempt.channel.pendingReads > 0) {
        attempt.channel.consumeRead({ value: attempt.value })
        return attempt.perform(attempt.value)
      } else Iif (attempt.channel.pendingWrites < attempt.channel.cap) {
        attempt.channel.pushWrite({ value: attempt.value })
        return attempt.perform(attempt.value)
      }
    } else if (attempt instanceof ReadAttempt) {
      Iif (attempt.channel.pendingWrites > 0) {
        const value = attempt.channel.consumeWrite()
        return attempt.perform({ value })
      }
    } else {
      throw new Error('Invalid attempt.')
    }
    attempts[j] = attempts[i]
    attempts[i] = attempt
  }
  return
}