mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2026-04-29 03:37:38 +02:00
worker file split, sending new questions to peers instantly
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { parentPort } from 'node:worker_threads'
|
||||
import { QuestionDb } from '../../types/basicTypes'
|
||||
import { Edits, editDb } from '../../utils/actions'
|
||||
import logger from '../../utils/logger'
|
||||
|
||||
export type DbEditTaskObject = {
|
||||
type: 'dbEdit'
|
||||
data: { dbIndex: number; edits: Edits }
|
||||
}
|
||||
|
||||
export const handleDbEdit = async (
|
||||
qdbs: QuestionDb[],
|
||||
msg: DbEditTaskObject,
|
||||
workerIndex: number,
|
||||
setQdbs: (newVal: Array<QuestionDb>) => void
|
||||
): Promise<void> => {
|
||||
const { dbIndex, edits }: { dbIndex: number; edits: Edits } = msg.data
|
||||
const { resultDb } = editDb(qdbs[dbIndex], edits)
|
||||
setQdbs(
|
||||
qdbs.map((qdb, i) => {
|
||||
if (i === dbIndex) {
|
||||
return resultDb
|
||||
} else {
|
||||
return qdb
|
||||
}
|
||||
})
|
||||
)
|
||||
logger.DebugLog(`Worker db edit ${workerIndex}`, 'worker update', 1)
|
||||
|
||||
parentPort.postMessage({
|
||||
msg: `From thread #${workerIndex}: db edit`,
|
||||
workerIndex: workerIndex,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user