Invoke.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. import Foundation
  5. import UIKit
  6. @objc public class Invoke: NSObject {
  7. public let command: String
  8. let callback: UInt64
  9. let error: UInt64
  10. let data: String
  11. let sendResponse: (UInt64, String?) -> Void
  12. let sendChannelData: (UInt64, String) -> Void
  13. public init(
  14. command: String, callback: UInt64, error: UInt64,
  15. sendResponse: @escaping (UInt64, String?) -> Void,
  16. sendChannelData: @escaping (UInt64, String) -> Void, data: String
  17. ) {
  18. self.command = command
  19. self.callback = callback
  20. self.error = error
  21. self.data = data
  22. self.sendResponse = sendResponse
  23. self.sendChannelData = sendChannelData
  24. }
  25. public func getRawArgs() -> String {
  26. return self.data
  27. }
  28. public func getArgs() throws -> JSObject {
  29. let jsonData = self.data.data(using: .utf8)!
  30. let data = try JSONSerialization.jsonObject(with: jsonData, options: [])
  31. return JSTypes.coerceDictionaryToJSObject(
  32. (data as! NSDictionary), formattingDatesAsStrings: true)!
  33. }
  34. public func parseArgs<T: Decodable>(_ type: T.Type) throws -> T {
  35. let jsonData = self.data.data(using: .utf8)!
  36. let decoder = JSONDecoder()
  37. decoder.userInfo[channelDataKey] = sendChannelData
  38. return try decoder.decode(type, from: jsonData)
  39. }
  40. func serialize(_ data: JsonValue) -> String {
  41. do {
  42. return try data.jsonRepresentation() ?? "\"Failed to serialize payload\""
  43. } catch {
  44. return "\"\(error)\""
  45. }
  46. }
  47. public func resolve() {
  48. sendResponse(callback, nil)
  49. }
  50. public func resolve(_ data: JsonObject) {
  51. resolve(.dictionary(data))
  52. }
  53. public func resolve(_ data: JsonValue) {
  54. sendResponse(callback, serialize(data))
  55. }
  56. public func resolve<T: Encodable>(_ data: T) {
  57. do {
  58. let json = try JSONEncoder().encode(data)
  59. sendResponse(callback, String(decoding: json, as: UTF8.self))
  60. } catch {
  61. sendResponse(self.error, "\"\(error)\"")
  62. }
  63. }
  64. public func reject(
  65. _ message: String, code: String? = nil, error: Error? = nil, data: JsonValue? = nil
  66. ) {
  67. let payload: NSMutableDictionary = [
  68. "message": message
  69. ]
  70. if let code = code {
  71. payload["code"] = code
  72. }
  73. if let error = error {
  74. payload["error"] = error
  75. }
  76. if let data = data {
  77. switch data {
  78. case .dictionary(let dict):
  79. for entry in dict {
  80. payload[entry.key] = entry.value
  81. }
  82. }
  83. }
  84. sendResponse(self.error, serialize(.dictionary(payload as! JsonObject)))
  85. }
  86. public func unimplemented() {
  87. unimplemented("not implemented")
  88. }
  89. public func unimplemented(_ message: String) {
  90. reject(message)
  91. }
  92. public func unavailable() {
  93. unavailable("not available")
  94. }
  95. public func unavailable(_ message: String) {
  96. reject(message)
  97. }
  98. }