Giter Club home page Giter Club logo

perl6-event-emitter's Introduction

#Event::Emitter

An extendable JS like event emitter but way more fun. Can use supplies or channels and is basically just some syntax sugar on already implemented Perl6 features.

##Syntax

Out of the box functionality

###Single thread

Event::Emitter uses a Supply in the back end

use Event::Emitter;

my Event::Emitter $e .= new;

#hey, i work with regex
$e.on(/^^ "Some regex"/, -> $data {
  qw<do something with your $data here>;
});

#your own callables to match events
my $event = { 'some flag' => 3, 'some other flag' => 5 };
$e.on({ $event<some flag> // Nil eq $*STATE }, -> $data {
  qw<do something with your $data here>;
});

#plain ol strings, just like mom used to make
$e.on('some str', -> $data {
  qw<do something with your $data here>;
});

#runs the some str listener
$e.emit('some str', @(1 .. 5)); 

#runs the regex because it matches the regex;
$e.emit('Some regex', { conn => IO::Socket::INET }); 

$e.emit({ 'some flag' => 5 }, { });

###Thread

Event::Emitter uses a Channel in the back end

use Event::Emitter;

my Event::Emitter $e .= new(:threaded);

##Rolling your own Event::Emitter

Want to make your own receiver/emitter? Here's a template

###Your new .pm6 file

use Event::Emitter::Role::Handler;

unit class My::Own::Emitter does Event::Emitter::Role::Handler;

method on($event, $data) {
  qw<do your thing>;
}

method emit($event, $data?) {
  qw<do your thing here>;
}

###Later in your .pl6

use Event::Emitter;


my $e = Event::Emitter.new(:class<My::Own::Emitter>);

#License

Free for all.

perl6-event-emitter's People

Contributors

jonathanstowe avatar tony-o avatar

Watchers

 avatar James Cloos avatar

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.