ReactiveKit 文档指南

ReactiveKit 是一种响应式的轻量化swift框架,该框架能让你快速上手.
该框架兼容所有的Apple和Linux平台.如果你正在开发一款iOS或macOS的app,你应该使用 Bond 框架来做UIKit和AppKit的控件绑定,代理和数据源的绑定.
这篇文档将介绍该框架.最后你会对该框架如何运行以及运用它的最好的方式有一个很好的理解.

目录

  • 介绍
  • 信号
  • 隐藏异步使产生信号
  • 处置信号
  • 转换信号
  • 关于报错
  • 创造简单的信号
  • 处置in a bag
  • 穿线?
  • bindings
  • 分享事件序列
  • 项目
  • 连接信号
  • 处理信号报错
  • 跟踪信号状态
  • 属性
  • 传输信号
  • 其他常见模式
  • 其他条件\要求
  • 安装
  • 交流\信息\通信
  • 附加文件
  • 执照\许可

介绍

想想当用户输入他的姓名的时候TextField是如何变化的.每输入一个字母呈现给我们的便是一种新的状态.

---[J]---[Ji]---[Jim]--->

We can think of these state changes as a sequence of events. It is quite similar to an array or a lists, but with the difference that events are generated over time as opposed to having them all in memory at once.

The idea behind reactive programming is that everything can be represented as a sequence. Let us consider another example - a network request
我们可以将这些状态改变看作一个事件序列.这跟数组很像,但是不同的是,虽然产生了三个事件,但是在内存中只存在一个字符串.
这种思想可以把所有的东西都当做一个序列.让我们看看另一个例子-一个网络请求

---[Response]--->

一个网络请求的结果就是得到一个响应.尽管我们只得到一个响应,我们还是可以把它看做一个序列,虽然只有一个元素.
Arrays are finite so they have a property that we call size. It is a measure of how much memory the array occupies. When we talk about sequences over time, we do not know how many events will they generate during their lifetime. We do not know how many letters will the user enter. However, we would still like to know when the sequence is done generating the events.
数组长度是有限的,因此它有大小这一属性.这是检测该数组占据了多少内存的测量方法.当我们谈到随着时间的序列,我们并不知道其整个一生会产生多少的项目.我们不知道用户会输入多少字母.然而,我们还是想知道当序列完成产生的项目.
To get that information, we can introduce a special kind of event - a completion event. It is an event that marks the end of a sequence. No event shall follow the completion event.
为了得到这个信息,我们介绍一个特别的事件-完成式事件.这是一个能标记序列完成的事件.完成式事件后面不会再出现事件.
We will denote completion event visually with a vertical bar
我们将用一竖表示完成式事件.

---[J]---[Ji]---[Jim]---|--->

Completion event is important because it tells us that whatever was going on is now over. We can finalize the work at that point and dispose any resources that might have been used in processing the sequence.
完成式事件很重要因为它告诉我们之前进行的到现在就结束了.我们可以在那时候结束工作,然后处置任何在运行序列的过程中被用过的资源.
Unfortunately, the universe is not governed by the order, rather by the chaos. Unexpected things happen and we have to anticipate that. For example, a network request can fail so instead of a response, we can receive an error
不幸的是,宇宙不是被命令统治的,也不是混乱.意料之外的事情总是发生,我们应该有这种预料意识.举个例子,一个网络请求不产生回应,我们会收到一个报错.

---!Error!--->

In order to represent errors in our sequences, we will introduce yet another kind of event. We will call it a failure event. Failure event will be generated when something unexpected happens. Just like the completion event, failure event will also represent the end of a sequence. No event shall follow the failure event.
为了代表我们序列中的报错,我们将介绍另外一种事件.我们将它称作失败事件.当意外发生时失败事件便会产生.就像完成式事件一样,失败事件也代表着一个序列的结束.失败事件后面不会再出现其他事件.
Let us see how the event is defined in ReactiveKit
我们来看看事件是如何被定义为ReactiveKit

/// An event of a sequence.
public enum Event<Element, Error: Swift.Error> {

  /// An event that carries next element.
  case next(Element)

  /// An event that represents failure. Carries an error.
  case failed(Error)

  /// An event that marks the completion of a sequence.
  case completed
}

It is just an enumeration of the three kinds of events we have. Sequences will usually have zero or more .next events followed by either a .completed or a .failed event.
这是三种事件中的另外一个.序列通常有0或更多??.next事件通常后面跟.complete.failed事件.
What about sequences? In ReactiveKit they are called signals. Here is the protocol that defines them.
那序列呢?在ReactiveKit里他们被称作信号.下面是协议里如何定义他们的.

/// Represents a sequence of events.
public protocol SignalProtocol {

  /// The type of elements generated by the signal.
  associatedtype Element

  /// The type of error that can terminate the signal.
  associatedtype Error: Swift.Error

  /// Register the given observer.
  /// - Parameter observer: A function that will receive events.
  /// - Returns: A disposable that can be used to cancel the observation.
  public func observe(with observer: @escaping Observer<Element, Error>) -> Disposable
}

A signal represents the sequence of events. The most important thing you can do on the sequence is observe the events it generates. Events are received by the observer. An observer is nothing more than a function that accepts an event.
一个信号代表着一个事件序列.对这个序列你能做的最重要的事情就是观察它产生的事件.观察者接收到事件.一个观察者仅仅是接收一个事件的功能.

/// Represents a type that receives events.
public typealias Observer<Element, Error: Swift.Error> = (Event<Element, Error>) -> Void

信号

We have seen the protocol that defines signals, but what about the implementation? Let us implement a basic signal type!

我们已经了解协议中是如何定义信号的,那么如何来实施呢?我们先来实施一个基本的信号类型.

public struct Signal<Element, Error: Swift.Error>: SignalProtocol {

  private let producer: (Observer<Element, Error>) -> Void

  public init(producer: @escaping (Observer<Element, Error>) -> Void) {
    self.producer = producer
  }

  public func observe(with observer: @escaping Observer<Element, Error>) {
    producer(observer)
  }
}

We have defined our signal as a struct of one property - a producer. As you can see, producer is just a function that takes the observer as an argument. When we start observing the signal, what we do is basically execute the producer with the given observer. That is how simple signals are!
我们已经将我们的信号定义为??一种性质-一个生产者.正如你所看到的,生产者只是一个将观察者作为一个论据的功能.当我们开始观察信号,我们所做的基本就是用被给的观察者让生产者执行.那就是简单的信号!

Signal in ReactiveKit is implemented almost like what we have shown here. It has few additions that give us some guarantees that we will talk about later

Let us create an instance of the signal that sends first three positive integers to the observer and then completes.
让我们来创建一个例子:信号发出前三个积极地整数给观察者,然后完成.
Visually that would look like:
视觉上看是这样的:

---[1]---[2]---[3]---|--->
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,839评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,543评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,116评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,371评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,384评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,111评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,416评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,053评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,558评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,007评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,117评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,756评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,324评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,315评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,539评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,578评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,877评论 2 345

推荐阅读更多精彩内容