Skip to content

Commit

Permalink
Fix: "Overriding declaration requires an 'override' keyword" for meth…
Browse files Browse the repository at this point in the history
…odQueue
  • Loading branch information
ikhvorost committed Feb 4, 2024
1 parent 3931285 commit 83b1d2d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,46 @@ Calendar.createEvent('Wedding', 'Las Vegas')
});
```

**Inheritance**

It's possible inherit an other native module (which implements `RCTBridgeModule` protocol) and override existing or append additional functionality. For instance, to signal events to JavaScript you can subclass `RCTEventEmitter`:

``` swift
@ReactModule
class EventEmitter: RCTEventEmitter {

static private(set) var shared: EventEmitter?

override init() {
super.init()
Self.shared = self
}

override func supportedEvents() -> [String]! {
["EventReminder"]
}

func sendReminderEvent(title: String) {
sendEvent(withName: "EventReminder", body: ["title" : title])
}
}

...

EventEmitter.shared?.sendReminderEvent(title: "Dinner Party")
```

Then in JavaScript you can create `NativeEventEmitter` with your module and subscribe to a particular event:

``` js
const { EventEmitter } = NativeModules;

this.eventEmitter = new NativeEventEmitter(EventEmitter);
this.emitterSubscription = this.eventEmitter.addListener('EventReminder', event => {
console.log(event); // Prints: { title: 'Dinner Party' }
});
```

For more details about Native Modules, see: https://reactnative.dev/docs/native-modules-ios.

### Native UI Component
Expand Down
2 changes: 1 addition & 1 deletion Sources/ReactBridgeMacros/ReactModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ extension ReactModule: MemberMacro {
]

if let queue = arguments["methodQueue"]?.stringValue {
items.append(methodQueue(queue: queue))
items.append(methodQueue(queue: queue, override: override))
}

return items
Expand Down
5 changes: 5 additions & 0 deletions Tests/ReactBridgeTests/ReactBridgeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ final class ReactMethodTests: XCTestCase {
"""
}

func test_diagnosticID() {
let diagnosticID = ErrorMessage.funcOnly(macroName: "ReactMethod").diagnosticID
XCTAssert("\(diagnosticID)" == #"MessageID(domain: "ReactBridge", id: "ReactBridge: @ReactMethod can only be applied to a func")"#)
}

func test_var() {
let diagnostic = DiagnosticSpec(message: ErrorMessage.funcOnly(macroName: "ReactMethod").message, line: 2, column: 3)

Expand Down

0 comments on commit 83b1d2d

Please sign in to comment.