This commit is contained in:
Rico Sta. Cruz 2017-07-30 17:59:47 +08:00
parent 12e97f9303
commit 5483781d4f
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
4 changed files with 118 additions and 1 deletions

View File

@ -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 } */
}
```

83
nodejs-stream.md Normal file
View File

@ -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>

View File

@ -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")

View File

@ -148,6 +148,7 @@ book = instance_double('Book', pages: 250)
```rb
allow(die).to receive(:roll)
allow(die).to receive(:roll) { 3 }
allow_any_instance_of(Die).to receive(:roll)
expect(die).to receive(:roll)
.with(1)