0.4.0 • Published 6 years ago

silo-react v0.4.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

Silo-React

React state manager based on redux

NPM version Build Status Coverage Status NPM downloads Dependencies

Quick start

import { createSiloStore, Provider, connect } from 'silo-react'
const store = createSiloStore()

store.createPath('todos', {
  initialState() {
    return {
      list: [],
    }
  },
  get: {
    count: ({ state }) => state.list.length,
  },
  set: {
    add({ state }) {
      return {
        ...state,
        list: state.list.concat({ title: `todo title ${state.list.length + 1}`, id: state.list.length })
      }
    }
  },
  onSet(payload) {
    // payload with tracker
  },
  action: {
    async fetchList({ set }) {
      return new Promise(res => setTimeout(() => set.add()), 10)
    }
  },
})

class Todos extends React.Component {
  componentDidMount() {
    this.props.action.fetchList()
  }
  render() {
    return <div>
      <h2>Todos ({this.props.get.count()})</h2>
      <ul>{this.props.state.list.map(item => <li key={item.id}>{item.title}</li>)}</ul>
      <button onClick={() => this.props.set.add()}>Add</button>
    </div>
  }
}
const TodoContainer = connect('todos')(Todos)

ReactDOM.render(
  <Provider store={store}>
    <TodoContainer />
  </Provider>
, document.getElementById('__content'))

Redux Middleware

const logger = s => next => action => {
  console.log(action)
  return next(action)
}
const store = createSiloStore({}, applyMiddleware(logger)(createStore))

Global exec path methods

store.getState('todos') // Get "todos" state
store.exec('action:todos/fetchList', ...params) // exec action with params
store.exec('get:todos/count', ...params) // exec get
store.exec('set:todos/add', ...params) // exec set

Action Records in development

const trackerRule = ({ path, type, method }) => `${type}:${path}/${method}`
const store = createSiloStore({}, undefined, trackerRule)
store.createPath('myPath', {
  action: {
    act1({ action }) {
      return action.act2()
    },
    act2({ action }) {
      return action.act3()
    },
    act3({ tracker }) {
      return tracker.records()
    }
  }
})
expect(store.exec('action:myPath/act1')).toEqual(["action:myPath/act1", "action:myPath/act2", "action:myPath/act3"])

Inject context arguments

const store = createSiloStore()

store.createPath('myPath', {
  action: {
    act({ myContext }) {
       console.log(myContext) // my context
    }
  },
  injectArgs(defaultArgs) {
    return {
      ...defaultArgs,
      myContext: { /* my context */}
    }
  },
})
0.4.0

6 years ago

0.4.0-alpha14

6 years ago

0.4.0-alpha13

6 years ago

0.4.0-alpha12

6 years ago

0.4.0-alpha11

6 years ago

0.4.0-alpha10

6 years ago

0.4.0-alpha9

6 years ago

0.4.0-alpha8

6 years ago

0.4.0-alpha7

6 years ago

0.4.0-alpha6

6 years ago

0.4.0-alpha5

6 years ago

0.4.0-alpha4

6 years ago

0.4.0-alpha3

6 years ago

0.4.0-alpha2

6 years ago

0.4.0-alpha

6 years ago

0.3.0

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago