| 1 | package Object::Deferred; | ||||||||||
| 2 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | # ABSTRACT: A simple API for handling asynchronous events. | ||||||||||
| 6 | |||||||||||
| 7 | |||||||||||
| 8 | has _resolve_callbacks => ( | ||||||||||
| 9 | is => 'rw', | ||||||||||
| 10 | isa => 'ArrayRef', | ||||||||||
| 11 | 17 | ▶ | default => sub {[]}, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | init_arg => undef, | |||||||||
| 13 | traits => ['Array'], | ||||||||||
| 14 | handles => { | ||||||||||
| 15 | add_resolve_callback => 'push', | ||||||||||
| 16 | resolve_callbacks => 'elements' | ||||||||||
| 17 | } | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | has _reject_callback => ( | ||||||||||
| 21 | is => 'rw', | ||||||||||
| 22 | isa => 'ArrayRef', | ||||||||||
| 23 | 17 | ▶ | default => sub {[]}, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 2 | init_arg => undef, | |||||||||
| 25 | traits => ['Array'], | ||||||||||
| 26 | handles => { | ||||||||||
| 27 | add_reject_callback => 'push', | ||||||||||
| 28 | reject_callbacks => 'elements' | ||||||||||
| 29 | } | ||||||||||
| 30 | ); | ||||||||||
| 31 | |||||||||||
| 32 | 2 | has _is_unfulfilled => ( | |||||||||
| 33 | is => 'rw', | ||||||||||
| 34 | isa => 'Bool', | ||||||||||
| 35 | default => 1, | ||||||||||
| 36 | reader => 'is_unfulfilled', | ||||||||||
| 37 | writer => '_is_unfulfilled' | ||||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | has resolution => ( | ||||||||||
| 42 | is => 'rw', | ||||||||||
| 43 | isa => 'ArrayRef', | ||||||||||
| 44 | predicate => 'is_resolved', | ||||||||||
| 45 | reader => 'resolution', | ||||||||||
| 46 | writer => '_resolution', | ||||||||||
| 47 | 14 | ▶ | trigger => sub { shift->_is_unfulfilled(0) } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 2 | ); | |||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | has rejection => ( | ||||||||||
| 52 | is => 'rw', | ||||||||||
| 53 | isa => 'ArrayRef', | ||||||||||
| 54 | predicate => 'is_rejected', | ||||||||||
| 55 | reader => 'rejection', | ||||||||||
| 56 | writer => '_rejection', | ||||||||||
| 57 | 3 | ▶ | trigger => sub { shift->_is_unfulfilled(0) } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 2 | ); | |||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | 10 | ▶ | sub then { | ||||||||
|
Subroutine
then: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 62 | 10 | my ( $self, $resolve, $reject ) = @_; | |||||||||
| 63 | |||||||||||
| 64 | 10 | ▶ | $self->add_resolve_callback($resolve) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | if defined($resolve); | ||||||||||
| 66 | |||||||||||
| 67 | 10 | ▶ | $self->add_reject_callback($reject) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | if defined($reject); | ||||||||||
| 69 | |||||||||||
| 70 | 10 | my $chain = Object::Deferred->new; | |||||||||
| 71 | 10 | ▶ | $self->add_resolve_callback(sub { $chain->resolve(@{$self->resolution}) }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 10 | ▶ | $self->add_reject_callback(sub { $chain->reject(@{$self->rejection}) }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 10 | return $chain; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | |||||||||||
| 77 | 14 | ▶ | sub resolve { | ||||||||
|
Subroutine
resolve: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 78 | 14 | my $self = shift; | |||||||||
| 79 | 14 | ▶ | return unless $self->is_unfulfilled; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 14 | $self->_resolution([@_]); | |||||||||
| 81 | 14 | foreach my $cb ( $self->resolve_callbacks ) { | |||||||||
| 82 | 6 | $cb->(@_); | |||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 20 | ▶ | after add_resolve_callback => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 87 | 20 | my($self, $cb) = @_; | |||||||||
| 88 | 20 | ▶ | $cb->(@{$self->resolution}) if $self->is_resolved; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 2 | }; | |||||||||
| 90 | |||||||||||
| 91 | |||||||||||
| 92 | 3 | ▶ | sub reject { | ||||||||
|
Subroutine
reject: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 93 | 3 | my $self = shift; | |||||||||
| 94 | 3 | ▶ | return unless $self->is_unfulfilled; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 3 | $self->_rejection([@_]); | |||||||||
| 96 | 3 | foreach my $cb ( $self->reject_callbacks ) { | |||||||||
| 97 | 2 | $cb->(@_); | |||||||||
| 98 | } | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 16 | ▶ | after add_reject_callback => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 102 | 16 | my($self, $cb) = @_; | |||||||||
| 103 | 16 | ▶ | $cb->(@{$self->rejection}) if $self->is_rejected; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 2 | }; | |||||||||
| 105 | |||||||||||
| 106 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||
| 107 | 2 | 1; | |||||||||
| 108 | |||||||||||
| 109 | __END__ | ||||||||||