All files / platform/core/src/services/CineService CineService.ts

76.74% Statements 33/43
87.5% Branches 7/8
62.5% Functions 10/16
75.6% Lines 31/41

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 123 124    71x                 71x       71x 71x       71x 71x       5079x       53x       2x       2x 2x         2x 2x                             314x   314x   314x                     133x                 1118x                             241x 71x     241x 170x   241x 170x   241x 170x     241x 71x     241x 71x     71x      
import { PubSubService } from '../_shared/pubSubServiceInterface';
 
class CineService extends PubSubService {
  public static readonly EVENTS = {
    CINE_STATE_CHANGED: 'event::cineStateChanged',
  };
 
  public static REGISTRATION = {
    name: 'cineService',
    altName: 'CineService',
    create: ({ configuration = {} }) => {
      return new CineService();
    },
  };
 
  serviceImplementation = {};
  startedClips = new Map();
  closedViewports = new Set();
 
  constructor() {
    super(CineService.EVENTS);
    this.serviceImplementation = {};
  }
 
  public getState() {
    return this.serviceImplementation._getState();
  }
 
  public setCine({ id, frameRate, isPlaying }) {
    return this.serviceImplementation._setCine({ id, frameRate, isPlaying });
  }
 
  public setIsCineEnabled(isCineEnabled) {
    this.serviceImplementation._setIsCineEnabled(isCineEnabled);
    // Todo: for some reason i need to do this setTimeout since the
    // reducer state does not get updated right away and if we publish the
    // event and we use the cineService.getState() it will return the old state
    if (isCineEnabled) {
      this.closedViewports.forEach(viewportId => {
        this.clearViewportCineClosed(viewportId);
      });
    }
 
    queueMicrotask(() => {
      this._broadcastEvent(this.EVENTS.CINE_STATE_CHANGED, { isCineEnabled });
    });
  }
 
  public playClip(element, playClipOptions) {
    const res = this.serviceImplementation._playClip(element, playClipOptions);
 
    this.startedClips.set(element, playClipOptions);
 
    this._broadcastEvent(this.EVENTS.CINE_STATE_CHANGED, { isPlaying: true });
 
    return res;
  }
 
  public stopClip(element, stopClipOptions) {
    const res = this.serviceImplementation._stopClip(element, stopClipOptions);
 
    this._broadcastEvent(this.EVENTS.CINE_STATE_CHANGED, { isPlaying: false });
 
    return res;
  }
 
  public onModeExit() {
    this.setIsCineEnabled(false);
    this.startedClips.forEach((value, key) => {
      this.stopClip(key, value);
    });
  }
 
  public getSyncedViewports(viewportId) {
    return this.serviceImplementation._getSyncedViewports(viewportId);
  }
 
  public setViewportCineClosed(viewportId) {
    this.closedViewports.add(viewportId);
  }
 
  public isViewportCineClosed(viewportId) {
    // Todo: we should move towards per viewport cine closed in next release
    return this.closedViewports.size > 0;
  }
 
  public clearViewportCineClosed(viewportId) {
    this.closedViewports.delete(viewportId);
  }
 
  public setServiceImplementation({
    getState: getStateImplementation,
    setCine: setCineImplementation,
    setIsCineEnabled: setIsCineEnabledImplementation,
    playClip: playClipImplementation,
    stopClip: stopClipImplementation,
    getSyncedViewports: getSyncedViewportsImplementation,
  }) {
    if (getSyncedViewportsImplementation) {
      this.serviceImplementation._getSyncedViewports = getSyncedViewportsImplementation;
    }
 
    if (getStateImplementation) {
      this.serviceImplementation._getState = getStateImplementation;
    }
    if (setCineImplementation) {
      this.serviceImplementation._setCine = setCineImplementation;
    }
    if (setIsCineEnabledImplementation) {
      this.serviceImplementation._setIsCineEnabled = setIsCineEnabledImplementation;
    }
 
    if (playClipImplementation) {
      this.serviceImplementation._playClip = playClipImplementation;
    }
 
    if (stopClipImplementation) {
      this.serviceImplementation._stopClip = stopClipImplementation;
    }
  }
}
 
export default CineService;