mirror of
https://github.com/cranci1/Sora.git
synced 2026-04-21 08:32:00 +00:00
clean up
This commit is contained in:
parent
85d2499a06
commit
166767a10c
2 changed files with 8 additions and 28 deletions
|
|
@ -78,8 +78,8 @@
|
||||||
133D7C6C2D2BE2500075467E /* Sora */ = {
|
133D7C6C2D2BE2500075467E /* Sora */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
13DC0C442D302C6A00D0F966 /* MediaPlayer */,
|
|
||||||
13DC0C412D2EC9BA00D0F966 /* Info.plist */,
|
13DC0C412D2EC9BA00D0F966 /* Info.plist */,
|
||||||
|
13DC0C442D302C6A00D0F966 /* MediaPlayer */,
|
||||||
133D7C852D2BE2640075467E /* Utils */,
|
133D7C852D2BE2640075467E /* Utils */,
|
||||||
133D7C7B2D2BE2630075467E /* Views */,
|
133D7C7B2D2BE2630075467E /* Views */,
|
||||||
133D7C6D2D2BE2500075467E /* SoraApp.swift */,
|
133D7C6D2D2BE2500075467E /* SoraApp.swift */,
|
||||||
|
|
|
||||||
|
|
@ -21,15 +21,13 @@ class JSController: ObservableObject {
|
||||||
}
|
}
|
||||||
context.setObject(logFunction, forKeyedSubscript: "log" as NSString)
|
context.setObject(logFunction, forKeyedSubscript: "log" as NSString)
|
||||||
|
|
||||||
// Because fetch isnt available in JSContext, it simulates the fetch api for Javascript.
|
|
||||||
// Performs network calls with URLSession
|
|
||||||
let fetchNativeFunction: @convention(block) (String, JSValue, JSValue) -> Void = { urlString, resolve, reject in
|
let fetchNativeFunction: @convention(block) (String, JSValue, JSValue) -> Void = { urlString, resolve, reject in
|
||||||
guard let url = URL(string: urlString) else {
|
guard let url = URL(string: urlString) else {
|
||||||
print("Invalid URL")
|
print("Invalid URL")
|
||||||
reject.call(withArguments: ["Invalid URL"])
|
reject.call(withArguments: ["Invalid URL"])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let task = URLSession.shared.dataTask(with: url) { data, _, error in
|
let task = URLSession.custom.dataTask(with: url) { data, _, error in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
print(url)
|
print(url)
|
||||||
print("Network error in fetchNativeFunction: \(error.localizedDescription)")
|
print("Network error in fetchNativeFunction: \(error.localizedDescription)")
|
||||||
|
|
@ -52,7 +50,6 @@ class JSController: ObservableObject {
|
||||||
}
|
}
|
||||||
context.setObject(fetchNativeFunction, forKeyedSubscript: "fetchNative" as NSString)
|
context.setObject(fetchNativeFunction, forKeyedSubscript: "fetchNative" as NSString)
|
||||||
|
|
||||||
// Define fetch for JavaScript
|
|
||||||
let fetchDefinition = """
|
let fetchDefinition = """
|
||||||
function fetch(url) {
|
function fetch(url) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
|
|
@ -196,7 +193,6 @@ class JSController: ObservableObject {
|
||||||
}.resume()
|
}.resume()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Use Javascript to fetch search results
|
|
||||||
func fetchJsSearchResults(keyword: String, module: ScrapingModule, completion: @escaping ([SearchItem]) -> Void) {
|
func fetchJsSearchResults(keyword: String, module: ScrapingModule, completion: @escaping ([SearchItem]) -> Void) {
|
||||||
if let exception = context.exception {
|
if let exception = context.exception {
|
||||||
print("JavaScript exception: \(exception)")
|
print("JavaScript exception: \(exception)")
|
||||||
|
|
@ -210,7 +206,6 @@ class JSController: ObservableObject {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call the JavaScript function, passing in the parameter
|
|
||||||
let promiseValue = searchResultsFunction.call(withArguments: [keyword])
|
let promiseValue = searchResultsFunction.call(withArguments: [keyword])
|
||||||
guard let promise = promiseValue else {
|
guard let promise = promiseValue else {
|
||||||
print("searchResults did not return a Promise")
|
print("searchResults did not return a Promise")
|
||||||
|
|
@ -218,7 +213,6 @@ class JSController: ObservableObject {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handles successful promise resolution.
|
|
||||||
let thenBlock: @convention(block) (JSValue) -> Void = { result in
|
let thenBlock: @convention(block) (JSValue) -> Void = { result in
|
||||||
|
|
||||||
if let jsonString = result.toString(),
|
if let jsonString = result.toString(),
|
||||||
|
|
@ -256,7 +250,6 @@ class JSController: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handles promise rejection.
|
|
||||||
let catchBlock: @convention(block) (JSValue) -> Void = { error in
|
let catchBlock: @convention(block) (JSValue) -> Void = { error in
|
||||||
print("Promise rejected: \(String(describing: error.toString()))")
|
print("Promise rejected: \(String(describing: error.toString()))")
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
|
@ -264,16 +257,13 @@ class JSController: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wrap the Swift blocks into JSValue functions
|
|
||||||
let thenFunction = JSValue(object: thenBlock, in: context)
|
let thenFunction = JSValue(object: thenBlock, in: context)
|
||||||
let catchFunction = JSValue(object: catchBlock, in: context)
|
let catchFunction = JSValue(object: catchBlock, in: context)
|
||||||
|
|
||||||
// Attach the 'then' and 'catch' callbacks to the Promise
|
promise.invokeMethod("then", withArguments: [thenFunction as Any])
|
||||||
promise.invokeMethod("then", withArguments: [thenFunction])
|
promise.invokeMethod("catch", withArguments: [catchFunction as Any])
|
||||||
promise.invokeMethod("catch", withArguments: [catchFunction])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Use Javascript to fetch details
|
|
||||||
func fetchDetailsJS(url: String, completion: @escaping ([MediaItem], [EpisodeLink]) -> Void) {
|
func fetchDetailsJS(url: String, completion: @escaping ([MediaItem], [EpisodeLink]) -> Void) {
|
||||||
guard let url = URL(string: url) else {
|
guard let url = URL(string: url) else {
|
||||||
completion([], [])
|
completion([], [])
|
||||||
|
|
@ -301,7 +291,6 @@ class JSController: ObservableObject {
|
||||||
var resultItems: [MediaItem] = []
|
var resultItems: [MediaItem] = []
|
||||||
var episodeLinks: [EpisodeLink] = []
|
var episodeLinks: [EpisodeLink] = []
|
||||||
|
|
||||||
// Call the JavaScript function, passing in the parameter
|
|
||||||
let promiseValueDetails = extractDetailsFunction.call(withArguments: [url.absoluteString])
|
let promiseValueDetails = extractDetailsFunction.call(withArguments: [url.absoluteString])
|
||||||
guard let promiseDetails = promiseValueDetails else {
|
guard let promiseDetails = promiseValueDetails else {
|
||||||
print("extractDetails did not return a Promise")
|
print("extractDetails did not return a Promise")
|
||||||
|
|
@ -309,7 +298,6 @@ class JSController: ObservableObject {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handles successful promise resolution.
|
|
||||||
let thenBlockDetails: @convention(block) (JSValue) -> Void = { result in
|
let thenBlockDetails: @convention(block) (JSValue) -> Void = { result in
|
||||||
|
|
||||||
if let jsonOfDetails = result.toString(),
|
if let jsonOfDetails = result.toString(),
|
||||||
|
|
@ -342,7 +330,6 @@ class JSController: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handles promise rejection.
|
|
||||||
let catchBlockDetails: @convention(block) (JSValue) -> Void = { error in
|
let catchBlockDetails: @convention(block) (JSValue) -> Void = { error in
|
||||||
print("Promise rejected of extractDetails: \(String(describing: error.toString()))")
|
print("Promise rejected of extractDetails: \(String(describing: error.toString()))")
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
|
@ -350,16 +337,13 @@ class JSController: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wrap the Swift blocks into JSValue functions
|
|
||||||
let thenFunctionDetails = JSValue(object: thenBlockDetails, in: context)
|
let thenFunctionDetails = JSValue(object: thenBlockDetails, in: context)
|
||||||
let catchFunctionDetails = JSValue(object: catchBlockDetails, in: context)
|
let catchFunctionDetails = JSValue(object: catchBlockDetails, in: context)
|
||||||
|
|
||||||
// Attach the 'then' and 'catch' callbacks to the Promise
|
promiseDetails.invokeMethod("then", withArguments: [thenFunctionDetails as Any])
|
||||||
promiseDetails.invokeMethod("then", withArguments: [thenFunctionDetails])
|
promiseDetails.invokeMethod("catch", withArguments: [catchFunctionDetails as Any])
|
||||||
promiseDetails.invokeMethod("catch", withArguments: [catchFunctionDetails])
|
|
||||||
|
|
||||||
|
|
||||||
// Call the JavaScript function, passing in the parameter
|
|
||||||
let promiseValueEpisodes = extractEpisodesFunction.call(withArguments: [url.absoluteString])
|
let promiseValueEpisodes = extractEpisodesFunction.call(withArguments: [url.absoluteString])
|
||||||
guard let promiseEpisodes = promiseValueEpisodes else {
|
guard let promiseEpisodes = promiseValueEpisodes else {
|
||||||
print("extractEpisodes did not return a Promise")
|
print("extractEpisodes did not return a Promise")
|
||||||
|
|
@ -367,7 +351,6 @@ class JSController: ObservableObject {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handles successful promise resolution.
|
|
||||||
let thenBlockEpisodes: @convention(block) (JSValue) -> Void = { result in
|
let thenBlockEpisodes: @convention(block) (JSValue) -> Void = { result in
|
||||||
|
|
||||||
if let jsonOfEpisodes = result.toString(),
|
if let jsonOfEpisodes = result.toString(),
|
||||||
|
|
@ -404,7 +387,6 @@ class JSController: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handles promise rejection.
|
|
||||||
let catchBlockEpisodes: @convention(block) (JSValue) -> Void = { error in
|
let catchBlockEpisodes: @convention(block) (JSValue) -> Void = { error in
|
||||||
print("Promise rejected of extractEpisodes: \(String(describing: error.toString()))")
|
print("Promise rejected of extractEpisodes: \(String(describing: error.toString()))")
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
|
@ -412,13 +394,11 @@ class JSController: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wrap the Swift blocks into JSValue functions
|
|
||||||
let thenFunctionEpisodes = JSValue(object: thenBlockEpisodes, in: context)
|
let thenFunctionEpisodes = JSValue(object: thenBlockEpisodes, in: context)
|
||||||
let catchFunctionEpisodes = JSValue(object: catchBlockEpisodes, in: context)
|
let catchFunctionEpisodes = JSValue(object: catchBlockEpisodes, in: context)
|
||||||
|
|
||||||
// Attach the 'then' and 'catch' callbacks to the Promise
|
promiseEpisodes.invokeMethod("then", withArguments: [thenFunctionEpisodes as Any])
|
||||||
promiseEpisodes.invokeMethod("then", withArguments: [thenFunctionEpisodes])
|
promiseEpisodes.invokeMethod("catch", withArguments: [catchFunctionEpisodes as Any])
|
||||||
promiseEpisodes.invokeMethod("catch", withArguments: [catchFunctionEpisodes])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue