feat(workspace): refresh desktop editing experience and prepare 0.1.2
Gitea CI / Build Android APK (pull_request) Canceled after 0s
Gitea CI / Build Windows x64 app (pull_request) Canceled after 0s
Gitea CI / Audit and package Windows x64 (pull_request) Canceled after 0s
Gitea CI / Assemble and validate CI artifacts (pull_request) Canceled after 0s
Gitea CI / Gitea CI passed (pull_request) Canceled after 0s
Gitea CI / Check code and tests (pull_request) Canceled after 6m29s

This commit is contained in:
lingjhf committed 2026-10-09 01:56:01 +08:00
1 parent aa5255d1e4
commit 2e0033ce2d
260 files changed
+43963 -5591

No files matched your search

+53 -2
View File
@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:cutdex_sdk/preview.dart';
import 'package:flutter/foundation.dart';
import 'package:cutdex_sdk/runtime.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
@@ -24,14 +25,57 @@ PreviewControllerRegistry previewControllerRegistry(Ref ref) {
/// Owns native preview controllers independently of the currently visible
/// timeline. Prepared controllers make timeline selection a synchronous swap.
final class PreviewControllerRegistry {
PreviewControllerRegistry(this._runtime, {this._onChanged});
PreviewControllerRegistry(
this._runtime, {
this._onChanged,
this.maximumCachedControllers = 4,
}) : assert(maximumCachedControllers > 0);
final int maximumCachedControllers;
final CutdexRuntime _runtime;
final void Function()? _onChanged;
final _controllers = <(String, String), PreviewController>{};
final _preparing = <(String, String), Future<PreviewController>>{};
final _leases = <(String, String), int>{};
bool _disposed = false;
/// Mounted providers retain their controller; idle entries are evictable.
VoidCallback retain((String, String) ids) {
if (_disposed) throw StateError('Preview registry is closed');
_leases[ids] = (_leases[ids] ?? 0) + 1;
var released = false;
return () {
if (released || _disposed) return;
released = true;
final count = (_leases[ids] ?? 1) - 1;
if (count == 0) {
_leases.remove(ids);
final controller = _controllers[ids];
if (controller != null) {
unawaited(
controller.pauseForCanvasInteraction().catchError((Object _) {}),
);
}
} else {
_leases[ids] = count;
}
_trimCache();
};
}
void _trimCache() {
if (_disposed) return;
while (_controllers.length > maximumCachedControllers) {
final oldest = _controllers.keys
.where(
(ids) => !_preparing.containsKey(ids) && (_leases[ids] ?? 0) == 0,
)
.firstOrNull;
if (oldest == null) return;
unawaited(invalidate(oldest).catchError((Object _) {}));
}
}
PreviewController? cached((String, String) ids) => _controllers[ids];
FutureOr<PreviewController?> prepare(
@@ -46,7 +90,12 @@ final class PreviewControllerRegistry {
final pending = _preparing[ids];
if (pending != null) return pending;
final cached = _controllers[ids];
if (cached != null) return cached;
if (cached != null) {
// Map insertion order is the access order for bounded idle entries.
_controllers.remove(ids);
_controllers[ids] = cached;
return cached;
}
final future = _initialize(ids, timeline);
_preparing[ids] = future;
return future;
@@ -82,6 +131,7 @@ final class PreviewControllerRegistry {
rethrow;
} finally {
_preparing.remove(ids);
_trimCache();
}
}
@@ -114,6 +164,7 @@ final class PreviewControllerRegistry {
final controllers = _controllers.values.toList();
_controllers.clear();
_preparing.clear();
_leases.clear();
for (final controller in controllers) {
unawaited(controller.close().catchError((Object _) {}));
}