Electron-ants help you create worker's window to execute heavy tasks without frezzing your UI.
- clone this repository
- npm install
- npm run start
#Setup
-
You need to have an worker html file used for the worker window and a worker javascript file linked
-
You need to call
Ant.Worker.init
in your main file (Entry point of your app)
Ant.Worker.init(pathToYourWorkerHtmlFile, mainWindow);
//example pathToYourWorkerHtmlFile = `file://${global.__ROOT }/worker/index.html`;
//mainWindow is a reference to your renderer window from where you gonna call your workers
- You need to call
Ant.Worker.initTask
inside your worker javascript file
Ant.Worker.initTask();
Inside your renderer process
Ant.Worker.create(yourTask, debug).then((worker) => {
console.log(`"${yourTask}" is ready`);
worker.onUpdate((payload) => {
//Executed when sendUpdate is called
console.log(`"${yourTask}" updated with this payload`, payload);
});
worker.onEnd((payload) => {
//Executed when sendEnd or worker.stop is called
console.log(`"${yourTask}" ended with this payload`, payload);
});
worker.onStop((payload) => {
//Executed when worker.stop is called
console.log(`"${yourTask}" stopped with this payload`, payload);
});
worker.onError((err) => {
//Executed when sendError is called
console.error(err);
});
worker.execute(params);
})
.catch(function(err){
console.error(err);
});
yourTask
(strin) path of your task to execute, relative to the worker html filedebug
(boolean) if true show the worker window and open the devtools window tooparams
(mixed) passed to your task's run method
Creating a task is very simple. Just create a javascript file
export default class MyTask{
constructor(workerId){
//this.intervalDelay = 1000;
//this.timeoutDelay = 1000;
}
run(params, sendUpdate, sendEnd, sendError){
//Do what you want here
}
close(done){
//Executed when the worker is stopped by an external call
done(); //You need to call done for the worker to actually stop and close the window
}
}
You can look in tasks directory to have examples
-
Normal task
Run is executed only once
-
Interval task
if
intervalDelay
is specified, run is executed inside asetInterval
. To stop the execution you have to callsendEnd
-
Timeout task
if
timeoutDelay
is specified, run is executed inside asetTimeout
. After eachsendUpdate
's callsetTimeout
is executed again. To stop the execution you have to callsendEnd
##Ant.Store
Store library inspired by electron-store but instead of saving in file it stay in memory
const defaultValues = {
'foo': 'bar'
};
const myStore = new Ant.store('myStore', defaultValues);
Now myStore
share the same API as dot-prop
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.