Code Monkey home page Code Monkey logo

remote-resource's Introduction

remote-resource Build Status

Remote Resource alllows you to handle foreground and background resources, using RemoteData.

For instance, let's say you want to retrieve a list of posts. You are using the RemoteData package, thus you can handle the state of your request (Loading, Success, etc...). You will probably display a loading icon while your request is in progress, and once it has been fully retrieved you will display your posts. But now, let's say you know your posts have changed and you would like to update them. You will probably want to send another request to your webservice with the same Elm RemoteData you used when you first retrieved your posts. That means your users will see your posts disapear for a few milliseconds (depending on the latency and network speed), replaced by a loading icon, to be finally updated and displayed again to your users.

Now, with RemoteResource you can refresh your posts in the background. Than means that you keep displaying your "old" posts to your users, and when the new data has been retrieved you can replace your old posts by the new ones without your users seeing some loading icon!

Getting started

Here is an example where you want to retrieve posts. When you receive a refresh event you retrieve the new posts while still showing the old posts to the users. In your view you will display a button when the new posts have been retrieved to let the user replace the old posts by the new ones:

type Msg
    = Refresh
    | OnDataRetrieved (RemoteData Http.Error (List Post))
    | UpdateData

type alias Model =
    { posts : RemoteResource Http.Error (List Post) }


init : (Model, Cmd Msg)
init =
    ( { posts = RemoteResource.init |> RemoteResource.loading }
    , retrievePosts OnDataRetrieved 
    )

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
    case msg of
        Refresh ->
            ( { model | posts = model.posts |> RemoteData.reloading }
            , retrievePosts OnDataRetrieved
            )

        OnDataRetrived data ->
            ( { model | posts = model.posts |> RemoteData.setBackground data }
            , Cmd.none
            )

        UpdateData ->
            ( { model | posts = model.posts |> RemoteData.replaceResourceByBackground }
            , Cmd.none
            )

view : Model -> Html Msg
view { posts } =
    div []
        [ if RemoteResource.hasNewResource comparePosts posts then
            button [ onClick UpdateData ] [ text "Show my new posts" ]
          else
            text ""
        , case posts of
            NotAsked ->
                loadingIcon

            Loading ->
                loadingIcon

            Failure err ->
                text "Failed"

            Success data ->
                div []
                    (List.map viewPost data)
        ]

subscriptions : Sub Msg
subscriptions =
    onRefresh Refresh

In the example below you will display the number of connected users. When you receive a refresh event you retrieve the new count of connected users and you replace the old count by the new count without showing the loading icon:

type Msg
    = Refresh
    | OnDataRetrieved (RemoteData Http.Error Int)

type alias Model =
    { count : RemoteResource Http.Error Int }


init : (Model, Cmd Msg)
init =
    ( { count = RemoteResource.init |> RemoteResource.loading }
    , retrieveConnectedUsersCount OnDataRetrieved 
    )

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
    case msg of
        Refresh ->
            ( { model | count = model.count |> RemoteData.reloading }
            , retrieveConnectedUsersCount OnDataRetrieved
            )

        OnDataRetrived data ->
            ( { model | count = model.count |> RemoteData.setResource data }
            , Cmd.none
            )

view : Model -> Html Msg
view { count } =
    div []
        [ case count of
            NotAsked ->
                loadingIcon

            Loading ->
                loadingIcon

            Failure err ->
                text "Failed"

            Success data ->
                div [] [ text ("Connected users: " ++ (String.fromInt data)) ]
        ]

subscriptions : Sub Msg
subscriptions =
    onRefresh Refresh

remote-resource's People

Contributors

fabienhenon avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.