Using Pool
PoolManager is a pool for repeatable or delayed task
Let's imagine you want a loop to send to your backend that your user is online.
For that we'll choose a component who's always loaded (let's take the NavBar)
import AbstractComponent from "../../Plasma/Abstract/AbstractComponent";
import Session from "../../Plasma/Security/Session";
export default class NavbarComponent extends AbstractComponent {
constructor() {
super();
this.setData({
userName: Session.getUserName()
})
this.script()
}
render() {
let template = this.getVue('include/navbar.twig')
return this.getTemplate(template, this.data)
}
script() {
}
}
First let's create our repeat method we'll call it imOnline (add it after script() {}
imOnline(controller){
//do your stuff
if (PLASMA.poolManager.isTaskExist("imOnlineLoop")) {
PLASMA.poolManager.execute("imOnlineLoop")
}
}
As you see your method need to have one param : the controller.
inside your method you'll have to call poolManager for two things :
first check if the task exist, then execute them. (this gonna create the loop)
Define the task
on your script() method we'll define our task
if (!PLASMA.poolManager.isTaskExist("imOnlineLoop")) {
PLASMA.poolManager.createTask("imOnlineLoop", this.imOnline, delay, this)
} else {
PLASMA.poolManager.startTask("imOnlineLoop")
}
first we check if the task exist, if not we'll create it. if exist we'll start it (changing route make loop to be trigger) this is why you need to be sure the loop exist or not.
Each loop has unique name , method to call, delay, component
The delay is in millis
Unload a task :
Remember the method unload on the controller ? This method is called when you change From one controller to an other. Simply add these lines inside the unload method :
PLASMA.poolManager.stopTask("imOnlineLoop")
Of course you can also call this method from everywhere. Method need the task name.
Last updated