All files / rpc-calls/src/channel CompactToNativeTransform.ts

16.66% Statements 3/18
100% Branches 0/0
0% Functions 0/3
21.42% Lines 3/14

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 301x 1x         1x                                              
import {map} from 'rxjs';
import {toMessage} from '@jsonjoy.com/rpc-messages';
import type {CompactMessage, RxMessage} from '@jsonjoy.com/rpc-messages';
import type {Observable} from 'rxjs';
import type {LogicalChannel} from './types';
 
export class CompactToNativeTransform implements LogicalChannel<CompactMessage[], CompactMessage[]> {
  public readonly msg$: Observable<CompactMessage[]>;
  public readonly err$: Observable<unknown>;
 
  constructor(protected readonly upstream: LogicalChannel<RxMessage[], RxMessage[]>) {
    this.err$ = upstream.err$;
    this.msg$ = upstream.msg$.pipe(
      map((messages) => {
        const compact: CompactMessage[] = [];
        const length = messages.length;
        for (let i = 0; i < length; i++) compact.push(messages[i].toCompact());
        return compact;
      }),
    );
  }
 
  public async send(outgoing: CompactMessage[]): Promise<void> {
    const native: RxMessage[] = [];
    const length = outgoing.length;
    for (let i = 0; i < length; i++) native.push(toMessage(outgoing[i]));
    return this.upstream.send(native);
  }
}