forked from cweatureapps/SwiftScraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaitForConditionStep.swift
98 lines (88 loc) · 3.28 KB
/
WaitForConditionStep.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// WaitForConditionStep.swift
// SwiftScraper
//
// Created by Ken Ko on 28/04/2017.
// Copyright © 2017 Ken Ko. All rights reserved.
//
import Foundation
/// Step that waits for condition to become true before proceeding,
/// failing if the condition is still false when timeout occurs.
public class WaitForConditionStep: Step {
private enum Constants {
static let refreshInterval: TimeInterval = 0.1
}
private var assertionName: String
private var timeoutInSeconds: TimeInterval
private var params: [Any]
private var paramsKeys: [String]
private var startRunDate: Date?
private weak var browser: Browser?
private var model: JSON?
private var completion: StepCompletionCallback?
/// Initializer.
///
/// - parameter assertionName: Name of JavaScript function that evaluates the conditions and returns a Boolean.
/// - parameter timeoutInSeconds: The number of seconds before the step fails due to timeout.
/// - parameter params: Parameters which will be passed to the JavaScript function.
/// - parameter paramsKeys: Look up the values from the JSON model dictionary using these keys,
/// and pass them as the parameters to the JavaScript function. If provided, these are used instead of `params`.
public init(assertionName: String, timeoutInSeconds: TimeInterval, params: Any..., paramsKeys: [String] = []) {
self.assertionName = assertionName
self.timeoutInSeconds = timeoutInSeconds
self.params = params
self.paramsKeys = paramsKeys
}
public func run(with browser: Browser, model: JSON, completion: @escaping StepCompletionCallback) {
startRunDate = Date()
self.browser = browser
self.model = model
self.completion = completion
handleTimer()
}
func handleTimer() {
guard let startRunDate, let browser, let model, let completion else {
return
}
let params = getParameters()
browser.runScript(functionName: assertionName, params: params) { [weak self] result in
guard let self else {
return
}
switch result {
case .success(let isOk):
if isOk as? Bool ?? false {
reset()
completion(.proceed(model))
} else {
if Date().timeIntervalSince(startRunDate) > timeoutInSeconds {
reset()
completion(.failure(SwiftScraperError.timeout, model))
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + Constants.refreshInterval) { [weak self] in
self?.handleTimer()
}
}
}
case .failure(let error):
reset()
completion(.failure(error, model))
}
}
}
private func getParameters() -> [Any] {
let params: [Any]
if paramsKeys.isEmpty {
params = self.params
} else {
params = paramsKeys.map { model?[$0] ?? NSNull() }
}
return params
}
private func reset() {
startRunDate = nil
browser = nil
model = nil
completion = nil
}
}