File Coverage

blib/lib/Thunderhorse/Context.pm
Criterion Covered Total %
statement 41 53 77.3
branch 2 4 50.0
condition 2 3 66.6
subroutine 10 13 76.9
pod 6 6 100.0
total 61 79 77.2


line stmt bran cond sub pod time code
1             package Thunderhorse::Context;
2             $Thunderhorse::Context::VERSION = '0.102';
3 21     21   347 use v5.40;
  21         163  
4 21     21   151 use Mooish::Base -standard;
  21         56  
  21         270  
5              
6 21     21   350911 use Devel::StrictMode;
  21         13325  
  21         1931  
7              
8 21     21   12851 use Thunderhorse::Request;
  21         58096  
  21         1381  
9 21     21   14376 use Thunderhorse::Response;
  21         12737  
  21         2500  
10 21     21   12360 use Thunderhorse::WebSocket;
  21         13127  
  21         1945  
11 21     21   13329 use Thunderhorse::SSE;
  21         13697  
  21         22687  
12              
13             extends 'Gears::Context';
14              
15             has param 'pagi' => (
16             (STRICT ? (isa => Tuple [HashRef, CodeRef, CodeRef]) : ()),
17             writer => -hidden,
18             );
19              
20             # match structure is recursive
21             has field 'match' => (
22             (STRICT ? (isa => (InstanceOf ['Gears::Router::Match']) | ArrayRef) : ()),
23             writer => 1,
24             );
25              
26             has field 'req' => (
27             (STRICT ? (isa => InstanceOf ['Thunderhorse::Request']) : ()),
28             default => sub ($self) { Thunderhorse::Request->new(context => $self) },
29             handles => [qw(stash)],
30             );
31              
32             has field 'res' => (
33             (STRICT ? (isa => InstanceOf ['Thunderhorse::Response']) : ()),
34             default => sub ($self) { Thunderhorse::Response->new(context => $self) },
35             );
36              
37             # NOTE: websocket must be lazy, because it will die if scope is not websocket
38             has field 'ws' => (
39             (STRICT ? (isa => InstanceOf ['Thunderhorse::WebSocket']) : ()),
40             predicate => 1,
41             lazy => sub ($self) { Thunderhorse::WebSocket->new(context => $self) },
42             );
43              
44             # NOTE: sse must be lazy, because it will die if scope is not sse
45             has field 'sse' => (
46             (STRICT ? (isa => InstanceOf ['Thunderhorse::SSE']) : ()),
47             predicate => 1,
48             lazy => sub ($self) { Thunderhorse::SSE->new(context => $self) },
49             );
50              
51             has field '_consumed' => (
52             (STRICT ? (isa => Bool) : ()),
53             writer => 1,
54             default => false,
55             );
56              
57 101         148 sub update ($self, $scope, $receive, $send)
  101         140  
  101         137  
58 101     101 1 160 {
  101         138  
  101         135  
59 101         2036 $self->_set_pagi([$scope, $receive, $send]);
60              
61 101         3403 $self->req->update($scope, $receive, $send);
62 101         608 $self->res->update($scope, $receive, $send);
63              
64 101 50       336 $self->ws->update($scope, $receive, $send)
65             if $self->has_ws;
66              
67 101 50       338 $self->sse->update($scope, $receive, $send)
68             if $self->has_sse;
69              
70 101         265 return;
71             }
72              
73             sub scope ($self)
74 0     0 1 0 {
  0         0  
  0         0  
75 0         0 return $self->pagi->[0];
76             }
77              
78             sub receiver ($self)
79 0     0 1 0 {
  0         0  
  0         0  
80 0         0 return $self->pagi->[1];
81             }
82              
83             sub sender ($self)
84 0     0 1 0 {
  0         0  
  0         0  
85 0         0 return $self->pagi->[2];
86             }
87              
88             sub consume ($self)
89 15     15 1 49 {
  15         26  
  15         26  
90 15         460 $self->_set_consumed(true);
91 15         536 return $self;
92             }
93              
94             sub is_consumed ($self)
95 307     307 1 574 {
  307         437  
  307         413  
96 307   66     1971 return $self->_consumed
97             || $self->res->is_sent
98             || ($self->has_ws && $self->ws->is_closed)
99             || ($self->has_sse && $self->sse->is_closed);
100             }
101              
102             __END__