如何:在执行循环操作时,使数据与外部更改保持一致

时间:2019-06-01 22:21:11

标签: javascript reactjs redux immutable.js

总而言之,我基本上是在创建一个Process Scheduler Simulator。正如标签所暗示的那样,我正在使用React + Redux + ImmutableJS。首先,请允许我承认我在这些工具上没有太多经验,因为我正在本项目中使用它们进行学习。

问题:我模拟的一种流程调度算法是最短作业优先(SJ),这是它的操作,称为:

SJF Action-驻留在Actions.js文件中 https://jsfiddle.net/p7rs93kf/

export const algorithmSJF = (coreList, processList, finishedProcessList, isFirst) => (dispatch) => {
if (isFirst) {
    let sortedProcessList = sortList(processList, 'totalExecutionTime')
    processList = sortedProcessList
}
setTimeout(() => {
    let availableCores = getAvailableCoreAmmount(coreList)

    //Keep running until empty process list
    if (processList) {

        // Allocating Process to Cores
        for (let i = 0; i < coreList.length; i++) {
            if(coreList[i].status === 'waiting for process' && availableCores > 0) {
                for (let j = 0; j < processList.length; j++) {
                    if(processList[j].status === 'ready') {
                        let freeProcessId = processList[j].id
                        processList[j].status = 'executing'
                        if(freeProcessId >= 0) {
                            coreList[i].processInExecution = 'P' + freeProcessId
                            coreList[i].status = 'executing'
                            coreList[i].processInExecutionRemainingTime = processList[j].remainingExecutionTime
                            availableCores--
                        } else {
                            coreList[i].processInExecution = 'none'
                            coreList[i].status = 'waiting for process'
                            coreList[i].processInExecutionRemainingTime = -1
                            availableCores++
                        }
                        break
                    } 
                }
            }
        }
        dispatch(updateCoreProcessLists({coreList, processList, finishedProcessList}))

        //Remove 0 Remaining Time Process
        for (let i = 0; i < coreList.length; i++) {
                let runningProcessId = coreList[i].processInExecution.substring(1)
                if (runningProcessId !== 'none'.substring(1)) {
                    let currentProcess = processList.find(process => process.id.toString() === runningProcessId)
                    if(currentProcess.remainingExecutionTime === 0) {
                        coreList[i].processInExecution = 'none'
                        coreList[i].status = 'waiting for process'
                        coreList[i].processInExecutionRemainingTime = -1
                        availableCores++
                    }
                }
            }

        let currentFinishedProcesses = processList.filter(function(process) {
            return process.remainingExecutionTime === 0
        })
        currentFinishedProcesses = currentFinishedProcesses.map(function(process){
            process.status = 'finished'
            return process
        })

        Array.prototype.push.apply(finishedProcessList, currentFinishedProcesses)

        processList = processList.filter(function(process) {
            return process.remainingExecutionTime > 0
        })

        dispatch(updateCoreProcessLists({coreList, processList, finishedProcessList}))

        // Updates Executing Processes
        for (let i = 0; i < coreList.length; i++) {
            if(coreList[i].status === 'executing') {
                coreList[i].processInExecutionRemainingTime--
            }
        }
        for (let i = 0; i < processList.length; i++) {
            if(processList[i].status === 'executing') {
                processList[i].remainingExecutionTime--
            }
        }

        dispatch(updateCoreProcessLists({coreList, processList, finishedProcessList}))

        dispatch(algorithmSJF(coreList, processList, finishedProcessList, false))
    } else {
        setTimeout(() => {
            this.props.history.push('/')
        }, 10000)
    }
}, 1000)

}

添加随机过程功能-驻留在调度程序组件内部 https://jsfiddle.net/3uL5r9av/

let totalExecutionTime = randomIntFromInterval(4, 20)
let priority = randomIntFromInterval(0, 3)
let bytesToExecute = randomIntFromInterval(32, 1024)
let processList = this.state.processList
let id = Math.max.apply(Math, 
         this.state.processList.map(function(process) { return 
         process.id; }))
let newProcess = {name: "P"+(id+1), id: id + 1, status: 'ready', 
                 totalExecutionTime: totalExecutionTime, 
                 remainingExecutionTime: 
                 totalExecutionTime, priority: priority, inserted: true, 
                 bytes: bytesToExecute}

 processList = [...this.state.processList, newProcess]

 if (this.state.algorithm === 'sjf') {
     processList = sortList(processList, 'totalExecutionTime')
 }

 //ISSUE HERE
 this.props.updateProcessList({processList})
 this.setState({
    processList
 })

在用户菜单上,当所有事情都在运行时,我有一个按钮,它将一个随机进程添加到“进程”列表中。这里的问题是,将新流程添加到列表的函数会更新列表(甚至新元素出现在视图上),但是当它返回到SJF Action并进行分派调用时,processList不会更新。我了解这与“自呼”事实和使用“ local” processList变量有关,但是有关如何解决或正确组织它的任何想法?

请随时询问更多信息,我将很乐意编辑问题。 预先感谢

0 个答案:

没有答案