All files / src/store/features/console consoleMiddleware.ts

0% Statements 0/70
0% Branches 0/45
0% Functions 0/11
0% Lines 0/65

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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179                                                                                                                                                                                                                                                                                                                                                                     
import { Middleware, Dispatch } from "redux";
import {
  actorName,
  customEventName,
  sceneName,
  triggerName,
} from "shared/lib/entities/entitiesHelpers";
import { RootState } from "store/configureStore";
import consoleActions from "store/features/console/consoleActions";
import {
  customEventSelectors,
  actorSelectors,
  sceneSelectors,
  triggerSelectors,
} from "store/features/entities/entitiesState";
import { ConsoleLink } from "./consoleState";
 
const getLinkToSymbol = (
  symbol: string,
  state: RootState
): ConsoleLink | undefined => {
  const allCustomScripts = customEventSelectors.selectAll(state);
  const allActors = actorSelectors.selectAll(state);
  const allTriggers = triggerSelectors.selectAll(state);
  const allScenes = sceneSelectors.selectAll(state);
 
  const customScript = allCustomScripts.find((s) => s.symbol === symbol);
  Iif (customScript) {
    // Matched symbol to a custom script
    return {
      linkText: customEventName(
        customScript,
        allCustomScripts.indexOf(customScript)
      ),
      type: "customEvent",
      entityId: customScript.id,
      sceneId: "",
    };
  }
  const actor = allActors.find((a) => {
    return symbol.startsWith(`${a.symbol}_`);
  });
  Iif (actor) {
    const scene = allScenes.find((s) => s?.actors.includes(actor.id));
    Iif (scene) {
      // Matched symbol to an actor
      return {
        linkText: actorName(actor, scene.actors.indexOf(actor.id)),
        type: "actor",
        entityId: actor.id,
        sceneId: scene.id,
      };
    }
  }
 
  const trigger = allTriggers.find((t) => {
    return symbol.startsWith(`${t.symbol}_`);
  });
  Iif (trigger) {
    const scene = allScenes.find((s) => s?.triggers.includes(trigger.id));
    Iif (scene) {
      // Matched symbol to a trigger
      return {
        linkText: triggerName(trigger, scene.triggers.indexOf(trigger.id)),
        type: "trigger",
        entityId: trigger.id,
        sceneId: scene.id,
      };
    }
  }
 
  const scene = allScenes.find((s) => {
    return symbol.startsWith(`${s.symbol}_`) || symbol === s.symbol;
  });
 
  Iif (scene) {
    // Matched symbol to a scene
    return {
      linkText: sceneName(scene, allScenes.indexOf(scene)),
      type: "scene",
      entityId: scene.id,
      sceneId: scene.id,
    };
  }
 
  return undefined;
};
 
const consoleMiddleware: Middleware<Dispatch, RootState> =
  (store) => (next) => async (action) => {
    Iif (consoleActions.stdErr.match(action)) {
      if (action.payload.text.includes("Object files too large")) {
        const state = store.getState();
        const textLines = action.payload.text.split("\n");
        const tooLargeSymbols = textLines
          .slice(1)
          .map((line) => line.trim().slice(0, -2));
        for (const symbol of tooLargeSymbols) {
          const link = getLinkToSymbol(symbol, state);
          Iif (link) {
            next({
              ...action,
              payload: {
                text: `Object file too large: ${symbol}.o`,
                link,
              },
            });
            continue;
          }
 
          // Not sure what entity this relates to, so just output text as is
          next({
            ...action,
            payload: {
              text: `Object file too large: ${symbol}.o`,
            },
          });
        }
 
        return;
      } else if (action.payload.text.includes("referenced by module")) {
        const symbol = action.payload.text.match(
          /referenced by module '([^']*)'/
        )?.[1];
        Iif (symbol) {
          const state = store.getState();
          const link = getLinkToSymbol(symbol, state);
          Iif (link) {
            return next({
              ...action,
              payload: {
                text: action.payload.text,
                link,
              },
            });
          }
        }
      } else if (
        action.payload.text.startsWith("Error") &&
        action.payload.text.includes("scene '")
      ) {
        const symbol = action.payload.text.match(/scene '([^']*)'/)?.[1];
        Iif (symbol) {
          const state = store.getState();
          const link = getLinkToSymbol(symbol, state);
          Iif (link) {
            return next({
              ...action,
              payload: {
                text: action.payload.text,
                link,
              },
            });
          }
        }
      } else Iif (action.payload.text.includes("removing")) {
        const symbol = action.payload.text.match(
          /removing .*[\\/]([^\\/]*).o/
        )?.[1];
        Iif (symbol) {
          const state = store.getState();
          const link = getLinkToSymbol(symbol, state);
          Iif (link) {
            return next({
              ...action,
              payload: {
                text: action.payload.text,
                link,
              },
            });
          }
        }
      }
    }
    return next(action);
  };
 
export default consoleMiddleware;