1111import Foundation
1212
1313/**
14- 一个简单的基础组件,主要目的是为 target/selector 模式和 block 调用提供统一的界面
15-
1614 A simple base component whose main purpose is to provide a unified interface for target/selector patterns and block calls.
1715 */
1816public final class Action {
19- /// Action 触发时接收 selector 消息的对象
20- ///
2117 /// The object that receives the selector message when the action is triggered.
2218 public weak var target : AnyObject ?
2319
24- /// Action 触发时向 target 发送的消息
25- ///
2620 /// The message sent to the target when the action is triggered.
2721 public var selector : Selector ?
2822
29- /// Action 触发时执行的闭包
30- ///
3123 /// The closure executed when the action is triggered.
3224 public var block : ( ( ) -> Void ) ?
3325
34- /// Action 初始化时设置的参照对象。当该对象被释放时,action 将变为无效的,任何操作将不再被执行
35- ///
3626 /// The reference object set when the action is initialized. When this object is released, the action will become invalid and no operation can be executed.
3727 private( set) weak var reference : AnyObject ?
3828 private let hasReferenceSet : Bool
3929
40- /// 创建一个 target/selector 模式的 Action 对象
4130 /// Create an Action object with target/selector patterns
4231 ///
4332 /// - Parameters:
44- /// - target: `target` 属性
4533 /// - target: The `target` property.
46- /// - selector: `selector` 属性
4734 /// - selector: The `selector` property.
48- /// - reference: `reference` 属性
4935 /// - reference: The `reference` property.
5036 public init ( target: AnyObject ? , selector: Selector , reference: AnyObject ? = nil ) {
5137 self . target = target
@@ -54,27 +40,21 @@ public final class Action {
5440 self . reference = reference
5541 }
5642
57- /// 创建一个闭包调用模式的 Action 对象
5843 /// Create an action object with closure call
5944 ///
6045 /// - Parameters:
61- /// - action: `block` 属性
6246 /// - action: The `block` property.
63- /// - reference: `reference` 属性
6447 /// - reference: The `reference` property.
6548 public init ( _ action: @escaping ( ) -> Void , reference: AnyObject ? = nil ) {
6649 block = action
6750 hasReferenceSet = reference != nil
6851 self . reference = reference
6952 }
7053
71- /// 执行 Action
7254 /// Perform this action
7355 ///
74- /// 如果 target、selector 和 block 均不为 nil,会先向 target 发送 selector,再调用 block
7556 /// If target, selector and block are not nil, the selector will be sent to the target first, and then the block will be called.
7657 ///
77- /// - Parameter obj: 向 target 发送 selector 消息时附带的对象,闭包调用忽略
7858 /// - Parameter obj: An object sent with the selector message to the target, ignored when the closure is called.
7959 public func perform( with obj: Any ? ) {
8060 guard isVaild else { return }
@@ -86,10 +66,8 @@ public final class Action {
8666 }
8767 }
8868
89- /// Action 是否仍有效,当 Action 是无效时,执行 perform 方法无操作
9069 /// Whether this action is still or not. Execute the perform method with no action if the action is invalid.
9170 ///
92- /// 若初始化时设置了 reference 对象,仅当该对象未释放时是有效的
9371 /// If a reference object is set during initialization, it is valid only when the reference is not released.
9472 public var isVaild : Bool {
9573 if hasReferenceSet, reference == nil { return false }
@@ -101,13 +79,10 @@ public final class Action {
10179import UIKit
10280
10381extension Action {
104- /// 通过响应者链发送 action 消息
10582 /// Perform action through responder chain.
10683 ///
107- /// 必需在主线程调用
10884 /// Must be called on the main queue
10985 ///
110- /// - Parameter sender: 向 target 发送 selector 消息时附带的对象,闭包调用忽略
11186 /// - Parameter sender: An object sent with the selector message to the target, ignored when the closure is called.
11287 public func perform( sender: Any ? ) {
11388 if #available( iOS 10 . 0 , * ) {
@@ -127,13 +102,10 @@ extension Action {
127102import AppKit
128103
129104extension Action {
130- /// 通过响应者链发送 action 消息
131105 /// Perform action through responder chain.
132106 ///
133- /// 必需在主线程调用
134107 /// Must be called on the main queue
135108 ///
136- /// - Parameter sender: 向 target 发送 selector 消息时附带的对象,闭包调用忽略
137109 /// - Parameter sender: An object sent with the selector message to the target, ignored when the closure is called.
138110 public func perform( sender: Any ? ) {
139111 if #available( macOS 10 . 12 , * ) {
@@ -165,43 +137,32 @@ extension Action: CustomDebugStringConvertible {
165137}
166138
167139/**
168- 延迟一段时间再执行 Action 对象,典型场景:setNeedsDoSomething 模式
169140 Perform an action object after delay. Typical scenarios: setNeedsDoSomething pattern.
170141
171- 例如:
172142 eg.
173143 ```
174- // 创建延迟控制器
175144 // Create delay controller
176145 lazy var needsSave = DelayAction(delay: 0.3, action: Action(target: self, selector: #selector(save)))
177146
178- // 当需要保存时调用
179147 // Called when saving is required
180148 needsSave.set()
181149 ```
182150 */
183151public final class DelayAction {
184- /// Action 对象
185152 /// An action object.
186153 public let action : Action
187154
188- /// 触发 Action 所在的队列
189155 /// The queue which the action is performed on.
190156 public let queue : DispatchQueue
191157
192- /// Action 延迟触发的时长
193158 /// The length of time that the action needs to be delayed.
194159 public let delay : TimeInterval
195160
196- /// 创建一个 DelayAction 对象
197161 /// Create a DelayAction object
198162 ///
199163 /// - Parameters:
200- /// - action: Action 对象
201164 /// - action: An action object
202- /// - delay: 延迟执行的时间,不能为负
203165 /// - delay: The duration of the action is delayed. Must not be negative.
204- /// - queue: Action 执行所在的队列,默认为主线程队列
205166 /// - queue: Action will be performed on this queue. If not specified, the main queue will be use.
206167 public init ( _ action: Action , delay: TimeInterval = 0 , queue: DispatchQueue = . main) {
207168 precondition ( delay >= 0 )
@@ -214,10 +175,8 @@ public final class DelayAction {
214175 work? . cancel ( )
215176 }
216177
217- /// 标记 Action 需要被执行,实际操作会在延时一定时间后在指定队列上触发
218178 /// Mark the action needs to be performed. The actual operation will be triggered on the specified queue after a certain delay.
219179 ///
220- /// - Parameter reschedule: 为 true 重新安排延迟时间
221180 /// - Parameter reschedule: Reschedule the delay time for true
222181 public func set( reschedule: Bool = false ) {
223182 lock. lock ( )
@@ -232,7 +191,6 @@ public final class DelayAction {
232191 work = newItem
233192 }
234193
235- /// 重置执行标记并取消计划中的执行
236194 /// Reset the marks and cancel any scheduled.
237195 public func cancel( ) {
238196 lock. lock ( )
0 commit comments