Skip to content

Commit 844e6db

Browse files
Add Web Sockets And Fix RN App (#1)
This PR fixes the RN app and adds web sockets as a package and uses it in the samples
1 parent b03dbc2 commit 844e6db

5 files changed

Lines changed: 100 additions & 12 deletions

File tree

examples/mobile/lib/app.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,18 @@ external void _consoleError(JSAny? message);
6262
/// Register the app with React Native
6363
void registerMobileApp() {
6464
_consoleLog('=== registerMobileApp() STARTING ==='.toJS);
65+
_consoleLog('=== Checking global.reactNative ==='.toJS);
6566
try {
67+
_consoleLog('=== About to call app() ==='.toJS);
6668
final appComponent = app();
67-
_consoleLog('=== app() created ==='.toJS);
69+
_consoleLog('=== app() returned successfully ==='.toJS);
70+
_consoleLog('=== appComponent type: ${appComponent.runtimeType} ==='.toJS);
71+
_consoleLog('=== About to call registerApp() ==='.toJS);
6872
registerApp('main', appComponent);
69-
_consoleLog('=== registerApp() completed ==='.toJS);
70-
} catch (e) {
73+
_consoleLog('=== registerApp() completed successfully ==='.toJS);
74+
} catch (e, st) {
7175
_consoleError('=== ERROR in registerMobileApp ==='.toJS);
72-
_consoleError(e.toString().toJS);
76+
_consoleError('Error: $e'.toJS);
77+
_consoleError('Stack: $st'.toJS);
7378
}
7479
}

examples/mobile/rn/app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"orientation": "portrait",
77
"icon": "./assets/icon.png",
88
"userInterfaceStyle": "light",
9-
"newArchEnabled": true,
9+
"newArchEnabled": false,
1010
"splash": {
1111
"backgroundColor": "#f5f5f5"
1212
},

examples/mobile/rn/index.js

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,76 @@
1-
// Load compiled Dart app
1+
// Heavy diagnostics for debugging
22
console.log('=== INDEX.JS STARTING ===');
3+
console.log('=== Checking globals ===');
4+
console.log('typeof global:', typeof global);
5+
console.log('typeof require:', typeof require);
6+
7+
// Import React and React Native FIRST
8+
console.log('=== Importing React ===');
9+
const React = require('react');
10+
console.log('React loaded:', !!React);
11+
console.log('React.createElement:', typeof React.createElement);
12+
13+
console.log('=== Importing React Native ===');
14+
const ReactNative = require('react-native');
15+
console.log('ReactNative loaded:', !!ReactNative);
16+
console.log('AppRegistry:', typeof ReactNative.AppRegistry);
17+
console.log('View:', typeof ReactNative.View);
18+
console.log('Text:', typeof ReactNative.Text);
19+
20+
// Make React and ReactNative available globally for Dart
21+
// Dart's @JS() looks at 'self' which the node preamble sets up
22+
console.log('=== Setting up globals for Dart ===');
23+
global.React = React;
24+
global.ReactNative = ReactNative;
25+
global.reactNative = ReactNative;
26+
27+
// Also set on self/this for Dart JS interop
28+
if (typeof self !== 'undefined') {
29+
console.log('Setting on self...');
30+
self.React = React;
31+
self.ReactNative = ReactNative;
32+
self.reactNative = ReactNative;
33+
}
34+
// For React Native, also set on globalThis
35+
if (typeof globalThis !== 'undefined') {
36+
console.log('Setting on globalThis...');
37+
globalThis.React = React;
38+
globalThis.ReactNative = ReactNative;
39+
globalThis.reactNative = ReactNative;
40+
}
41+
42+
console.log('global.React set:', !!global.React);
43+
console.log('global.reactNative set:', !!global.reactNative);
44+
console.log('Checking self.React:', typeof self !== 'undefined' ? !!self.React : 'self undefined');
45+
console.log('Checking globalThis.React:', typeof globalThis !== 'undefined' ? !!globalThis.React : 'globalThis undefined');
46+
47+
// Load compiled Dart app
48+
console.log('=== Loading Dart app.js ===');
349
try {
4-
console.log('Loading app.js...');
550
require('../build/app.js');
6-
console.log('=== APP.JS LOADED ===');
51+
console.log('=== APP.JS LOADED SUCCESSFULLY ===');
752
} catch (e) {
8-
console.error('=== ERROR LOADING APP.JS ===', e);
53+
console.error('=== ERROR LOADING APP.JS ===');
54+
console.error('Error:', e.message);
55+
console.error('Stack:', e.stack);
56+
57+
// Register a fallback error component
58+
console.log('=== Registering fallback error component ===');
59+
const { AppRegistry, View, Text } = ReactNative;
60+
61+
const ErrorApp = () => {
62+
return React.createElement(
63+
View,
64+
{ style: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: 'red' } },
65+
React.createElement(
66+
Text,
67+
{ style: { color: 'white', fontSize: 18, textAlign: 'center', padding: 20 } },
68+
'Error loading Dart app: ' + e.message
69+
)
70+
);
71+
};
72+
73+
AppRegistry.registerComponent('DartMobile', () => ErrorApp);
974
}
75+
76+
console.log('=== INDEX.JS COMPLETE ===');

packages/dart_node_react_native/lib/src/core.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,20 @@ AppRegistry get appRegistry {
4848
};
4949
}
5050

51+
@JS('console.log')
52+
external void _rnCoreLog(JSAny? message);
53+
5154
/// Register the main app component
5255
void registerApp(String appName, JSFunction component) {
56+
_rnCoreLog('=== registerApp() called with appName: $appName ==='.toJS);
57+
_rnCoreLog('=== Getting appRegistry ==='.toJS);
58+
final registry = appRegistry;
59+
_rnCoreLog('=== Got appRegistry ==='.toJS);
60+
_rnCoreLog('=== Creating provider function ==='.toJS);
5361
final provider = (() => component).toJS;
54-
appRegistry.callMethod('registerComponent'.toJS, appName.toJS, provider);
62+
_rnCoreLog('=== Calling registerComponent ==='.toJS);
63+
registry.callMethod('registerComponent'.toJS, appName.toJS, provider);
64+
_rnCoreLog('=== registerComponent called successfully ==='.toJS);
5565
}
5666

5767
/// Create a React Native element

packages/dart_node_ws/lib/src/websocket_types.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ class WebSocketClient {
7171

7272
void onClose(CloseHandler handler) => _ws.on(
7373
'close',
74-
((int code, JSAny reason) => handler((
74+
((int code, JSAny? reason) => handler((
7575
code: code,
76-
reason: (reason as JSString?)?.toDart ?? '',
76+
reason: _extractCloseReason(reason),
7777
))).toJS,
7878
);
7979

80+
String _extractCloseReason(JSAny? reason) => switch (reason) {
81+
null => '',
82+
final JSString s => s.toDart,
83+
_ => reason.toString(),
84+
};
85+
8086
void onError(ErrorHandler handler) =>
8187
_ws.on('error', ((JSAny error) => handler(error)).toJS);
8288
}

0 commit comments

Comments
 (0)