File Coverage

blib/lib/Promise/XS/Deferred.pm
Criterion Covered Total %
statement 6 25 24.0
branch n/a
condition 0 6 0.0
subroutine 2 11 18.1
pod 0 3 0.0
total 8 45 17.7


line stmt bran cond sub pod time code
1             package Promise::XS::Deferred;
2              
3 26     26   143 use strict;
  26         35  
  26         600  
4 26     26   98 use warnings;
  26         41  
  26         10351  
5              
6             =encoding utf-8
7              
8             =head1 NAME
9              
10             Promise::XS::Deferred
11              
12             =head1 SYNOPSIS
13              
14             See L.
15              
16             =head1 DESCRIPTION
17              
18             This class implements a promise’s “producer” behavior. It is not
19             to be instantiated directly, but rather via L.
20              
21             =head1 BASIC METHODS
22              
23             The following are what’s needed to implement normal promise workflows:
24              
25             =head2 $obj = I->resolve( @ARGUMENTS )
26              
27             Resolves I’s promise, assigning the given @ARGUMENTS as the value.
28             Returns I.
29              
30             B Behavior here is B defined if anything in @ARGUMENTS is
31             itself a promise.
32              
33             =head2 $obj = I->reject( @ARGUMENTS )
34              
35             Like C but rejects the promise instead.
36              
37             =head1 ADDITIONAL METHODS
38              
39             =head2 $yn = I->is_pending()
40              
41             Returns a boolean that indicates whether the promise is still pending
42             (as opposed to resolved or rejected).
43              
44             This shouldn’t normally be necessary but can be useful in debugging.
45              
46             For compatibility with preexisting promise libraries, C
47             exists as an alias for this logic.
48              
49             =head2 $obj = I->clear_unhandled_rejection()
50              
51             Ordinarily, if a promise’s rejection is “unhandled”, a warning about the
52             unhandled rejection is produced. Call this after C to silence
53             that warning. (It’s generally better, of course, to handle all errors.)
54              
55             =cut
56              
57             #----------------------------------------------------------------------
58              
59             *is_in_progress = *is_pending;
60              
61             our $_NOTHING_CR = sub { };
62              
63             #----------------------------------------------------------------------
64             # Undocumented, by design:
65              
66             sub set_deferral_AnyEvent() {
67 0     0 0   require AnyEvent;
68              
69 0   0       ___set_deferral_generic(
70             AnyEvent->can('postpone') || \&_anyevent_postpone_compat,
71             undef,
72             _DEFER_ANYEVENT(),
73             );
74             }
75              
76             sub set_deferral_IOAsync {
77 0     0 0   my ($loop) = @_;
78              
79             ___set_deferral_generic(
80             $loop->can('later') || \&_ioasync_later_compat,
81             $loop,
82             _DEFER_IOASYNC(),
83 0     0     sub { $loop->stop() },
84 0   0       );
85             }
86              
87             sub set_deferral_Mojo() {
88 0     0 0   require Mojo::IOLoop;
89              
90             ___set_deferral_generic(
91             Mojo::IOLoop->can('next_tick') || \&_mojo_next_tick_compat,
92             'Mojo::IOLoop',
93             _DEFER_MOJO(),
94 0     0     sub { Mojo::IOLoop->stop() },
95 0   0       );
96             }
97              
98             #----------------------------------------------------------------------
99             # Polyfills for old versions of the event loops that didn’t include
100             # zero-timer convenience functions:
101              
102             my %ae_timer;
103              
104             sub _anyevent_postpone_compat {
105 0     0     my $cb = $_[0];
106              
107 0           my ($w, $w_str);
108              
109             $w = AnyEvent->timer( after => 0, cb => sub {
110 0     0     delete $ae_timer{$w_str};
111 0           $cb->();
112 0           } );
113              
114 0           $w_str = "$w";
115 0           $ae_timer{$w_str} = $w;
116             }
117              
118             sub _ioasync_later_compat {
119 0     0     local ($@, $!);
120 0           require IO::Async::Timer;
121              
122 0           $_[0]->add( IO::Async::Timer->new(
123             mode => 'countdown',
124             delay => 0,
125             on_expire => $_[1],
126             )->start() );
127             }
128              
129             sub _mojo_next_tick_compat {
130 0     0     Mojo::IOLoop->timer( 0, $_[1] );
131             }
132              
133             1;
134              
135             __END__