All files / Consumer Middleware.js

75% Statements 24/32
100% Branches 0/0
100% Functions 1/1
74.19% Lines 23/31

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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 1231x 1x 1x 1x 1x 1x 1x 1x 1x 1x                       5x 5x 5x   5x 5x 5x 5x   5x                                                           1x 1x                                                                                               1x 1x                 1x      
import createState from '../State/createState';
import createPlayer from '../Player/createPlayer';
import ConsumerInput from './ConsumerInput';
import ConsumerOutput from './ConsumerOutput';
import ApiServices from '../Api/ApiServices';
import ErrorsHandler from '../Errors/ErrorsHandler';
import connectStoreSubscriptions from '../State/connectStoreSubscriptions';
import connectApiSubscriptions from '../Api/connectApiSubscriptions';
import connectPlayerSubscriptions from '../Player/connectPlayerSubscriptions';
import connectErrorSubscriptions from '../Errors/connectErrorSubscriptions';
 
/**
 * A class acting as a "mediator" between all domains of the application,
 * and the primary entry point into it.
 *
 * Each instance holds a self-contained instance of the middleware state
 * and manages the subscribing and unsubscribing of streams connecting
 * the various domains.
 */
 
export default class Middleware {
    constructor(parentElement, options) {
        this.state = createState({ config: options });
        this.errorsHandler = new ErrorsHandler();
 
        this.apiServices = new ApiServices(this);
        this.player = createPlayer(parentElement, this.state.stores.config.value);
        this.consumerInput = new ConsumerInput();
        this.consumerOutput = new ConsumerOutput(this.player, this.errorsHandler.consumerError$);
 
        this.subscriptions = [
            ...connectPlayerSubscriptions(this),
            ...connectApiSubscriptions(this),
            ...connectStoreSubscriptions(this),
            ...connectErrorSubscriptions(this),
        ];
    }
 
    get legacyShim$() {
        return this.consumerOutput.legacyShim$;
    }
 
    get playerInteractions$() {
        return this.consumerOutput.playerInteractions$;
    }
 
    /**
     * Configures the middleware with zero or more configuration
     * options.
     */
 
    configure(options) {
        this.consumerInput.config$.next(options);
    }
 
    /**
     * Provides a new asset to the middleware.
     */
 
    loadAsset(asset) {
        this.player.showControls();
        this.consumerInput.asset$.next(asset);
    }
 
    /**
     * Fades out the player volume.
     */
 
    fadeOutVolume() {
        this.player.fadeOutVolume();
    }
 
    /**
     * Fades in the player volume.
     */
 
    fadeInVolume() {
        this.player.fadeInVolume();
    }
 
    /**
     * Shows the controls if hidden/idle.
     */
 
    showControls() {
        this.player.showControls();
    }
 
    /**
     * Hides the controls if active.
     */
 
    hideControls() {
        this.player.hideControls();
    }
 
    /**
     * Exits fullscreen mode if enabled.
     */
 
    exitFullscreen() {
        this.player.exitFullscreen();
    }
 
    /**
     * Destroys the middleware by unsubscribing from all streams.
     */
 
    destroy() {
        this.disconnectSubscriptions();
        this.player.destroy();
    }
 
    /**
     * Disconnects all active stream subscriptions, and garbage collects
     * references to them.
     */
 
    disconnectSubscriptions() {
        while (this.subscriptions.length > 0) this.subscriptions.pop().unsubscribe();
    }
}