Fixed some cleanup related to closing documents

The cleanup was happening on document close requests rather than when
documents are actually being closed, and depending on the connection
order it may even have happened after the close request was already
handled.
Fixed the file system watcher in case a watched file is replaced

This approach seems to work for files saved with vim. Maybe it also
fixes the problem with watching tilesets saved by Photoshop.