Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/jaspr/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fixed a server built with `dart build cli` on Linux with Dart 3.14 or later not finding its `web` directory and failing with "Could not resolve project directory containing pubspec.yaml" when no `pubspec.yaml` is above it. Since Dart 3.14 such a bundle keeps the snapshot in `lib/libdartaot<name>.so`, which `Platform.script` points at.
- Fixed `jaspr serve` with Flutter embedding or plugins failing on Flutter 3.50, which moved the precompiled DDC SDK to `kernel/ddc/canary` and `kernel/ddc/stable`.
- Fixed a browser that `jaspr serve` skipped because it didn't respond in time (e.g. a tab in the background) showing outdated content until the next change. It now catches up on the reload it missed as soon as it responds again.

## 0.24.0-beta.1

Expand Down
176 changes: 152 additions & 24 deletions packages/jaspr_cli/lib/src/dev/client_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:vm_service/vm_service.dart' as vm;
import 'package:vm_service/vm_service_io.dart';

import '../logging.dart';
import '../project.dart';
import 'client_call_guard.dart';
import 'dev_workflow.dart';

Expand Down Expand Up @@ -100,6 +101,21 @@ class ClientManager {
}
}

/// An update the dev workflow applies to the connected clients.
///
/// The values are ordered by how much of the page they replace,
/// so a later update includes the ones before it.
enum ClientUpdate {
/// A hot reload, which keeps the state of the app.
reload,

/// A hot restart, which runs the app from the start.
restart,

/// A full reload of the page.
refresh,
}

class ClientConnection {
ClientConnection(this.appConnection, this.dwds, this.manager);

Expand All @@ -125,18 +141,18 @@ class ClientConnection {
tag: .client,
level: .warning,
),
onResponsive: () {
manager.workflow.logger.write(
'Client ${appConnection.request.appId} is responding again, it may show outdated content until the next reload.',
tag: .client,
);
// Changed stylesheets aren't part of the next reload, so deliver them now.
if (_pendingStylesheets.isNotEmpty && !_isDisposed) {
unawaited(reloadStylesheets(const []));
}
},
onResponsive: () => unawaited(_catchUp()),
);

/// The update this client missed while it didn't respond.
ClientUpdate? _missedUpdate;

/// Whether this client is applying what it missed while it didn't respond.
bool _isCatchingUp = false;

/// Whether updates skip this client because it doesn't have the previous ones yet.
bool get _isBehind => _callGuard.isUnresponsive || _isCatchingUp;

/// Stylesheets that changed while this client didn't receive them.
final Set<String> _pendingStylesheets = {};

Expand Down Expand Up @@ -229,19 +245,124 @@ class ClientConnection {
}
}

Future<vm.Response?> restart() => _callGuard.run(() async {
/// Applies [update] to this client and returns whether it succeeded.
///
/// A client that doesn't respond in time is skipped,
/// and catches up on [update] once it responds again.
Future<bool> update(ClientUpdate update) async {
if (_isBehind) {
_markMissed(update);
return false;
}

// Include an update that the catch-up couldn't apply.
_markMissed(update);
final pending = _missedUpdate!;
_missedUpdate = null;

final succeeded = await _apply(pending, _callGuard.run);
// A refresh that timed out still reloads the page once it gets through,
// so there is nothing left to catch up on.
if (_callGuard.isUnresponsive && pending != .refresh) {
_markMissed(pending);
}
return succeeded;
}

void _markMissed(ClientUpdate update) {
final missed = _missedUpdate;
if (missed == null || update.index > missed.index) {
_missedUpdate = update;
}
}

/// Applies what this client missed while it didn't respond.
///
/// The calls aren't limited by a timeout. A client that responds slowly,
/// such as a background tab throttled by the browser, would otherwise time
/// out again and never get up to date. Updates that arrive in the meantime
/// skip this client and are applied afterwards, so nothing waits for it.
Future<void> _catchUp() async {
final appId = appConnection.request.appId;
final logger = manager.workflow.logger;
logger.write(
switch (_missedUpdate) {
final update? => 'Client $appId is responding again, catching up on the missed ${update.name}.',
null => 'Client $appId is responding again.',
},
tag: .client,
);
if (_isCatchingUp) return;

_isCatchingUp = true;
ClientUpdate? update;
try {
while (!_isDisposed) {
update = _missedUpdate;
if (update != null) {
_missedUpdate = null;
if (!await _apply(update, _unguarded)) break;
update = null;
continue;
}

// Changed stylesheets aren't part of an update, so deliver them afterwards.
final stylesheets = _pendingStylesheets.toList();
if (stylesheets.isEmpty) break;
_pendingStylesheets.clear();
if (await _reloadStylesheets(stylesheets) == null) {
// Keep them for the next stylesheet reload instead of retrying forever.
_pendingStylesheets.addAll(stylesheets);
break;
}
}
} catch (error, stackTrace) {
logger.write('Catching up failed for client $appId: $error', tag: .client, level: .warning);
logger.write(stackTrace.toString(), tag: .client, level: .verbose);
} finally {
// An update that couldn't be applied stays missed and is included in the
// next update, instead of being retried forever.
if (update != null) {
_markMissed(update);
}
_isCatchingUp = false;
}
}

static Future<T?> _unguarded<T>(Future<T?> Function() call) => call();

/// Runs the calls of [update] through [run] and returns whether it succeeded.
Future<bool> _apply(ClientUpdate update, Future<T?> Function<T>(Future<T?> Function() call) run) async {
switch (update) {
case ClientUpdate.reload:
if (await run(_performHotReload) != true) {
// Skip reassemble and html reload if client hot-reload fails.
return false;
}
await run(_reassemble);
case ClientUpdate.restart:
if (await run(_restart) == null) {
// Skip html reload if client restart fails.
return false;
}
case ClientUpdate.refresh:
return await run(_refreshPage) != null;
}
if (manager.workflow.project.requireMode != JasprMode.client) {
await run(_reloadHtml);
}
return true;
}

Future<vm.Response?> restart() => _callGuard.run(_restart);

Future<vm.Response?> _restart() async {
final restartMethod = _registeredMethodsForService['hotRestart'] ?? 'hotRestart';
final response = await vmService?.callServiceExtension(restartMethod);
return response;
});

/// Performs a hot reload and returns whether it succeeded.
///
/// Returns `false` if the client didn't respond in time.
Future<bool> performHotReload() async {
return await _callGuard.run(_performHotReload) ?? false;
}

/// Performs a hot reload and returns whether it succeeded.
Future<bool> _performHotReload() async {
final vmService = this.vmService;
if (vmService == null) {
Expand Down Expand Up @@ -281,6 +402,9 @@ class ClientConnection {
/// Reloads the stylesheets at [urls], together with any stylesheets this client missed before.
Future<vm.Response?> reloadStylesheets(List<String> urls) async {
_pendingStylesheets.addAll(urls);
// Delivered at the end of the catch-up.
if (_isCatchingUp) return null;

final pending = _pendingStylesheets.toList();
final response = await _callGuard.run(() => _reloadStylesheets(pending));
if (response != null) {
Expand All @@ -305,7 +429,9 @@ class ClientConnection {
return null;
}

Future<vm.Response?> reassemble() => _callGuard.run(() async {
Future<vm.Response?> reassemble() => _callGuard.run(_reassemble);

Future<vm.Response?> _reassemble() async {
final reassembleMethod = _registeredMethodsForService['ext.jaspr.reassemble'] ?? 'ext.jaspr.reassemble';
for (var i = 0; i < 5; i++) {
try {
Expand All @@ -316,9 +442,11 @@ class ClientConnection {
}
}
return null;
});
}

Future<vm.Response?> reloadHtml([String? path]) => _callGuard.run(() => _reloadHtml(path));

Future<vm.Response?> reloadHtml([String? path]) => _callGuard.run(() async {
Future<vm.Response?> _reloadHtml([String? path]) async {
final reloadMethod = _registeredMethodsForService['ext.jaspr.reload_html'] ?? 'ext.jaspr.reload_html';
final args = path != null ? {'path': path} : <String, String>{};
for (var i = 0; i < 5; i++) {
Expand All @@ -330,9 +458,9 @@ class ClientConnection {
}
}
return null;
});
}

Future<vm.Response?> refreshPage() => _callGuard.run(() async {
Future<vm.Response?> _refreshPage() async {
final reloadMethod =
_registeredMethodsForService['fullReload'] ?? _registeredMethodsForService['ext.dwds.reload'] ?? 'fullReload';
try {
Expand All @@ -341,7 +469,7 @@ class ClientConnection {
} catch (_) {
return null;
}
});
}

int? _buildProgressEventId;
var _progressEventId = 0;
Expand Down
17 changes: 3 additions & 14 deletions packages/jaspr_cli/lib/src/dev/dev_workflow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,8 @@ class DevWorkflow {

var allReloaded = true;
await clients.forEachConnection('Hot reload', (clientConnection) async {
if (!await clientConnection.performHotReload()) {
if (!await clientConnection.update(.reload)) {
allReloaded = false;
// Skip reassemble and html reload if client hot-reload fails.
return;
}
await clientConnection.reassemble();
if (project.requireMode != JasprMode.client) {
await clientConnection.reloadHtml();
}
});
if (allReloaded) {
Expand Down Expand Up @@ -189,13 +183,8 @@ class DevWorkflow {

var allRestarted = true;
await clients.forEachConnection('Hot restart', (clientConnection) async {
if (await clientConnection.restart() == null) {
if (!await clientConnection.update(.restart)) {
allRestarted = false;
// Skip html reload if client restart fails.
return;
}
if (project.requireMode != JasprMode.client) {
await clientConnection.reloadHtml();
}
});
if (allRestarted) {
Expand All @@ -220,7 +209,7 @@ class DevWorkflow {

var allRefreshed = true;
await clients.forEachConnection('Refresh', (client) async {
if (await client.refreshPage() == null) {
if (!await client.update(.refresh)) {
allRefreshed = false;
}
});
Expand Down
Loading
Loading