An open API service indexing awesome lists of open source software.

https://github.com/ryym/thisy

A toy state management library heavily depending on JavaScript 'this'!
https://github.com/ryym/thisy

javascript-library

Last synced: about 2 months ago
JSON representation

A toy state management library heavily depending on JavaScript 'this'!

Awesome Lists containing this project

README

        

# Thisy

Thisy is a toy library to manage application state using plain classes.

```javascript
import { methodsOf, makeStore } from 'thisy'

// 1.
// Define a state class.
class CounterState {
constructor(count = 0) {
this.count = count
}

getCount() {
return this.count
}

// A state modifier method must have a `$` prefix.
$increment(n = 1) {
this.count += n
}
}

// 2.
// Extract the state class methods.
// They are `Action`s in Flux.
const Act = methodsOf(CounterState)
console.log(typeof Act.$increment) //=> 'function'

// 3.
// Create a store.
const store = makeStore(() => [new CounterState()])

// You can subscribe state updates.
store.subscribe(act => console.log('Updated by', act.name))

// 4.
// Send an action to update the state!
store.send(Act.$increment, 10)
//=> 'Updated by $increment'

// 5.
// `send` can also be used to get state data.
console.log(store.send(Act.getCount))
//=> 1
```

Using with React:

```javascript
import { Provider, connect } from 'thisy-react'
import { Act } from '../store/counter'

// Define a component.
const Counter = ({ send, title, count }) => (


{title}


Count: {count}


send(Act.$increment)}>
Increment


)

// Connect the component with your store.
const ConnectedCounter = connect(Conunter, {
mapProps: send => ({ title }) => ({
send,
title,
count: send(Act.getCount),
}),
})

ReactDOM.render(


,
document.getElementByID('counter')
)
```