All files / Api/UserActionsWatched/Streams createWatchedEvent.js

36.84% Statements 7/19
0% Branches 0/2
16.67% Functions 1/6
35.29% Lines 6/17

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 791x               1x   1x 1x   1x   5x                                                                                                                              
import {
    map,
    pluck,
    filter,
    switchMap,
    distinctUntilChanged,
    bufferCount,
} from 'rxjs/operators';
import { combineLatest } from 'rxjs';
 
import createTimer from './createTimer';
import interval$ from '../../../Shared/Streams/interval';
 
const BUFFER_COUNT = 3;
 
const createWatchedEvent = (
    tokenUpdate$,
    userActionsUpdate$,
    assetUpdate$,
    player,
) =>
    combineLatest(
        tokenUpdate$,
        assetUpdate$,
        userActionsUpdate$,
    )
        .pipe(
            switchMap(([
                latestToken,
                latestAsset,
                latestUserActions,
            ]) => {
                const playbackStatus$ = player.state.stores.playback.update$
                    .pipe(
                        pluck('playbackStatus'),
                        distinctUntilChanged(),
                    );
                const start$ = playbackStatus$
                    .pipe(
                        filter(value => value === 'PLAYING'),
                    );
 
                const pause$ = playbackStatus$
                    .pipe(
                        filter(value => (
                            value === 'PAUSED' ||
                            value === 'BUFFERING'
                        )),
                    );
 
                const stop$ = playbackStatus$
                    .pipe(
                        filter(value => value === 'UNLOADED'),
                    );
 
                const maxDurationInSeconds = latestUserActions.watched[latestAsset.type];
 
                const maptoWatchedEvent = () => ({
                    token: latestToken,
                    assetId: latestAsset.assetId,
                    duration: maxDurationInSeconds,
                    type: latestAsset.type,
                });
 
                return createTimer(
                    start$,
                    pause$,
                    stop$,
                    interval$.pipe(bufferCount(BUFFER_COUNT)),
                    maxDurationInSeconds,
                )
                    .pipe(
                        map(maptoWatchedEvent),
                    );
            })
        );
 
export default createWatchedEvent;