<commit_before>import React, {useState} from 'react';
import * as fdc3 from 'openfin-fdc3';
import {InterApplicationBusHooks} from 'openfin-react-hooks';

let latestChartListener;
let currentChartListener;

let latestNewsListener;
let currentNewsListener;

let windows = [];

function App() {
  const [content, setContent] = useState(undefined);
  const [newsContent, setNewsContent] = useState(undefined);

  const createWindow = async (context, windowName) => {
    const parentWindowOptions = await window.fin.Window.getCurrentSync().getOptions();
    const manifestOptions = await fetch(`${parentWindowOptions.customData.apiBaseUrl}/apps/v1/${context.appName}/app.json`, {
      method: 'GET',
    });
    const windowOptions = await manifestOptions.json();
    const winOption = {
      ...windowOptions.startup_app,
      // uuid must match that of the parent window
      uuid: "stockflux-container",
      name: windowName,
      customData: {
        symbol: context.name,
        apiBaseUrl: parentWindowOptions.customData.apiBaseUrl
      }
    };
    return await window.fin.Window.create(winOption);
  }

  const windowHandler = async (context, currentListener, latestListener, isChart) => {
    if (context && currentListener === latestListener) {
      const windowName = context.appName + '-' + context.name;
      if (!windows.find(window => window === windowName)) {
          const window = await createWindow(context, windowName);
          if (window) {

            if (isChart) {
              setContent({
                symbol: context.name,
                name: context.id.default
              });
            } else {
              setNewsContent({
                symbol: context.name,
              });
            }

            windows = [...windows, windowName];
            
            window.addListener("closed", () => {
              if (removeWindow(windowName)) {
                closeParentContainer();
              }
            })
          }
      }
    }
  }
  
  const removeWindow = (windowName) => {
    windows = windows.filter(name => name !== windowName);
    return windows.length === 0;
  }
  
  const closeParentContainer = () => {
    window.fin.Window.getCurrentSync().close(true);
  }

  currentChartListener = fdc3.addIntentListener("ViewChart", context => {
    windowHandler(context, currentChartListener, latestChartListener, true);
  });
  latestChartListener = currentChartListener;
  InterApplicationBusHooks.usePublish('stockFluxChart:' + (content ? content.symbol : ''), content);

  currentNewsListener = fdc3.addIntentListener("ViewNews", context => {
    windowHandler(context, currentNewsListener, latestNewsListener, false);
  });
  latestNewsListener = currentNewsListener;
  InterApplicationBusHooks.usePublish('stockFluxNews:' + (newsContent ? newsContent.symbol : ''), newsContent);

  return (
    <></>
  );<commit_msg>Refactor message publishing, fix bug that tried to create duplicate windows<commit_after>import React, {useState} from 'react';
import * as fdc3 from 'openfin-fdc3';

let latestChartListener;

let latestNewsListener;

let windows = [];

function App() {
  const [content, setContent] = useState(undefined);
  const [newsContent, setNewsContent] = useState(undefined);

  const { InterApplicationBus } = window.fin;

  const createWindow = async (context, windowName) => {
    const parentWindowOptions = await window.fin.Window.getCurrentSync().getOptions();
    const manifestOptions = await fetch(`${parentWindowOptions.customData.apiBaseUrl}/apps/v1/${context.appName}/app.json`, {
      method: 'GET',
    });
    const windowOptions = await manifestOptions.json();
    const winOption = {
      ...windowOptions.startup_app,
      // uuid must match that of the parent window
      uuid: "stockflux-container",
      name: windowName,
      customData: {
        symbol: context.name,
        apiBaseUrl: parentWindowOptions.customData.apiBaseUrl
      }
    };
    return await window.fin.Window.create(winOption);
  }

  const windowHandler = async (context, currentListener, latestListener, isChart) => {
    if (context && currentListener === latestListener) {
      const windowName = context.appName + '-' + context.name;
      if (!windows.find(window => window === windowName)) {
          const window = await createWindow(context, windowName);
          if (window) {
            windows = [...windows, windowName];
            if (isChart) {
              setContent({
                symbol: context.name,
                name: context.id.default
              });
            } else {
              setNewsContent({
                symbol: context.name,
              });
            }
            window.addListener("closed", () => {
              if (removeWindow(windowName)) {
                closeParentContainer();
              }
            })
          }
      }
    }
  }
  
  const removeWindow = (windowName) => {
    windows = windows.filter(name => name !== windowName);
    return windows.length === 0;
  }
  
  const closeParentContainer = () => {
    window.fin.Window.getCurrentSync().close(true);
  }

  const currentChartListener = fdc3.addIntentListener("ViewChart", context => {
    windowHandler(context, currentChartListener, latestChartListener, true);
  });
  latestChartListener = currentChartListener;

  if (InterApplicationBus && content && content.symbol) {
    InterApplicationBus.publish('stockFluxChart:' + content.symbol, content)
  }
  
  const currentNewsListener = fdc3.addIntentListener("ViewNews", context => {
    windowHandler(context, currentNewsListener, latestNewsListener, false);
  });
  latestNewsListener = currentNewsListener;
  
  if (InterApplicationBus && newsContent && newsContent.symbol) {
    InterApplicationBus.publish('stockFluxNews:' + newsContent.symbol, newsContent)
  }

  return (
    <></>
  );
{'fraction_non_alphanumeric': 0.1196188531563723, 'fraction_numerical': 0.002041858090862685, 'mean_word_length': 2.8218465539661897, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2951438', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1561, 'n_words': 333}