Skip to content

Commit bd47b66

Browse files
committed
feat: add docstrings to postMessage methods
1 parent ef52dd3 commit bd47b66

3 files changed

Lines changed: 61 additions & 2 deletions

File tree

packages/react-native-brownfield/android/src/main/java/com/callstack/reactnativebrownfield/ReactNativeBrownfield.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,32 @@ class ReactNativeBrownfield private constructor(val reactHost: ReactHost) {
121121
}
122122
}
123123

124+
/**
125+
* Send a serialized JSON message to the React Native JS application. This resembles the web `window.postMessage` API.
126+
* @note This method is available only on the New Architecture - on Old Architecture, it will be a no-op.
127+
* @param message - The serialized JSON message to send to the React Native JS application.
128+
* @example
129+
* val json = JSONObject().put("text", text).toString()
130+
* ReactNativeBrownfield.shared.postMessage(json)
131+
*/
124132
fun postMessage(message: String) {
125133
ReactNativeBrownfieldModule.emitMessageFromNative(message)
126134
}
127135

136+
/**
137+
* Register a listener for messages sent from the React Native JS application.
138+
* @note This method is available only on the New Architecture - on Old Architecture, it will be a no-op.
139+
* @param listener - The listener to register.
140+
*/
128141
fun addMessageListener(listener: OnMessageListener) {
129142
messageListeners.add(listener)
130143
}
131144

145+
/**
146+
* Remove a previously registered message listener.
147+
* @note This method is available only on the New Architecture - on Old Architecture, it will be a no-op.
148+
* @param listener - The listener to remove.
149+
*/
132150
fun removeMessageListener(listener: OnMessageListener) {
133151
messageListeners.remove(listener)
134152
}

packages/react-native-brownfield/ios/ReactNativeBrownfield.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,13 @@ internal import Expo
136136
}
137137

138138
/**
139-
* Send a JSON-serialized message to the React Native JS layer.
139+
* Send a serialized JSON message to the React Native JS application.
140140
* The message is delivered as a `brownfieldMessage` DeviceEventEmitter event.
141141
*
142-
* @param message A JSON string to send to JavaScript.
142+
* @param message - The serialized JSON message to send to the React Native JS application.
143+
* @example
144+
* let json = "{\"text\":\"\(text)\"}"
145+
* ReactNativeBrownfield.shared.postMessage(json)
143146
*/
144147
@objc public func postMessage(_ message: String) {
145148
let userInfo: [String: Any] = ["message": message]

packages/react-native-brownfield/src/index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ export interface MessageEvent {
77
}
88

99
const ReactNativeBrownfield = {
10+
/**
11+
* Pop to the native screen.
12+
* @param animated - Whether to animate the transition (iOS only).
13+
* @platform android, ios
14+
* @example
15+
* ReactNativeBrownfield.popToNative(true);
16+
*/
1017
popToNative: (animated?: boolean): void => {
1118
if (Platform.OS === 'ios') {
1219
ReactNativeBrownfieldModule.popToNative(!!animated);
@@ -17,6 +24,14 @@ const ReactNativeBrownfield = {
1724
}
1825
},
1926

27+
/**
28+
* Enable or disable the iOS native back gesture and Android hardware back button.
29+
* @note This method is available both on the New Architecture and the Old Architecture.
30+
* @param enabled - Whether to enable native back gesture and button.
31+
* @platform android, ios
32+
* @example
33+
* ReactNativeBrownfield.setNativeBackGestureAndButtonEnabled(true);
34+
*/
2035
setNativeBackGestureAndButtonEnabled: (enabled: boolean): void => {
2136
if (Platform.OS === 'ios') {
2237
ReactNativeBrownfieldModule.setPopGestureRecognizerEnabled(enabled);
@@ -27,11 +42,34 @@ const ReactNativeBrownfield = {
2742
}
2843
},
2944

45+
/**
46+
* Send a JSON-serializable message to the native host application. This resembles the web `window.postMessage` API.
47+
* @note This method is available both on the New Architecture and the Old Architecture.
48+
* @note This method requires the `data` to be JSON-serializable. The method does not catch any serialization errors thrown
49+
* by the `JSON.stringify` function, this is the responsibility of the caller.
50+
* @param data - The data to send to the native host application.
51+
* @platform android, ios
52+
* @example
53+
* ReactNativeBrownfield.postMessage({ text: 'Hello from React Native!', id: 2 });
54+
*/
3055
postMessage: (data: unknown): void => {
3156
const serialized = JSON.stringify(data);
3257
ReactNativeBrownfieldModule.postMessage(serialized);
3358
},
3459

60+
/**
61+
* Subscribe to messages sent from the native host application. Returns a subscription object with a `remove()` method for cleanup.
62+
* @param callback - The callback to invoke when a message is received from the native host application.
63+
* @returns A subscription object with a `remove` method for cleanup.
64+
* @platform android, ios
65+
* @example
66+
* const subscription = ReactNativeBrownfield.onMessage((event: MessageEvent) => {
67+
* console.log('Received from native:', event.data);
68+
* });
69+
*
70+
* // Later, to unsubscribe:
71+
* subscription.remove();
72+
*/
3573
onMessage: (
3674
callback: (event: MessageEvent) => void
3775
): { remove: () => void } => {

0 commit comments

Comments
 (0)