| 1 | 2 | ▶ | use v5.44; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 2 | ▶ | use experimental 'class'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | # A joined (or joining) channel. Obtain one from BoardStreams::Client->join. | ||||||||||
| 5 | # | ||||||||||
| 6 | # Every call of join returns a channel object of its own, and the ones for | ||||||||||
| 7 | # the same name share a single membership on the server. So separate parts | ||||||||||
| 8 | # of a program can each join a channel and each leave it when done: the | ||||||||||
| 9 | # client only leaves when the last of them has. | ||||||||||
| 10 | # | ||||||||||
| 11 | # This is a port of the Channel class of the JavaScript client and is meant | ||||||||||
| 12 | # to behave identically. | ||||||||||
| 13 | 2 | class BoardStreams::Client::Channel { | |||||||||
| 14 | 2 | our $VERSION = "v0.0.60"; | |||||||||
| 15 | |||||||||||
| 16 | 2 | ▶ | use Mojo::Promise; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use RxPerl::Mojo qw(rx_behavior_subject rx_observable rx_replay_subject rx_subject op_filter op_map); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | ▶ | use BoardStreams::Client::Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 2 | field $membership :param; # the BoardStreams::Client::Membership behind this object | |||||||||
| 22 | |||||||||||
| 23 | 2 | field $state_subject = rx_replay_subject->new(1); | |||||||||
| 24 | 2 | field $events_subject = rx_subject->new; | |||||||||
| 25 | 2 | field $gaps_subject = rx_subject->new; | |||||||||
| 26 | 2 | field $status_subject; | |||||||||
| 27 | 2 | field @feed; # subscriptions to the membership | |||||||||
| 28 | 2 | field $joined = Mojo::Promise->new; | |||||||||
| 29 | 2 | field $done = 0; # true once this object has left or the membership is over | |||||||||
| 30 | 2 | field $last_state; | |||||||||
| 31 | |||||||||||
| 32 | 38 | ▶ | ADJUST { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 33 | # A refused join that nobody waits for is not an unhandled error. | ||||||||||
| 34 | 38 | $joined->catch(sub { }); | |||||||||
| 35 | 38 | ▶ | $joined->resolve if $membership->has_joined; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 38 | $status_subject = rx_behavior_subject->new($membership->status_subject->get_value); | |||||||||
| 37 | push @feed, | ||||||||||
| 38 | $membership->state_subject->subscribe(sub ($state) { $state_subject->next($state) }), | ||||||||||
| 39 | $membership->events_subject->subscribe(sub ($event) { $events_subject->next($event) }), | ||||||||||
| 40 | $membership->gaps_subject->subscribe(sub { $gaps_subject->next(undef) }), | ||||||||||
| 41 | $membership->status_subject->subscribe(sub ($status) { | ||||||||||
| 42 | $status_subject->next($status) if $status ne $status_subject->get_value; | ||||||||||
| 43 | 38 | }); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | method name { $membership->name } | ||||||||
|
Subroutine
name: not called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||
| 47 | |||||||||||
| 48 | # The channel's state; replays the latest value and emits on every | ||||||||||
| 49 | # change. Its first value arrives with the first join, but a later | ||||||||||
| 50 | # rejoin that missed nothing does not emit: use status_o to follow | ||||||||||
| 51 | # rejoins. | ||||||||||
| 52 | 6 | ▶ | method state_o { _read_only($state_subject) } | ||||||||
|
Subroutine
state_o: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 53 | |||||||||||
| 54 | # Every event of the channel, in order, as { name, payload, seq }. | ||||||||||
| 55 | 1 | ▶ | method events_o { _read_only($events_subject) } | ||||||||
|
Subroutine
events_o: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 56 | |||||||||||
| 57 | # Emits when this client was away for longer than the server keeps | ||||||||||
| 58 | # history: the state is current again, but some events were missed. | ||||||||||
| 59 | 1 | ▶ | method gaps_o { _read_only($gaps_subject) } | ||||||||
|
Subroutine
gaps_o: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 60 | |||||||||||
| 61 | # Replays the current status: 'joining', 'joined', 'rejected' or | ||||||||||
| 62 | # 'left'. Emits 'joined' on every join, including each automatic rejoin | ||||||||||
| 63 | # after a reconnection, and 'joining' while disconnected. | ||||||||||
| 64 | 8 | ▶ | method status_o { _read_only($status_subject) } | ||||||||
|
Subroutine
status_o: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 65 | |||||||||||
| 66 | # The current state, or undef before the first join completes. | ||||||||||
| 67 | 44 | ▶ | method state { $done ? $last_state : $membership->state } | ||||||||
|
Subroutine
state: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | # A promise that resolves once, when the channel is first joined (at | ||||||||||
| 70 | # once if another part of the program has joined it already), and | ||||||||||
| 71 | # rejects with a BoardStreams::Client::Error if the server refuses. | ||||||||||
| 72 | 14 | ▶ | method joined { $joined } | ||||||||
|
Subroutine
joined: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 73 | |||||||||||
| 74 | # The payloads of the events with the given name. | ||||||||||
| 75 | 3 | ▶ | method on ($event_name) { | ||||||||
|
Subroutine
on: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 76 | return $events_subject->pipe( | ||||||||||
| 77 | 5 | ▶ | op_filter(sub { $_[0]{name} eq $event_name }), | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 78 | 3 | ▶ | op_map(sub { $_[0]{payload} }), | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 79 | ); | ||||||||||
| 80 | 2 | } | |||||||||
| 81 | |||||||||||
| 82 | # Sends an action. It is delivered once, even across reconnections. | ||||||||||
| 83 | 4 | ▶ | method action ($action_name, $payload = undef) { | ||||||||
|
Subroutine
action: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||
| 84 | 4 | ▶ | $membership->submit($self, 'action', $action_name, $payload) unless $done; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 4 | return; | |||||||||
| 86 | 2 | } | |||||||||
| 87 | |||||||||||
| 88 | # Sends a request and returns a promise of the response payload, which | ||||||||||
| 89 | # rejects with a BoardStreams::Client::Error on failure. By the time it | ||||||||||
| 90 | # settles, state and the events already reflect whatever the request | ||||||||||
| 91 | # changed on this channel. | ||||||||||
| 92 | 13 | ▶ | method request ($request_name, $payload = undef) { | ||||||||
|
Subroutine
request: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 93 | 13 | my $promise = Mojo::Promise->new; | |||||||||
| 94 | 13 | ▶ | return $promise->reject(BoardStreams::Client::Error->new(reason => $status_subject->get_value)) if $done; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | $membership->submit($self, 'request', $request_name, $payload, | ||||||||||
| 96 | 3 | ▶ | sub ($response) { $promise->resolve($response) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 97 | 11 | ▶ | sub ($error) { $promise->reject($error) }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 98 | 11 | return $promise; | |||||||||
| 99 | 2 | } | |||||||||
| 100 | |||||||||||
| 101 | # Ends this use of the channel: its observables complete and its | ||||||||||
| 102 | # unanswered requests are rejected. The client leaves the channel on | ||||||||||
| 103 | # the server when every object that join returned for it has left. | ||||||||||
| 104 | # Calling it again does nothing. | ||||||||||
| 105 | 8 | ▶ | method leave { | ||||||||
|
Subroutine
leave: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 106 | 8 | ▶ | return if $done; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 7 | my $error = BoardStreams::Client::Error->new(reason => 'left'); | |||||||||
| 108 | 7 | $self->_finish('left', $error); | |||||||||
| 109 | 7 | $membership->release($self, $error); | |||||||||
| 110 | 7 | $self->_complete; | |||||||||
| 111 | 7 | return; | |||||||||
| 112 | 2 | } | |||||||||
| 113 | |||||||||||
| 114 | # The methods below are for BoardStreams::Client::Membership. | ||||||||||
| 115 | |||||||||||
| 116 | 19 | ▶ | method _joined { $joined->resolve } | ||||||||
|
Subroutine
_joined: called CC=1 SCAR=0.0
| |||||||||||
| 117 | |||||||||||
| 118 | # The membership itself is over: left by the client's close, or refused. | ||||||||||
| 119 | 30 | ▶ | method _ended ($status, $error) { | ||||||||
|
Subroutine
_ended: called CC=2 SCAR=7.6
| |||||||||||
| 120 | 30 | ▶ | $self->_finish($status, $error) unless $done; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 2 | } | |||||||||
| 122 | |||||||||||
| 123 | 37 | ▶ | method _finish ($status, $error) { | ||||||||
|
Subroutine
_finish: called CC=2 SCAR=6.9
| |||||||||||
| 124 | 37 | $done = 1; | |||||||||
| 125 | 37 | $last_state = $membership->state; | |||||||||
| 126 | 37 | $_->unsubscribe for splice @feed; | |||||||||
| 127 | 37 | $status_subject->next($status); | |||||||||
| 128 | 37 | $joined->reject($error); | |||||||||
| 129 | 2 | } | |||||||||
| 130 | |||||||||||
| 131 | 37 | ▶ | method _complete { | ||||||||
|
Subroutine
_complete: called CC=2 SCAR=6.9
| |||||||||||
| 132 | 37 | $_->complete for $state_subject, $events_subject, $gaps_subject, $status_subject; | |||||||||
| 133 | 2 | } | |||||||||
| 134 | |||||||||||
| 135 | # An observable that passes on what a subject emits, without the | ||||||||||
| 136 | # subject's next, error and complete methods. | ||||||||||
| 137 | 16 | ▶ | sub _read_only ($subject) { | ||||||||
|
Subroutine
_read_only: called CC=1 SCAR=0.0
| |||||||||||
| 138 | 16 | ▶ | return rx_observable->new(sub ($subscriber) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 139 | 16 | $subject->subscribe($subscriber); | |||||||||
| 140 | 16 | return; | |||||||||
| 141 | 16 | }); | |||||||||
| 142 | } | ||||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 2 | 1; | |||||||||