preact pull stream
Create a duplex stream from a preact component
This will take a preact component, and return a new component with some additional properties -- .createSource()
, and .sink
, so that you can easily connect a view to a pull-stream. The source produces events from the dom, and the sink consumes state objects that get passed down as props, re-rendering for each new state.
install
npm install preact-pull-stream
example
var h render = var createViewStream = var assert = var xtend = var S = var scan = var initState = hello: 'world' n: 0 // create a duplex stream from a preact componentvar View = { var emit = props if propsn === 0 process return <div> hello: propshello propsn <br /> /* `emit('foo')` returns a function that takes a dom event, and emits events to the stream in the form [type, data], eg: `['foo', <input event>]` The curried emit functions are memoized based on type, so we do not create a new function on each render */ <input type="text" value=propshello onInput= autofocus=true /> </div>} // transform view events into new statesvar states$ = // View.sink will re-render on each event in the stream