all files / synchronizator/src/action/ Handler.js

0% Statements 0/36
0% Branches 0/9
0% Functions 0/6
0% Lines 0/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117                                                                                                                                                                                                                                         
let rimraf = require('rimraf');
let fs = require('fs');
let filecopy = require('filecopy');
let fifo = require('fifo');
 
/**
 * Handles actions
 *
 * @author Jan Busfy <jan.busfy@unitedclassifieds.sk>
 */
module.exports = function(srcDir, destDir)
{
    /**
     * Private context
     *
     * @var Object
     */
    let _this = {};
    
    /**
     * Source directory
     *
     * @var String
     */
    _this.srcDir = srcDir;
    
    /**
     * Destination directory
     *
     * @var String
     */
    _this.destDir = destDir;
    
    /**
     * FIFO front of actions to handle
     *
     * @var fifo
     */
    _this.actionsToHandle = fifo();
    
    
    /**
     * Handle loop
     */
    _this.handleActionLoop = function() {
        let action = _this.actionsToHandle.shift();
      
        if (!action) {
            setTimeout(function() {
                _this.handleActionLoop();
            }, 250);
            return;
        }
      
        _this.handleAction(action, function() {
            _this.handleActionLoop();
        });
    };
    
    /**
     * Handle single action
     *
     * @param ActionData actionData - action to handle
     * @param Function onFinish() - called when action was handled
     */
    _this.handleAction = function(actionData, onFinish) {
        let remoteFilePath = destDir + actionData.getPath().substring(srcDir.length);
      
        switch (actionData.getType()) {
            case 'add':
            case 'change':
                filecopy(
                    actionData.getPath(),
                    remoteFilePath,
                    {
                        mkdirp: true
                    }
                ).then(() => {
                    onFinish();
                });
                break;
              
            case 'unlink':
                fs.unlink(remoteFilePath, (error) => {
                    onFinish();
                });
                break;
              
            case 'addDir':
            case 'changeDir':
                onFinish();
                break;
 
            case 'unlinkDir':
                rimraf(remoteFilePath, () => {
                    onFinish();
                });
                break;
                
            default:
                console.log("Unhandled event: " + actionData.getType());
                onFinish();
        }
    };
    
    /**
     * Adds new action
     *
     * @param ActionData actionData - action to add
     */
    this.add = function(actionData)
    {
        _this.actionsToHandle.push(actionData);
    };
    
    _this.handleActionLoop();
};