苏 dismiss 是一个用于 iOS/macOS/tvOS/watchOS 和 Android 的单向数据流架构实现,受 Redux 强烈启发。它提供了一个易于使用的库,有助于创建一致、可预测和可扩展的应用程序。
苏 dismiss 专注于提供良好的开发体验和工具,如可定制日志(customizable logging)和状态变化监控(state changes monitoring)。
加入我们的 gitter 聊天频道 有任何问题。或检查 苏 dismiss 文档网站。
本页有哪些内容
有关如何使用苏 dismiss 的更详细文档,请访问 苏 dismiss 网站,苏 dismiss API 接口 或直接访问 使用苏 dismiss 开发的应用程序列表。
苏 dismiss 应用程序流程和组件
苏 dismiss 架构由五个核心元素组成
- Store:主要组件,包含一个 Reducer(或一组 Reducers),以及主要应用程序的 State。监听器(Listeners)订阅它以获取状态变化。引起状态变化的行为(Actions)将其分发到它上面。
- State:定义组件/屏幕或一组组件/屏幕的状态。
- Action:每个操作指定我们希望在状态上实施的变化。
- Reducer:包含基于特定接收到的动作来更改状态的逻辑。
- 监听器:Listener:当状态改变时被通知的回调。
以下动画描述了Suas的运行流程。
为什么选择Suas
Suas帮助您构建动态性高、一致的移动应用。
- 跨平台;Suas-iOS支持iOS、macOS、tvOS和watchOS。《Suas-Android》支持所有API级别,并提供友好的Kotlin接口。
- 专注于开发者体验,配备插件/工具如《LoggerMiddleware》和《Suas Monitor》。
- 代码库小,操作占用低。请查看源代码。
🙂 . - 静态类型和类型信息在Store、Reducer和Listener中得以保留。
- 快速开箱即用,开发者可定制以实现更快的过滤监听器。
安装
Suas在iOS设备上可以通过Carthage或CocoaPods进行安装。
使用Carthage安装
打开您的Cartfile
文件,并附加以下内容
github "zendesk/suas-ios"
然后使用carthage update --platform ...
构建
使用CocoaPod安装
将pod 'Suas'
添加到您的Podfile
文件。
use_frameworks!
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'Suas'
然后运行pod install
开始使用
让我们通过构建一个计数应用来开始。
在Suas中构建应用程序时,我们首先定义计数器的状态。在这个例子中,计数器状态是一个包含计数器值的结构体。
struct Counter {
var value: Int
}
接下来,我们定义影响状态的动作。对于计数器,我们需要增加和减少的动作。
struct IncrementAction: Action {
let incrementValue: Int
}
struct DecrementAction: Action {
let decrementValue: Int
}
现在我们有了State
和Actions
,我们需要指定动作如何影响状态。这种逻辑在reducer中实现。计数器状态reducer如下所示
struct CounterReducer: Reducer {
var initialState = Counter(value: 0)
func reduce(state: CounterReducer.StateType, action: Action) -> CounterReducer.StateType? {
// Handle each action
if let action = action as? IncrementAction {
var newState = state
newState.value += action.incrementValue
return newState
}
if let action = action as? DecrementAction {
var newState = state
newState.value -= action.decrementValue
return newState
}
// Important: If action does not affec the state, return ni
return nil
}
}
reducer定义了两件事
- 存储的初始状态,即初始的
Counter
值。 - reduce函数,该函数接收分发的
Action
和当前的State
。这个函数根据Action
确定要返回的什么State
。如果reducer没有更改状态,它应返回nil
Store
是我们应用程序中与之交互的主要组件。存储包含
- 应用程序的状态。
- reducer或reducers。
- (高级) 中间件
我们使用以下代码片段创建一个商店
let store = Suas.createStore(reducer: counterReducer)
现在我们可以向其分派动作并向其添加监听器。让我们看看我们的UI。
class CounterViewController: UIViewController {
@IBOutlet weak var counterValue: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Finally: Use the store
self.counterValue.text = "\(store.state.value(forKeyOfType: Counter.self)!.value)"
// Add a listener to the store
// Notice the [weak self] so we dont leak listeners
let subscription = store.addListener(forStateType: Counter.self) { [weak self] state in
self?.counterValue.text = "\(state.value)"
}
// When this object is deallocated, the listener will be removed
// Alternatively, you have to delete it by hand `subscription.removeListener()`
subscription.linkLifeCycleTo(object: self)
}
@IBAction func incrementTapped(_ sender: Any) {
// Dispatch actions to the store
store.dispatch(action: IncrementAction(incrementValue: 1))
}
@IBAction func decrementTapped(_ sender: Any) {
// Dispatch actions to the store
store.dispatch(action: DecrementAction(decrementValue: 1))
}
}
让我们分析上面的代码
- 我们通过调用
store.addListener(forStateType: Counter.self)
并向其传递状态类型来添加存储的监听器。请注意,在回调块中我们必须使用[weak self]
引用以防止强引用循环。 - 通过调用
subscription.linkLifeCycleTo(object: self)
,我们将监听器生命周期链接到视图控制器。当控制器被释放时,监听器被移除。 - 点击增加或减少按钮会使用
store.dispatch(action:)
分发动作,从而改变状态。
就是这样,请访问我们的文档网站查看Suas组件的完整参考,并查看使用Suas构建的示例列表。
开发者体验和工具
Suas专注于开发者体验和工具。它提供两种插件,以中间件的形式。
可定制日志记录
当LoggerMiddleware
记录所有接收到的动作和状态变化时。
了解更多关于如何使用LoggerMiddleware的信息。
状态转换监控
MonitorMiddleware
帮助跟踪状态转换和动作派发历史。当使用 MonitorMiddleware
时,派发的 动作
和状态的变化会被发送到我们的 Suas Monitor 桌面应用。
通过访问 MonitorMiddleware 入门文章 了解如何安装和开始使用 MonitorMiddleware
。底层,Suas Monitor
使用出色的 Redux DevTools 来提供状态和动作信息。
使用 Suas 构建的应用示例
访问 Suas 网站,查看使用 Suas 构建的 示例列表。
下一步是什么
获取更多关于 Suas 的信息
- 前往 Suas 网站 了解 Suas 的更多使用知识。
- 查看 Suas API 参考。
- 查看一些使用 Suas 构建的应用示例来了解如何使用 Suas。
- 加入 Suas gitter 频道 的讨论或与 Suas 团队联系。
贡献
我们热爱所有的贡献。从修改 Suas 的工作内部,修改 Suas 方法以及公共 API,到修改说明和 文档主题。
可以在 GitHub 仓库提出建议,或在 Suas gitter 频道 直接 suggestions。
有关参考,请查看我们的 贡献指南。
联系我们
加入我们的gitter频道,与其他Suas开发者交流。
有任何疑问、建议或者仅仅是打招呼,你可以在twitter上找到核心团队。
Suais未来
为了帮助打造Suas的未来版本,请加入我们的gitter频道。
版权和许可
Copyright 2017 Zendesk, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://apache.ac.cn/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.