UNPKG

@sammwy/milkshake

Version:

<p align="center"> <img src="https://raw.githubusercontent.com/sammwyy/milkshake/master/docs/milkshake.png" height="64px"> <h1 align="center">MilkShake</h1> <p align="center"> <b>Automatize Tasks, Compile, concatenate and minify automaticall

49 lines (42 loc) 1.27 kB
const fs = require("fs"); const path = require("path"); class Watcher { constructor () { this.events = new Map(); this.directories = []; this.timeouts = new Set(); } emit (eventName, ...args) { if (this.timeouts.has(args[0])) { return; } else { this.timeouts.add(args[0]); setTimeout(() => { this.timeouts.delete(args[0]); }, 100 ); } let callbacks = this.events.get(eventName) || []; for (let callback of callbacks) { callback(...args); } } on (eventName, callback){ let callbacks = this.events.get(eventName) || []; callbacks.push(callback); this.events.set(eventName, callbacks); } watch (directory) { if (this.directories.includes(directory)) { return; } else { this.directories.push(directory); } fs.watch(directory, (eventType, fileName) => { setTimeout(() => { this.emit("*", directory, fileName); this.emit(eventType, directory, fileName) }, 100); }); } } module.exports = Watcher;