All files / app/assets/javascripts/ide/stores/plugins terminal_sync.js

100% Statements 21/21
88.88% Branches 8/9
90% Functions 9/10
100% Lines 21/21

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            2x                 6x 6x   6x 5x     6x   8x 3x     5x     6x 2x 2x     6x 5x     5x             6x 13x   7x 5x 2x 2x            
import { debounce } from 'lodash';
import { commitActionTypes } from '~/ide/constants';
import eventHub from '~/ide/eventhub';
import { isEndingStatus, isRunningStatus } from '../modules/terminal/utils';
import terminalSyncModule from '../modules/terminal_sync';
 
const UPLOAD_DEBOUNCE = 200;
 
/**
 * Registers and controls the terminalSync vuex module based on IDE events.
 *
 * - Watches the terminal session status state to control start/stop.
 * - Listens for file change event to control upload.
 */
export default function createMirrorPlugin() {
  return (store) => {
    store.registerModule('terminalSync', terminalSyncModule());
 
    const upload = debounce(() => {
      store.dispatch(`terminalSync/upload`);
    }, UPLOAD_DEBOUNCE);
 
    const onFilesChange = (payload) => {
      // Do nothing on a file update since we only want to trigger manually on "save".
      if (payload?.type === commitActionTypes.update) {
        return;
      }
 
      upload();
    };
 
    const stop = () => {
      store.dispatch(`terminalSync/stop`);
      eventHub.$off('ide.files.change', onFilesChange);
    };
 
    const start = () => {
      store
        .dispatch(`terminalSync/start`)
        .then(() => {
          eventHub.$on('ide.files.change', onFilesChange);
        })
        .catch(() => {
          // error is handled in store
        });
    };
 
    store.watch(
      (x) => x.terminal && x.terminal.session && x.terminal.session.status,
      (val) => {
        if (isRunningStatus(val)) {
          start();
        } else Eif (isEndingStatus(val)) {
          stop();
        }
      },
    );
  };
}