AnyEvent-CallbackStack

 view release on metacpan or  search on metacpan

lib/AE/CS.pm  view on Meta::CPAN

Use L<AE::CS> with the following style.

	use feature 'say';
	use AnyEvent::CallbackStack;
	
	my $cs = AE::CS;
	my $cv = AE::cv;
	
	$cs->add( sub { $cv->send( $_[0]->recv ) } );
	$cs->start('hello world');
	say $cv->recv;

# or

	my $cs = AE::CS;
	http_get http://BlueT.org => sub { $cs->start($_[0]) };
	$cs->add( sub { say $_[0]->recv } );

# or

	my $cs = AE::CS;
	my %foo = (bar => vbar, yohoo => vyohoo);
	
	$cs->start( %foo );
	$cs->add( sub {
		my %foo = $_[0]->recv;
		$cs->next( $foo{'bar'}, $foo{'yohoo'} );
	});
	
	$cv = $cs->last;
	$cv->cb( sub {
		my @a = $_[0]->recv;
		$cv->send( $a[0].$a[1] )
	});
	
	say $cv->recv;


=head1 METHODS

=head2 start

Start and walk through the Callback Stack from step 0.

	$cs->start( 'foo' );

lib/AnyEvent/CallbackStack.pm  view on Meta::CPAN

		do_something;
		$cs->next( $bar, $yohoo );
	});
	
	$cv = $cs->last;
	return $cv;

# or

	http_get http://BlueT.org => sub { $cs->start($_[0]) };
	$cs->add( sub { say $_[0]->recv; $cs->next } );
	$cs->last->cb(sub {
		# do something after that
		# and maybe let me know someone's using my module :3
	});

# or

	$cs->add( sub { say 'I got the ball'; $cs->next( $_[0]->recv ); } )
	print 'Your name please?: ';
	chomp(my $in = <STDIN>);
	$cs->start($in);
	$cs->add( sub { say "Lucky you, $_[0]->recv" } );

# or

	my $cs = AE::CS;

=head1 METHODS

=head2 new

No paramater needed.

lib/AnyEvent/CallbackStack.pm  view on Meta::CPAN

	
	my @cbq = ();
	push @cbq, AE::cv;
	
	my $self  = {
		cbq		=> \@cbq,
		current_step	=> 0,
	};
	
	bless ($self, $class);
	say 'NEW '.Dumper($self) if DEBUG;
	
	return $self;
}

=head2 start

Start and walk through the Callback Stack from step 0.

	$cs->start( 'foo' );

=cut

sub start {
	my $self = shift;
	$self->current_step(0);
	
	say 'Start '.Dumper ($self) if DEBUG;
	$self->step($self->current_step, @_);
}

=head2 add

Add (append) callback into the Callback Stack.

	$cs->add( $code_ref );

=cut

sub add {
	my $self = shift;
	$self->cbq(AE::cv);
	
	say 'ADD '.Dumper ($self) if DEBUG;
	
	($self->cbq)[-2]->cb( shift );
}

=head2 next

Check out from the current step and pass value to the next callback in callback stack.

	$cs->next( @result );

IMPORTANT:
Remember that only if you call this method, the next callback in stack will be triggered.

=cut

sub next {
	my $self = shift;
	$self->current_step( $self->current_step +1);
	
	say 'NEXT $self->current_step '.Dumper ($self) if DEBUG;
	
	$self->step($self->current_step, @_);
}

=head2 last

Get the very last L<AnyEvent::CondVar> object.

Usually it's called when you are writing a module and need to return it to your caller.

	my $cv = $cs->last;
	# or
	return $cs->last;
	

=cut

sub last {
	my $self = shift;
	
	say 'LAST '.Dumper ($self) if DEBUG;
	
	return ($self->cbq)[-1];
}

=head2 step

Experimental.

Start the callback flow from the specified step.

	$cs->step( 3, @data );

=cut

sub step {
	my $self = shift;
	$_[0] =~ /^\d+?$/ ? $self->current_step(shift) : die 'input is not a number in step()';
	
	say 'STEP '.Dumper ($self) if DEBUG;
	
	($self->cbq)[$self->current_step]->send( @_ );
}

=head2 cbq

Experimental.

Callback Queue Getter/Setter.



( run in 1.250 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )