-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as React from 'react'; | ||
import { withState } from '../../src/lib/withState'; | ||
import { ChainableComponent } from '../../src/ChainableComponent'; | ||
import Step from '../Step'; | ||
import { DoBuilder } from '../../src'; | ||
import { withHandler } from '../../src/lib/withHandlers'; | ||
|
||
export const WithHandlersDemo = | ||
DoBuilder | ||
.bind('b', withState(0)) | ||
.bind('a', withState(0)) | ||
.bindL('handler', ({a, b}) => { | ||
console.log('handler', a.value, b.value) | ||
return withHandler(() => alert(`A's count is : ${a.value}`), [b.value]) | ||
}) | ||
.done() | ||
.render(({a, b, handler}) => ( | ||
<div> | ||
<div>a: {a.value} <button onClick={() => a.update(a.value + 1)}>+</button></div> | ||
<div>b: {b.value} <button onClick={() => b.update(b.value + 1)}>+</button></div> | ||
<button onClick={handler}>alert</button> | ||
</div> | ||
)) | ||
|
||
export default () => ( | ||
<Step title="WithHandlersDemo Demo"> | ||
<pre className='code-sample'> | ||
{``} | ||
</pre> | ||
{WithHandlersDemo} | ||
</Step> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
type Dependency<A> = { | ||
_type: '@@ChainableComponentsDependency', | ||
value: A, | ||
equals: (l: A, r: A) => boolean | ||
} | ||
|
||
function isDep<T>(a: any): a is Dependency<T> { | ||
return a.type === '@@ChainableComponentsDependency' | ||
} | ||
|
||
type Dependencies = (any | Dependency<any>)[] | ||
|
||
export function equals(l: Dependencies, r: Dependencies): boolean { | ||
return l.every((dep, i) => { | ||
const rightDep = r[i] | ||
if(isDep(dep)) { | ||
if(isDep(rightDep)) { | ||
return dep.equals(dep.value, rightDep.value) | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
return dep === rightDep; | ||
} | ||
}) | ||
} | ||
|
||
export function itemEquals(l: any, r: any): boolean { | ||
if(l && isDep(l) ) { | ||
} | ||
return l.equals(l.value, r.value) | ||
} | ||
|
||
export function depEquals<A>(l: Dependency<A>, r: Dependency<A>): boolean { | ||
return l.equals(l.value, r.value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<State> | ||
{state => ( | ||
<WithHandler handler={() => foo(state)} deps={[state]}> | ||
{handler => ( | ||
<div onClick={handler}></div> | ||
)} | ||
</WithHandler> | ||
)} | ||
</State> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ChainableComponent, fromRenderProp, RenderPropsProps } from '../ChainableComponent'; | ||
import { withLifecycle } from './withLifecycle'; | ||
import { equals } from '../Dependencies'; | ||
|
||
export function withHandler<F extends Function>(cb: F, dependencies: any[]): ChainableComponent<F> { | ||
return withLifecycle<[F, any[]]>({ | ||
init: () => ([cb, dependencies]), | ||
componentWillUpdate: ([f,deps]) => { | ||
if(equals(deps, dependencies)) { | ||
console.log('still equal, ', deps, dependencies) | ||
return [f, deps]; | ||
} else { | ||
console.log('not equal! ', deps, dependencies) | ||
return [cb, dependencies]; | ||
} | ||
} | ||
}).map(a => a[0]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters