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   283 use 5.010;
  17         50  
2 17     17   90 use strict;
  17         32  
  17         312  
3 17     17   70 use warnings;
  17         49  
  17         475  
4 17     17   78 use utf8;
  17         41  
  17         99  
5              
6             package Neo4j::Driver::Events;
7             # ABSTRACT: Event manager for Neo4j::Driver plug-ins
8             $Neo4j::Driver::Events::VERSION = '0.40';
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   1005 use Carp qw(croak);
  17         63  
  17         1517  
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   117 use Scalar::Util qw(weaken);
  17         25  
  17         14077  
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 27869 my ($class) = @_;
31            
32 177         329 my $self = bless {}, $class;
33 177         424 $self->_init_default_handlers;
34 177         573 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   257 my ($self) = @_;
41            
42             $self->{default_handlers}->{error} = sub {
43             # Unlike regular handlers, default handlers don't receive a callback.
44 25     25   66 my ($error) = @_;
45            
46 25 100       74 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         32 my @errors;
50 24         37 do { push @errors, $error } while $error = $error->related;
  33         154  
51 24         126 @errors = map { $_->as_string } @errors;
  33         123  
52 24 100       527 croak join "\n", @errors if $self->{die_on_error};
53 2         23 Carp::carp join "\n", @errors;
54 177         1000 };
55 177         612 weaken $self;
56             }
57              
58              
59             sub add_event_handler {
60             # uncoverable pod (see Deprecations.pod)
61 1     1 0 1037 warnings::warnif deprecated => __PACKAGE__ . "->add_event_handler() is deprecated";
62 1         949 shift->add_handler(@_);
63             }
64              
65              
66             sub add_handler {
67             # uncoverable pod (see Plugin.pm)
68 103     103 0 6493 my ($self, $event, $handler, @extra) = @_;
69            
70 103 100       238 croak "Too many arguments for method 'add_handler'" if @extra;
71 101 100       299 croak "Event handler must be a subroutine reference" unless ref $handler eq 'CODE';
72 98 100       234 croak "Event name must be defined" unless defined $event;
73            
74 97         134 push @{$self->{handlers}->{$event}}, $handler;
  97         448  
75             }
76              
77              
78             sub trigger_event {
79             # uncoverable pod (see Deprecations.pod)
80 1     1 0 1131 warnings::warnif deprecated => __PACKAGE__ . "->trigger_event() is deprecated";
81 1         921 shift->trigger(@_);
82             }
83              
84              
85             sub trigger {
86             # uncoverable pod (see Plugin.pm)
87 232     232 0 16984 my ($self, $event, @params) = @_;
88            
89 232         425 my $default_handler = $self->{default_handlers}->{$event};
90 232 100       672 my $handlers = $self->{handlers}->{$event}
    100          
91             or return $default_handler ? $default_handler->(@params) : ();
92            
93 188   100 3   468 my $callback = $default_handler // sub {};
94 188         383 for my $handler ( reverse @$handlers ) {
95 195         254 my $continue = $callback;
96 195     192   779 $callback = sub { $handler->($continue, @params) };
  192         526  
97             }
98 188         379 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   8716 my ($self, $plugin) = @_;
108            
109 87 100       633 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       388 croak "Method register() not implemented by package $plugin (is this a Neo4j::Driver plug-in?)" unless $plugin->can('register');
112 82 100       268 warnings::warnif deprecated => "Neo4j::Driver->plugin() with module name is deprecated" if ref $plugin eq '';
113            
114 82 100       1158 $plugin = $plugin->new if ref $plugin eq '';
115 82         355 $plugin->register($self);
116             }
117              
118              
119             1;