All files / src/store/features/music musicMiddleware.ts

92.31% Statements 36/39
88.24% Branches 15/17
75% Functions 6/8
91.89% Lines 34/37

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  3x   3x 3x 3x 3x   3x 3x       3x 2x 2x 2x 2x 2x       3x 3x             1x 1x                       1x 1x   1x       3x 3x 1x 1x 1x 1x 1x     1x     2x 1x 1x             1x     3x     3x  
import { Dispatch, Middleware } from "@reduxjs/toolkit";
import ScripTracker from "renderer/lib/vendor/scriptracker/scriptracker";
import { RootState } from "store/configureStore";
import soundfxActions from "store/features/soundfx/soundfxActions";
import navigationActions from "store/features/navigation/navigationActions";
import actions from "./musicActions";
import { musicSelectors } from "store/features/entities/entitiesState";
import { MusicSettings } from "shared/lib/entities/entitiesTypes";
import { assetPath } from "shared/lib/helpers/assets";
import API from "renderer/lib/api";
 
let modPlayer: ScripTracker;
 
export function initMusic() {
  modPlayer = new ScripTracker();
  modPlayer.on(ScripTracker.Events.playerReady, onSongLoaded);
  window.removeEventListener("click", initMusic);
  window.removeEventListener("keydown", initMusic);
  return modPlayer;
}
 
// Initialise audio on first click
window.addEventListener("click", initMusic);
window.addEventListener("keydown", initMusic);
 
function onSongLoaded(player: ScripTracker) {
  player.play();
}
 
function playMOD(filename: string, settings: MusicSettings) {
  if (modPlayer) {
    modPlayer.loadModule(
      `gbs://project/${filename}`,
      !!settings.disableSpeedConversion
    );
  }
}
 
async function playUGE(filename: string, _settings: MusicSettings) {
  API.music.playUGE(filename);
}
 
function pause() {
  if (modPlayer && modPlayer.isPlaying) {
    modPlayer.stop();
  }
  API.music.closeMusic();
}
 
const musicMiddleware: Middleware<Dispatch, RootState> =
  (store) => (next) => (action) => {
    if (actions.playMusic.match(action)) {
      const state = store.getState();
      const track = musicSelectors.selectById(state, action.payload.musicId);
      if (track) {
        const filename = assetPath("music", track);
        Iif (track.type === "uge") {
          playUGE(filename, track.settings);
        } else {
          playMOD(filename, track.settings);
        }
      }
    } else if (actions.pauseMusic.match(action)) {
      pause();
    } else if (
      soundfxActions.playSoundFxBeep.match(action) ||
      soundfxActions.playSoundFxTone.match(action) ||
      soundfxActions.playSoundFxCrash.match(action) ||
      navigationActions.setSection.match(action) ||
      navigationActions.setNavigationId.match(action)
    ) {
      store.dispatch(actions.pauseMusic());
    }
 
    return next(action);
  };
 
export default musicMiddleware;