forked from cweatureapps/SwiftScraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptStep.swift
83 lines (74 loc) · 2.92 KB
/
ScriptStep.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
//
// ScriptStep.swift
// SwiftScraper
//
// Created by Ken Ko on 21/04/2017.
// Copyright © 2017 Ken Ko. All rights reserved.
//
import Foundation
// MARK: - Types
/// Callback invoked when a `ScriptStep` or `AsyncScriptStep` is finished.
///
/// - parameter response: Data returned from JavaScript.
/// - parameter model: The model JSON dictionary which can be modified by the step.
/// - returns: The `StepFlowResult` which allows control flow of the steps.
public typealias ScriptStepHandler = (_ response: Any?, _ model: inout JSON) -> StepFlowResult
// MARK: - ScriptStep
/// Step that runs some JavaScript which will return a result immediately from the JavaScript function.
///
/// The `StepFlowResult` returned by the `handler` can be used to drive control flow of the steps.
public class ScriptStep: Step {
private var functionName: String
var params: [Any]
private var paramsKeys: [String]
private var handler: ScriptStepHandler
/// Initializer.
///
/// - parameter functionName: The name of the JavaScript function to call. The module namespace is automatically
/// added.
/// - 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`.
/// - parameter handler: Callback function which returns data from JavaScript, and passes the model JSON dictionary
/// for modification.
public init(
functionName: String,
params: Any...,
paramsKeys: [String] = [],
handler: @escaping ScriptStepHandler
) {
self.functionName = functionName
self.params = params
self.paramsKeys = paramsKeys
self.handler = handler
}
public func run(with browser: Browser, model: JSON, completion: @escaping StepCompletionCallback) {
let params: [Any]
if paramsKeys.isEmpty {
params = self.params
} else {
params = paramsKeys.map { model[$0] ?? NSNull() }
}
runScript(browser: browser, functionName: functionName, params: params) { [weak self] result in
guard let self else {
return
}
switch result {
case .failure(let error):
completion(.failure(error, model))
case .success(let response):
var modelCopy = model
let result = handler(response, &modelCopy)
completion(result.convertToStepCompletionResult(with: modelCopy))
}
}
}
func runScript(
browser: Browser,
functionName: String,
params: [Any],
completion: @escaping ScriptResponseResultCallback
) {
browser.runScript(functionName: functionName, params: params, completion: completion)
}
}