diff --git a/docs/docs/hybrid-objects.md b/docs/docs/hybrid-objects.md index a99fa2e31..45ee34723 100644 --- a/docs/docs/hybrid-objects.md +++ b/docs/docs/hybrid-objects.md @@ -112,10 +112,6 @@ Hybrid Objects can be implemented in C++, Swift or Kotlin: ```swift title="HybridMath.swift" class HybridMath : HybridMathSpec { - public override var memorySize: Int { - return 0 - } - public var pi: Double { return Double.pi } @@ -128,9 +124,6 @@ Hybrid Objects can be implemented in C++, Swift or Kotlin: ```kotlin title="HybridMath.kt" class HybridMath : HybridMathSpec() { - override val memorySize: Long - get() = 0L - override var pi: Double get() = Double.PI diff --git a/docs/docs/nitrogen.md b/docs/docs/nitrogen.md index e8471439e..07fe2e271 100644 --- a/docs/docs/nitrogen.md +++ b/docs/docs/nitrogen.md @@ -206,10 +206,6 @@ To implement `Math` now, you just need to implement the spec: ```swift title="HybridMath.swift" class HybridMath : HybridMathSpec { - public override var memorySize: Int { - return 0 - } - public func add(a: Double, b: Double) throws -> Double { return a + b } @@ -219,9 +215,6 @@ To implement `Math` now, you just need to implement the spec: ```kotlin title="HybridMath.kt" class HybridMath : HybridMathSpec() { - override val memorySize: Long - get() = 0L - override fun add(a: Double, b: Double): Double { return a + b } diff --git a/docs/docs/using-nitro-in-your-app.md b/docs/docs/using-nitro-in-your-app.md index e7eae3a3f..a8e6e1c70 100644 --- a/docs/docs/using-nitro-in-your-app.md +++ b/docs/docs/using-nitro-in-your-app.md @@ -211,10 +211,6 @@ After installing Nitro, you can start creating your [Hybrid Objects](hybrid-obje ```swift title="HybridMath.swift" class HybridMath : HybridMathSpec { - public override var memorySize: Int { - return 0 - } - public var pi: Double { return Double.pi } @@ -227,9 +223,6 @@ After installing Nitro, you can start creating your [Hybrid Objects](hybrid-obje ```kotlin title="HybridMath.kt" class HybridMath : HybridMathSpec() { - override val memorySize: Long - get() = 0L - override var pi: Double get() = Double.PI diff --git a/docs/docs/view-components.md b/docs/docs/view-components.md index 3dbfb83d5..265022f9e 100644 --- a/docs/docs/view-components.md +++ b/docs/docs/view-components.md @@ -93,7 +93,8 @@ It is up to the developer on how to handle this most efficiently, but here's an ```swift class NitroImageView : UIView { - static var globalViewsMap: NSMapTable = NSMapTable(keyOptions: .strongMemory, valueOptions: .weakMemory) + static var globalViewsMap: NSMapTable + = NSMapTable(keyOptions: .strongMemory, valueOptions: .weakMemory) @objc var nitroId: NSNumber = -1 { didSet { @@ -163,15 +164,10 @@ Now implement `NitroImageViewManager` in Swift and Kotlin, and assume it has to ```swift class HybridNitroImageViewManager: HybridNitroImageViewManagerSpec { - public override var memorySize: Int { - return 0 - } private var nitroId: Double? = nil private var view: NitroImageView? { get { - guard let viewId = self.nitroId else { - return nil - } + guard let viewId = self.nitroId else { return nil } return NitroImageView.globalViewsMap.object(forKey: NSNumber(value: viewId)) } } @@ -196,10 +192,9 @@ Now implement `NitroImageViewManager` in Swift and Kotlin, and assume it has to ```kotlin class HybridNitroImageViewManager: HybridNitroImageViewManagerSpec() { private var nitroId: Double? = null - private var view: WeakReference? = null get() { - return nitroId.let { + return nitroId?.let { return NitroImageView.globalViewsMap[it]?.get() } } @@ -208,10 +203,12 @@ Now implement `NitroImageViewManager` in Swift and Kotlin, and assume it has to get() = view.image set(newValue) = view.image = newValue + override var opacity: Double { get() = view.opacity set(newValue) = view.opacity = newValue + fun setNitroId(nitroId: Double?) { this.nitroId = nitroId } diff --git a/packages/nitrogen/src/syntax/swift/SwiftHybridObject.ts b/packages/nitrogen/src/syntax/swift/SwiftHybridObject.ts index a99ce5f30..dd9fcfda0 100644 --- a/packages/nitrogen/src/syntax/swift/SwiftHybridObject.ts +++ b/packages/nitrogen/src/syntax/swift/SwiftHybridObject.ts @@ -11,7 +11,7 @@ export function createSwiftHybridObject(spec: HybridObjectSpec): SourceFile[] { const properties = spec.properties.map((p) => p.getCode('swift')).join('\n') const methods = spec.methods.map((p) => p.getCode('swift')).join('\n') - const protocolBaseClasses = ['AnyObject'] + const protocolBaseClasses = ['HybridObject'] const classBaseClasses: string[] = [] for (const base of spec.baseTypes) { const baseName = getHybridObjectName(base.name) @@ -39,11 +39,6 @@ public ${hasBaseClass ? 'override func' : 'func'} getCxxWrapper() -> ${name.Hybr } }`.trim() ) - if (!hasBaseClass) { - // It doesn't have a base class - implement the `HybridObject` base protocol - classBaseClasses.push('HybridObject') - baseMembers.push(`public var memorySize: Int { return 0 }`) - } const protocolCode = ` ${createFileMetadataString(`${protocolName}.swift`)} @@ -61,7 +56,7 @@ public protocol ${protocolName}_protocol: ${protocolBaseClasses.join(', ')} { } /// See \`\`${protocolName}\`\` -public class ${protocolName}_base: ${classBaseClasses.join(', ')} { +public class ${protocolName}_base${classBaseClasses.length > 0 ? `: ${classBaseClasses.join(',')}` : ''} { ${baseMembers.length > 0 ? indent(baseMembers.join('\n'), ' ') : `/* inherited */`} } diff --git a/packages/react-native-nitro-image/ios/HybridImage.swift b/packages/react-native-nitro-image/ios/HybridImage.swift index 4307de5b7..58b5a9210 100644 --- a/packages/react-native-nitro-image/ios/HybridImage.swift +++ b/packages/react-native-nitro-image/ios/HybridImage.swift @@ -25,7 +25,7 @@ class HybridImage : HybridImageSpec { * Get the memory size of the Swift class, and the `UIImage` we allocated so JS * can efficiently garbage collect it when needed. */ - public override var memorySize: Int { + public var memorySize: Int { return uiImage.memorySize } diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridBaseSpec.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridBaseSpec.swift index 17b99bc3a..3abb26513 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridBaseSpec.swift +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridBaseSpec.swift @@ -9,7 +9,7 @@ import Foundation import NitroModules /// See ``HybridBaseSpec`` -public protocol HybridBaseSpec_protocol: AnyObject { +public protocol HybridBaseSpec_protocol: HybridObject { // Properties var baseValue: Double { get } @@ -18,7 +18,7 @@ public protocol HybridBaseSpec_protocol: AnyObject { } /// See ``HybridBaseSpec`` -public class HybridBaseSpec_base: HybridObject { +public class HybridBaseSpec_base { private weak var cxxWrapper: HybridBaseSpec_cxx? = nil public func getCxxWrapper() -> HybridBaseSpec_cxx { #if DEBUG @@ -34,7 +34,6 @@ public class HybridBaseSpec_base: HybridObject { return cxxWrapper } } - public var memorySize: Int { return 0 } } /** diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridChildSpec.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridChildSpec.swift index 2d8b46a96..6082c70bb 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridChildSpec.swift +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridChildSpec.swift @@ -9,7 +9,7 @@ import Foundation import NitroModules /// See ``HybridChildSpec`` -public protocol HybridChildSpec_protocol: AnyObject, HybridBaseSpec_protocol { +public protocol HybridChildSpec_protocol: HybridObject, HybridBaseSpec_protocol { // Properties var childValue: Double { get } diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridImageFactorySpec.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridImageFactorySpec.swift index 527ba5217..add46a8fc 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridImageFactorySpec.swift +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridImageFactorySpec.swift @@ -9,7 +9,7 @@ import Foundation import NitroModules /// See ``HybridImageFactorySpec`` -public protocol HybridImageFactorySpec_protocol: AnyObject { +public protocol HybridImageFactorySpec_protocol: HybridObject { // Properties @@ -21,7 +21,7 @@ public protocol HybridImageFactorySpec_protocol: AnyObject { } /// See ``HybridImageFactorySpec`` -public class HybridImageFactorySpec_base: HybridObject { +public class HybridImageFactorySpec_base { private weak var cxxWrapper: HybridImageFactorySpec_cxx? = nil public func getCxxWrapper() -> HybridImageFactorySpec_cxx { #if DEBUG @@ -37,7 +37,6 @@ public class HybridImageFactorySpec_base: HybridObject { return cxxWrapper } } - public var memorySize: Int { return 0 } } /** diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridImageSpec.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridImageSpec.swift index bade63586..4e742e3b9 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridImageSpec.swift +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridImageSpec.swift @@ -9,7 +9,7 @@ import Foundation import NitroModules /// See ``HybridImageSpec`` -public protocol HybridImageSpec_protocol: AnyObject { +public protocol HybridImageSpec_protocol: HybridObject { // Properties var size: ImageSize { get } var pixelFormat: PixelFormat { get } @@ -21,7 +21,7 @@ public protocol HybridImageSpec_protocol: AnyObject { } /// See ``HybridImageSpec`` -public class HybridImageSpec_base: HybridObject { +public class HybridImageSpec_base { private weak var cxxWrapper: HybridImageSpec_cxx? = nil public func getCxxWrapper() -> HybridImageSpec_cxx { #if DEBUG @@ -37,7 +37,6 @@ public class HybridImageSpec_base: HybridObject { return cxxWrapper } } - public var memorySize: Int { return 0 } } /** diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridTestObjectSwiftKotlinSpec.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridTestObjectSwiftKotlinSpec.swift index ab0e04cda..edd7b6bd2 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridTestObjectSwiftKotlinSpec.swift +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridTestObjectSwiftKotlinSpec.swift @@ -9,7 +9,7 @@ import Foundation import NitroModules /// See ``HybridTestObjectSwiftKotlinSpec`` -public protocol HybridTestObjectSwiftKotlinSpec_protocol: AnyObject { +public protocol HybridTestObjectSwiftKotlinSpec_protocol: HybridObject { // Properties var thisObject: (any HybridTestObjectSwiftKotlinSpec) { get } var optionalHybrid: (any HybridTestObjectSwiftKotlinSpec)? { get set } @@ -79,7 +79,7 @@ public protocol HybridTestObjectSwiftKotlinSpec_protocol: AnyObject { } /// See ``HybridTestObjectSwiftKotlinSpec`` -public class HybridTestObjectSwiftKotlinSpec_base: HybridObject { +public class HybridTestObjectSwiftKotlinSpec_base { private weak var cxxWrapper: HybridTestObjectSwiftKotlinSpec_cxx? = nil public func getCxxWrapper() -> HybridTestObjectSwiftKotlinSpec_cxx { #if DEBUG @@ -95,7 +95,6 @@ public class HybridTestObjectSwiftKotlinSpec_base: HybridObject { return cxxWrapper } } - public var memorySize: Int { return 0 } } /** diff --git a/packages/react-native-nitro-modules/ios/core/HybridObject.swift b/packages/react-native-nitro-modules/ios/core/HybridObject.swift index d8af0371b..385300365 100644 --- a/packages/react-native-nitro-modules/ios/core/HybridObject.swift +++ b/packages/react-native-nitro-modules/ios/core/HybridObject.swift @@ -29,6 +29,11 @@ public protocol HybridObject: AnyObject { var memorySize: Int { get } } +public extension HybridObject { + // By default, this returns `0`. + var memorySize: Int { return 0 } +} + @available(*, deprecated, message: "HybridObjectSpec has been renamed to HybridObject. Update Nitrogen and re-generate your specs.") public typealias HybridObjectSpec = HybridObject