Update
This commit is contained in:
parent
12e97f9303
commit
5483781d4f
24
flowtype.md
24
flowtype.md
|
@ -119,6 +119,7 @@ function filter<T> (list: Array<T>, callback: (item: T) => boolean): Array<T> {
|
|||
|
||||
```js
|
||||
import type { Person } from '../person'
|
||||
import typeof Config from '../config'
|
||||
```
|
||||
|
||||
```js
|
||||
|
@ -142,3 +143,26 @@ var add: ((num1: number, num2: number) => number) = function(num1, num2) {
|
|||
return num1 + num2;
|
||||
};
|
||||
```
|
||||
|
||||
## Comment syntax
|
||||
|
||||
```js
|
||||
/*::
|
||||
export type Foo = { ... }
|
||||
*/
|
||||
|
||||
function add(n /*: number */) { ... }
|
||||
```
|
||||
|
||||
## React
|
||||
|
||||
```js
|
||||
React$Element<any>
|
||||
```
|
||||
|
||||
```js
|
||||
class Foo extends React.Component {
|
||||
/*:: state: { open: boolean } */
|
||||
/*:: props: { open: boolean } */
|
||||
}
|
||||
```
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
---
|
||||
title: Stream
|
||||
category: Node.js
|
||||
---
|
||||
|
||||
```js
|
||||
const Readable = require('stream').Readable
|
||||
const Writable = require('stream').Writable
|
||||
const Transform = require('stream').Transform
|
||||
```
|
||||
|
||||
### Piping
|
||||
|
||||
```js
|
||||
clock() // Readable stream
|
||||
.pipe(xformer()) // Transform stream
|
||||
.pipe(renderer()) // Writable stream
|
||||
```
|
||||
|
||||
### Readable streams
|
||||
|
||||
Readable streams are generators of data. Write data using `stream.push()`.
|
||||
|
||||
```js
|
||||
function clock () {
|
||||
const stream = new Readable({
|
||||
objectMode: true,
|
||||
read: () => {} // implement this if you need on-demand reading
|
||||
})
|
||||
|
||||
setInterval(() => {
|
||||
stream.push({ time: new Date() })
|
||||
|
||||
if (error) return stream.emit('error', error)
|
||||
if (eof) return stream.push(null)
|
||||
}, 1000)
|
||||
|
||||
return stream
|
||||
}
|
||||
```
|
||||
|
||||
### Transform streams
|
||||
|
||||
Pass the updated chunk to `done(null, chunk)`.
|
||||
|
||||
```js
|
||||
function xformer () {
|
||||
let count = 0
|
||||
|
||||
return new Transform({
|
||||
objectMode: true,
|
||||
transform: (data, _, done) => {
|
||||
done(null, { time: data.time, index: count++ })
|
||||
}
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### Writable streams
|
||||
|
||||
```js
|
||||
function renderer () {
|
||||
return new Writable({
|
||||
objectMode: true,
|
||||
write: (data, _, done) => {
|
||||
console.log('<-', data)
|
||||
done()
|
||||
}
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### Reading
|
||||
|
||||
```js
|
||||
const st = source()
|
||||
st.on('data', (data) => { console.log('<-', data) })
|
||||
st.on('close', () => { console.log('** bye') })
|
||||
```
|
||||
|
||||
## Also see
|
||||
|
||||
- <https://github.com/substack/stream-handbook>
|
|
@ -348,9 +348,18 @@ Other API
|
|||
|
||||
### Joining
|
||||
|
||||
Student.joins(:schools).where(:schools => { :type => 'public' })
|
||||
Student.joins(:schools).where(schools: { type: 'public' })
|
||||
Student.joins(:schools).where('schools.type' => 'public' )
|
||||
|
||||
# multiple associations
|
||||
Article.joins(:category, :comments)
|
||||
|
||||
# nested assocations
|
||||
Article.joins(comments: :guest)
|
||||
|
||||
# sql
|
||||
Author.joins("INNER JOIN posts ON posts.author_id = authors.id AND posts.published = 't'")
|
||||
|
||||
### Where interpolation
|
||||
|
||||
where("name = ?", "John")
|
||||
|
|
Loading…
Reference in New Issue