mirror of
				https://gitlab.com/MrFry/mrfrys-node-server
				synced 2025-04-01 20:24:18 +02:00 
			
		
		
		
	worker db edit/update refactor
This commit is contained in:
		@@ -1,6 +1,12 @@
 | 
			
		||||
import { isMainThread, parentPort, workerData } from 'worker_threads'
 | 
			
		||||
import logger from './logger'
 | 
			
		||||
import { Question, QuestionData, Subject } from '../types/basicTypes'
 | 
			
		||||
import {
 | 
			
		||||
  Question,
 | 
			
		||||
  QuestionData,
 | 
			
		||||
  QuestionDb,
 | 
			
		||||
  Subject,
 | 
			
		||||
} from '../types/basicTypes'
 | 
			
		||||
import { editDb } from './actions'
 | 
			
		||||
 | 
			
		||||
interface SearchResultQuestion extends Question {
 | 
			
		||||
  match: number
 | 
			
		||||
@@ -581,7 +587,7 @@ function doSearch(
 | 
			
		||||
 | 
			
		||||
if (!isMainThread) {
 | 
			
		||||
  const { workerIndex } = workerData
 | 
			
		||||
  let qdbs: Array<any> = workerData.initData
 | 
			
		||||
  let qdbs: Array<QuestionDb> = workerData.initData
 | 
			
		||||
 | 
			
		||||
  logger.Log(
 | 
			
		||||
    `[THREAD #${workerIndex}]: Worker ${workerIndex} reporting for duty`
 | 
			
		||||
@@ -670,9 +676,64 @@ if (!isMainThread) {
 | 
			
		||||
      //     !isNaN(index) ? `#${index}` : ''
 | 
			
		||||
      //   }done!`
 | 
			
		||||
      // )
 | 
			
		||||
    } else if (msg.type === 'update') {
 | 
			
		||||
      qdbs = msg.qdbs
 | 
			
		||||
      logger.DebugLog(`Worker update ${workerIndex}`, 'worker update', 1)
 | 
			
		||||
    } else if (msg.type === 'dbEdit') {
 | 
			
		||||
      const { dbIndex, edits } = msg.data
 | 
			
		||||
      const { resultDb } = editDb(qdbs[dbIndex], edits)
 | 
			
		||||
      qdbs[dbIndex] = resultDb
 | 
			
		||||
      logger.DebugLog(`Worker db edit ${workerIndex}`, 'worker update', 1)
 | 
			
		||||
 | 
			
		||||
      parentPort.postMessage({
 | 
			
		||||
        msg: `From thread #${workerIndex}: db edit`,
 | 
			
		||||
        workerIndex: workerIndex,
 | 
			
		||||
      })
 | 
			
		||||
    } else if (msg.type === 'newQuestions') {
 | 
			
		||||
      const { subjName, qdbIndex, newQuestions } = msg.data
 | 
			
		||||
      let added = false
 | 
			
		||||
      qdbs = qdbs.map((qdb) => {
 | 
			
		||||
        if (qdb.index === qdbIndex) {
 | 
			
		||||
          return {
 | 
			
		||||
            ...qdb,
 | 
			
		||||
            data: qdb.data.map((subj) => {
 | 
			
		||||
              if (subj.Name === subjName) {
 | 
			
		||||
                added = true
 | 
			
		||||
                return {
 | 
			
		||||
                  Name: subj.Name,
 | 
			
		||||
                  Questions: [...subj.Questions, ...newQuestions],
 | 
			
		||||
                }
 | 
			
		||||
              } else {
 | 
			
		||||
                return subj
 | 
			
		||||
              }
 | 
			
		||||
            }),
 | 
			
		||||
          }
 | 
			
		||||
        } else {
 | 
			
		||||
          return qdb
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
 | 
			
		||||
      if (!added) {
 | 
			
		||||
        qdbs = qdbs.map((qdb) => {
 | 
			
		||||
          if (qdb.index === qdbIndex) {
 | 
			
		||||
            return {
 | 
			
		||||
              ...qdb,
 | 
			
		||||
              data: [
 | 
			
		||||
                ...qdb.data,
 | 
			
		||||
                {
 | 
			
		||||
                  Name: subjName,
 | 
			
		||||
                  Questions: [...newQuestions],
 | 
			
		||||
                },
 | 
			
		||||
              ],
 | 
			
		||||
            }
 | 
			
		||||
          } else {
 | 
			
		||||
            return qdb
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
      }
 | 
			
		||||
      logger.DebugLog(`Worker new question ${workerIndex}`, 'worker update', 1)
 | 
			
		||||
 | 
			
		||||
      // parentPort.postMessage({
 | 
			
		||||
      //   msg: `From thread #${workerIndex}: update done`,
 | 
			
		||||
      //   workerIndex: workerIndex,
 | 
			
		||||
      // })
 | 
			
		||||
 | 
			
		||||
      // console.log(`[THREAD #${workerIndex}]: update`)
 | 
			
		||||
    } else if (msg.type === 'newdb') {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user