diff options
author | Birte Kristina Friesel <derf@finalrewind.org> | 2023-07-23 20:18:10 +0200 |
---|---|---|
committer | Birte Kristina Friesel <derf@finalrewind.org> | 2023-07-23 20:18:10 +0200 |
commit | 38ad42b42cbf6da6254a2498a2fc553507cb3f18 (patch) | |
tree | aafcec15240e9804555e66cfb4af1ebccff823c9 /lib/Travelynx | |
parent | c0754f9e871e68019cd6109e8fc2649b515b9fc1 (diff) |
convert checkout to promises (checkout_p)
Diffstat (limited to 'lib/Travelynx')
-rw-r--r-- | lib/Travelynx/Command/work.pm | 35 | ||||
-rwxr-xr-x | lib/Travelynx/Controller/Traveling.pm | 153 |
2 files changed, 122 insertions, 66 deletions
diff --git a/lib/Travelynx/Command/work.pm b/lib/Travelynx/Command/work.pm index 3086737..23d2925 100644 --- a/lib/Travelynx/Command/work.pm +++ b/lib/Travelynx/Command/work.pm @@ -86,15 +86,15 @@ sub run { # train. if ($checked_in) { - # check out (adds a cancelled journey and resets journey state - # to checkin - $self->app->checkout( + # check out (adds a cancelled journey and resets journey state + # to checkin + $self->app->checkout_p( station => $arr, - force => 1, + force => 2, dep_eva => $dep, arr_eva => $arr, uid => $uid - ); + )->wait; } } else { @@ -153,15 +153,15 @@ sub run { if ( $checked_in and $train->arrival_is_cancelled ) { - # check out (adds a cancelled journey and resets journey state - # to destination selection) - $self->app->checkout( + # check out (adds a cancelled journey and resets journey state + # to destination selection) + $self->app->checkout_p( station => $arr, force => 0, dep_eva => $dep, arr_eva => $arr, uid => $uid - ); + )->wait; } else { $self->app->add_route_timestamps( @@ -174,21 +174,24 @@ sub run { } } elsif ( $entry->{real_arr_ts} ) { - my ( undef, $error ) = $self->app->checkout( + my ( undef, $error ) = $self->app->checkout_p( station => $arr, - force => 1, + force => 2, dep_eva => $dep, arr_eva => $arr, uid => $uid - ); - if ($error) { - die("${error}\n"); - } + )->catch( + sub { + my ($error) = @_; + $self->app->log->error("work($uid)/arrival: $@"); + $errors += 1; + } + )->wait; } }; if ($@) { - $errors += 1; $self->app->log->error("work($uid)/arrival: $@"); + $errors += 1; } eval { } diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index 48fa944..3e051a1 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -622,17 +622,26 @@ sub travel_action { if ( $params->{action} eq 'checkin' ) { my $status = $self->get_user_status; + my $promise; + if ( $status->{checked_in} and $status->{arr_eva} and $status->{arrival_countdown} <= 0 ) { - $self->checkout( station => $status->{arr_eva} ); + $promise = $self->checkout_p( station => $status->{arr_eva} ); + } + else { + $promise = Mojo::Promise->resolve; } $self->render_later; - $self->checkin_p( - station => $params->{station}, - train_id => $params->{train} + $promise->then( + sub { + return $self->checkin_p( + station => $params->{station}, + train_id => $params->{train} + ); + } )->then( sub { my $destination = $params->{dest}; @@ -648,17 +657,26 @@ sub travel_action { # Silently ignore errors -- if they are permanent, the user will see # them when selecting the destination manually. - my ( $still_checked_in, undef ) = $self->checkout( + return $self->checkout_p( station => $destination, force => 0 ); - my $station_link = '/s/' . $destination; - $self->render( - json => { - success => 1, - redirect_to => $still_checked_in ? '/' : $station_link, - }, - ); + } + )->then( + sub { + my ( $still_checked_in, undef ) = @_; + if ( my $destination = $params->{dest} ) { + my $station_link = '/s/' . $destination; + $self->render( + json => { + success => 1, + redirect_to => $still_checked_in + ? '/' + : $station_link, + }, + ); + } + return; } )->catch( sub { @@ -673,28 +691,47 @@ sub travel_action { )->wait; } elsif ( $params->{action} eq 'checkout' ) { - my ( $still_checked_in, $error ) = $self->checkout( + $self->render_later; + $self->checkout_p( station => $params->{station}, force => $params->{force} - ); - my $station_link = '/s/' . $params->{station}; + )->then( + sub { + my ( $still_checked_in, $error ) = @_; + my $station_link = '/s/' . $params->{station}; - if ($error) { - $self->render( - json => { - success => 0, - error => $error, - }, - ); - } - else { - $self->render( - json => { - success => 1, - redirect_to => $still_checked_in ? '/' : $station_link, - }, - ); - } + if ($error) { + $self->render( + json => { + success => 0, + error => $error, + }, + ); + } + else { + $self->render( + json => { + success => 1, + redirect_to => $still_checked_in + ? '/' + : $station_link, + }, + ); + } + return; + } + )->catch( + sub { + my ($error) = @_; + $self->render( + json => { + success => 0, + error => $error, + }, + ); + return; + } + )->wait; } elsif ( $params->{action} eq 'undo' ) { my $status = $self->get_user_status; @@ -747,27 +784,43 @@ sub travel_action { )->wait; } elsif ( $params->{action} eq 'cancelled_to' ) { - my ( undef, $error ) = $self->checkout( + $self->render_later; + $self->checkout_p( station => $params->{station}, force => 1 - ); - - if ($error) { - $self->render( - json => { - success => 0, - error => $error, - }, - ); - } - else { - $self->render( - json => { - success => 1, - redirect_to => '/', - }, - ); - } + )->then( + sub { + my ( undef, $error ) = @_; + if ($error) { + $self->render( + json => { + success => 0, + error => $error, + }, + ); + } + else { + $self->render( + json => { + success => 1, + redirect_to => '/', + }, + ); + } + return; + } + )->catch( + sub { + my ($error) = @_; + $self->render( + json => { + success => 0, + error => $error, + }, + ); + return; + } + )->wait; } elsif ( $params->{action} eq 'delete' ) { my $error = $self->journeys->delete( |