Expresso.Watch.Files (expresso v0.1.0)

Copy Markdown View Source

Finds a change to the files of a deck

Expresso.Watch takes a snapshot of the files two times each second, and changed?/2 compares it with the snapshot before it. Expresso.DeckFile gives the list of the files.

A snapshot holds the size of each file and the time of its last change. The file system gives that time to the second, so a file that changed in the last two seconds also gets a digest of its bytes. changed?/2 compares two digests only when both snapshots have one:

  • Two changes in the same second give two different digests, so the second change is found.
  • An old file gets no digest, so a digest that goes away is not a change.

The section "The watch mode" of docs/architecture.md tells why the watch mode reads the file system, and not the events of the operating system.

Summary

Types

The state of each file of a deck

Functions

Tell whether a file changed from one snapshot to the next

Record the state of each file in a list

Types

snapshot()

@type snapshot() :: %{
  required(Path.t()) =>
    {integer(), non_neg_integer(), binary() | nil} | :missing
}

The state of each file of a deck

Functions

changed?(old, new)

@spec changed?(snapshot(), snapshot()) :: boolean()

Tell whether a file changed from one snapshot to the next

A file changed when:

  • its size or its time of change is different,
  • it appeared or it went away,
  • both snapshots have a digest, and the digests are different, or
  • its path is in only one of the two snapshots.

snapshot(paths)

@spec snapshot([Path.t()]) :: snapshot()

Record the state of each file in a list

A file that does not exist gets :missing. A new file or a removed file thus also gives a different snapshot.