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

Commit e9eacb6

Browse files
committed
added hooks for devtool
ensure production builds do not include devtool
1 parent 71af551 commit e9eacb6

File tree

9 files changed

+129
-16
lines changed

9 files changed

+129
-16
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"react"
1717
],
1818
"rules": {
19+
"no-useless-return": "off"
1920
},
2021
"globals": {
2122
}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.2.0 (June 11, 2018)
2+
* added hooks for devtool
3+
* ensure production builds do not include devtool
4+
15
## 1.1.2 (June 10, 2018)
26
* added composed state samples to readme
37

build.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const {rollup} = require('rollup')
2-
const commonjs = require('rollup-plugin-commonjs')
2+
const conditional = require('rollup-plugin-conditional')
33
const nodeResolve = require('rollup-plugin-node-resolve')
4+
const commonjs = require('rollup-plugin-commonjs')
5+
const stripCode = require('rollup-plugin-strip-code')
46
const babel = require('rollup-plugin-babel')
57
const {terser} = require('rollup-plugin-terser')
68

@@ -24,14 +26,20 @@ async function main () {
2426
external: ['preact', 'preact-context']
2527
}
2628
]
27-
function build (min = false) {
29+
function build ({devBuild = false, min = false} = {}) {
2830
return Promise.all(configs.map(async ({input, output, globals, external}) => {
2931
const bundle = await rollup({
3032
input: input,
3133
external,
3234
plugins: [
3335
nodeResolve(),
3436
commonjs(),
37+
conditional(!devBuild, [
38+
stripCode({
39+
start_comment: ' START DEVELOPMENT BUILD ONLY ',
40+
end_comment: ' END DEVELOPMENT BUILD ONLY '
41+
})
42+
]),
3543
babel({
3644
externalHelpers: false,
3745
exclude: 'node_modules/**'
@@ -40,15 +48,15 @@ async function main () {
4048
]
4149
})
4250
await bundle.write({
43-
file: `dist/${output}${min ? '.min' : ''}.js`,
51+
file: `dist/${output}${devBuild ? '.dev' : ''}${min ? '.min' : ''}.js`,
4452
format: 'umd',
4553
name: 'flipstate',
4654
globals
4755
})
4856
}))
4957
}
5058

51-
return Promise.all([build(), build(true)])
59+
return Promise.all([build(), build({devBuild: true}), build({min: true})])
5260
}
5361

5462
main()

dev-tool.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export default (getThat, getUpdate) => {
2+
if (!process || !process.env || process.env.NODE_ENV === 'production') {
3+
return () => {}
4+
}
5+
let devToolSubscribed
6+
function sendStateToDevTool () {
7+
const {origin, source} = devToolSubscribed
8+
source.postMessage({
9+
protocol: 'flipstate-devtool v1',
10+
type: 'application state',
11+
location: window.location.href,
12+
state: JSON.stringify(getThat().state)
13+
}, origin)
14+
devToolSubscribed = undefined
15+
}
16+
window.addEventListener('message', (event) => {
17+
const {origin, data = {}, source} = event
18+
if (!origin.startsWith('http://localhost:') && origin !== 'https://concept-not-found.github.io') {
19+
return
20+
}
21+
switch (data.type) {
22+
case 'get state':
23+
devToolSubscribed = {
24+
source,
25+
origin
26+
}
27+
return sendStateToDevTool()
28+
case 'set state':
29+
return getUpdate()(JSON.parse(data.state))
30+
case 'subscribe state update':
31+
devToolSubscribed = {
32+
source,
33+
origin
34+
}
35+
return
36+
default:
37+
// ignore messages we don't understand
38+
}
39+
}, false)
40+
41+
return () => {
42+
if (devToolSubscribed) {
43+
sendStateToDevTool()
44+
}
45+
}
46+
}

factory.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import isObject from 'is-plain-object'
1+
import isPlainObject from 'is-plain-object'
22

33
function deepMerge (target, source) {
4-
if (!isObject(target) || !isObject(source)) {
4+
if (!isPlainObject(target) || !isPlainObject(source)) {
55
return source
66
}
77
const destination = {}
@@ -37,7 +37,10 @@ function path ([head, ...tail], object) {
3737
return path(tail, object[head])
3838
}
3939

40-
export default (h, Component, createContext) => (initialSpecification = {}) => {
40+
export default (devTool, h, Component, createContext) => (initialSpecification = {}) => {
41+
/* START DEVELOPMENT BUILD ONLY */
42+
const onStateUpdate = devTool(() => that, () => update)
43+
/* END DEVELOPMENT BUILD ONLY */
4144
const State = createContext({})
4245

4346
let update
@@ -75,7 +78,6 @@ export default (h, Component, createContext) => (initialSpecification = {}) => {
7578
this.state = wireSpecification(this, [], initialSpecification)
7679

7780
this.value = this.value.bind(this)
78-
this.dangerouslySetState = this.dangerouslySetState.bind(this)
7981
}
8082

8183
componentWillMount () {
@@ -88,11 +90,10 @@ export default (h, Component, createContext) => (initialSpecification = {}) => {
8890
return this.state
8991
}
9092

91-
dangerouslySetState (state) {
92-
this.setState(state)
93-
}
94-
9593
render () {
94+
/* START DEVELOPMENT BUILD ONLY */
95+
onStateUpdate()
96+
/* END DEVELOPMENT BUILD ONLY */
9697
return h(State.Provider, {
9798
value: this.state
9899
}, this.props.children)

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import {createElement, Component, createContext} from 'react'
22

3+
/* START DEVELOPMENT BUILD ONLY */
4+
import devTool from './dev-tool'
5+
/* END DEVELOPMENT BUILD ONLY */
6+
37
import factory from './factory'
48

5-
export default factory(createElement, Component, createContext)
9+
export default factory(devTool, createElement, Component, createContext)

package-lock.json

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

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flipstate",
3-
"version": "1.1.2",
3+
"version": "1.2.0",
44
"description": "Scalable state management with Context API",
55
"main": "index.js",
66
"scripts": {
@@ -44,7 +44,9 @@
4444
"rollup": "0.60.1",
4545
"rollup-plugin-babel": "3.0.4",
4646
"rollup-plugin-commonjs": "9.1.3",
47+
"rollup-plugin-conditional": "1.1.1",
4748
"rollup-plugin-node-resolve": "3.3.0",
49+
"rollup-plugin-strip-code": "0.2.5",
4850
"rollup-plugin-terser": "1.0.1"
4951
}
5052
}

preact.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import {h, Component} from 'preact'
22
import {createContext} from 'preact-context'
33

4+
/* START DEVELOPMENT BUILD ONLY */
5+
import devTool from './dev-tool'
6+
/* END DEVELOPMENT BUILD ONLY */
7+
48
import factory from './factory'
59

6-
export default factory(h, Component, createContext)
10+
export default factory(devTool, h, Component, createContext)

0 commit comments

Comments
 (0)