NAME
Venus::Role::Subscribable - Subscribable Role
ABSTRACT
Subscribable Role for Perl 5
SYNOPSIS
package Example;
use Venus::Class;
with 'Venus::Role::Subscribable';
sub execute {
$_[0]->publish('on.execute');
}
package main;
my $example = Example->new;
# $example->subscribe('on.execute', sub{...});
# bless(..., 'Example')
# $example->publish('on.execute');
# bless(..., 'Example')
DESCRIPTION
This package provides a mechanism for publishing and subscribing to events.
METHODS
This package provides the following methods:
publish
publish(Str $name, Any @args) (Self)
The publish method notifies all subscribers for a given event and returns the invocant.
Since 1.75
- publish example 1
-
# given: synopsis package main; $example = $example->publish; # bless(..., 'Example')
- publish example 2
-
# given: synopsis package main; $example = $example->publish('on.execute'); # bless(..., 'Example')
- publish example 3
-
# given: synopsis package main; $example->subscribe('on.execute', sub {$example->{emitted} = [@_]}); $example = $example->publish('on.execute'); # bless(..., 'Example')
- publish example 4
-
# given: synopsis package main; $example->subscribe('on.execute', sub {$example->{emitted} = [@_]}); $example = $example->publish('on.execute', [1..4]); # bless(..., 'Example')
subscribe
subscribe(Str $name, CodeRef $code) (Self)
The subscribe method registers a subscribers (i.e. callbacks) for a given event, and returns the invocant.
Since 1.75
- subscribe example 1
-
# given: synopsis package main; $example = $example->subscribe('on.execute', sub {$example->{emitted} = [@_]}); # bless(..., 'Example')
- subscribe example 2
-
# given: synopsis package main; $example = $example->subscribe('on.execute', sub {$example->{emitted_1} = [@_]}); # bless(..., 'Example') $example = $example->subscribe('on.execute', sub {$example->{emitted_2} = [@_]}); # bless(..., 'Example') $example = $example->subscribe('on.execute', sub {$example->{emitted_3} = [@_]}); # bless(..., 'Example') # $example->publish('on.execute'); # bless(..., 'Example')
subscribers
subscribers(Str $name) (Int)
The subscribers method returns the number of subscribers (i.e. callbacks) for a given event.
Since 1.75
- subscribers example 2
-
# given: synopsis package main; $example = $example->subscribers('on.execute'); # 0
- subscribers example 3
-
# given: synopsis package main; $example = $example->subscribe('on.execute', sub {$example->{emitted_1} = [@_]}); $example = $example->subscribe('on.execute', sub {$example->{emitted_2} = [@_]}); $example = $example->subscribe('on.execute', sub {$example->{emitted_3} = [@_]}); $example = $example->subscribers('on.execute'); # 3
unsubscribe
unsubscribe(Str $name, CodeRef $code) (Self)
The unsubscribe method deregisters all subscribers (i.e. callbacks) for a given event, or a specific callback if provided, and returns the invocant.
Since 1.75
- unsubscribe example 1
-
# given: synopsis package main; $example = $example->unsubscribe; # bless(..., 'Example')
- unsubscribe example 2
-
# given: synopsis package main; $example = $example->unsubscribe('on.execute'); # bless(..., 'Example')
- unsubscribe example 3
-
# given: synopsis package main; $example = $example->subscribe('on.execute', sub {$example->{emitted_1} = [@_]}); $example = $example->subscribe('on.execute', sub {$example->{emitted_2} = [@_]}); $example = $example->subscribe('on.execute', sub {$example->{emitted_3} = [@_]}); $example = $example->unsubscribe('on.execute'); # bless(..., 'Example')
- unsubscribe example 4
-
# given: synopsis package main; my $execute = sub {$example->{execute} = [@_]}; $example = $example->subscribe('on.execute', $execute); $example = $example->unsubscribe('on.execute', $execute); # bless(..., 'Example')
AUTHORS
Awncorp, awncorp@cpan.org
LICENSE
Copyright (C) 2000, Al Newkirk.
This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.