File Coverage

lib/Neo4j/Driver/Events.pm
Criterion Covered Total %
statement 59 59 100.0
branch 24 24 100.0
condition 2 2 100.0
subroutine 16 16 100.0
pod 0 5 100.0
total 101 106 100.0


line stmt bran cond sub pod time code
1 17     17   286 use 5.010;
  17         51  
2 17     17   75 use strict;
  17         26  
  17         337  
3 17     17   69 use warnings;
  17         48  
  17         520  
4 17     17   104 use utf8;
  17         32  
  17         132  
5              
6             package Neo4j::Driver::Events;
7             # ABSTRACT: Event manager for Neo4j::Driver plug-ins
8             $Neo4j::Driver::Events::VERSION = '0.39';
9              
10             # This package is not part of the public Neo4j::Driver API.
11             # (except as far as documented in Plugin.pm)
12              
13              
14 17     17   1034 use Carp qw(croak);
  17         41  
  17         1473  
15             our @CARP_NOT = qw(
16             Neo4j::Driver::Result::Bolt
17             Neo4j::Driver::Result::Jolt
18             Neo4j::Driver::Result::JSON
19             Neo4j::Driver::Result::Text
20             );
21              
22 17     17   129 use Scalar::Util qw(weaken);
  17         39  
  17         14114  
23              
24              
25             our $STACK_TRACE = 0; # die with stack trace on error; for debugging only
26              
27              
28             sub new {
29             # uncoverable pod
30 177     177 0 27840 my ($class) = @_;
31            
32 177         338 my $self = bless {}, $class;
33 177         442 $self->_init_default_handlers;
34 177         587 return $self;
35             }
36              
37              
38             # Set up the default handlers for generic events when creating the manager.
39             sub _init_default_handlers {
40 177     177   293 my ($self) = @_;
41            
42             $self->{default_handlers}->{error} = sub {
43             # Unlike regular handlers, default handlers don't receive a callback.
44 25     25   57 my ($error) = @_;
45            
46 25 100       72 die $error->trace if $STACK_TRACE;
47            
48             # Join all errors into a multi-line string for backwards compatibility with pre-0.36.
49 24         34 my @errors;
50 24         31 do { push @errors, $error } while $error = $error->related;
  33         170  
51 24         163 @errors = map { $_->as_string } @errors;
  33         118  
52 24 100       558 croak join "\n", @errors if $self->{die_on_error};
53 2         28 Carp::carp join "\n", @errors;
54 177         1039 };
55 177         601 weaken $self;
56             }
57              
58              
59             sub add_event_handler {
60             # uncoverable pod (see Deprecations.pod)
61 1     1 0 948 warnings::warnif deprecated => __PACKAGE__ . "->add_event_handler() is deprecated";
62 1         1001 shift->add_handler(@_);
63             }
64              
65              
66             sub add_handler {
67             # uncoverable pod (see Plugin.pm)
68 103     103 0 6350 my ($self, $event, $handler, @extra) = @_;
69            
70 103 100       258 croak "Too many arguments for method 'add_handler'" if @extra;
71 101 100       290 croak "Event handler must be a subroutine reference" unless ref $handler eq 'CODE';
72 98 100       237 croak "Event name must be defined" unless defined $event;
73            
74 97         143 push @{$self->{handlers}->{$event}}, $handler;
  97         500  
75             }
76              
77              
78             sub trigger_event {
79             # uncoverable pod (see Deprecations.pod)
80 1     1 0 1084 warnings::warnif deprecated => __PACKAGE__ . "->trigger_event() is deprecated";
81 1         960 shift->trigger(@_);
82             }
83              
84              
85             sub trigger {
86             # uncoverable pod (see Plugin.pm)
87 232     232 0 16997 my ($self, $event, @params) = @_;
88            
89 232         453 my $default_handler = $self->{default_handlers}->{$event};
90 232 100       748 my $handlers = $self->{handlers}->{$event}
    100          
91             or return $default_handler ? $default_handler->(@params) : ();
92            
93 188   100 3   512 my $callback = $default_handler // sub {};
94 188         378 for my $handler ( reverse @$handlers ) {
95 195         241 my $continue = $callback;
96 195     192   824 $callback = sub { $handler->($continue, @params) };
  192         520  
97             }
98 188         422 return $callback->();
99            
100             # Right now, ALL events get a continuation callback.
101             # But this will almost certainly change eventually.
102             }
103              
104              
105             # Tell a new plugin to register itself using this manager.
106             sub _register_plugin {
107 87     87   8376 my ($self, $plugin) = @_;
108            
109 87 100       682 croak "Can't locate object method new() via package $plugin (perhaps you forgot to load \"$plugin\"?)" unless $plugin->can('new');
110 85 100       681 croak "Package $plugin is not a Neo4j::Driver::Plugin" unless $plugin->DOES('Neo4j::Driver::Plugin');
111 83 100       397 croak "Method register() not implemented by package $plugin (is this a Neo4j::Driver plug-in?)" unless $plugin->can('register');
112 82 100       265 warnings::warnif deprecated => "Neo4j::Driver->plugin() with module name is deprecated" if ref $plugin eq '';
113            
114 82 100       1209 $plugin = $plugin->new if ref $plugin eq '';
115 82         403 $plugin->register($self);
116             }
117              
118              
119             1;