<commit_before>const {
  Machine,
  actions: { assign },
} = require(`xstate`)

module.exports = Machine(
  {
    id: `pageComponents`,
    initial: `inactive`,
    context: {
      isInBootstrap: true,
      componentPath: ``,
      query: ``,
    },
    on: {
      BOOTSTRAP_FINISHED: {
        actions: `setBootstrapFinished`,
      },
      DELETE_PAGE: {
        actions: `deletePage`,
      },
      NEW_PAGE_CREATED: {
        actions: `setPage`,
      },
      QUERY_EXTRACTION_GRAPHQL_ERROR: `queryExtractionGraphQLError`,
      QUERY_EXTRACTION_BABEL_ERROR: `queryExtractionBabelError`,
    },
    states: {
      inactive: {
        on: {
          // Transient transition
          // Will transition to either 'inactiveWhileBootstrapping' or idle
          // immediately upon entering 'inactive' state if the condition is met.
          "": [
            { target: `inactiveWhileBootstrapping`, cond: `isBootstrapping` },
            { target: `idle`, cond: `isNotBootstrapping` },
          ],
        },
      },
      inactiveWhileBootstrapping: {
        on: {
          BOOTSTRAP_FINISHED: {
            target: `idle`,
            actions: `setBootstrapFinished`,
          },
          QUERY_CHANGED: `runningPageQueries`,
        },
      },
      queryExtractionGraphQLError: {
        on: {
          QUERY_DID_NOT_CHANGE: `idle`,
          QUERY_CHANGED: `runningPageQueries`,
        },
      },
      queryExtractionBabelError: {
        on: {
          QUERY_EXTRACTION_BABEL_SUCCESS: `idle`,
        },
      },
      runningPageQueries: {
        onEntry: [`setQuery`, `runPageComponentQueries`],
        on: {
          QUERIES_COMPLETE: `idle`,
        },
      },
      idle: {
        on: {
          QUERY_CHANGED: `runningPageQueries`,
        },
      },
    },
  },
  {
    guards: {
      isBootstrapping: context => context.isInBootstrap,
      isNotBootstrapping: context => !context.isInBootstrap,
    },
    actions: {
      runPageComponentQueries: (context, event) => {
        const queryUtil = require(`../../query`)
        // Wait a bit as calling this function immediately triggers
        // an Action call which Redux squawks about.
        setTimeout(() => {
          queryUtil.enqueueExtractedPageComponent(context.componentPath)
        }, 0)
      },
      setQuery: assign({
        query: (ctx, event) => {
          if (typeof event.query !== `undefined` || event.query !== null) {
            return event.query
          } else {
            return ctx.query
          }
        },
      }),
      setPage: assign({
        pages: (ctx, event) => {
          if (event.path) {
            const queryUtil = require(`../../query`)
            // Wait a bit as calling this function immediately triggers
            // an Action call which Redux squawks about.
            setTimeout(() => {
              if (!ctx.isInBootstrap) {
                queryUtil.enqueueExtractedQueryId(event.path)
                queryUtil.runQueuedQueries(event.path)
              }
            }, 0)

            return ctx.pages.concat(event.path)
          } else {
            return ctx.pages
          }
        },
      }),
      deletePage: assign({
        pages: (ctx, event) => ctx.pages.filter(p => p !== event.page.path),
      }),
      setBootstrapFinished: assign({
        isInBootstrap: false,<commit_msg>fix(gatsby): speed up and lower memory of createPages (#15503)<commit_after>const {
  Machine,
  actions: { assign },
} = require(`xstate`)

module.exports = Machine(
  {
    id: `pageComponents`,
    initial: `inactive`,
    context: {
      isInBootstrap: true,
      componentPath: ``,
      query: ``,
    },
    on: {
      BOOTSTRAP_FINISHED: {
        actions: `setBootstrapFinished`,
      },
      DELETE_PAGE: {
        actions: `deletePage`,
      },
      NEW_PAGE_CREATED: {
        actions: `setPage`,
      },
      QUERY_EXTRACTION_GRAPHQL_ERROR: `queryExtractionGraphQLError`,
      QUERY_EXTRACTION_BABEL_ERROR: `queryExtractionBabelError`,
    },
    states: {
      inactive: {
        on: {
          // Transient transition
          // Will transition to either 'inactiveWhileBootstrapping' or idle
          // immediately upon entering 'inactive' state if the condition is met.
          "": [
            { target: `inactiveWhileBootstrapping`, cond: `isBootstrapping` },
            { target: `idle`, cond: `isNotBootstrapping` },
          ],
        },
      },
      inactiveWhileBootstrapping: {
        on: {
          BOOTSTRAP_FINISHED: {
            target: `idle`,
            actions: `setBootstrapFinished`,
          },
          QUERY_CHANGED: `runningPageQueries`,
        },
      },
      queryExtractionGraphQLError: {
        on: {
          QUERY_DID_NOT_CHANGE: `idle`,
          QUERY_CHANGED: `runningPageQueries`,
        },
      },
      queryExtractionBabelError: {
        on: {
          QUERY_EXTRACTION_BABEL_SUCCESS: `idle`,
        },
      },
      runningPageQueries: {
        onEntry: [`setQuery`, `runPageComponentQueries`],
        on: {
          QUERIES_COMPLETE: `idle`,
        },
      },
      idle: {
        on: {
          QUERY_CHANGED: `runningPageQueries`,
        },
      },
    },
  },
  {
    guards: {
      isBootstrapping: context => context.isInBootstrap,
      isNotBootstrapping: context => !context.isInBootstrap,
    },
    actions: {
      runPageComponentQueries: (context, event) => {
        const queryUtil = require(`../../query`)
        // Wait a bit as calling this function immediately triggers
        // an Action call which Redux squawks about.
        setTimeout(() => {
          queryUtil.enqueueExtractedPageComponent(context.componentPath)
        }, 0)
      },
      setQuery: assign({
        query: (ctx, event) => {
          if (typeof event.query !== `undefined` || event.query !== null) {
            return event.query
          } else {
            return ctx.query
          }
        },
      }),
      setPage: assign({
        pages: (ctx, event) => {
          if (event.path) {
            const queryUtil = require(`../../query`)
            // Wait a bit as calling this function immediately triggers
            // an Action call which Redux squawks about.
            setTimeout(() => {
              if (!ctx.isInBootstrap) {
                queryUtil.enqueueExtractedQueryId(event.path)
                queryUtil.runQueuedQueries(event.path)
              }
            }, 0)
            ctx.pages.push(event.path)
            return ctx.pages
          } else {
            return ctx.pages
          }
        },
      }),
      deletePage: assign({
        pages: (ctx, event) => ctx.pages.filter(p => p !== event.page.path),
      }),
      setBootstrapFinished: assign({
        isInBootstrap: false,
{'fraction_non_alphanumeric': 0.1186764705882353, 'fraction_numerical': 0.0013235294117647058, 'mean_word_length': 1.7291332263242376, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 21, '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': '4327791', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1967, 'n_words': 384}