Coder Social home page Coder Social logo

electron-ants's Introduction

What ?

Electron-ants help you create worker's window to execute heavy tasks without frezzing your UI.

Demo

  1. clone this repository
  2. npm install
  3. npm run start

#Setup

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();

How to use

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 file
  • debug (boolean) if true show the worker window and open the devtools window too
  • params (mixed) passed to your task's run method

Tasks

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

Different types of task

  • Normal task

    Run is executed only once

  • Interval task

    if intervalDelay is specified, run is executed inside a setInterval. To stop the execution you have to call sendEnd

  • Timeout task

    if timeoutDelay is specified, run is executed inside a setTimeout. After each sendUpdate's call setTimeout is executed again. To stop the execution you have to call sendEnd

##Ant.Store

Store library inspired by electron-store but instead of saving in file it stay in memory

How to use

const defaultValues = {
    'foo': 'bar'
};

const myStore = new Ant.store('myStore', defaultValues);

Now myStore share the same API as dot-prop

#License WTFPL

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.

electron-ants's People

Contributors

geelik avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.