Skip to content

Instantly share code, notes, and snippets.

@zindel
Created February 25, 2019 18:28
Show Gist options
  • Save zindel/72082ad1e1a7242a6e52c12b55683f3c to your computer and use it in GitHub Desktop.
Save zindel/72082ad1e1a7242a6e52c12b55683f3c to your computer and use it in GitHub Desktop.
module type Impl = {
let startProcess: string => Lwt.t(result(Process.t, string));
let getFiles: Process.t => Lwt.t(list(string));
};
module Backend = (Impl: Impl) => {
type t = {
root: string,
process: Process.t,
filterFilename: string => bool,
};
let make = (root, filterFilename) =>
switch%lwt (Impl.startProcess(root)) {
| Error(error) => Lwt.return_error(error)
| Ok(process) => Lwt.return_ok({root, process, filterFilename})
};
let rec getFiles = ({root, process, filterFilename} as backend) =>
switch%lwt (Impl.getFiles(process)) {
| [] => getFiles(backend)
| files =>
let files =
files
|> List.map(filename => FS.abs_path(root, filename))
|> List.filter(filterFilename)
|> List.fold_left(
(set, filename) => StringSet.add(filename, set),
StringSet.empty,
);
if (files == StringSet.empty) {
getFiles(backend);
} else {
Lwt.return(files);
};
};
let finalize = ({process, _}) => Process.finalize(process);
};
module Watchman = Backend({
let startProcess = (_) => failwith("not impl");
let getFiles = (_) => failwith("not impl");
}: Impl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment