diff --git a/README.md b/README.md index 47c6061..9302dcb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Sources/ReactBridgeMacros/ReactModule.swift b/Sources/ReactBridgeMacros/ReactModule.swift index 1d5dfc7..8520bac 100644 --- a/Sources/ReactBridgeMacros/ReactModule.swift +++ b/Sources/ReactBridgeMacros/ReactModule.swift @@ -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 diff --git a/Tests/ReactBridgeTests/ReactBridgeTests.swift b/Tests/ReactBridgeTests/ReactBridgeTests.swift index 4862ada..cdf66f9 100644 --- a/Tests/ReactBridgeTests/ReactBridgeTests.swift +++ b/Tests/ReactBridgeTests/ReactBridgeTests.swift @@ -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)