Skip to content

Instantly share code, notes, and snippets.

@developit
Created January 29, 2018 15:17
Show Gist options
  • Save developit/f256933605c31d44eaf8c3887ec4287a to your computer and use it in GitHub Desktop.
Save developit/f256933605c31d44eaf8c3887ec4287a to your computer and use it in GitHub Desktop.
unistore + lit-html

lit-html + unistore

import { html, render } from 'lit'
import { provide, connect } from './unlit'

const app = provide(store)( () => html`<div>${counter}</div>` )

const counter = connect('count', {
	add: ({ count }) => ({ count: count+1 })
})( ({ count, actions }) => html`
	<button on-click="${actions.add}">${count}</button>
`);

render(app, document.body)
import { select, mapActions } from '../util'; // ~unistore/src/util
import { directive } from 'lit-html';
export function connect(mapState, createActions) {
if (typeof mapState!=='function') mapState = select(mapState || [])
return comp => props => {
let actions = createActions ? mapActions(createActions, props.store) : {}
let upd = state => comp({ actions, ...mapState(state, props), ...props })
return directive(part => {
store.subscribe( state => part.setValue(upd(state)) )
return upd(store.getState())
})
}
}
export const provide = store => comp => props => comp({ ...props, store });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment