| 1 | package Venus::Role::Subscribable; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.018; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | # IMPORTS | ||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use Venus::Role 'with'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | # METHODS | ||||||||||
| 13 | |||||||||||
| 14 | 35 | ▶ | sub name { | ||||||||
|
Subroutine
name: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 15 | 35 | my ($name) = @_; | |||||||||
| 16 | |||||||||||
| 17 | 35 | ▶ | $name = lc $name =~ s/\W+/_/gr if $name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | |||||||||||
| 19 | 35 | return $name; | |||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | 6 | ▶ | sub publish { | ||||||||
|
Subroutine
publish: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 23 | 6 | my ($self, $name, @args) = @_; | |||||||||
| 24 | |||||||||||
| 25 | 6 | ▶ | $name = name($name) or return $self; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | |||||||||||
| 27 | 5 | &$_(@args) for @{$self->{subscriptions}->{$name}}; | |||||||||
| 28 | |||||||||||
| 29 | 5 | return $self; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 15 | ▶ | sub subscribe { | ||||||||
|
Subroutine
subscribe: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 33 | 15 | my ($self, $name, $code) = @_; | |||||||||
| 34 | |||||||||||
| 35 | 15 | ▶ | $name = name($name) or return $self; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | |||||||||||
| 37 | 15 | push @{$self->{subscriptions}->{$name}}, $code; | |||||||||
| 38 | |||||||||||
| 39 | 15 | return $self; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 8 | ▶ | sub subscribers { | ||||||||
|
Subroutine
subscribers: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 43 | 8 | my ($self, $name) = @_; | |||||||||
| 44 | |||||||||||
| 45 | 8 | ▶ | $name = name($name) or return 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | |||||||||||
| 47 | 7 | ▶ | if (exists $self->{subscriptions}->{$name}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 4 | return 0+@{$self->{subscriptions}->{$name}}; | |||||||||
| 49 | } | ||||||||||
| 50 | else { | ||||||||||
| 51 | 3 | return 0; | |||||||||
| 52 | } | ||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 6 | ▶ | sub unsubscribe { | ||||||||
|
Subroutine
unsubscribe: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 56 | 6 | my ($self, $name, $code) = @_; | |||||||||
| 57 | |||||||||||
| 58 | 6 | ▶ | $name = name($name) or return $self; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | |||||||||||
| 60 | 5 | ▶ | if ($code) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | $self->{subscriptions}->{$name} = [ | ||||||||||
| 62 | 3 | grep { $code ne $_ } @{$self->{subscriptions}->{$name}} | |||||||||
| 63 | ]; | ||||||||||
| 64 | } | ||||||||||
| 65 | else { | ||||||||||
| 66 | 2 | delete $self->{subscriptions}->{$name}; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 5 | ▶ | delete $self->{subscriptions}->{$name} if !$self->subscribers($name); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | |||||||||||
| 71 | 5 | return $self; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | # EXPORTS | ||||||||||
| 75 | |||||||||||
| 76 | 14 | ▶ | sub EXPORT { | ||||||||
|
Subroutine
EXPORT: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 77 | 14 | ['publish', 'subscribe', 'subscribers', 'unsubscribe'] | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 1 | 1; | |||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | =head1 NAME | ||||||||||
| 85 | |||||||||||
| 86 | Venus::Role::Subscribable - Subscribable Role | ||||||||||
| 87 | |||||||||||
| 88 | =cut | ||||||||||
| 89 | |||||||||||
| 90 | =head1 ABSTRACT | ||||||||||
| 91 | |||||||||||
| 92 | Subscribable Role for Perl 5 | ||||||||||
| 93 | |||||||||||
| 94 | =cut | ||||||||||
| 95 | |||||||||||
| 96 | =head1 SYNOPSIS | ||||||||||
| 97 | |||||||||||
| 98 | package Example; | ||||||||||
| 99 | |||||||||||
| 100 | use Venus::Class; | ||||||||||
| 101 | |||||||||||
| 102 | with 'Venus::Role::Subscribable'; | ||||||||||
| 103 | |||||||||||
| 104 | sub execute { | ||||||||||
| 105 | $_[0]->publish('on.execute'); | ||||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | package main; | ||||||||||
| 109 | |||||||||||
| 110 | my $example = Example->new; | ||||||||||
| 111 | |||||||||||
| 112 | # $example->subscribe('on.execute', sub{...}); | ||||||||||
| 113 | |||||||||||
| 114 | # bless(..., 'Example') | ||||||||||
| 115 | |||||||||||
| 116 | # $example->publish('on.execute'); | ||||||||||
| 117 | |||||||||||
| 118 | # bless(..., 'Example') | ||||||||||
| 119 | |||||||||||
| 120 | =cut | ||||||||||
| 121 | |||||||||||
| 122 | =head1 DESCRIPTION | ||||||||||
| 123 | |||||||||||
| 124 | This package provides a mechanism for publishing and subscribing to events. | ||||||||||
| 125 | |||||||||||
| 126 | =cut | ||||||||||
| 127 | |||||||||||
| 128 | =head1 METHODS | ||||||||||
| 129 | |||||||||||
| 130 | This package provides the following methods: | ||||||||||
| 131 | |||||||||||
| 132 | =cut | ||||||||||
| 133 | |||||||||||
| 134 | =head2 publish | ||||||||||
| 135 | |||||||||||
| 136 | publish(string $name, any @args) (object) | ||||||||||
| 137 | |||||||||||
| 138 | The publish method notifies all subscribers for a given event and returns the | ||||||||||
| 139 | invocant. | ||||||||||
| 140 | |||||||||||
| 141 | I<Since C<1.75>> | ||||||||||
| 142 | |||||||||||
| 143 | =over 4 | ||||||||||
| 144 | |||||||||||
| 145 | =item publish example 1 | ||||||||||
| 146 | |||||||||||
| 147 | # given: synopsis | ||||||||||
| 148 | |||||||||||
| 149 | package main; | ||||||||||
| 150 | |||||||||||
| 151 | $example = $example->publish; | ||||||||||
| 152 | |||||||||||
| 153 | # bless(..., 'Example') | ||||||||||
| 154 | |||||||||||
| 155 | =back | ||||||||||
| 156 | |||||||||||
| 157 | =over 4 | ||||||||||
| 158 | |||||||||||
| 159 | =item publish example 2 | ||||||||||
| 160 | |||||||||||
| 161 | # given: synopsis | ||||||||||
| 162 | |||||||||||
| 163 | package main; | ||||||||||
| 164 | |||||||||||
| 165 | $example = $example->publish('on.execute'); | ||||||||||
| 166 | |||||||||||
| 167 | # bless(..., 'Example') | ||||||||||
| 168 | |||||||||||
| 169 | =back | ||||||||||
| 170 | |||||||||||
| 171 | =over 4 | ||||||||||
| 172 | |||||||||||
| 173 | =item publish example 3 | ||||||||||
| 174 | |||||||||||
| 175 | # given: synopsis | ||||||||||
| 176 | |||||||||||
| 177 | package main; | ||||||||||
| 178 | |||||||||||
| 179 | $example->subscribe('on.execute', sub {$example->{emitted} = [@_]}); | ||||||||||
| 180 | |||||||||||
| 181 | $example = $example->publish('on.execute'); | ||||||||||
| 182 | |||||||||||
| 183 | # bless(..., 'Example') | ||||||||||
| 184 | |||||||||||
| 185 | =back | ||||||||||
| 186 | |||||||||||
| 187 | =over 4 | ||||||||||
| 188 | |||||||||||
| 189 | =item publish example 4 | ||||||||||
| 190 | |||||||||||
| 191 | # given: synopsis | ||||||||||
| 192 | |||||||||||
| 193 | package main; | ||||||||||
| 194 | |||||||||||
| 195 | $example->subscribe('on.execute', sub {$example->{emitted} = [@_]}); | ||||||||||
| 196 | |||||||||||
| 197 | $example = $example->publish('on.execute', [1..4]); | ||||||||||
| 198 | |||||||||||
| 199 | # bless(..., 'Example') | ||||||||||
| 200 | |||||||||||
| 201 | =back | ||||||||||
| 202 | |||||||||||
| 203 | =cut | ||||||||||
| 204 | |||||||||||
| 205 | =head2 subscribe | ||||||||||
| 206 | |||||||||||
| 207 | subscribe(string $name, coderef $code) (object) | ||||||||||
| 208 | |||||||||||
| 209 | The subscribe method registers a subscribers (i.e. callbacks) for a given event, | ||||||||||
| 210 | and returns the invocant. | ||||||||||
| 211 | |||||||||||
| 212 | I<Since C<1.75>> | ||||||||||
| 213 | |||||||||||
| 214 | =over 4 | ||||||||||
| 215 | |||||||||||
| 216 | =item subscribe example 1 | ||||||||||
| 217 | |||||||||||
| 218 | # given: synopsis | ||||||||||
| 219 | |||||||||||
| 220 | package main; | ||||||||||
| 221 | |||||||||||
| 222 | $example = $example->subscribe('on.execute', sub {$example->{emitted} = [@_]}); | ||||||||||
| 223 | |||||||||||
| 224 | # bless(..., 'Example') | ||||||||||
| 225 | |||||||||||
| 226 | =back | ||||||||||
| 227 | |||||||||||
| 228 | =over 4 | ||||||||||
| 229 | |||||||||||
| 230 | =item subscribe example 2 | ||||||||||
| 231 | |||||||||||
| 232 | # given: synopsis | ||||||||||
| 233 | |||||||||||
| 234 | package main; | ||||||||||
| 235 | |||||||||||
| 236 | $example = $example->subscribe('on.execute', sub {$example->{emitted_1} = [@_]}); | ||||||||||
| 237 | |||||||||||
| 238 | # bless(..., 'Example') | ||||||||||
| 239 | |||||||||||
| 240 | $example = $example->subscribe('on.execute', sub {$example->{emitted_2} = [@_]}); | ||||||||||
| 241 | |||||||||||
| 242 | # bless(..., 'Example') | ||||||||||
| 243 | |||||||||||
| 244 | $example = $example->subscribe('on.execute', sub {$example->{emitted_3} = [@_]}); | ||||||||||
| 245 | |||||||||||
| 246 | # bless(..., 'Example') | ||||||||||
| 247 | |||||||||||
| 248 | # $example->publish('on.execute'); | ||||||||||
| 249 | |||||||||||
| 250 | # bless(..., 'Example') | ||||||||||
| 251 | |||||||||||
| 252 | =back | ||||||||||
| 253 | |||||||||||
| 254 | =cut | ||||||||||
| 255 | |||||||||||
| 256 | =head2 subscribers | ||||||||||
| 257 | |||||||||||
| 258 | subscribers(string $name) (number) | ||||||||||
| 259 | |||||||||||
| 260 | The subscribers method returns the number of subscribers (i.e. callbacks) for a | ||||||||||
| 261 | given event. | ||||||||||
| 262 | |||||||||||
| 263 | I<Since C<1.75>> | ||||||||||
| 264 | |||||||||||
| 265 | =over 4 | ||||||||||
| 266 | |||||||||||
| 267 | =item subscribers example 1 | ||||||||||
| 268 | |||||||||||
| 269 | # given: synopsis | ||||||||||
| 270 | |||||||||||
| 271 | package main; | ||||||||||
| 272 | |||||||||||
| 273 | $example = $example->subscribers; | ||||||||||
| 274 | |||||||||||
| 275 | # 0 | ||||||||||
| 276 | |||||||||||
| 277 | =back | ||||||||||
| 278 | |||||||||||
| 279 | =over 4 | ||||||||||
| 280 | |||||||||||
| 281 | =item subscribers example 2 | ||||||||||
| 282 | |||||||||||
| 283 | # given: synopsis | ||||||||||
| 284 | |||||||||||
| 285 | package main; | ||||||||||
| 286 | |||||||||||
| 287 | $example = $example->subscribers('on.execute'); | ||||||||||
| 288 | |||||||||||
| 289 | # 0 | ||||||||||
| 290 | |||||||||||
| 291 | =back | ||||||||||
| 292 | |||||||||||
| 293 | =over 4 | ||||||||||
| 294 | |||||||||||
| 295 | =item subscribers example 3 | ||||||||||
| 296 | |||||||||||
| 297 | # given: synopsis | ||||||||||
| 298 | |||||||||||
| 299 | package main; | ||||||||||
| 300 | |||||||||||
| 301 | $example = $example->subscribe('on.execute', sub {$example->{emitted_1} = [@_]}); | ||||||||||
| 302 | |||||||||||
| 303 | $example = $example->subscribe('on.execute', sub {$example->{emitted_2} = [@_]}); | ||||||||||
| 304 | |||||||||||
| 305 | $example = $example->subscribe('on.execute', sub {$example->{emitted_3} = [@_]}); | ||||||||||
| 306 | |||||||||||
| 307 | $example = $example->subscribers('on.execute'); | ||||||||||
| 308 | |||||||||||
| 309 | # 3 | ||||||||||
| 310 | |||||||||||
| 311 | =back | ||||||||||
| 312 | |||||||||||
| 313 | =cut | ||||||||||
| 314 | |||||||||||
| 315 | =head2 unsubscribe | ||||||||||
| 316 | |||||||||||
| 317 | unsubscribe(string $name, coderef $code) (object) | ||||||||||
| 318 | |||||||||||
| 319 | The unsubscribe method deregisters all subscribers (i.e. callbacks) for a given | ||||||||||
| 320 | event, or a specific callback if provided, and returns the invocant. | ||||||||||
| 321 | |||||||||||
| 322 | I<Since C<1.75>> | ||||||||||
| 323 | |||||||||||
| 324 | =over 4 | ||||||||||
| 325 | |||||||||||
| 326 | =item unsubscribe example 1 | ||||||||||
| 327 | |||||||||||
| 328 | # given: synopsis | ||||||||||
| 329 | |||||||||||
| 330 | package main; | ||||||||||
| 331 | |||||||||||
| 332 | $example = $example->unsubscribe; | ||||||||||
| 333 | |||||||||||
| 334 | # bless(..., 'Example') | ||||||||||
| 335 | |||||||||||
| 336 | =back | ||||||||||
| 337 | |||||||||||
| 338 | =over 4 | ||||||||||
| 339 | |||||||||||
| 340 | =item unsubscribe example 2 | ||||||||||
| 341 | |||||||||||
| 342 | # given: synopsis | ||||||||||
| 343 | |||||||||||
| 344 | package main; | ||||||||||
| 345 | |||||||||||
| 346 | $example = $example->unsubscribe('on.execute'); | ||||||||||
| 347 | |||||||||||
| 348 | # bless(..., 'Example') | ||||||||||
| 349 | |||||||||||
| 350 | =back | ||||||||||
| 351 | |||||||||||
| 352 | =over 4 | ||||||||||
| 353 | |||||||||||
| 354 | =item unsubscribe example 3 | ||||||||||
| 355 | |||||||||||
| 356 | # given: synopsis | ||||||||||
| 357 | |||||||||||
| 358 | package main; | ||||||||||
| 359 | |||||||||||
| 360 | $example = $example->subscribe('on.execute', sub {$example->{emitted_1} = [@_]}); | ||||||||||
| 361 | |||||||||||
| 362 | $example = $example->subscribe('on.execute', sub {$example->{emitted_2} = [@_]}); | ||||||||||
| 363 | |||||||||||
| 364 | $example = $example->subscribe('on.execute', sub {$example->{emitted_3} = [@_]}); | ||||||||||
| 365 | |||||||||||
| 366 | $example = $example->unsubscribe('on.execute'); | ||||||||||
| 367 | |||||||||||
| 368 | # bless(..., 'Example') | ||||||||||
| 369 | |||||||||||
| 370 | =back | ||||||||||
| 371 | |||||||||||
| 372 | =over 4 | ||||||||||
| 373 | |||||||||||
| 374 | =item unsubscribe example 4 | ||||||||||
| 375 | |||||||||||
| 376 | # given: synopsis | ||||||||||
| 377 | |||||||||||
| 378 | package main; | ||||||||||
| 379 | |||||||||||
| 380 | my $execute = sub {$example->{execute} = [@_]}; | ||||||||||
| 381 | |||||||||||
| 382 | $example = $example->subscribe('on.execute', $execute); | ||||||||||
| 383 | |||||||||||
| 384 | $example = $example->unsubscribe('on.execute', $execute); | ||||||||||
| 385 | |||||||||||
| 386 | # bless(..., 'Example') | ||||||||||
| 387 | |||||||||||
| 388 | =back | ||||||||||
| 389 | |||||||||||
| 390 | =cut | ||||||||||
| 391 | |||||||||||
| 392 | =head1 AUTHORS | ||||||||||
| 393 | |||||||||||
| 394 | Awncorp, C<awncorp@cpan.org> | ||||||||||
| 395 | |||||||||||
| 396 | =cut | ||||||||||
| 397 | |||||||||||
| 398 | =head1 LICENSE | ||||||||||
| 399 | |||||||||||
| 400 | Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>. | ||||||||||
| 401 | |||||||||||
| 402 | This program is free software, you can redistribute it and/or modify it under | ||||||||||
| 403 | the terms of the Apache license version 2.0. | ||||||||||
| 404 | |||||||||||
| 405 | =cut | ||||||||||