| 1 | package Backbone::Events; | ||
| 2 | $Backbone::Events::VERSION = '0.0.3'; | ||
| 3 | use Carp qw(confess); | ||
| 4 | use List::MoreUtils qw(any none); | ||
| 5 | use Scalar::Util qw(blessed); | ||
| 6 | use Moo::Role; | ||
| 7 | use namespace::autoclean -also => qr/^__/; | ||
| 8 | |||
| 9 | # ABSTRACT: a port of the Backbone.js event API | ||
| 10 | |||
| 11 | |||
| 12 | has _bbe_events => ( | ||
| 13 | is => 'ro', | ||
| 14 | default => sub { {} }, | ||
| 15 | ); | ||
| 16 | |||
| 17 | has _bbe_id => ( | ||
| 18 | is => 'ro', | ||
| 19 | default => sub { __new_id() }, | ||
| 20 | ); | ||
| 21 | |||
| 22 | has _bbe_listening_to => ( | ||
| 23 | is => 'ro', | ||
| 24 | default => sub { {} }, | ||
| 25 | ); | ||
| 26 | |||
| 27 | our $__last_id; | ||
| 28 | sub __new_id { ++$__last_id } | ||
| 29 | |||
| 30 | sub _bbe_trigger { | ||
| 31 | my ($self, $event_ref, $event, @args) = @_; | ||
| 32 | my $cb = $event_ref->{cb}; | ||
| 33 | |||
| 34 | if (any {$_ eq 'all'} @{$event_ref}{qw(ns type)}) { | ||
| 35 | $cb->($event, @args); | ||
| 36 | } else { | ||
| 37 | $cb->(@args); | ||
| 38 | } | ||
| 39 | |||
| 40 | if ($event_ref->{once}) { | ||
| 41 | my ($event, $listen_id) = @{$event_ref}{qw(event listen_id)}; | ||
| 42 | $self->off($event, $cb, listen_id => $listen_id//''); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | sub __wrap_multiple_events { | ||
| 47 | my ($orig, $self, $events, @args) = @_; | ||
| 48 | if (ref $events eq 'HASH') { | ||
| 49 | $self->$orig($_, $events->{$_}, @args) for keys %$events; | ||
| 50 | } elsif ($events and $events =~ /\s+/) { | ||
| 51 | my $result; | ||
| 52 | $result = $self->$orig($_, @args) for split /\s+/, $events; | ||
| 53 | # return last result | ||
| 54 | return $result; | ||
| 55 | } else { | ||
| 56 | return $self->$orig($events, @args); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub ___wrap_multiple_events2 { | ||
| 61 | my ($orig, $self, $other, $events, @args) = @_; | ||
| 62 | if (ref $events eq 'HASH') { | ||
| 63 | $self->$orig($other, $_, $events->{$_}, @args) for keys %$events; | ||
| 64 | } elsif ($events and $events =~ /\s+/) { | ||
| 65 | my $result; | ||
| 66 | $result = $self->$orig($other, $_, @args) for split /\s+/, $events; | ||
| 67 | # return last result | ||
| 68 | return $result; | ||
| 69 | } else { | ||
| 70 | return $self->$orig($other, $events, @args); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | sub __parse_event { | ||
| 75 | my ($event) = @_; | ||
| 76 | # handle two edge cases | ||
| 77 | # if passed undef, return empty strings so comparisons don't warn | ||
| 78 | return ( '', '' ) unless defined $event; | ||
| 79 | # and the 'all' event should get the 'all' namespace | ||
| 80 | return ( 'all', 'all' ) if $event eq 'all'; | ||
| 81 | |||
| 82 | if (my ($ns, $type) = $event =~ /(.*):(.*)/) { | ||
| 83 | return ($ns, $type); | ||
| 84 | } else { | ||
| 85 | return ('', $event); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub __query { | ||
| 90 | my ($ids, $q) = @_; | ||
| 91 | return grep { | ||
| 92 | my $id = $_; | ||
| 93 | my $match = 1; | ||
| 94 | for my $field (keys %$q) { | ||
| 95 | my $have = $ids->{$id}{$field} // ''; | ||
| 96 | my $want = $q->{$field}; | ||
| 97 | |||
| 98 | my $type = ref $want; | ||
| 99 | if ($type eq 'ARRAY') { | ||
| 100 | if (none {$_ eq $have} @$want) { | ||
| 101 | $match = 0; | ||
| 102 | last; | ||
| 103 | } | ||
| 104 | } else { | ||
| 105 | if ($want ne $have) { | ||
| 106 | $match = 0; | ||
| 107 | last; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | $match; | ||
| 112 | } keys %$ids; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub __does_events { | ||
| 116 | my ($obj) = @_; | ||
| 117 | return $obj | ||
| 118 | && blessed($obj) | ||
| 119 | && $obj->DOES(__PACKAGE__); | ||
| 120 | } | ||
| 121 | |||
| 122 | around on => \&__wrap_multiple_events; | ||
| 123 | sub on { | ||
| 124 | my ($self, $event, $cb, %opts) = @_; | ||
| 125 | my ($ns, $type) = __parse_event($event); | ||
| 126 | $self->_bbe_events->{__new_id()} = { | ||
| 127 | %opts, | ||
| 128 | cb => $cb, | ||
| 129 | ns => $ns, | ||
| 130 | type => $type, | ||
| 131 | }; | ||
| 132 | return $cb; | ||
| 133 | } | ||
| 134 | |||
| 135 | around off => \&__wrap_multiple_events; | ||
| 136 | sub off { | ||
| 137 | my ($self, $event, $cb, %opts) = @_; | ||
| 138 | my ($ns, $type) = __parse_event($event); | ||
| 139 | |||
| 140 | my @ids = __query($self->_bbe_events, { | ||
| 141 | %opts, | ||
| 142 | ( cb => $cb )x!! $cb, | ||
| 143 | ( ns => $ns )x!! $ns, | ||
| 144 | ( type => $type )x!! $type, | ||
| 145 | }); | ||
| 146 | delete @{$self->_bbe_events}{@ids}; | ||
| 147 | } | ||
| 148 | |||
| 149 | around trigger => \&__wrap_multiple_events; | ||
| 150 | sub trigger { | ||
| 151 | my ($self, $event, @args) = @_; | ||
| 152 | my ($ns, $type) = __parse_event($event); | ||
| 153 | |||
| 154 | my @ids = __query($self->_bbe_events, { | ||
| 155 | ns => [ 'all', $ns ], | ||
| 156 | type => [ 'all', $type ], | ||
| 157 | }); | ||
| 158 | |||
| 159 | for my $id (@ids) { | ||
| 160 | my $event_ref = $self->_bbe_events->{$id}; | ||
| 161 | $self->_bbe_trigger($event_ref, $event, @args); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | around once => \&__wrap_multiple_events; | ||
| 166 | sub once { | ||
| 167 | my ($self, $event, $cb) = @_; | ||
| 168 | $self->on($event, $cb, once => 1); | ||
| 169 | return $cb; | ||
| 170 | } | ||
| 171 | |||
| 172 | around listen_to => \&___wrap_multiple_events2; | ||
| 173 | sub listen_to { | ||
| 174 | my ($self, $other, $event, $cb, %opts) = @_; | ||
| 175 | confess "Cannot call listen_to on object that does not consume Backbone::Events" | ||
| 176 | if not __does_events($other); | ||
| 177 | |||
| 178 | my ($ns, $type) = __parse_event($event); | ||
| 179 | $self->_bbe_listening_to->{__new_id()} = { | ||
| 180 | %opts, | ||
| 181 | cb => $cb, | ||
| 182 | event => $event, | ||
| 183 | ns => $ns, | ||
| 184 | other => $other, | ||
| 185 | other_id => $other->_bbe_id, | ||
| 186 | type => $type, | ||
| 187 | }; | ||
| 188 | $other->on($event, $cb, %opts, listen_id => $self->_bbe_id); | ||
| 189 | |||
| 190 | return $cb; | ||
| 191 | } | ||
| 192 | |||
| 193 | around stop_listening => \&___wrap_multiple_events2; | ||
| 194 | sub stop_listening { | ||
| 195 | my ($self, $other, $event, $cb) = @_; | ||
| 196 | my ($ns, $type) = __parse_event($event); | ||
| 197 | confess "Cannot call stop_listening on object that does not consume Backbone::Events" | ||
| 198 | if $other and not __does_events($other); | ||
| 199 | |||
| 200 | my $query = { | ||
| 201 | ( cb => $cb )x!! $cb, | ||
| 202 | ( ns => $ns )x!! $ns, | ||
| 203 | ( type => $type )x!! $type, | ||
| 204 | }; | ||
| 205 | $query->{other_id} = $other->_bbe_id if $other; | ||
| 206 | my @ids = __query($self->_bbe_listening_to, $query); | ||
| 207 | |||
| 208 | for my $id (@ids) { | ||
| 209 | my $listen_ref = $self->_bbe_listening_to->{$id}; | ||
| 210 | my $other_obj = $listen_ref->{other}; | ||
| 211 | my @args = @{$listen_ref}{qw(event cb)}; | ||
| 212 | $other_obj->off(@args, listen_id => $self->_bbe_id); | ||
| 213 | } | ||
| 214 | delete @{$self->_bbe_listening_to}{@ids}; | ||
| 215 | } | ||
| 216 | |||
| 217 | around listen_to_once => \&___wrap_multiple_events2; | ||
| 218 | sub listen_to_once { | ||
| 219 | my ($self, $other, $event, $cb) = @_; | ||
| 220 | $self->listen_to($other, $event, $cb, once => 1); | ||
| 221 | return $cb; | ||
| 222 | } | ||
| 223 | |||
| 224 | 1; | ||
| 225 | |||
| 226 | __END__ |