Giter Site home page Giter Site logo

pull-stream-app-demo's Introduction

pull stream app demo

Use pull-streams in the browser as a framework for async events. This is like the pattern in cycle.js, but using pull-streams.

use

Start a local server that does everything for you

$ npm install
$ npm run dev

src/index.js

import { h, render } from 'preact'
var toStream = require('preact-pull-stream')
var renderApp = require('./view')
var _ = require('pull-stream-util')
var S = require('pull-stream')
var compose = require('@f/compose')

var AppView = toStream(renderApp.events, renderApp)

function StringState () {
    return { hello: 'world' }
}

StringState.update = {
    foo: (state, ev) => ({ hello: 'foo' }),
    bar: (state, ev) => ({ hello: 'bar' }),
    baz: (state, ev) => ({ hello: 'baz' })
}

function echo (data, cb) {
    setTimeout(cb.bind(null, null, data), 1000)
}

function Effects () {
    return {
        echo: _.chain(compose(
            _.HTTP.fromCb(echo),
            () => 'foo'
        ))
    }
}

var echoState = {
    resolving: 0,
    resolved: []
}
function echoUpdate (state, ev) {
    if (ev.type === 'start') {
        return {
            resolving: state.resolving + 1,
            resolved: state.resolved
        }
    }

    if (ev.type === 'resolve') {
        return {
            resolving: state.resolving - 1,
            resolved: state.resolved.concat([ev.res])
        }
    }
}


// ----------------------------------------------

function main (sources) {
    var effects = Effects()

    var echo$ = S(
        sources.foo(),
        effects.echo
    )

    var echoState$ = _.cat([
        S.once(echoState),
        S( echo$, _.scan(echoUpdate, echoState) )
    ])

    var stringState$ = _.cat([
        S.once(StringState()),
        S(
            sources(),
            _.scan(StringState.update, StringState())
        )
    ])

    return _.combineLatest({
        string: stringState$,
        echos: echoState$
    })
}

S(
    main(AppView.sources),
    AppView.sink
)

render(h(AppView.view), document.getElementById('app'))

pull-stream-app-demo's People

Contributors

nichoth avatar dependabot[bot] avatar

Stargazers

Andrejs Agejevs avatar Carl Barrdahl avatar Jake Burden avatar

Watchers

James Cloos avatar  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.