AnyEvent-Filesys-Notify

 view release on metacpan or  search on metacpan

lib/AnyEvent/Filesys/Notify.pm  view on Meta::CPAN

package AnyEvent::Filesys::Notify;

# ABSTRACT: An AnyEvent compatible module to monitor files/directories for changes

use Moo;
use Moo::Role ();
use MooX::late;
use namespace::autoclean;
use AnyEvent;
use Path::Iterator::Rule;
use Cwd qw/abs_path/;
use AnyEvent::Filesys::Notify::Event;
use Carp;
use Try::Tiny;

our $VERSION = '1.23';
my $AEFN = 'AnyEvent::Filesys::Notify';

has dirs         => ( is => 'ro', isa => 'ArrayRef[Str]', required => 1 );
has cb           => ( is => 'rw', isa => 'CodeRef',       required => 1 );
has interval     => ( is => 'ro', isa => 'Num',           default  => 2 );
has no_external  => ( is => 'ro', isa => 'Bool',          default  => 0 );
has backend      => ( is => 'ro', isa => 'Str',           default  => '' );
has filter       => ( is => 'rw', isa => 'RegexpRef|CodeRef' );
has parse_events => ( is => 'rw', isa => 'Bool',          default  => 0 );
has skip_subdirs => ( is => 'ro', isa => 'Bool',          default  => 0 );
has _fs_monitor  => ( is => 'rw', );
has _old_fs => ( is => 'rw', isa => 'HashRef' );
has _watcher => ( is => 'rw', );

sub BUILD {
    my $self = shift;

    $self->_old_fs( $self->_scan_fs( $self->dirs ) );

    $self->_load_backend;
    return $self->_init;    # initialize the backend
}

sub _process_events {
    my ( $self, @raw_events ) = @_;

    # Some implementations provided enough information to parse the raw events,
    # other require rescanning the file system (ie, Mac::FSEvents).
    # The original behavior was to rescan in all implementations, so we
    # have added a flag to avoid breaking old code.

    my @events;

    if ( $self->parse_events and $self->can('_parse_events') ) {
        @events =
          $self->_parse_events( sub { $self->_apply_filter(@_) }, @raw_events );
    } else {
        my $new_fs = $self->_scan_fs( $self->dirs );
        @events =
          $self->_apply_filter( $self->_diff_fs( $self->_old_fs, $new_fs ) );
        $self->_old_fs($new_fs);

        # Some backends (when not using parse_events) need to add files
        # (KQueue) or directories (Inotify2) to the watch list after they are
        # created. Give them a chance to do that here.
        $self->_post_process_events(@events)
          if $self->can('_post_process_events');
    }

    $self->cb->(@events) if @events;

    return \@events;



( run in 1.497 second using v1.01-cache-2.11-cpan-39bf76dae61 )