<commit_before>/** *
 * Jobs of this module
 * - Maintain the list of components in the Redux store. So monitor new components
 *   and add/remove components.
 * - Watch components for query changes and extract these and update the store.
 * - Ensure all page queries are run as part of bootstrap and report back when
 *   this is done
 * - Whenever a query changes, re-run all pages that rely on this query.
 ***/

const _ = require(`lodash`)
const chokidar = require(`chokidar`)
const path = require(`path`)
const slash = require(`slash`)

const { store } = require(`../../redux/`)
const { boundActionCreators } = require(`../../redux/actions`)
const queryCompiler = require(`./query-compiler`).default
const report = require(`gatsby-cli/lib/reporter`)
const {
  queueQueryForPathname,
  runQueuedActions: runQueuedQueries,
} = require(`./page-query-runner`)
const debug = require(`debug`)(`gatsby:query-watcher`)

const getQueriesSnapshot = () => {
  const state = store.getState()

  const snapshot = {
    components: new Map(state.components),
    staticQueryComponents: new Map(state.staticQueryComponents),
  }

  return snapshot
}

const handleComponentsWithRemovedQueries = (
  { components, staticQueryComponents },
  queries
) => {
  // If a component previously with a query now doesn't — update the
  // store.
  components.forEach(c => {
    if (c.query !== `` && !queries.has(c.componentPath)) {
      debug(`Page query was removed from ${c.componentPath}`)
      boundActionCreators.replaceComponentQuery({
        query: ``,
        componentPath: c.componentPath,
      })
      queueQueriesForPageComponent(c.componentPath)
    }
  })

  // If a component had static query and it doesn't have it
  // anymore - update the store
  staticQueryComponents.forEach(c => {
    if (c.query !== `` && !queries.has(c.componentPath)) {
      debug(`Static query was removed from ${c.componentPath}`)
      store.dispatch({
        type: `REMOVE_STATIC_QUERY`,
        payload: c.jsonName,
      })
      boundActionCreators.deleteComponentsDependencies([c.jsonName])
    }
  })
}

const handleQuery = (
  { components, staticQueryComponents },
  query,
  component
) => {
  // If this is page query
  if (components.has(component)) {
    if (components.get(component).query !== query.text) {
      boundActionCreators.replaceComponentQuery({
        query: query.text,
        componentPath: component,
      })

      debug(
        `Page query in ${component} ${
          components.get(component).query.length === 0
            ? `was added`
            : `has changed`
        }.`
      )
      queueQueriesForPageComponent(component)
    }
    return true

    // Add action / reducer + watch staticquery files
  } else if (query.isStaticQuery) {
    const isNewQuery = !staticQueryComponents.has(query.jsonName)
    if (
      isNewQuery ||
      staticQueryComponents.get(query.jsonName).query !== query.text
    ) {
      boundActionCreators.replaceStaticQuery({
        name: query.name,
        componentPath: query.path,
        id: query.jsonName,
        jsonName: query.jsonName,
        query: query.text,
        hash: query.hash,
      })

      debug(
        `Static query in ${component} ${
          isNewQuery ? `was added` : `has changed`
        }.`
      )

      boundActionCreators.deleteComponentsDependencies([query.jsonName])
      queueQueryForPathname(query.jsonName)<commit_msg>Check static query before page query so any static query inside page component is processed (#6597)<commit_after>/** *
 * Jobs of this module
 * - Maintain the list of components in the Redux store. So monitor new components
 *   and add/remove components.
 * - Watch components for query changes and extract these and update the store.
 * - Ensure all page queries are run as part of bootstrap and report back when
 *   this is done
 * - Whenever a query changes, re-run all pages that rely on this query.
 ***/

const _ = require(`lodash`)
const chokidar = require(`chokidar`)
const path = require(`path`)
const slash = require(`slash`)

const { store } = require(`../../redux/`)
const { boundActionCreators } = require(`../../redux/actions`)
const queryCompiler = require(`./query-compiler`).default
const report = require(`gatsby-cli/lib/reporter`)
const {
  queueQueryForPathname,
  runQueuedActions: runQueuedQueries,
} = require(`./page-query-runner`)
const debug = require(`debug`)(`gatsby:query-watcher`)

const getQueriesSnapshot = () => {
  const state = store.getState()

  const snapshot = {
    components: new Map(state.components),
    staticQueryComponents: new Map(state.staticQueryComponents),
  }

  return snapshot
}

const handleComponentsWithRemovedQueries = (
  { components, staticQueryComponents },
  queries
) => {
  // If a component previously with a query now doesn't — update the
  // store.
  components.forEach(c => {
    if (c.query !== `` && !queries.has(c.componentPath)) {
      debug(`Page query was removed from ${c.componentPath}`)
      boundActionCreators.replaceComponentQuery({
        query: ``,
        componentPath: c.componentPath,
      })
      queueQueriesForPageComponent(c.componentPath)
    }
  })

  // If a component had static query and it doesn't have it
  // anymore - update the store
  staticQueryComponents.forEach(c => {
    if (c.query !== `` && !queries.has(c.componentPath)) {
      debug(`Static query was removed from ${c.componentPath}`)
      store.dispatch({
        type: `REMOVE_STATIC_QUERY`,
        payload: c.jsonName,
      })
      boundActionCreators.deleteComponentsDependencies([c.jsonName])
    }
  })
}

const handleQuery = (
  { components, staticQueryComponents },
  query,
  component
) => {
  // If this is a static query
  // Add action / reducer + watch staticquery files
  if (query.isStaticQuery) {
    const isNewQuery = !staticQueryComponents.has(query.jsonName)
    if (
      isNewQuery ||
      staticQueryComponents.get(query.jsonName).query !== query.text
    ) {
      boundActionCreators.replaceStaticQuery({
        name: query.name,
        componentPath: query.path,
        id: query.jsonName,
        jsonName: query.jsonName,
        query: query.text,
        hash: query.hash,
      })

      debug(
        `Static query in ${component} ${
          isNewQuery ? `was added` : `has changed`
        }.`
      )

      boundActionCreators.deleteComponentsDependencies([query.jsonName])
      queueQueryForPathname(query.jsonName)
    }
    return true

    // If this is page query
  } else if (components.has(component)) {
    if (components.get(component).query !== query.text) {
      boundActionCreators.replaceComponentQuery({
        query: query.text,
        componentPath: component,
      })

      debug(
        `Page query in ${component} ${
          components.get(component).query.length === 0
            ? `was added`
            : `has changed`
        }.`
      )
      queueQueriesForPageComponent(component)
{'fraction_non_alphanumeric': 0.11851745438873725, 'fraction_numerical': 0.00086194512282718, 'mean_word_length': 2.9266779469825157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '375094', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1957, 'n_words': 516}