Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Default-implement memorySize via Swift extension #497

Merged
merged 5 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions docs/docs/hybrid-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ Hybrid Objects can be implemented in C++, Swift or Kotlin:
<TabItem value="swift" label="Swift" default>
```swift title="HybridMath.swift"
class HybridMath : HybridMathSpec {
public override var memorySize: Int {
return 0
}

public var pi: Double {
return Double.pi
}
Expand All @@ -128,9 +124,6 @@ Hybrid Objects can be implemented in C++, Swift or Kotlin:
<TabItem value="kotlin" label="Kotlin">
```kotlin title="HybridMath.kt"
class HybridMath : HybridMathSpec() {
override val memorySize: Long
get() = 0L

override var pi: Double
get() = Double.PI

Expand Down
7 changes: 0 additions & 7 deletions docs/docs/nitrogen.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,6 @@ To implement `Math` now, you just need to implement the spec:
<TabItem value="swift" label="Swift" default>
```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
}
Expand All @@ -219,9 +215,6 @@ To implement `Math` now, you just need to implement the spec:
<TabItem value="kotlin" label="Kotlin">
```kotlin title="HybridMath.kt"
class HybridMath : HybridMathSpec() {
override val memorySize: Long
get() = 0L

override fun add(a: Double, b: Double): Double {
return a + b
}
Expand Down
7 changes: 0 additions & 7 deletions docs/docs/using-nitro-in-your-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,6 @@ After installing Nitro, you can start creating your [Hybrid Objects](hybrid-obje
<TabItem value="swift" label="Swift" default>
```swift title="HybridMath.swift"
class HybridMath : HybridMathSpec {
public override var memorySize: Int {
return 0
}

public var pi: Double {
return Double.pi
}
Expand All @@ -227,9 +223,6 @@ After installing Nitro, you can start creating your [Hybrid Objects](hybrid-obje
<TabItem value="kotlin" label="Kotlin">
```kotlin title="HybridMath.kt"
class HybridMath : HybridMathSpec() {
override val memorySize: Long
get() = 0L

override var pi: Double
get() = Double.PI

Expand Down
15 changes: 6 additions & 9 deletions docs/docs/view-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSNumber, NitroImageView> = NSMapTable(keyOptions: .strongMemory, valueOptions: .weakMemory)
static var globalViewsMap: NSMapTable<NSNumber, NitroImageView>
= NSMapTable(keyOptions: .strongMemory, valueOptions: .weakMemory)

@objc var nitroId: NSNumber = -1 {
didSet {
Expand Down Expand Up @@ -163,15 +164,10 @@ Now implement `NitroImageViewManager` in Swift and Kotlin, and assume it has to
<TabItem value="swift" label="iOS (Swift)" default>
```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))
}
}
Expand All @@ -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<NitroImageView>? = null
get() {
return nitroId.let {
return nitroId?.let {
return NitroImageView.globalViewsMap[it]?.get()
}
}
Expand All @@ -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
}
Expand Down
9 changes: 2 additions & 7 deletions packages/nitrogen/src/syntax/swift/SwiftHybridObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`)}
Expand All @@ -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 */`}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-nitro-image/ios/HybridImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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
Expand All @@ -34,7 +34,6 @@ public class HybridBaseSpec_base: HybridObject {
return cxxWrapper
}
}
public var memorySize: Int { return 0 }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
import NitroModules

/// See ``HybridImageFactorySpec``
public protocol HybridImageFactorySpec_protocol: AnyObject {
public protocol HybridImageFactorySpec_protocol: HybridObject {
// Properties


Expand All @@ -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
Expand All @@ -37,7 +37,6 @@ public class HybridImageFactorySpec_base: HybridObject {
return cxxWrapper
}
}
public var memorySize: Int { return 0 }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand All @@ -37,7 +37,6 @@ public class HybridImageSpec_base: HybridObject {
return cxxWrapper
}
}
public var memorySize: Int { return 0 }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand All @@ -95,7 +95,6 @@ public class HybridTestObjectSwiftKotlinSpec_base: HybridObject {
return cxxWrapper
}
}
public var memorySize: Int { return 0 }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading