Skip to content

Commit

Permalink
removed white spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
sakuntala-motukuri committed Sep 3, 2024
1 parent df5c4d6 commit 32b9c57
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
52 changes: 26 additions & 26 deletions ios/core/Sources/Types/Hooks/Hook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import JavaScriptCore
/// A base for implementing JS backed hooks
open class BaseJSHook {
private let baseValue: JSValue

/// The JS reference to the hook
public var hook: JSValue { baseValue.objectForKeyedSubscript("hooks").objectForKeyedSubscript(name) }

/// The JSContext for the hook
public var context: JSContext { hook.context }

/// The name of the hook
public let name: String

/// Retrieves a hook by name from an object in JS
/// - Parameters:
/// - baseValue: The object that has `hooks`
Expand All @@ -39,7 +39,7 @@ public class Hook<T>: BaseJSHook where T: CreatedFromJSValue {
/**
Attach a closure to the hook, so when the hook is fired in the JS runtime
we receive the event in the native runtime
- parameters:
- hook: A function to run when the JS hook is fired
*/
Expand All @@ -51,7 +51,7 @@ public class Hook<T>: BaseJSHook where T: CreatedFromJSValue {
else { return }
hook(hookValue)
}

self.hook.invokeMethod("tap", withArguments: [name, JSValue(object: tapMethod, in: context) as Any])
}
}
Expand All @@ -64,7 +64,7 @@ public class Hook2<T, U>: BaseJSHook where T: CreatedFromJSValue, U: CreatedFrom
/**
Attach a closure to the hook, so when the hook is fired in the JS runtime
we receive the event in the native runtime
- parameters:
- hook: A function to run when the JS hook is fired
*/
Expand All @@ -78,7 +78,7 @@ public class Hook2<T, U>: BaseJSHook where T: CreatedFromJSValue, U: CreatedFrom
else { return }
hook(hookValue, hookValue2)
}

self.hook.invokeMethod("tap", withArguments: [name, JSValue(object: tapMethod, in: context) as Any])
}
}
Expand All @@ -91,7 +91,7 @@ public class HookDecode<T>: BaseJSHook where T: Decodable {
/**
Attach a closure to the hook, so when the hook is fired in the JS runtime
we receive the event in the native runtime
- parameters:
- hook: A function to run when the JS hook is fired
*/
Expand All @@ -103,7 +103,7 @@ public class HookDecode<T>: BaseJSHook where T: Decodable {
else { return }
hook(hookValue)
}

self.hook.invokeMethod("tap", withArguments: [name, JSValue(object: tapMethod, in: context) as Any])
}
}
Expand All @@ -116,13 +116,13 @@ public class Hook2Decode<T, U>: BaseJSHook where T: Decodable, U: Decodable {
/**
Attach a closure to the hook, so when the hook is fired in the JS runtime
we receive the event in the native runtime
- parameters:
- hook: A function to run when the JS hook is fired
*/
public func tap(_ hook: @escaping (T, U) -> Void) {
let tapMethod: @convention(block) (JSValue?, JSValue?) -> Void = { value, value2 in

let decoder = JSONDecoder()
guard
let val = value,
Expand All @@ -132,7 +132,7 @@ public class Hook2Decode<T, U>: BaseJSHook where T: Decodable, U: Decodable {
else { return }
hook(hookValue, hookValue2)
}

self.hook.invokeMethod("tap", withArguments: [name, JSValue(object: tapMethod, in: context) as Any])
}
}
Expand All @@ -143,13 +143,13 @@ public class Hook2Decode<T, U>: BaseJSHook where T: Decodable, U: Decodable {
*/
public class AsyncHook<T>: BaseJSHook where T: CreatedFromJSValue {
private var handler: AsyncHookHandler?

public typealias AsyncHookHandler = (T) async throws -> JSValue?

/**
Attach a closure to the hook, so when the hook is fired in the JS runtime
we receive the event in the native runtime
- parameters:
- hook: A function to run when the JS hook is fired
*/
Expand All @@ -159,7 +159,7 @@ public class AsyncHook<T>: BaseJSHook where T: CreatedFromJSValue {
let val = value,
let hookValue = T.createInstance(value: val) as? T
else { return JSValue() }

let promise =
JSUtilities.createPromise(context: self.context, handler: { (resolve, _) in
Task {
Expand All @@ -169,10 +169,10 @@ public class AsyncHook<T>: BaseJSHook where T: CreatedFromJSValue {
}
}
})

return promise ?? JSValue()
}

self.hook.invokeMethod("tap", withArguments: [name, JSValue(object: tapMethod, in: context) as Any])
}

Check warning on line 177 in ios/core/Sources/Types/Hooks/Hook.swift

View check run for this annotation

Codecov / codecov/patch

ios/core/Sources/Types/Hooks/Hook.swift#L158-L177

Added lines #L158 - L177 were not covered by tests
}
Expand All @@ -184,13 +184,13 @@ public class AsyncHook<T>: BaseJSHook where T: CreatedFromJSValue {
*/
public class AsyncHook2<T, U>: BaseJSHook where T: CreatedFromJSValue, U: CreatedFromJSValue {
private var handler: AsyncHookHandler?

public typealias AsyncHookHandler = (T, U) async throws -> JSValue?

/**
Attach a closure to the hook, so when the hook is fired in the JS runtime
we receive the event in the native runtime
- parameters:
- hook: A function to run when the JS hook is fired
*/
Expand All @@ -202,8 +202,8 @@ public class AsyncHook2<T, U>: BaseJSHook where T: CreatedFromJSValue, U: Create
let hookValue = T.createInstance(value: val) as? T,
let hookValue2 = U.createInstance(value: val2) as? U
else { return JSValue() }


let promise =
JSUtilities.createPromise(context: self.context, handler: { (resolve, _) in
Task {
Expand All @@ -213,10 +213,10 @@ public class AsyncHook2<T, U>: BaseJSHook where T: CreatedFromJSValue, U: Create
}
}
})

return promise ?? JSValue()
}

self.hook.invokeMethod("tap", withArguments: [name, JSValue(object: tapMethod, in: context) as Any])
}
}
Expand Down
36 changes: 18 additions & 18 deletions plugins/async-node/ios/Sources/AsyncNodePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public extension AsyncNodeHandlerType {
$0.toJSValue(context: context)
}
return context.objectForKeyedSubscript("Array").objectForKeyedSubscript("from").call(withArguments: [jsValueArray])

case .singleNode(let replacementNode):
return replacementNode.toJSValue(context: context)

case .emptyNode:
return nil
}
Expand All @@ -67,42 +67,42 @@ public extension AsyncNodeHandlerType {
*/
public class AsyncNodePlugin: JSBasePlugin, NativePlugin {
public var hooks: AsyncNodeHook?

private var asyncHookHandler: AsyncHookHandler?

public var plugins: [JSBasePlugin] = []

/**
Constructs the AsyncNodePlugin
- Parameters:
- handler: The callback that is used to tap into the core `onAsyncNode` hook
exposed to users of the plugin allowing them to supply the replacement node used in the tap callback
*/
public convenience init(plugins: [JSBasePlugin] = [AsyncNodePluginPlugin()], _ handler: @escaping AsyncHookHandler) {

self.init(fileName: "AsyncNodePlugin.native", pluginName: "AsyncNodePlugin.AsyncNodePlugin")
self.asyncHookHandler = handler
self.plugins = plugins
}

override public func setup(context: JSContext) {
super.setup(context: context)

if let pluginRef = pluginRef {
self.hooks = AsyncNodeHook(onAsyncNode: AsyncHook2(baseValue: pluginRef, name: "onAsyncNode"))
}

hooks?.onAsyncNode.tap({ node, callback in
// hook value is the original node
guard let asyncHookHandler = self.asyncHookHandler else {
return JSValue()
}

let replacementNode = try await (asyncHookHandler)(node, callback)
return replacementNode.handlerTypeToJSValue(context:context) ?? JSValue()
})
}

/**
Retrieves the arguments for constructing this plugin, this is necessary because the arguments need to be supplied after
construction of the swift object, once the context has been provided
Expand All @@ -112,10 +112,10 @@ public class AsyncNodePlugin: JSBasePlugin, NativePlugin {
for plugin in plugins {
plugin.context = self.context
}

return [["plugins": plugins.map { $0.pluginRef }]]
}

override open func getUrlForFile(fileName: String) -> URL? {
#if SWIFT_PACKAGE

Check warning on line 120 in plugins/async-node/ios/Sources/AsyncNodePlugin.swift

View check run for this annotation

Codecov / codecov/patch

plugins/async-node/ios/Sources/AsyncNodePlugin.swift#L120

Added line #L120 was not covered by tests
ResourceUtilities.urlForFile(name: fileName, ext: "js", bundle: Bundle.module)
Expand All @@ -140,10 +140,10 @@ public struct AsyncNodeHook {
public enum ReplacementNode: Encodable {
case concrete(JSValue)
case encodable(Encodable)

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()

switch self {
case .encodable(let value):
try container.encode(value)
Expand All @@ -157,12 +157,12 @@ public struct AssetPlaceholderNode: Encodable {
public enum CodingKeys: String, CodingKey {
case asset
}

var asset: Encodable
public init(asset: Encodable) {
self.asset = asset
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try? container.encode(asset, forKey: .asset)
Expand All @@ -185,7 +185,7 @@ public class AsyncNodePluginPlugin: JSBasePlugin {
public convenience init() {
self.init(fileName: "AsyncNodePlugin.native", pluginName: "AsyncNodePlugin.AsyncNodePluginPlugin")
}

override open func getUrlForFile(fileName: String) -> URL? {
#if SWIFT_PACKAGE
ResourceUtilities.urlForFile(name: fileName, ext: "js", bundle: Bundle.module)
Expand Down

0 comments on commit 32b9c57

Please sign in to comment.