Code Monkey home page Code Monkey logo

fswatch's Introduction

FSWatch

Build Status Dub version Dub downloads

A cross platform library simplifying watching for file changes using a non-blocking interface using the Win32 API, inotify or std.file.dirEntries as fallback.

Comparison between implementations

Platform Watching Directory Watching File
Windows ReadDirectoryChangesW (very fast, accurate) polling using std.file.timeLastModified and std.file.exists (fast, not accurate for quick write)
Linux inotify (very fast, accurate) inotify (very fast, accurate)
Other polling using std.file.dirEntries (slow, not accurate) polling using std.file.timeLastModified and std.file.exists (fast, not accurate for quick write)

To force usage of the std.file polling implementation you can just add version = FSWForcePoll;

Don't do this unless you have a specific reason as you will lose speed and accuracy!

Example

void main()
{
	// Initializes a FileWatch instance to watch on `project1/` (first argument) recursively (second argument)
	auto watcher = FileWatch("project1/", true);
	while (true)
	{
		// This will fetch all queued events or an empty array if there are none
		foreach (event; watcher.getEvents())
		{
			// paths are relative to the watched directory in most cases
			if (event.path == "dub.json" || event.path == "dub.sdl")
			{
				if (event.type == FileChangeEventType.create)
				{
					// dub.json or dub.sdl got created
				}
				else if (event.type == FileChangeEventType.modify)
				{
					// dub.json or dub.sdl got modified
				}
				else if (event.type == FileChangeEventType.remove)
				{
					// dub.json or dub.sdl got deleted
				}
			}
			if (event.type == FileChangeEventType.rename)
			{
				// The file `event.path` has been removed to `event.newPath`
			}
			else if (event.type == FileChangeEventType.createSelf)
			{
				// the folder we are watching has been created
			}
			else if (event.type == FileChangeEventType.removeSelf)
			{
				// the folder we are watching has been deleted or renamed
			}
		}
		// ...
		myapp.handleInput();
		myapp.update();
		myapp.draw();
	}
}

fswatch's People

Contributors

aravindavk avatar dkorpel avatar framecca avatar freeslave avatar lempiji avatar webfreak001 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.