| 1 | package AnyEvent::ZeroMQ::Publish; | ||
| 2 | 3 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 3 | $AnyEvent::ZeroMQ::Publish::VERSION = '0.01'; | |
| 4 | } | ||
| 5 | # ABSTRACT: Non-blocking OO abstraction over ZMQ_PUB publish/subscribe sockets | ||
| 6 | 3 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 3 | ▶ | use MooseX::Aliases; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 3 | ▶ | use true; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 3 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use ZeroMQ::Raw::Constants qw(ZMQ_PUB); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use Params::Util qw(_CODELIKE); | ||
| 13 | |||
| 14 | with 'AnyEvent::ZeroMQ::Role::WithHandle' => | ||
| 15 | { socket_type => ZMQ_PUB, socket_direction => 'w' }, | ||
| 16 | 'MooseX::Traits'; | ||
| 17 | |||
| 18 | has '+_trait_namespace' => ( default => 'AnyEvent::ZeroMQ::Publish::Trait' ); | ||
| 19 | |||
| 20 | sub mangle_message { | ||
| 21 | my ($self, $msg, %args) = @_; | ||
| 22 | warn 'ignoring unused mangle arguments '. join(', ', map { "'$_'" } keys %args) | ||
| 23 | if %args; | ||
| 24 | return $msg; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub publish { | ||
| 28 | my ($self, $msg, %args) = @_; | ||
| 29 | |||
| 30 | if(_CODELIKE($msg)){ # not to be confused with 'if _CATLIKE($tobias)' | ||
| 31 | $self->handle->push_write(sub { | ||
| 32 | my $txt = $msg->(@_); | ||
| 33 | return $self->mangle_message($txt, %args); | ||
| 34 | }); | ||
| 35 | } | ||
| 36 | else { | ||
| 37 | $self->handle->push_write($self->mangle_message($msg, %args)); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | alias 'push_write' => 'publish'; | ||
| 42 | |||
| 43 | with 'AnyEvent::ZeroMQ::Handle::Role::Generic', | ||
| 44 | 'AnyEvent::ZeroMQ::Handle::Role::Writable'; | ||
| 45 | |||
| 46 | __PACKAGE__->meta->make_immutable; | ||
| 47 | |||
| 48 | __END__ | ||