Within my codebase, there exists a class named Process
. This class has a constructor that takes in a type of object () => void
. Initially, everything seemed to be working perfectly fine when I passed this object into the class. However, issues arose when I tried to call it from a singleton object called "Kernel." The Kernel iterates over all the Process[]
objects stored within my Scheduler
class. How can I correctly invoke this function? Additionally, is there a way to make the function in the Process
class capable of accepting other types of functions with arguments without adding extra properties?
Process
export class Process {
id: Id<Process>
run: () => void
cpuUsed?: number
priority: ProcessPriority
constructor(id: Id<Process>, priority: ProcessPriority, task: () => void) {
this.id = id
this.priority = priority
this.run = task
}
}
Kernel
The primary purpose of the Kernel
object is to manage how often tasks added to the scheduler are executed. I anticipate that the executeTasks()
function will correctly trigger the .run()
method stored in the map retrieved from the scheduler.
export class Kernel implements ITaskManager {
private static _instance?: Kernel
static getInstance() {
if (this._instance) return this._instance
return this._instance = new Kernel()
}
scheduler: Scheduler
executeTasks() {
console.log("Task count: " + this.scheduler.taskQueue.size)
for(const value in Array.from(this.scheduler.taskQueue.keys())) {
let task = this.scheduler.taskQueue.get(value as Id<Process>)
task?.run()
}
}
kill(): void {
throw new Error("Method not implemented.")
}
pause(): void {
throw new Error("Method not implemented.")
}
skip(): void {
throw new Error("Method not implemented.")
}
constructor() {
console.log("Constructed scheduler.")
this.scheduler = new Scheduler()
}
}
Scheduler
This section contains the function that I'm attempting to invoke. For testing and brevity purposes, I've set it up to add a new instance of Process
in the constructor. This approach helps me confirm that I am doing it correctly.
export class Scheduler {
taskQueue: Map<Id<Process>, Process>
constructor() {
this.taskQueue = new Map()
let tempProcess = new Process("someID" as Id<Process>, ProcessPriority.INDIFFERENT, this.someFunction)
this.addTask(tempProcess)
}
// This is the function I'm trying to call.
someFunction = function(): void {
console.log("It kinda works.")
}
addTask(task: Process) {
console.log("adding task: " + task.id + " | " + task)
this.taskQueue.set(task.id, task)
}
getTask(id: Id<Process>): Process | undefined{
return this.taskQueue.get(id)
}
}