Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

docs(i18n-zh): translate combination operators #310

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/i18n/zh/combination/combineAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { OperatorDoc } from '../../../operator-docs';

export const combineAll: OperatorDoc = {
name: 'combineAll',
operatorType: 'combination',
signature: 'public combineAll(project: function): Observable',
parameters: [
{
name: 'project',
type: 'function',
attribute: '可选的',
description: `将每个内部 Observable 的最新值映射成一个新结果的可选函数。
按顺序将每个收集到的内部 Observable 中接收最新值作为参数。`
}
],
marbleUrl: 'http://reactivex.io/rxjs/img/combineAll.png',
shortDescription: {
description: `当高阶 Observable 完成时,通过使用 <a href="/#/operators/combineLatest" class="markdown-code">combineLatest</a> 将其打平。`,
extras: []
},
walkthrough: {
description: `
<p>
接受一个返回 Observables 的 Observable, 并从中收集所有的 Observables 。
一旦最外部的 Observable 完成, 会订阅所有收集带的 Observables,
然后通过 <a href="/#/operators/combineLatest" class="markdown-code">combineLatest</a> 的策略来合并值,
规则如下:
</p>
<ul>
<li>每次内部 Observable 发出值时, 外部 Observable 也发出值。</li>
<li>当返回的 observable 发出值时, 它会通过如下方式发出所有最新的值:
<ul>
<li>
如果提供了 <span class="markdown-code">project</span> 函数, 该函数会按内部 Observable 到达的顺序
依次使用每个内部 Observable 的最新值进行调用, <span class="markdown-code">project</span> 函数的
结果将由输出 Observable 发出。
</li>
<li>
如果没有提供 <span class="markdown-code">project</span> 函数, 输出 Observable 会发出包含所有最新数据的数组。
</li>
</ul>
</li>
</ul>
`
},
examples: [
{
name:
'将两个点击事件映射为有限的 interval Observable,然后应用 <span class="markdown-code">combineAll</span>',
code: `
import { map, combineAll, take } from 'rxjs/operators';
import { fromEvent } from 'rxjs/observable/fromEvent';

const clicks = fromEvent(document, 'click');
const higherOrder = clicks.pipe(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add the expected output as comment and a $ as variable postfix for observables. Could you change this also for the following examples...

map(ev =>
interval(Math.random()*2000).pipe(take(3))
),
take(2)
);
const result = higherOrder.pipe(
combineAll()
);
result.subscribe(x => console.log(x));
`,
externalLink: {
platform: 'JSBin',
url: 'http://jsbin.com/peparawuvo/1/embed?js,console,output'
}
}
],
relatedOperators: ['combineLatest', 'mergeAll'],
additionalResources: []
};
78 changes: 78 additions & 0 deletions src/i18n/zh/combination/combineLatest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { OperatorDoc } from '../../../operator-docs';

export const combineLatest: OperatorDoc = {
name: 'combineLatest',
operatorType: 'combination',
signature:
'public combineLatest(observables: ...Observable, project: function): Observable',
useInteractiveMarbles: true,
parameters: [
{
name: 'other',
type: 'Observable',
attribute: '',
description: '将要和源 Observable 结合的输入 Observable。可以传入多个。'
},
{
name: 'project',
type: 'function',
attribute: '可选的',
description: '可选的投射函数,将输出 Observable 返回的值投射为要发出的新的值。'
}
],
marbleUrl: 'http://reactivex.io/rxjs/img/combineLatest.png',
shortDescription: {
description: `
组合多个 Observables 来创建一个 Observable ,该 Observable 的值
根据每个输入 Observable 的最新值计算得出的。
`,
extras: [
{
type: 'Tip',
text: `
注意: 只有当所有源 Observables 都至少发出一次值后,combineLatest 才会开始发出值。
通过使用 <a href="#/operators/startWith" class="markdown-code">startWith</a>
来为源 Observable 添加一个默认值,combineLatest 则会立即生效。
`
}
]
},
walkthrough: {
description: `
<p>
<span class="markdown-code">combineLatest</span> 组合传入的多个 Observables 。
通过顺序的订阅每个输入 Observable, 在每次任意输入 Observables 发出值时,收集每个
输入 Observables 的最新值并组成一个数组, 然后要么将这个数组传给可选的投射函数并发
出投射函数返回的结果, 或者在没有提供投射函数时仅仅发出该数组。
</p>
`
},
examples: [
{
name: '根据身高的 Observable 和体重的 Observable 来动态地计算 BMI 指数',
code: `
import { combineLatest } from 'rxjs/operators;
import { of } from 'rxjs/observable/of';

const weight = of(70, 72, 76, 79, 75);
const height = of(1.76, 1.77, 1.78);
const bmi = weight.pipe(
combineLatest(height, (w, h) => w / (h * h))
);
/*
输出:
BMI is 24.212293388429753
BMI is 23.93948099205209
BMI is 23.671253629592222
*/
bmi.subscribe(x => console.log('BMI is ' + x));
`,
externalLink: {
platform: 'JSBin',
url: 'http://jsbin.com/pivowunedu/1/embed?js,console'
}
}
],
relatedOperators: ['combineAll', 'merge', 'withLatestFrom'],
additionalResources: []
};
109 changes: 109 additions & 0 deletions src/i18n/zh/combination/concat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { OperatorDoc } from '../../../operator-docs';

export const concat: OperatorDoc = {
name: 'concat',
operatorType: 'combination',
signature:
'public static concat(input1: ObservableInput, input2: ObservableInput, scheduler: Scheduler): Observable',
parameters: [
{
name: 'input1',
type: 'ObservableInput',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it just an observable?

attribute: '',
description: '可以与其他 Observable 连接的输入 Observable 。'
},
{
name: 'input2',
type: 'ObservableInput',
attribute: '',
description: '可以与其他 Observable 连接的输入 Observable 。可以接收多个输入 Observable。'
},
{
name: 'scheduler',
type: 'Scheduler',
attribute: '可选的,默认值: null',
description: '可选的调度器,控制每个输入 Observable 的订阅。'
}
],
marbleUrl: 'http://reactivex.io/rxjs/img/concat.png',
shortDescription: {
description: '创建一个输出 Observable,它按顺序发出每个给定的输入 Observable 中的所有值。',
extras: [
{
type: 'Tip',
text: '通过顺序地发出多个 Observables 的值来将它们一个接一个的连接起来。'
}
]
},
walkthrough: {
description: `
<p><span class='markdown-code'>concat</span> 通过一次订阅一个将多个 Observables 连接起来,并将值合并到输出 Observable 中。
你可以传递一个输入 Observable 数组,或者直接把它们当做参数传递。 传递一个空数组会 导致输出 Observable 立马触发完成状态。</p>

<p><span class='markdown-code'>concat</span> 会订阅第一个输入 Observable 并且发出它的所有值, 不去做任何干预。当这个
输入 Observable 完成时, 订阅第二个输入 Observable,同样的发出它的所有值。这个过程会不断重复直到输入 Observable 都用过了。
当最后一个输入 Observable 完成时,<span class='markdown-code'>concat</span> 也会完成。 任何时刻都只会有一个输入 Observable 发出值。
如果你想让所有的输入 Observable 并行发出数据,请查看 <a href='/#/operators/merge' class='markdown-code'>merge</a>,
尤其是使用可选的 <span class='markdown-code'>concurrent</span> 参数。 事实上, <span class='markdown-code'>concat</span> 和
<span class='markdown-code'>concurrent</span 设置为 1 的 <a href='/#/operators/merge' class='markdown-code'>merge</a> 效果是一样的。</p>

<p>注意,如果输入 Observable 一直都不完成, <span class='markdown-code'>concat</span> 也会一直不能完成并且下一个输入 Observable 将
永远不能被订阅. 另一方面, 如果某个输入 Observable 在它被订阅后立马处于完成状态, 那么它对 <span class='markdown-code'>concat</span> 是
不可见的, 仅仅会转向下一个输入 Observable 。</p>

<p>如果输入 Observable 链中的任一成员发生错误, <span class='markdown-code'>concat</span> 会立马触发错误状态,而不去控制下一个
输入 Observable. 发生错误的输入 Observable 之后的输入 Observable 不会被订阅.</p>

<p>如果你将同一输入 Observable 传递给 <span class='markdown-code'>concat</span> 多次,结果流会在每次订阅的时候“重复播放”,
这意味着 你可以重复 Observable 多次. 如果你乏味的给 <span class='markdown-code'>concat</span> 传递同一输入 Observable 1000次,
你可以试着用用 <span class='markdown-code'>repeat</span> 。</p>
`
},
examples: [
{
name: '将从 0 数到 3 的定时器和从 1到 10 的同步序列进行连接',
code: `
import { take } from 'rxjs/operators';
import { interval } from 'rxjs/observable/interval';
import { range } from 'rxjs/observable/range';
import { concat } from 'rxjs/observable/concat';

const timer = interval(1000).pipe(take(4));
const sequence = range(1, 10);
const result = concat(timer, sequence);
result.subscribe(x => console.log(x));

// 结果为:
// 0 -1000ms-> 1 -1000ms-> 2 -1000ms-> 3 -immediate-> 1 ... 10
`,
externalLink: {
platform: 'JSBin',
url: 'http://jsbin.com/doqoyimaxu/embed?js,console'
}
},
{
name: '连接有 3 个 Observables',
code: `
import { take, concat } from 'rxjs/operators';
import { interval } from 'rxjs/observable/interval';

const timer1 = interval(1000).pipe(take(10));
const timer2 = interval(2000).pipe(take(6));
const timer3 = interval(500).pipe(take(10));
const result = timer1.pipe(concat(timer2, timer3));
result.subscribe(x => console.log(x));

// 结果如下:
// (按顺序打印到控制台)
// -1000ms-> 0 -1000ms-> 1 -1000ms-> ... 9
// -2000ms-> 0 -2000ms-> 1 -2000ms-> ... 5
// -500ms-> 0 -500ms-> 1 -500ms-> ... 9
`,
externalLink: {
platform: 'JSBin',
url: 'http://jsbin.com/decaromone/1/embed?js,console'
}
}
],
relatedOperators: ['concatAll', 'concatMap', 'concatMapTo']
};
69 changes: 69 additions & 0 deletions src/i18n/zh/combination/concatAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { OperatorDoc } from '../../../operator-docs';

export const concatAll: OperatorDoc = {
name: 'concatAll',
operatorType: 'combination',
signature: 'public concatAll(): Observable',
parameters: [],
marbleUrl: 'http://reactivex.io/rxjs/img/concatAll.png',
shortDescription: {
description: '通过顺序地连接内部 Observable,将高阶 Observable 转化为一阶 Observable 。',
extras: [
{
type: 'Tip',
text: '通过一个接一个的连接内部 Observable ,将高阶 Observable 打平。'
}
]
},
walkthrough: {
description: `
串行连接源 (高阶 Observable) 所发出的每个 Observable,只有当一个内部 Observable 完成的时候才订阅下一个
内部 Observable,并将它们的所有值合并到返回的 Observable 中。
`,
extras: [
{
type: 'Warning',
text: `
如果源 Observable 很快并且不停地发送 Observables, 内部 Observables 完成速度比源 Observable 的发出速度慢,
你会遇到内存问题,因为传入的 Observables 在无限制的缓冲区中进行收集的。
`
},
{
type: 'Tip',
text: `concatAll 等价于 concurrency 参数(最大并发数)为 1 的 mergeAll 。`
}
]
},
examples: [
{
name: '每次点击都会触发从 0 到 3 的定时器 (时间间隔为 1 秒),定时器之间是串行的',
code: `
import { map, take, concatAll } from 'rxjs/operators';
import { fromEvent } from 'rxjs/observable/fromEvent';
import { interval } from 'rxjs/observable/interval';

const clicks = fromEvent(document, 'click');
const higherOrder = clicks.pipe(
map(ev => interval(1000).pipe(take(4)))
);
const firstOrder = higherOrder.pipe(concatAll());
firstOrder.subscribe(x => console.log(x));
`,
externalLink: {
platform: 'JSBin',
url: 'http://jsbin.com/guhefeyahi/embed?js,console,output'
}
}
],
relatedOperators: [
'combineAll',
'concat',
'concatMap',
'concatMapTo',
'exhaust',
'mergeAll',
'switch',
'zipAll'
],
additionalResources: []
};
Loading