| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | const path = require("path");
|
|---|
| 8 | const DirectoryWatcher = require("./DirectoryWatcher");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("./index").EventMap} EventMap */
|
|---|
| 11 | /** @typedef {import("./DirectoryWatcher").DirectoryWatcherOptions} DirectoryWatcherOptions */
|
|---|
| 12 | /** @typedef {import("./DirectoryWatcher").DirectoryWatcherEvents} DirectoryWatcherEvents */
|
|---|
| 13 | /** @typedef {import("./DirectoryWatcher").FileWatcherEvents} FileWatcherEvents */
|
|---|
| 14 | /**
|
|---|
| 15 | * @template {EventMap} T
|
|---|
| 16 | * @typedef {import("./DirectoryWatcher").Watcher<T>} Watcher
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | class WatcherManager {
|
|---|
| 20 | /**
|
|---|
| 21 | * @param {DirectoryWatcherOptions=} options options
|
|---|
| 22 | */
|
|---|
| 23 | constructor(options = {}) {
|
|---|
| 24 | this.options = options;
|
|---|
| 25 | /** @type {Map<string, DirectoryWatcher>} */
|
|---|
| 26 | this.directoryWatchers = new Map();
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * @param {string} directory a directory
|
|---|
| 31 | * @returns {DirectoryWatcher} a directory watcher
|
|---|
| 32 | */
|
|---|
| 33 | getDirectoryWatcher(directory) {
|
|---|
| 34 | const watcher = this.directoryWatchers.get(directory);
|
|---|
| 35 | if (watcher === undefined) {
|
|---|
| 36 | const newWatcher = new DirectoryWatcher(this, directory, this.options);
|
|---|
| 37 | this.directoryWatchers.set(directory, newWatcher);
|
|---|
| 38 | newWatcher.on("closed", () => {
|
|---|
| 39 | this.directoryWatchers.delete(directory);
|
|---|
| 40 | });
|
|---|
| 41 | return newWatcher;
|
|---|
| 42 | }
|
|---|
| 43 | return watcher;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * @param {string} file file
|
|---|
| 48 | * @param {number=} startTime start time
|
|---|
| 49 | * @returns {Watcher<FileWatcherEvents> | null} watcher or null if file has no directory
|
|---|
| 50 | */
|
|---|
| 51 | watchFile(file, startTime) {
|
|---|
| 52 | const directory = path.dirname(file);
|
|---|
| 53 | if (directory === file) return null;
|
|---|
| 54 | return this.getDirectoryWatcher(directory).watch(file, startTime);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * @param {string} directory directory
|
|---|
| 59 | * @param {number=} startTime start time
|
|---|
| 60 | * @returns {Watcher<DirectoryWatcherEvents>} watcher
|
|---|
| 61 | */
|
|---|
| 62 | watchDirectory(directory, startTime) {
|
|---|
| 63 | return this.getDirectoryWatcher(directory).watch(directory, startTime);
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | const watcherManagers = new WeakMap();
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * @param {DirectoryWatcherOptions} options options
|
|---|
| 71 | * @returns {WatcherManager} the watcher manager
|
|---|
| 72 | */
|
|---|
| 73 | module.exports = (options) => {
|
|---|
| 74 | const watcherManager = watcherManagers.get(options);
|
|---|
| 75 | if (watcherManager !== undefined) return watcherManager;
|
|---|
| 76 | const newWatcherManager = new WatcherManager(options);
|
|---|
| 77 | watcherManagers.set(options, newWatcherManager);
|
|---|
| 78 | return newWatcherManager;
|
|---|
| 79 | };
|
|---|
| 80 | module.exports.WatcherManager = WatcherManager;
|
|---|