GithubHelp home page GithubHelp logo

grinnz / role-eventemitter Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 34 KB

Role::EventEmitter - Event emitter role

Home Page: https://metacpan.org/pod/Role::EventEmitter

License: Other

Perl 100.00%

role-eventemitter's Introduction

NAME

Role::EventEmitter - Event emitter role

SYNOPSIS

package Channel;
use Class::Tiny; # or object system of choice
use Role::Tiny::With;
with 'Role::EventEmitter';

# Emit events
sub send_message {
  my $self = shift;
  $self->emit(message => @_);
}

package main;

# Subscribe to events
my $channel_a = Channel->new;
$channel_a->on(message => sub {
  my ($channel, $text) = @_;
  say "Received message: $text";
});
$channel_a->send_message('All is well');

DESCRIPTION

Role::EventEmitter is a simple Role::Tiny role for event emitting objects based on Mojo::EventEmitter. This role can be applied to any hash-based object class such as those created with Class::Tiny, Moo, or Moose.

EVENTS

Role::EventEmitter can emit the following events.

error

$e->on(error => sub {
  my ($e, $err) = @_;
  ...
});

This is a special event for errors, it will not be emitted directly by this role but is fatal if unhandled.

$e->on(error => sub {
  my ($e, $err) = @_;
  say "This looks bad: $err";
});

METHODS

Role::EventEmitter composes the following methods.

catch

$e = $e->catch(sub {...});

Subscribe to "error" event.

# Longer version
$e->on(error => sub {...});

emit

$e = $e->emit('foo');
$e = $e->emit('foo', 123);

Emit event.

has_subscribers

my $bool = $e->has_subscribers('foo');

Check if event has subscribers.

on

my $cb = $e->on(foo => sub {...});

Subscribe to event.

$e->on(foo => sub {
  my ($e, @args) = @_;
  ...
});

once

my $cb = $e->once(foo => sub {...});

Subscribe to event and unsubscribe again after it has been emitted once.

$e->once(foo => sub {
  my ($e, @args) = @_;
  ...
});

once_f

my $f = $e->once_f('foo');

Subscribe to event as in "once", returning a Future that will be marked complete after it has been emitted once. Requires Future to be installed.

my $f = $e->once_f('foo')->on_done(sub {
  my ($e, @args) = @_;
  ...
});

To unsubscribe the returned Future early, cancel it or any subsequent chained Future.

$f->cancel;

once_p

my $p = $e->once_p('foo');

Subscribe to event as in "once", returning a Mojo::Promise that will be resolved after it has been emitted once. Requires Mojo::Promise to be installed. Note that promises will not settle (or continue the chain) until the next tick of the Mojo::IOLoop.

my $p = $e->once_p('foo')->then(sub {
  my ($e, @args) = @_;
  ...
});
$e->emit('foo');
$p->wait;

Resolving or rejecting the originally returned Mojo::Promise will unsubscribe it early. Note that this must be done on the returned promise and not a chained promise!

my $p = $e->once_p('foo');
$p->then(sub { ... });
$p->reject;

subscribers

my $subscribers = $e->subscribers('foo');

All subscribers for event.

# Unsubscribe last subscriber
$e->unsubscribe(foo => $e->subscribers('foo')->[-1]);

# Change order of subscribers
@{$e->subscribers('foo')} = reverse @{$e->subscribers('foo')};

unsubscribe

$e = $e->unsubscribe('foo');
$e = $e->unsubscribe(foo => $cb);

Unsubscribe from event. Related Futures will also be cancelled.

DEBUGGING

You can set the ROLE_EVENTEMITTER_DEBUG environment variable to get some advanced diagnostics information printed to STDERR.

ROLE_EVENTEMITTER_DEBUG=1

BUGS

Report any issues on the public bugtracker.

AUTHOR

Dan Book <[email protected]>

Code and tests adapted from Mojo::EventEmitter, an event emitter base class by the Mojolicious team.

COPYRIGHT AND LICENSE

Copyright (c) 2008-2015 Sebastian Riedel.

Copyright (c) 2015 Dan Book for adaptation to a role and further changes.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)

SEE ALSO

Mojo::EventEmitter, Mixin::Event::Dispatch, Beam::Emitter, Event::Distributor

role-eventemitter's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

role-eventemitter's Issues

clarify synopsys

if Role::EventEmitter is Role::Tiny it would be better to use it in SYNOPSIS.

current example:

package Channel;
use Moo;
with 'Role::EventEmitter';

force me to think that I must to install Moo to use this module

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.