'use strict';

var chokidar = require('chokidar');
var debounce = require('mout/function/debounce');
var mixIn = require('mout/object/mixIn');

var signal = require('./signal');

var Watcher = function () {
    var output = signal();
    var watcher;
    var pending = [];

    var debouncedEmit = debounce(function () {
        output.push(pending);
        pending = [];
    }.bind(this), 600);

    this.listen = output.map.bind(output);

    this.watch = function (paths) {
        this.close();

        watcher = chokidar.watch(paths);

        watcher.on('change', function (path) {
            if (pending.indexOf(path) === -1) {
                pending.push(path);
                debouncedEmit();
            }
        }.bind(this));
    };

    this.close = function () {
        if (watcher !== undefined) {
            watcher.close();
        }
    };
};

Watcher.create = function () {
    return new Watcher();
};

module.exports = Watcher.create;

@@ -483,65 +483,161 @@
     
-this.close();%0A%0A        watcher = chokidar.watch(paths
+if (watcher !== undefined) %7B%0A            watcher.close();%0A        %7D%0A%0A        watcher = chokidar.watch(paths, %7B persistent: false, useFsEvents: false %7D
 );%0A
-%0A
     
@@ -656,14 +656,11 @@
 on('
-change
+raw
 ', f
@@ -668,20 +668,36 @@
 nction (
-path
+event, path, details
 ) %7B%0A    
@@ -836,136 +836,10 @@
    %7D
-.bind(this));%0A    %7D;%0A%0A    this.close = function () %7B%0A        if (watcher !== undefined) %7B%0A            watcher.close();%0A        %7D
+);
 %0A   

tweak watcher