Skip to content

Conversation

@waitingsong
Copy link
Contributor

No description provided.

@SangKa SangKa added the polish label Feb 8, 2018
Copy link
Member

@SangKa SangKa left a comment

Choose a reason for hiding this comment

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

感谢 PR ,帮忙修复了不少问题

```
为什么展示这个跟 Subjects 没半点关系的示例?好吧,一点是它展示了为什么不总是需要使用 Subject,另外一点这有个隐藏的 subject... (某种程度上可以说是 subject )。这里要注意的一点是,Observable 是通过 addEventListener 来包装按钮的处理函数的注册,而 addEventListener 本身就是一个 subject 。…至少根据 [“Gang Of Four” 的观察者模式](https://en.wikipedia.org/wiki/Design_Patterns)来说是这样的。
为什么展示这个跟 Subjects 没半点关系的示例?好吧,一点是它展示了为什么不总是需要使用 Subject,另外一点这有个隐藏的 subject... (某种程度上可以说是 subject )。这里要注意的一点是,这个 Observable 包装了对于按钮的 addEventListener 事件处理函数的注册操作,而 addEventListener 本身就是一个 subject 。…至少根据 [“Gang Of Four” 的观察者模式](https://en.wikipedia.org/wiki/Design_Patterns)来说是这样的。
Copy link
Member

Choose a reason for hiding this comment

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

这句翻的确实有问题 你看这样如何:
这里使用 addEventListener 来注册按钮的点击处理函数,并使用 Observable 对注册过程进行了包装,

Copy link
Contributor Author

@waitingsong waitingsong Feb 8, 2018

Choose a reason for hiding this comment

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

原文不合适代码与合适代码的差别在于 obervable 和 addEventListener 谁包裹谁,所以

is that the Observable is wrapping the registering of the handler on the button via addEventListener

这儿是以wrapping为动词来强调包裹。我如此修改是为了符合原文意思,并且与上文

理想的是在 Observable 中包装事件注册,既可以监听事件,又可以取消事件监听。

这句话做呼应——相同的主语、谓语、宾语。如此加强原文想要表达的含义。

Copy link
Member

Choose a reason for hiding this comment

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

恩 对于语句 你理解的完全正确 我之前翻译时没理解到位
现在定一下译文吧

模式本身很简单。Observers 是具有通知方法的类,Subject 也是类,它具有向内部观察者列表添加或删除观察者的方法和通知观察者列表的方法。
RxJS 中的 Subjects 并没有太差区别。当使用 observer 对 Rx Subject 调用 `subscribe` 时,Subject 会将该 observer 添加到内部的观察者列表中。同样的,如果使用一到三个函数来调用 `subscribe`,Subject 会将它们包装成一个 observer,然后添加到观察者列表中。当调用 Subject 的 `next(value)` 时,它会遍历观察者列表并将 `value` 传递给 `next` 方法。对于 `error``complete` 也是同样的。要想从 subject 的观察者列表中移除 observer,只需简单调用 subscription 的 `unsubscribe` 方法即可,subscription 是将 observer 添加到观察者列表中时返回的
RxJS 中的 Subjects 并没有大差别。当对一个 Rx Subject 使用一个 observer 调用 `subscribe` 时,Subject 会将该 observer 添加到内部的观察者列表中。同样的,如果使用一到三个函数来调用 `subscribe`,Subject 会将它们包装成一个 observer,然后添加到观察者列表中。当调用 Subject 的 `next(value)` 时,它会遍历观察者列表并将 `value` 一并传递给观察者们的 `next` 方法。对于 `error``complete` 也是同样的。要想从 subject 的观察者列表中移除 observer,只需简单调用 subscription 的 `unsubscribe` 方法即可,subscription 是在将 observer 添加到观察者列表中时被返回的对象
Copy link
Member

Choose a reason for hiding this comment

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

太大差别 更贴近原文

Copy link
Member

Choose a reason for hiding this comment

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

当使用 observer 来调用 Rx Subject 的 subscribe 方法时, 更通顺些

Copy link
Member

Choose a reason for hiding this comment

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

subscription 是在将 observer 添加到观察者列表中时被返回的对象 感觉不用强调是对象

Copy link
Contributor Author

Choose a reason for hiding this comment

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

兄弟,原文是“太差区别”啊。。。

Copy link
Contributor Author

@waitingsong waitingsong Feb 8, 2018

Choose a reason for hiding this comment

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

subscription 是在将 observer 添加到观察者列表中时被返回的对象 原文没提到对象,我是特地加上去的。一是译文原文“……时返回的” 后面少个宾语,作为一段话的结尾我感觉读下来一口气下不来,有点虎头蛇尾的味道。而加上“对象”这个宾语后相比较添加“东西”或者其他单词是想表示这个返回的(对象)是具有(unsubscribe)方法可以调用的。

Copy link
Member

Choose a reason for hiding this comment

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

ok 那加上吧 同意你的观点

大概 Subject 和 Observable 之间一个很重要的区别就是 Subject 是有状态的,它维护观察者列表。另一方面,[Observable 真的只是一个函数](https://medium.com/@benlesh/learning-observable-by-building-observable-d5da57405d87),它建立了观察本身。
虽然 Subjects 是 Observables,但 Subjects 还实现了 Observer 接口。也就是说,它们拥有 `next``error``complete` 方法。这些方法用来通知 subject 内部观察者列表中的 observers 。这意味着 subject 可以用作订阅任何 observable 的 observer 。
虽然 Subjects 是 Observables,但 Subjects 还实现了 Observer 接口。也就是说,它们拥有 `next``error``complete` 方法。这些方法用来通知 subject 内部观察者列表中的 observers 。这意味着 subject 可以作为 observer 来订阅任何 observable
Copy link
Member

Choose a reason for hiding this comment

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

通顺很多 👍

## 这种方式可靠吗?

RxJS 5 是相当新的库,它的文档扔在进行中,所以这种方式还没有在文档中出现,只是在内部使用。公开的 [TypeScript 签名](https://github.com/ReactiveX/rxjs/blob/5.4.3/src/operator/multicast.ts#L7-L10)指出了并非永远返回的是 `ConnectableObservable`,也有相对应的[单元测试](https://github.com/ReactiveX/rxjs/blob/5.4.3/spec/operators/multicast-spec.ts#L86-L144)
RxJS 5 是相当新的库,它的文档仍在进行中,所以这种方式还没有在文档中出现,只是在内部使用。公开的 [TypeScript 签名](https://github.com/ReactiveX/rxjs/blob/5.4.3/src/operator/multicast.ts#L7-L10)指出了并非永远返回的是 `ConnectableObservable`,也有相对应的[单元测试](https://github.com/ReactiveX/rxjs/blob/5.4.3/spec/operators/multicast-spec.ts#L86-L144)
Copy link
Member

Choose a reason for hiding this comment

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

nice track 👏

```

调用 `connect` 时,传入 `multicast` 操作符的 subject 会订阅源 observable,而 subject 的观察者会收到多播通知,这正符合 RxJS 多播的基本心智模型。
调用 `connect` 时,被传入 `multicast` 操作符的 subject 会订阅源 observable,而 subject 的观察者会收到多播通知,这正符合 RxJS 多播的基本心智模型。
Copy link
Member

Choose a reason for hiding this comment

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

这里不需要强调

* `b` 是在 `connect` 调用后订阅的,此时 subject 已经收到了源 observable 的第一个 `next` 通知,所以 `b` 能收到重放的 `next` 通知、源 observable 的第二个 `next` 通知和 `complete` 通知。
* `c` 是在源 observable 完成后订阅的,所以它能收到重放的 `next` 通知和 `complete` 通知。
* `b` 是在 `connect` 调用后订阅的,此时 subject 已经收到了源 observable 的第一个 `next` 通知,所以 `b` 能收到重放的第一个 `next` 通知、源 observable 的第二个 `next` 通知和 `complete` 通知。
* `c` 是在源 observable 完成后订阅的,所以它能收到重放的一个 `next` 通知和一个 `complete` 通知(译注:调用 publishReplay(1) 参数 1 决定只重放一个 next )
Copy link
Member

Choose a reason for hiding this comment

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

一个 不用译或强调
译注其实也不需要,这里并非是 publishReplay 的入门文章,面向的人群是想深入了解多播操作符的

* `c` 是在源 observable 完成后订阅的,所以它能收到重放的一个 `next` 通知和一个 `complete` 通知(译注:调用 publishReplay(1) 参数 1 决定只重放一个 next )

来看看 `c` 的行为,很明显,不同于 `publish` 操作符,`publishReplay` 操作符适合使用 `refCount` 方法,因为观察者在源 observable 完成后订阅依然能收到任意数量的重放的 `next` 通知。
来看看 `c` 的行为,很明显,不同于 `publish` 操作符,`publishReplay` 操作符适合使用 `refCount` 方法,因为即使在源 observable 完成后对其订阅,观察者依然能收到任意数量重放的 `next` 通知。
Copy link
Member

Choose a reason for hiding this comment

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

这里感觉还是我翻译的更通顺些 😊

observer c: complete
```

(译注)也可能是如下输出:
Copy link
Member

Choose a reason for hiding this comment

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

这个译注的意图是什么? 没看懂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

区别是 complete 事件输出的位置与原文是不相同的

Copy link
Member

Choose a reason for hiding this comment

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

不是的 原文的输出是正确的 这个我翻译的时候试过

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我知道原文的输出是正确的,但不确定输出顺序是唯一的。因为我这儿测试输出顺序就是添加译注的那个顺序。

Copy link
Member

Choose a reason for hiding this comment

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

如果和原文中的代码一致的话 输出顺序应该是唯一的

Copy link
Member

Choose a reason for hiding this comment

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

我知道原因了 版本问题 demo 里用的还是5.0 beta

Copy link
Contributor Author

Choose a reason for hiding this comment

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

难怪之前其他一些demo的顺序也有不同的。想更换到其他版本来测试,不过当时没找到合适的cdn链接

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

用你这个版本,输出如下

"observer a: 90"
"observer b: 90"
"observer a: complete"
"observer b: complete"
"observer c: 90"
"observer c: complete"

这个算个坑吧。。。

Copy link
Contributor Author

@waitingsong waitingsong Feb 11, 2018

Choose a reason for hiding this comment

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

呃,在jsbin界面 add library菜单中可以自动添加rxjs 5.0.3 版本。这个版本输出和原文也是一致的

* `c` 是在源 observable 完成后订阅的,它能收到带有第二个随机数的 `next` 通知和 `complete` 通知。

`publishReplay` 类似,`publishLast` 操作符适合使用 `refCount` 方法,因为观察者在源 observable 完成后订阅依然能收到任意数量的重放的 `next` 通知。
`publishReplay` 类似,`publishLast` 操作符适合使用 `refCount` 方法,因为即使在源 observable 完成后对其订阅,观察者依然能收到**最后一个** `next` 通知。
Copy link
Member

Choose a reason for hiding this comment

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

这块翻译的有问题
还是保留之前的翻译形式,但改为 最后一个
因为观察者在源 observable 完成后订阅依然能收到最后一个重放的 next 通知。


* 如果源 observable 报错,由 `publish` 返回的 observable 的任何将来的订阅者都将收到 `error` 通知。
* 但是,由 `share` 返回的 observable 的任何将来的订阅者会生成源 observable 的一个新订阅,因为错误会自动取消任何订阅者的订阅,将其引用计数归零。
* 然而,由 `share` 返回的 observable 的任何将来的订阅者会生成源 observable 的一个新订阅,因为错误会自动取消任何订阅者的订阅,将其引用计数归零。
Copy link
Member

Choose a reason for hiding this comment

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

这里有什么特殊的原因一定要将 howerer 译成 然而

Copy link
Contributor Author

@waitingsong waitingsong Feb 8, 2018

Choose a reason for hiding this comment

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

however 作为连接副词起语气转折作用,有但是以及然而的含义。我认为原文这儿并不是要强调但是这么重的语气也没有要否定前一句话的意思,而是以然而作为一个插入语转折下而已。

Copy link
Member

Choose a reason for hiding this comment

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

同意你的看法 之前没想这么细致

@SangKa
Copy link
Member

SangKa commented Feb 11, 2018

@waitingsong 统一整理下再提交下 然后我好 mege 谢了

@waitingsong
Copy link
Contributor Author

更新了。你看看

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants