omis - lifecycle of store supporting

This commit is contained in:
dntzhang 2019-07-22 15:00:24 +08:00
parent 0f32813a48
commit c1af1b5da7
7 changed files with 1477 additions and 11 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,10 @@
<html>
<head></head>
<body>
<script src="b.js"></script>
</body>
</html>

View File

@ -0,0 +1,76 @@
import { render } from '../../src/omis'
const Counter = (props, store) => {
return (
<div>
<button onClick={store.sub}>-</button>
<span>{store.count}</span>
<button onClick={store.add}>+</button>
</div>
)
}
Counter.store = _ => {
return {
count: 1,
add(e) {
this.count++
this.update()
_.props.onChange(this.count)
},
sub() {
this.count--
this.update()
_.props.onChange(this.count)
},
install(){
console.log('install')
},
installed(){
console.log('installed')
},
uninstall(){
console.log('uninstall')
},
beforeUpdate(){
console.log('beforeUpdate')
},
updated(){
console.log('updated')
},
beforeRender(){
console.log('beforeRender')
},
receiveProps(){
console.log('receiveProps')
}
}
}
const App = (props, store) => {
return (
<div>
<div onClick={store.toggle}>Count from child event: {store.count}</div>
{store.show && <Counter onChange={store.changeHandle}></Counter>}
</div>
)
}
App.store = _ => {
class Store {
count = null
show = true
changeHandle = (count) => {
this.count = count
this.update()
}
toggle = ()=>{
this.show =!this.show
this.update()
}
}
return new Store
}
render(<App />, 'body')

View File

@ -28,7 +28,7 @@ export function Component(props, context) {
* @type {object}
*/
this.context = context;
this.store = {}
/**
* @public
* @type {object}

View File

@ -26,10 +26,16 @@ export function setComponentProps(component, props, renderMode, context, mountAl
if (typeof component.constructor.getDerivedStateFromProps === 'undefined') {
if (!component.base || mountAll) {
if (component.componentWillMount) component.componentWillMount();
//if (component.componentWillMount) component.componentWillMount();
if (component.store.install) component.store.install()
}
else if (component.componentWillReceiveProps) {
component.componentWillReceiveProps(props, context);
else {
// if (component.componentWillReceiveProps) {
// component.componentWillReceiveProps(props, context);
// }
if (component.store.receiveProps) {
component.store.receiveProps(props, context)
}
}
}
@ -98,8 +104,13 @@ export function renderComponent(component, renderMode, mountAll, isChild) {
&& component.shouldComponentUpdate(props, state, context) === false) {
skip = true;
}
else if (component.componentWillUpdate) {
component.componentWillUpdate(props, state, context);
else {
// if (component.componentWillUpdate) {
// component.componentWillUpdate(props, state, context);
// }
if(component.store.beforeUpdate){
component.store.beforeUpdate(props, state, context)
}
}
component.props = props;
component.state = state;
@ -111,6 +122,9 @@ export function renderComponent(component, renderMode, mountAll, isChild) {
if (!skip) {
options.runTimeComponent = component
if(component.store.beforeRender){
component.store.beforeRender()
}
rendered = component.render(props, state, context);
options.runTimeComponent = null
@ -199,8 +213,11 @@ export function renderComponent(component, renderMode, mountAll, isChild) {
// Note: disabled as it causes duplicate hooks, see https://github.com/developit/preact/issues/750
// flushMounts();
if (component.componentDidUpdate) {
component.componentDidUpdate(previousProps, previousState, snapshot);
// if (component.componentDidUpdate) {
// component.componentDidUpdate(previousProps, previousState, snapshot);
// }
if (component.store.updated) {
component.store.updated(previousProps, previousState, snapshot);
}
if (options.afterUpdate) options.afterUpdate(component);
}
@ -274,8 +291,8 @@ export function unmountComponent(component) {
component._disable = true;
if (component.componentWillUnmount) component.componentWillUnmount();
//if (component.componentWillUnmount) component.componentWillUnmount();
if (component.store.uninstall) component.store.uninstall();
component.base = null;
// recursively tear down & recollect high-order component children:

View File

@ -34,7 +34,8 @@ export function flushMounts() {
addStyleToHead(c.props.css, '_ds'+c.elementId)
}
if (options.afterMount) options.afterMount(c);
if (c.componentDidMount) c.componentDidMount();
//if (c.componentDidMount) c.componentDidMount();
if (c.store.installed) c.store.installed()
}
}