react-native-demo-library-by-rinkal 1.2.7 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,298 @@
1
+ # Quick Reference Guide - MiniApp-Container Communication
2
+
3
+ ## 🚀 Quick Start (30 seconds)
4
+
5
+ ### MiniApp
6
+ ```typescript
7
+ import MiniAppCommunication from 'react-native-demo-library-by-rinkal';
8
+
9
+ MiniAppCommunication.initialize();
10
+ MiniAppCommunication.sendEvent('hello', { message: 'Hi!' });
11
+ ```
12
+
13
+ ### Container
14
+ ```typescript
15
+ import ContainerCommunication from 'react-native-demo-library-by-rinkal';
16
+
17
+ ContainerCommunication.initialize();
18
+ ContainerCommunication.onEvent('hello', (data) => console.log(data));
19
+ ```
20
+
21
+ ---
22
+
23
+ ## 📚 6 Communication Patterns
24
+
25
+ ### 1️⃣ Event Emitter (Fire & Forget)
26
+ **MiniApp → Container**
27
+ ```typescript
28
+ MiniAppCommunication.sendEvent('buttonClicked', { id: 123 });
29
+ ```
30
+ **Container**
31
+ ```typescript
32
+ ContainerCommunication.onEvent('buttonClicked', (data) => {
33
+ console.log('Button:', data.id);
34
+ });
35
+ ```
36
+ **📖 Reference**: https://reactnative.dev/docs/native-modules-ios#sending-events-to-javascript
37
+
38
+ ---
39
+
40
+ ### 2️⃣ Callback (Direct Invocation)
41
+ **MiniApp**
42
+ ```typescript
43
+ MiniAppCommunication.registerCallback('onSuccess', (result) => {
44
+ console.log('Success:', result);
45
+ });
46
+ ```
47
+ **📖 Reference**: https://reactnative.dev/docs/native-modules-ios#callbacks
48
+
49
+ ---
50
+
51
+ ### 3️⃣ Promise (Async Request-Response)
52
+ **MiniApp**
53
+ ```typescript
54
+ const data = await MiniAppCommunication.request('getUser', { id: 123 });
55
+ console.log(data); // { name: 'John', email: 'john@example.com' }
56
+ ```
57
+ **Container**
58
+ ```typescript
59
+ ContainerCommunication.registerHandler('getUser', async (params) => {
60
+ return { name: 'John', email: 'john@example.com' };
61
+ });
62
+ ```
63
+ **📖 Reference**: https://reactnative.dev/docs/native-modules-ios#promises
64
+
65
+ ---
66
+
67
+ ### 4️⃣ Shared State (Bidirectional Sync)
68
+ **MiniApp**
69
+ ```typescript
70
+ MiniAppCommunication.setState('theme', 'dark');
71
+ MiniAppCommunication.subscribeToState('theme', (newTheme) => {
72
+ console.log('Theme changed:', newTheme);
73
+ });
74
+ ```
75
+ **Container**
76
+ ```typescript
77
+ ContainerCommunication.setState('theme', 'light');
78
+ const theme = ContainerCommunication.getState('theme');
79
+ ```
80
+ **📖 Reference**: https://redux.js.org/introduction/getting-started
81
+
82
+ ---
83
+
84
+ ### 5️⃣ Native Bridge (Native Module Calls)
85
+ **MiniApp**
86
+ ```typescript
87
+ const info = await MiniAppCommunication.callNative(
88
+ 'DeviceInfo',
89
+ 'getBatteryLevel'
90
+ );
91
+ console.log('Battery:', info);
92
+ ```
93
+ **📖 Reference**: https://reactnative.dev/docs/native-modules-intro
94
+
95
+ ---
96
+
97
+ ### 6️⃣ Message Queue (Reliable Delivery)
98
+ **MiniApp**
99
+ ```typescript
100
+ MiniAppCommunication.queueMessage('analytics', {
101
+ event: 'page_view',
102
+ page: 'home'
103
+ });
104
+ ```
105
+ **Container**
106
+ ```typescript
107
+ ContainerCommunication.registerMessageHandler('analytics', (msg) => {
108
+ console.log('Analytics:', msg.payload);
109
+ });
110
+ ```
111
+ **📖 Reference**: https://en.wikipedia.org/wiki/Message_queue
112
+
113
+ ---
114
+
115
+ ## 🎯 When to Use Each Pattern
116
+
117
+ | Pattern | Use When |
118
+ |---------|----------|
119
+ | **Event Emitter** | Notifying about user actions, UI updates |
120
+ | **Callback** | Simple function calls, synchronous operations |
121
+ | **Promise** | Fetching data, waiting for results |
122
+ | **Shared State** | Syncing settings, user preferences |
123
+ | **Native Bridge** | Accessing device features (camera, GPS) |
124
+ | **Message Queue** | Analytics, logging, offline support |
125
+
126
+ ---
127
+
128
+ ## 🔧 Common Operations
129
+
130
+ ### Initialize (Required)
131
+ ```typescript
132
+ // In MiniApp
133
+ MiniAppCommunication.initialize();
134
+
135
+ // In Container
136
+ ContainerCommunication.initialize();
137
+ ```
138
+
139
+ ### Cleanup (Recommended)
140
+ ```typescript
141
+ useEffect(() => {
142
+ return () => {
143
+ MiniAppCommunication.cleanup();
144
+ };
145
+ }, []);
146
+ ```
147
+
148
+ ### Error Handling
149
+ ```typescript
150
+ try {
151
+ const result = await MiniAppCommunication.request('getData', {});
152
+ } catch (error) {
153
+ console.error('Request failed:', error);
154
+ }
155
+ ```
156
+
157
+ ---
158
+
159
+ ## 📱 Complete React Component Example
160
+
161
+ ### MiniApp Component
162
+ ```typescript
163
+ import React, { useEffect, useState } from 'react';
164
+ import { View, Button, Text } from 'react-native';
165
+ import MiniAppCommunication from 'react-native-demo-library-by-rinkal';
166
+
167
+ export const MyMiniApp = () => {
168
+ const [data, setData] = useState(null);
169
+
170
+ useEffect(() => {
171
+ MiniAppCommunication.initialize();
172
+
173
+ const sub = MiniAppCommunication.onEvent('update', setData);
174
+
175
+ return () => {
176
+ sub.remove();
177
+ MiniAppCommunication.cleanup();
178
+ };
179
+ }, []);
180
+
181
+ const fetchData = async () => {
182
+ const result = await MiniAppCommunication.request('getData', {});
183
+ setData(result);
184
+ };
185
+
186
+ return (
187
+ <View>
188
+ <Button title="Fetch Data" onPress={fetchData} />
189
+ <Text>{JSON.stringify(data)}</Text>
190
+ </View>
191
+ );
192
+ };
193
+ ```
194
+
195
+ ### Container Component
196
+ ```typescript
197
+ import React, { useEffect } from 'react';
198
+ import { View, Button } from 'react-native';
199
+ import ContainerCommunication from 'react-native-demo-library-by-rinkal';
200
+
201
+ export const MyContainer = () => {
202
+ useEffect(() => {
203
+ ContainerCommunication.initialize();
204
+
205
+ ContainerCommunication.registerHandler('getData', async () => {
206
+ return { message: 'Hello from container!' };
207
+ });
208
+
209
+ const sub = ContainerCommunication.onEvent('buttonClicked', (data) => {
210
+ console.log('MiniApp button clicked:', data);
211
+ });
212
+
213
+ return () => {
214
+ sub.remove();
215
+ ContainerCommunication.cleanup();
216
+ };
217
+ }, []);
218
+
219
+ const sendUpdate = () => {
220
+ ContainerCommunication.sendEvent('update', {
221
+ timestamp: Date.now()
222
+ });
223
+ };
224
+
225
+ return (
226
+ <View>
227
+ <Button title="Send Update" onPress={sendUpdate} />
228
+ </View>
229
+ );
230
+ };
231
+ ```
232
+
233
+ ---
234
+
235
+ ## 🐛 Troubleshooting
236
+
237
+ ### Events Not Received?
238
+ ✅ Both sides initialized?
239
+ ✅ Correct event names?
240
+ ✅ Listener registered before sending?
241
+
242
+ ### Promise Timeout?
243
+ ✅ Handler registered in container?
244
+ ✅ Network/processing time < 30s?
245
+ ✅ Check console logs for errors?
246
+
247
+ ### State Not Syncing?
248
+ ✅ SharedState initialized?
249
+ ✅ Same key names used?
250
+ ✅ Subscriptions active?
251
+
252
+ ---
253
+
254
+ ## 📖 Full Documentation
255
+
256
+ - **Complete Guide**: [COMMUNICATION_DOCS.md](./COMMUNICATION_DOCS.md)
257
+ - **Official References**: [OFFICIAL_REFERENCES.md](./OFFICIAL_REFERENCES.md)
258
+ - **Examples**: [CommunicationExample.tsx](./CommunicationExample.tsx)
259
+
260
+ ---
261
+
262
+ ## 🔗 Key Official Links
263
+
264
+ 1. React Native Events: https://reactnative.dev/docs/native-modules-ios#sending-events-to-javascript
265
+ 2. React Native Callbacks: https://reactnative.dev/docs/native-modules-ios#callbacks
266
+ 3. React Native Promises: https://reactnative.dev/docs/native-modules-ios#promises
267
+ 4. Native Modules: https://reactnative.dev/docs/native-modules-intro
268
+ 5. Node.js Events: https://nodejs.org/api/events.html
269
+ 6. JavaScript Promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
270
+
271
+ ---
272
+
273
+ ## ⚡ Performance Tips
274
+
275
+ 1. **Batch Events**: Use message queue for multiple events
276
+ 2. **Debounce**: Limit frequent state updates
277
+ 3. **Cleanup**: Always cleanup listeners in useEffect
278
+ 4. **Error Handling**: Wrap all async calls in try-catch
279
+ 5. **Logging**: Use console logs during development
280
+
281
+ ---
282
+
283
+ ## 📦 Installation
284
+
285
+ ```bash
286
+ npm install react-native-demo-library-by-rinkal
287
+ ```
288
+
289
+ ---
290
+
291
+ ## 📄 License
292
+ ISC
293
+
294
+ ## 👨‍💻 Author
295
+ Rinkal
296
+
297
+ ## 📌 Version
298
+ 1.3.0