'Call function stored as class property
I have a class Process
that takes in a type of object () => void
in its constructor. Whenever I pass this into the object, all appears fine and well, however later I attempt to call it from a "Kernel" singleton object that iterates over all the Process[]
objects stored in my Scheduler
class. How do I properly call this function? Bonus, is there a way to make the function on the Process
class able to accept other types of functions, perhaps with arguments, outside adding additional 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 idea for this Kernel
object is to control how often the tasks, added in the scheduler, are actually run. I'm expecting the executeTasks()
function to properly trigger the .run()
stored in the map obtained 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 block contains the function that I'm attempting to call. For the sake of testing and brevity, I've set it up to add the new Process
in the constructor, namely because I want to be sure I'm 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 attempting 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)
}
}
Solution 1:[1]
The problem was the executeTasks()
function. It was iterating over the map improperly. The correct way to iterate over a map is this. It was resulting in a key
of 0
instead of the expected "someID"
string.
for (let [, value] of this.scheduler.taskQueue) {
value.run()
}
// OR This
this.scheduler.taskQueue.forEach(x => x.run())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | xTwisteDx |