-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sync ios code for blocked transitions
- Loading branch information
Showing
10 changed files
with
209 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// JSValue+Extensions.swift | ||
// PlayerUI | ||
// | ||
// Created by Zhao Xia Wu on 2024-01-18. | ||
// | ||
|
||
import Foundation | ||
import JavaScriptCore | ||
|
||
extension JSValue { | ||
|
||
|
||
/** | ||
A way to catch errors for functions not called inside a player process. Can be called on functions with a return value and void with discardableResult. | ||
- parameters: | ||
- args: List of arguments taken by the function | ||
*/ | ||
@discardableResult | ||
public func tryCatch(args: Any...) throws -> JSValue? { | ||
var tryCatchWrapper: JSValue? { | ||
self.context.evaluateScript( | ||
""" | ||
(fn, args) => { | ||
try { | ||
return fn(...args) | ||
} catch(e) { | ||
return e | ||
} | ||
} | ||
""") | ||
} | ||
|
||
var errorCheckWrapper: JSValue? { | ||
self.context.evaluateScript( | ||
""" | ||
(obj) => (obj instanceof Error) | ||
""") | ||
} | ||
let result = tryCatchWrapper?.call(withArguments: [self, args]) | ||
|
||
let isError = errorCheckWrapper?.call(withArguments: [result as Any]) | ||
|
||
let errorMessage = result?.toString() ?? "" | ||
|
||
if isError?.toBool() == true { | ||
throw JSValueError.thrownFromJS(message: errorMessage) | ||
} else { | ||
return result | ||
} | ||
} | ||
} | ||
|
||
/** | ||
Represents the different errors that occur when evaluating JSValue | ||
*/ | ||
public enum JSValueError: Error, Equatable { | ||
case thrownFromJS(message: String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// JSValueExtensionsTests.swift | ||
// PlayerUI-Unit-Unit | ||
// | ||
// Created by Zhao Xia Wu on 2024-01-22. | ||
// | ||
|
||
|
||
import Foundation | ||
import XCTest | ||
import JavaScriptCore | ||
@testable import PlayerUI | ||
|
||
class JSValueExtensionsTests: XCTestCase { | ||
let context: JSContext = JSContext() | ||
func testTryCatchWrapperReturningError() { | ||
|
||
let functionReturningError = self.context | ||
.evaluateScript(""" | ||
(() => { | ||
throw new Error("Fail") | ||
}) | ||
""") | ||
|
||
do { | ||
let _ = try functionReturningError?.tryCatch(args: [] as [String]) | ||
} catch let error { | ||
XCTAssertEqual(error as? JSValueError, JSValueError.thrownFromJS(message: "Error: Fail")) | ||
} | ||
} | ||
|
||
func testTryCatchWrapperReturningNumber() { | ||
let functionReturningInt = self.context | ||
.evaluateScript(""" | ||
(() => { | ||
return 1 | ||
}) | ||
""") | ||
|
||
do { | ||
let result = try functionReturningInt?.tryCatch(args: [] as [String]) | ||
XCTAssertEqual(result?.toInt32(), 1) | ||
} catch let error { | ||
XCTFail("Should have returned Int but failed with \(error)") | ||
} | ||
} | ||
|
||
func testTransitionDuringAnActiveTransitionShouldCatchErrorUsingTryCatchWrapper() { | ||
let player = HeadlessPlayerImpl(plugins: []) | ||
|
||
let expectation = expectation(description: "Wait for on update") | ||
|
||
player.hooks?.viewController.tap { viewController in | ||
viewController.hooks.view.tap { view in | ||
view.hooks.onUpdate.tap { value in | ||
guard view.id == "view-2" else { | ||
do { | ||
try (player.state as? InProgressState)?.controllers?.flow.transition(with: "NEXT") | ||
} catch let error { | ||
XCTAssertEqual(error as? JSValueError, JSValueError.thrownFromJS(message: "Error: Transitioning while ongoing transition from VIEW_1 is in progress is not supported")) | ||
expectation.fulfill() | ||
} | ||
|
||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
player.start(flow: FlowData.MULTIPAGE, completion: {_ in}) | ||
wait(for: [expectation], timeout: 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters