Skip to content

Commit 29f15dd

Browse files
committed
add readme for type n search
1 parent 4355108 commit 29f15dd

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ finstead of imperative describe what you want to do with data at certain step, w
5050
sinks are composable and reusable, not like reducer in redux, where switch statement are hard to break and compose.
5151

5252
### Transducers support
53-
[transducer](https://github.com/cognitect-labs/transducers-js) is another high perfomance way to compose data flow other then monadic.
53+
[transducer](https://github.com/cognitect-labs/transducers-js) is another high perfomance functional way to compose data flow other than monadic.
5454

5555
writing actions in transducers improve reusablity.
5656

examples/type-n-search/README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Github Repo Type N Search Example
2+
3+
## install
4+
```sh
5+
npm install
6+
npm run build
7+
npm start
8+
```
9+
10+
## Code Walk Through
11+
12+
it's really simple example(only 40 LOC) that reactively search github repo according to your query
13+
14+
0. create normal React Component
15+
```js
16+
const TypeNsearch = (props)=>{
17+
let {search} = props.actions
18+
return <div>
19+
<input onChange={e=>search(e.target.value)}></input>
20+
<ul>
21+
{props.results&&props.results.map(item=>{
22+
return <li key={item.id}><a href={item.html_url}>{item.full_name} ({item.stargazers_count})</a></li>
23+
})}
24+
</ul>
25+
</div>
26+
}
27+
```
28+
1. HOC(Higher Order Component)
29+
using `connect` to create a HOC over TypeNsearch
30+
```js
31+
const MostTypeNSearch = connect(DATAFLOW)(TypeNsearch)
32+
```
33+
2. Compose Dataflow
34+
you see the place holder `DATAFLOW`, now we gonna fill in the real data flow how we enable the reactive action of our Component
35+
1. filter out stream only with `intent.type` of 'search'
36+
```js
37+
function(intent$){
38+
let updateSink$ = intent$.filter(i=>i.type=='search')
39+
.debounce(500)
40+
...
41+
```
42+
using `debounce` will transform the stream to stream which only bouncing at certain time point
43+
```
44+
---冷笑--冷笑话-->
45+
46+
--------冷笑话-->
47+
```
48+
2. compose a VALID query URL
49+
```js
50+
...
51+
.map(intent=>intent.value)
52+
.filter(query=>query.length > 0)
53+
.map(query=>GITHUB_SEARCH_API + query)
54+
...
55+
```
56+
3. flatMap the Response to our stream
57+
```js
58+
.flatMap(url=>most.fromPromise(
59+
rest(url).then(resp=>({
60+
type: 'dataUpdate',
61+
value: resp.entity
62+
}))))
63+
```
64+
65+
`flatMap` is simply just `map` and then `flat`
66+
67+
> just pretent one `-` as one sec
68+
69+
```
70+
intentStream --urlA---urlB--->
71+
rest(urlA) -------respA---->
72+
rest(urlB) ---------respB-->
73+
flatMap(rest)-------respA--respB--->
74+
```
75+
4. model
76+
now our intent stream become a data stream, let's make it a modle stream.
77+
```js
78+
.filter(i=>i.type=='dataUpdate')
79+
.map(data=>JSON.parse(data.value).items)
80+
.map(items=>items.slice(0,10))
81+
```
82+
parse it to JS Object and only get the first ten results
83+
5. create state transforming stream
84+
```
85+
.map(items=>state=>({results: items}))
86+
```
87+
88+
```
89+
modleStream ---mA---mB--->
90+
stateStream ---state=>({results:mA})---state=>({results:mB})--->
91+
```
92+
93+
3. return `actions` and `sinks`
94+
```js
95+
return {
96+
search: value=>({type:'search',value}),
97+
updateSink$,
98+
}
99+
```
100+
return `search` then you can use `props.actions.search` in your Component
101+
102+
return `updateSink$` then it can be appled to HOC's state, HOC will pass the state to your Component as props

0 commit comments

Comments
 (0)