New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: result is undefined when using tasks #431

Closed
hoelzro opened this Issue Oct 28, 2015 · 1 comment

Comments

Projects
None yet
2 participants
@hoelzro

hoelzro commented Oct 28, 2015

I'm not sure if this belongs here or in evancs/elm-http, but from what I understand, I think this may be a general bug with tasks (if it's a bug at all).

I've been experimenting with Elm, and I wanted to create an example that sends requests to an HTTP server every click, but ignore clicks that happen when a request is in flight, so as to not overload the server or deal with requests that would involve stale data. I very well may be doing in wrong, but my example program results in the following JS error:

TypeError: result is undefined Test.elm:9216:11

The line in question looks like this:

if (tag === 'AndThen' || tag === 'Catch')
    {
        var result = mark('runnable', task.task);
        while (result.status === 'runnable') // line 9216
        {
            result = stepTask(onComplete, root, result.task);
        }

I should also mention that Elm keeps sending requests to the server even when I'm not clicking. The code I wrote to come across this bug is below; please also feel free to give me any feedback on how to write better Elm, as I am a newbie. =)

Test.elm:

import Http

import Graphics.Element exposing (Element, show)
import Mouse
import Task exposing (Task, andThen)

import Debug

type alias ClickState = {
    clicks          : Int,
    requestInFlight : Bool,
    lastResponse    : String
}

initialState : ClickState
initialState = {
    clicks          = 0,
    requestInFlight = False,
    lastResponse    = "" }

type Action =
      Click
    | Response String

squareMailbox : Signal.Mailbox String
squareMailbox = Signal.mailbox "0"

squareTask : Int -> Task Http.Error ()
squareTask value =
  let url    = "http://localhost:5000/square/" ++ (toString value)
      httpTask = Http.getString url
      sendTask = Signal.send squareMailbox.address
  in
      httpTask `andThen` sendTask

applicationEvents : Signal Action
applicationEvents =
  Signal.merge
    (Signal.map (\_ -> Click) Mouse.clicks)
    (Signal.map Response squareMailbox.signal)

update : Action -> ClickState -> ClickState
update action {clicks, requestInFlight, lastResponse} =
  case action of
    Click        -> { clicks = clicks + 1, requestInFlight = True, lastResponse = lastResponse }
    Response res -> { clicks = clicks, requestInFlight = False, lastResponse = res }

mainSignal : Signal ClickState
mainSignal =
  Signal.foldp update initialState applicationEvents

extractRequest : ClickState -> Maybe Int
extractRequest {clicks, requestInFlight} =
    if requestInFlight
      then Nothing
      else Just clicks

port squareTaskPort : Signal (Task Http.Error ())
port squareTaskPort = Signal.map squareTask <| Signal.filterMap extractRequest 0 mainSignal

view : ClickState -> Element
view {clicks, lastResponse} =
  show (clicks, lastResponse)

main : Signal Element
main = Signal.map view mainSignal

app.psgi (a simple stupid Perl webserver that forwards everything but /square/ to elm-reactor; /square/ itself takes a number in the URL, squares that after sleeping a second, and outputs the result)

#!/usr/bin/env perl

use strict;
use warnings;

use Plack::App::Proxy;
use Plack::Builder;

my $square_app = sub {
    my ( $env ) = @_;

    my $value = 0;

    if($env->{'PATH_INFO'} =~ m{/(\d+)$}) {
        sleep 1;
        $value = $1;
    }

    my $answer = $value * $value;

    [
        200,
        ['Content-Type' => 'text/plain'],
        [$answer],
    ]
};

my $proxy = Plack::App::Proxy->new(remote => 'http://localhost:8000');

builder {
    mount '/square' => $square_app;
    mount '/'       => $proxy->to_app;
};
@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Sep 22, 2016

Member

I'm not sure if this problem still exists, but we should migrate it to an http://sscce.org that uses Elm 0.17.1 and the latest libraries.

Member

evancz commented Sep 22, 2016

I'm not sure if this problem still exists, but we should migrate it to an http://sscce.org that uses Elm 0.17.1 and the latest libraries.

@evancz evancz closed this Sep 22, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment