Skip to content

Commit ee198cd

Browse files
committed
modifying injector to always inject client and introduce runMainAtStart flag
1 parent 64492b2 commit ee198cd

File tree

8 files changed

+72
-100
lines changed

8 files changed

+72
-100
lines changed

dwds/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## 24.3.11-wip
1+
## 24.3.11
22

3+
- Changed DWDS to always inject the client and use `runMainAtStart` flag to control whether main() execution is deferred or runs immediately.
34
- Added WebSocket-based hot reload support: `reloadSources` in `ChromeProxyService` and `DevHandler` now handle hot reload requests and responses over WebSockets.
45
- Refactored the injected client to use a reusable function for handling hot reload requests and responses over WebSockets.
56

dwds/lib/dart_web_debug_service.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Dwds {
6666
required Stream<BuildResult> buildResults,
6767
required ConnectionProvider chromeConnection,
6868
required ToolConfiguration toolConfiguration,
69-
bool injectDebuggingSupportCode = true,
69+
bool runMainAtStart = false,
7070
}) async {
7171
globalToolConfiguration = toolConfiguration;
7272
final debugSettings = toolConfiguration.debugSettings;
@@ -120,7 +120,7 @@ class Dwds {
120120

121121
final injected = DwdsInjector(
122122
extensionUri: extensionUri,
123-
injectDebuggingSupportCode: injectDebuggingSupportCode,
123+
runMainAtStart: runMainAtStart,
124124
);
125125

126126
final devHandler = DevHandler(

dwds/lib/src/handlers/injector.dart

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,23 @@ const _clientScript = 'dwds/src/injected/client';
3030
/// to include the injected DWDS client, enabling debugging capabilities
3131
/// and source mapping when running in a browser environment.
3232
///
33-
/// The `_injectDebuggingSupportCode` flag determines whether debugging-related
34-
/// functionality should be included:
35-
/// - When `true`, the DWDS client is injected, enabling debugging features.
36-
/// - When `false`, debugging support is disabled, meaning the application will
37-
/// run without debugging.
33+
/// The `_runMainAtStart` flag determines whether main() execution should be
34+
/// deferred or run immediately:
35+
/// - When `true`, main() is executed immediately when the app is loaded.
36+
/// - When `false`, main() execution is deferred, allowing for setup or
37+
/// debugging initialization before main() runs.
3838
///
3939
/// This separation allows for scenarios where debugging is not needed or
4040
/// should be explicitly avoided.
4141
class DwdsInjector {
4242
final Future<String>? _extensionUri;
4343
final _devHandlerPaths = StreamController<String>();
4444
final _logger = Logger('DwdsInjector');
45-
final bool _injectDebuggingSupportCode;
45+
final bool _runMainAtStart;
4646

47-
DwdsInjector({
48-
Future<String>? extensionUri,
49-
bool injectDebuggingSupportCode = true,
50-
}) : _extensionUri = extensionUri,
51-
_injectDebuggingSupportCode = injectDebuggingSupportCode;
47+
DwdsInjector({Future<String>? extensionUri, bool runMainAtStart = true})
48+
: _extensionUri = extensionUri,
49+
_runMainAtStart = runMainAtStart;
5250

5351
/// Returns the embedded dev handler paths.
5452
///
@@ -110,17 +108,15 @@ class DwdsInjector {
110108
await globalToolConfiguration.loadStrategy.trackEntrypoint(
111109
entrypoint,
112110
);
113-
// If true, inject the debugging client and hoist the main function
114-
// to enable debugging support.
115-
if (_injectDebuggingSupportCode) {
116-
body = await _injectClientAndHoistMain(
117-
body,
118-
appId,
119-
devHandlerPath,
120-
entrypoint,
121-
await _extensionUri,
122-
);
123-
}
111+
// Always inject the debugging client and hoist the main function.
112+
body = await _injectClientAndHoistMain(
113+
body,
114+
appId,
115+
devHandlerPath,
116+
entrypoint,
117+
await _extensionUri,
118+
_runMainAtStart,
119+
);
124120
body += await globalToolConfiguration.loadStrategy.bootstrapFor(
125121
entrypoint,
126122
);
@@ -156,6 +152,7 @@ Future<String> _injectClientAndHoistMain(
156152
String devHandlerPath,
157153
String entrypointPath,
158154
String? extensionUri,
155+
bool runMainAtStart,
159156
) async {
160157
final bodyLines = body.split('\n');
161158
final extensionIndex = bodyLines.indexWhere(
@@ -174,6 +171,7 @@ Future<String> _injectClientAndHoistMain(
174171
devHandlerPath,
175172
entrypointPath,
176173
extensionUri,
174+
runMainAtStart,
177175
);
178176
result += '''
179177
// Injected by dwds for debugging support.
@@ -205,6 +203,7 @@ Future<String> _injectedClientSnippet(
205203
String devHandlerPath,
206204
String entrypointPath,
207205
String? extensionUri,
206+
bool runMainAtStart,
208207
) async {
209208
final loadStrategy = globalToolConfiguration.loadStrategy;
210209
final buildSettings = loadStrategy.buildSettings;
@@ -223,6 +222,7 @@ Future<String> _injectedClientSnippet(
223222
'window.\$dartEmitDebugEvents = ${debugSettings.emitDebugEvents};\n'
224223
'window.\$isInternalBuild = ${appMetadata.isInternalBuild};\n'
225224
'window.\$isFlutterApp = ${buildSettings.isFlutterApp};\n'
225+
'window.\$runMainAtStart = $runMainAtStart;\n'
226226
'${loadStrategy is DdcLibraryBundleStrategy ? 'window.\$hotReloadSourcesPath = "${loadStrategy.hotReloadSourcesUri.toString()}";\n' : ''}'
227227
'${loadStrategy.loadClientSnippet(_clientScript)}';
228228

0 commit comments

Comments
 (0)