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 {

  /// 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) -> 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 = (Event) -> Void

信号

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

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

public struct Signal: SignalProtocol {

  private let producer: (Observer) -> Void

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

  public func observe(with observer: @escaping Observer) {
    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]---|--->

你可能感兴趣的:(ReactiveKit 文档指南)