Skip to content
This repository was archived by the owner on Jun 16, 2022. It is now read-only.

Commit 58365bf

Browse files
committed
added ability to read from state component with .value
added ability for child state components to delete itself fixed cdn build names
1 parent b27427d commit 58365bf

File tree

8 files changed

+69
-18
lines changed

8 files changed

+69
-18
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## 1.1.0 (June 9, 2018)
2+
* added ability to read from state component with .value
3+
* added ability for child state components to delete itself
4+
* fixed cdn build names
5+
6+
## 1.0.0 (June 9, 2018)
7+
* implemented `createState`, which returns an `StateProvider`, `GlobalState`, `addState`
8+
* support react and preact
9+
* cdn builds

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ Highlights
5454
* state is composed of data + actions
5555
* actions return an update to merge into the state
5656
* actions can be `async`
57+
* read state `value` directly off state components and call `delete` to remove itself (make sure it is actually no longer used or else `undefined` will be rendered)
5758
* use dynamic import to code split new data/actions/state/components
5859
* supports preact, just import 'flipstate/preact'. Requires https://github.com/valotas/preact-context
5960
* incoming dev tool which will allow you to flip to any state
61+
62+
Samples
63+
-------
64+
* [Counter](https://codesandbox.io/s/rro76qy63m)
65+
* [Code splitting](https://codesandbox.io/s/pwk9kwl2nm)

build.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ async function main () {
88
const configs = [
99
{
1010
input: 'index.js',
11-
output: 'context-state',
11+
output: 'flipstate',
1212
globals: {
1313
react: 'React'
1414
},
1515
external: ['react']
1616
},
1717
{
1818
input: 'preact.js',
19-
output: 'context-state.preact',
19+
output: 'flipstate.preact',
2020
globals: {
2121
preact: 'preact',
2222
'preact-context': 'preactContext'
@@ -42,7 +42,7 @@ async function main () {
4242
await bundle.write({
4343
file: `dist/${output}${min ? '.min' : ''}.js`,
4444
format: 'umd',
45-
name: 'contextState',
45+
name: 'flipstate',
4646
globals
4747
})
4848
}))

factory.js

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ export default (h, Component, createContext) => (initialSpecification = {}) => {
7070

7171
that = this
7272
update = (partial) => {
73-
const nextState = deepMerge(this.state, partial)
74-
this.setState(nextState)
73+
this.setState((state) => deepMerge(state, partial))
7574
}
7675
this.state = wireSpecification(this, [], initialSpecification)
76+
77+
this.value = this.value.bind(this)
78+
this.dangerouslySetState = this.dangerouslySetState.bind(this)
7779
}
7880

7981
componentWillMount () {
@@ -82,6 +84,14 @@ export default (h, Component, createContext) => (initialSpecification = {}) => {
8284
})
8385
}
8486

87+
value () {
88+
return this.state
89+
}
90+
91+
dangerouslySetState (state) {
92+
this.setState(state)
93+
}
94+
8595
render () {
8696
return h(State.Provider, {
8797
value: this.state
@@ -99,15 +109,41 @@ export default (h, Component, createContext) => (initialSpecification = {}) => {
99109
} else {
100110
preAddedStates.push({scope, specification})
101111
}
102-
return ({children, render}) => {
103-
const closure = typeof children === 'function'
104-
? children
105-
: children[0] || render
106-
if (!closure) {
107-
return
112+
let referenceCount = 0
113+
let markForDeletion = false
114+
class ChildState extends Component {
115+
componentDidMount () {
116+
referenceCount++
117+
}
118+
119+
componentWillUnmount () {
120+
referenceCount--
121+
if (markForDeletion && referenceCount === 0) {
122+
update(createObjectByPath(scope, undefined))
123+
}
124+
}
125+
126+
render () {
127+
const {children, render} = this.props
128+
const closure = typeof children === 'function'
129+
? children
130+
: children[0] || render
131+
if (!closure) {
132+
return
133+
}
134+
return h(State.Consumer, {}, (state) => closure(path(scope, state)))
108135
}
109-
return h(State.Consumer, {}, (state) => closure(path(scope, state)))
110136
}
137+
Object.defineProperty(ChildState, 'value', {
138+
get: () => path(scope, that.state)
139+
})
140+
Object.defineProperty(ChildState, 'delete', {
141+
value () {
142+
markForDeletion = true
143+
},
144+
writable: false
145+
})
146+
return ChildState
111147
}
112148
}
113149
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flipstate",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Scalable state management with Context API",
55
"main": "index.js",
66
"scripts": {

sample/preact.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
<meta charset="UTF-8" />
55
<script src="https://unpkg.com/preact@8.2.9/dist/preact.js"></script>
66
<script src="https://unpkg.com/preact-context@1.0.2/dist/context.js"></script>
7-
<script src="../dist/context-state.preact.js"></script>
7+
<script src="../dist/flipstate.preact.js"></script>
88
</head>
99

1010
<body>
1111
<script>
1212
const {h, render} = preact
13-
const createState = contextState
13+
const createState = flipstate
1414

1515
const {
1616
StateProvider,

sample/react.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<meta charset="UTF-8" />
55
<script src="https://unpkg.com/react@16.4.0/umd/react.development.js"></script>
66
<script src="https://unpkg.com/react-dom@16.4.0/umd/react-dom.development.js"></script>
7-
<script src="../dist/context-state.js"></script>
7+
<script src="../dist/flipstate.js"></script>
88
</head>
99

1010
<body>
1111
<div id="entry"></div>
1212
<script>
1313
const h = React.createElement
1414
const {render} = ReactDOM
15-
const createState = contextState
15+
const createState = flipstate
1616

1717
const {
1818
StateProvider,

0 commit comments

Comments
 (0)