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   304 use 5.010;
  17         60  
2 17     17   85 use strict;
  17         25  
  17         348  
3 17     17   72 use warnings;
  17         64  
  17         464  
4 17     17   86 use utf8;
  17         33  
  17         111  
5              
6             package Neo4j::Driver::Events;
7             # ABSTRACT: Event manager for Neo4j::Driver plug-ins
8             $Neo4j::Driver::Events::VERSION = '0.38';
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   1054 use Carp qw(croak);
  17         40  
  17         1548  
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   125 use Scalar::Util qw(weaken);
  17         42  
  17         14677  
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 33439 my ($class) = @_;
31            
32 177         309 my $self = bless {}, $class;
33 177         457 $self->_init_default_handlers;
34 177         645 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   279 my ($self) = @_;
41            
42             $self->{default_handlers}->{error} = sub {
43             # Unlike regular handlers, default handlers don't receive a callback.
44 23     23   54 my ($error) = @_;
45            
46 23 100       62 die $error->trace if $STACK_TRACE;
47            
48             # Join all errors into a multi-line string for backwards compatibility with pre-0.36.
49 22         31 my @errors;
50 22         33 do { push @errors, $error } while $error = $error->related;
  31         159  
51 22         118 @errors = map { $_->as_string } @errors;
  31         136  
52 22 100       436 croak join "\n", @errors if $self->{die_on_error};
53 2         23 Carp::carp join "\n", @errors;
54 177         1067 };
55 177         576 weaken $self;
56             }
57              
58              
59             sub add_event_handler {
60             # uncoverable pod (see Deprecations.pod)
61 1     1 0 951 warnings::warnif deprecated => __PACKAGE__ . "->add_event_handler() is deprecated";
62 1         1060 shift->add_handler(@_);
63             }
64              
65              
66             sub add_handler {
67             # uncoverable pod (see Plugin.pm)
68 103     103 0 10088 my ($self, $event, $handler, @extra) = @_;
69            
70 103 100       282 croak "Too many arguments for method 'add_handler'" if @extra;
71 101 100       314 croak "Event handler must be a subroutine reference" unless ref $handler eq 'CODE';
72 98 100       214 croak "Event name must be defined" unless defined $event;
73            
74 97         130 push @{$self->{handlers}->{$event}}, $handler;
  97         417  
75             }
76              
77              
78             sub trigger_event {
79             # uncoverable pod (see Deprecations.pod)
80 1     1 0 1110 warnings::warnif deprecated => __PACKAGE__ . "->trigger_event() is deprecated";
81 1         931 shift->trigger(@_);
82             }
83              
84              
85             sub trigger {
86             # uncoverable pod (see Plugin.pm)
87 229     229 0 19765 my ($self, $event, @params) = @_;
88            
89 229         411 my $default_handler = $self->{default_handlers}->{$event};
90 229 100       706 my $handlers = $self->{handlers}->{$event}
    100          
91             or return $default_handler ? $default_handler->(@params) : ();
92            
93 187   100 3   469 my $callback = $default_handler // sub {};
94 187         370 for my $handler ( reverse @$handlers ) {
95 194         249 my $continue = $callback;
96 194     191   808 $callback = sub { $handler->($continue, @params) };
  191         558  
97             }
98 187         364 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   9878 my ($self, $plugin) = @_;
108            
109 87 100       606 croak "Can't locate object method new() via package $plugin (perhaps you forgot to load \"$plugin\"?)" unless $plugin->can('new');
110 85 100       702 croak "Package $plugin is not a Neo4j::Driver::Plugin" unless $plugin->DOES('Neo4j::Driver::Plugin');
111 83 100       362 croak "Method register() not implemented by package $plugin (is this a Neo4j::Driver plug-in?)" unless $plugin->can('register');
112 82 100       280 warnings::warnif deprecated => "Neo4j::Driver->plugin() with module name is deprecated" if ref $plugin eq '';
113            
114 82 100       1288 $plugin = $plugin->new if ref $plugin eq '';
115 82         486 $plugin->register($self);
116             }
117              
118              
119             1;