Repository: gatsbyjs/gatsby
File: packages/gatsby/src/query/better-queue-custom-store.ts (TypeScript)

Code before:
      let needToUpdatePriorityKeys = false
      if (oldTask) {
        const oldPriority = taskIdToPriority.get(taskId)

        if (oldPriority && oldPriority !== priority) {
          const oldPriorityTasks = queueMap.get(oldPriority) || []
          oldPriorityTasks.splice(oldPriorityTasks.indexOf(taskId), 1)

          if (
            addTaskWithPriority(taskId, priority) ||
            oldPriorityTasks.length === 0
          ) {
            needToUpdatePriorityKeys = true
          }
        }
      } else {
        needToUpdatePriorityKeys = addTaskWithPriority(taskId, priority)
      }

      if (needToUpdatePriorityKeys) {
        updatePriorityKeys()
      }
      cb(null)
    },
    takeFirstN: function (n, cb): void {
      const lockId = `` + uuid++
      let remainingTasks = n
      let needToUpdatePriorityKeys = false
      let haveSomeTasks = false
      const tasksToRun: RunningTasks = {}

      for (const priority of priorityKeys) {
        const tasksWithSamePriority = queueMap.get(priority)
        const grabbedTaskIds =
          tasksWithSamePriority?.splice(0, remainingTasks) ?? []
        grabbedTaskIds.forEach(taskId => {
          // add task to task that will run
          // and remove it from waiting list
          const task = tasks.get(taskId)
          if (task) {
            tasksToRun[taskId] = task
            tasks.delete(taskId)
            taskIdToPriority.delete(taskId)
            haveSomeTasks = true
          }
        })

        remainingTasks -= grabbedTaskIds.length
        if (tasksWithSamePriority?.length === 0) {
          queueMap.delete(priority)
          needToUpdatePriorityKeys = true

Review comment (suggestion):
⚠️ So this has changed. The interface from `better-queue` states that the `lockId` should be a string. 

Code after:
          const oldPriorityTasks = queueMap.get(oldPriority) ?? []
          oldPriorityTasks.splice(oldPriorityTasks.indexOf(taskId), 1)

          if (
            addTaskWithPriority(taskId, priority) // ||
            // oldPriorityTasks.length === 0
          ) {
            needToUpdatePriorityKeys = true
          }
        }
      } else {
        needToUpdatePriorityKeys = addTaskWithPriority(taskId, priority)
      }

      if (needToUpdatePriorityKeys) {
        updatePriorityKeys()
      }
      cb(null)
    },
    takeFirstN: function (n, cb): void {
      const lockId = String(uuid++)
      let remainingTasks = n
      let needToUpdatePriorityKeys = false
      let haveSomeTasks = false
      const tasksToRun: RunningTasks = {}

      for (const priority of priorityKeys) {
        const tasksWithSamePriority = queueMap.get(priority)
        const grabbedTaskIds =
          tasksWithSamePriority?.splice(0, remainingTasks) ?? []
        grabbedTaskIds.forEach(taskId => {
          // add task to task that will run
          // and remove it from waiting list
          const task = tasks.get(taskId)
          if (task) {
            tasksToRun[taskId] = task
            tasks.delete(taskId)
            taskIdToPriority.delete(taskId)
            haveSomeTasks = true
          }
        })

        remainingTasks -= grabbedTaskIds.length
        if (tasksWithSamePriority?.length === 0) {
          queueMap.delete(priority)
          needToUpdatePriorityKeys = true
        }
        if (remainingTasks <= 0) {
          break
        }
      }