Skip to content

Instantly share code, notes, and snippets.

@davidgrenier
Last active December 29, 2015 09:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidgrenier/7649065 to your computer and use it in GitHub Desktop.
Save davidgrenier/7649065 to your computer and use it in GitHub Desktop.
type FileChange =
| Changed of string
| Deleted of string
| Created of string
| Renamed of string * string
let watch folder =
async {
use fsw = new System.IO.FileSystemWatcher(folder, "*.fsx", EnableRaisingEvents = true)
let created = fsw.Created |> Event.map (fun x -> Created x.FullPath)
let changed = fsw.Changed |> Event.pairwise |> Event.map (fun (_, x) -> Changed x.FullPath)
let deleted = fsw.Deleted |> Event.map (fun x -> Deleted x.FullPath)
let renamed = fsw.Renamed |> Event.map (fun x -> Renamed(x.OldFullPath, x.FullPath))
let merged =
created
|> Event.merge changed
|> Event.merge deleted
|> Event.merge renamed
while true do
let! change = Async.AwaitEvent merged
printfn "%A" change
} |> Async.Start
watch "C:\Scripts"
@davidgrenier
Copy link
Author

Be careful there I'm thinking you could be missing events this way.

Might be safer to subscribe directly instead and push as messages to a mailboxprocessor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment