| 1 | package Backbone::Events; | ||
| 2 | $Backbone::Events::VERSION = '0.0.2'; | ||
| 3 | use Carp qw(confess); | ||
| 4 | use List::MoreUtils qw(none); | ||
| 5 | use Scalar::Util qw(blessed); | ||
| 6 | use Moo::Role; | ||
| 7 | use namespace::autoclean -also => qr/^__bbe_/; | ||
| 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 { __bbe_new_id() }, | ||
| 20 | ); | ||
| 21 | |||
| 22 | has _bbe_listening_to => ( | ||
| 23 | is => 'ro', | ||
| 24 | default => sub { {} }, | ||
| 25 | ); | ||
| 26 | |||
| 27 | our $__bbe_last_id; | ||
| 28 | sub __bbe_new_id { ++$__bbe_last_id } | ||
| 29 | |||
| 30 | sub _bbe_trigger { | ||
| 31 | my ($self, $event_ref, $event, @args) = @_; | ||
| 32 | my $cb = $event_ref->{cb}; | ||
| 33 | |||
| 34 | if ($event_ref->{ns} eq 'all') { | ||
| 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 __bbe_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 ___bbe_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 __bbe_parse_ns { | ||
| 75 | my ($event) = @_; | ||
| 76 | my ($ns, $type) = split(':', $event//'', 2); | ||
| 77 | return ($ns//'', $type//''); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub __bbe_query { | ||
| 81 | my ($ids, $q) = @_; | ||
| 82 | return grep { | ||
| 83 | my $id = $_; | ||
| 84 | my $match = 1; | ||
| 85 | for my $field (keys %$q) { | ||
| 86 | my $have = $ids->{$id}{$field} // ''; | ||
| 87 | my $want = $q->{$field}; | ||
| 88 | |||
| 89 | my $type = ref $want; | ||
| 90 | if ($type eq 'ARRAY') { | ||
| 91 | if (none {$_ eq $have} @$want) { | ||
| 92 | $match = 0; | ||
| 93 | last; | ||
| 94 | } | ||
| 95 | } else { | ||
| 96 | if ($want ne $have) { | ||
| 97 | $match = 0; | ||
| 98 | last; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | $match; | ||
| 103 | } keys %$ids; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub __bbe_does_events { | ||
| 107 | my ($obj) = @_; | ||
| 108 | return $obj | ||
| 109 | && blessed($obj) | ||
| 110 | && $obj->DOES(__PACKAGE__); | ||
| 111 | } | ||
| 112 | |||
| 113 | around on => \&__bbe_wrap_multiple_events; | ||
| 114 | sub on { | ||
| 115 | my ($self, $event, $cb, %opts) = @_; | ||
| 116 | my ($ns, $type) = __bbe_parse_ns($event); | ||
| 117 | $self->_bbe_events->{__bbe_new_id()} = { | ||
| 118 | %opts, | ||
| 119 | cb => $cb, | ||
| 120 | ns => $ns, | ||
| 121 | type => $type, | ||
| 122 | }; | ||
| 123 | return $cb; | ||
| 124 | } | ||
| 125 | |||
| 126 | around off => \&__bbe_wrap_multiple_events; | ||
| 127 | sub off { | ||
| 128 | my ($self, $event, $cb, %opts) = @_; | ||
| 129 | my ($ns, $type) = __bbe_parse_ns($event); | ||
| 130 | |||
| 131 | my @ids = __bbe_query($self->_bbe_events, { | ||
| 132 | %opts, | ||
| 133 | ( cb => $cb )x!! $cb, | ||
| 134 | ( ns => $ns )x!! $ns, | ||
| 135 | ( type => $type )x!! $type, | ||
| 136 | }); | ||
| 137 | delete @{$self->_bbe_events}{@ids}; | ||
| 138 | } | ||
| 139 | |||
| 140 | around trigger => \&__bbe_wrap_multiple_events; | ||
| 141 | sub trigger { | ||
| 142 | my ($self, $event, @args) = @_; | ||
| 143 | my ($ns, $type) = __bbe_parse_ns($event); | ||
| 144 | |||
| 145 | my @ids = __bbe_query($self->_bbe_events, { | ||
| 146 | ns => [ 'all', $ns ], | ||
| 147 | type => [ $type ? ($type, '') : ('') ], | ||
| 148 | }); | ||
| 149 | |||
| 150 | for my $id (@ids) { | ||
| 151 | my $event_ref = $self->_bbe_events->{$id}; | ||
| 152 | $self->_bbe_trigger($event_ref, $event, @args); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | around once => \&__bbe_wrap_multiple_events; | ||
| 157 | sub once { | ||
| 158 | my ($self, $event, $cb) = @_; | ||
| 159 | $self->on($event, $cb, once => 1); | ||
| 160 | return $cb; | ||
| 161 | } | ||
| 162 | |||
| 163 | around listen_to => \&___bbe_wrap_multiple_events2; | ||
| 164 | sub listen_to { | ||
| 165 | my ($self, $other, $event, $cb, %opts) = @_; | ||
| 166 | confess "Cannot call listen_to on object that does not consume Backbone::Events" | ||
| 167 | if $other and not __bbe_does_events($other); | ||
| 168 | |||
| 169 | my ($ns, $type) = __bbe_parse_ns($event); | ||
| 170 | $self->_bbe_listening_to->{__bbe_new_id()} = { | ||
| 171 | %opts, | ||
| 172 | cb => $cb, | ||
| 173 | event => $event, | ||
| 174 | ns => $ns, | ||
| 175 | other => $other, | ||
| 176 | other_id => $other->_bbe_id, | ||
| 177 | type => $type, | ||
| 178 | }; | ||
| 179 | $other->on($event, $cb, %opts, listen_id => $self->_bbe_id); | ||
| 180 | |||
| 181 | return $cb; | ||
| 182 | } | ||
| 183 | |||
| 184 | around stop_listening => \&___bbe_wrap_multiple_events2; | ||
| 185 | sub stop_listening { | ||
| 186 | my ($self, $other, $event, $cb) = @_; | ||
| 187 | my ($ns, $type) = __bbe_parse_ns($event); | ||
| 188 | confess "Cannot call stop_listening on object that does not consume Backbone::Events" | ||
| 189 | if $other and not __bbe_does_events($other); | ||
| 190 | |||
| 191 | my $query = { | ||
| 192 | ( cb => $cb )x!! $cb, | ||
| 193 | ( ns => $ns )x!! $ns, | ||
| 194 | ( type => $type )x!! $type, | ||
| 195 | }; | ||
| 196 | $query->{other_id} = $other->_bbe_id if $other; | ||
| 197 | my @ids = __bbe_query($self->_bbe_listening_to, $query); | ||
| 198 | |||
| 199 | for my $id (@ids) { | ||
| 200 | my $listen_ref = $self->_bbe_listening_to->{$id}; | ||
| 201 | my $other_obj = $listen_ref->{other}; | ||
| 202 | my @args = @{$listen_ref}{qw(event cb)}; | ||
| 203 | $other_obj->off(@args, listen_id => $self->_bbe_id); | ||
| 204 | } | ||
| 205 | delete @{$self->_bbe_listening_to}{@ids}; | ||
| 206 | } | ||
| 207 | |||
| 208 | around listen_to_once => \&___bbe_wrap_multiple_events2; | ||
| 209 | sub listen_to_once { | ||
| 210 | my ($self, $other, $event, $cb) = @_; | ||
| 211 | $self->listen_to($other, $event, $cb, once => 1); | ||
| 212 | return $cb; | ||
| 213 | } | ||
| 214 | |||
| 215 | 1; | ||
| 216 | |||
| 217 | __END__ |