Files
cutdex/lib/integrations/mcp/mcp_endpoint_controller.dart
T
lingjhf 38ca180cf7
Gitea CI / Check code and tests (pull_request) Failing after 8s
Gitea CI / Build Android APK (pull_request) Skipped
Gitea CI / Build Windows x64 app (pull_request) Skipped
Gitea CI / Audit and package Windows x64 (pull_request) Skipped
Gitea CI / Assemble and validate CI artifacts (pull_request) Skipped
Gitea CI / Gitea CI passed (pull_request) Failing after 1s
refactor(core): own editor UI and pin verified native releases
2026-10-12 03:20:29 +08:00

53 lines
1.2 KiB
Dart

import 'dart:async';
import 'package:cutdex_core/mcp.dart';
import 'local_mcp_bridge.dart';
/// Serializes endpoint replacement so late cleanup cannot close its successor.
class EditorMcpEndpoint {
Future<void> _tail = Future.value();
LocalMcpBridge? _current;
bool _closed = false;
Future<T> _run<T>(Future<T> Function() action) {
final result = _tail.then((_) => action());
_tail = result.then<void>((_) {}, onError: (Object _, StackTrace _) {});
return result;
}
Future<LocalMcpBridge> open(
CutdexMcpServer server, {
required int port,
required String token,
}) => _run(() async {
if (_closed) throw StateError('MCP endpoint owner is closed');
await _current?.close();
_current = null;
return _current = await LocalMcpBridge.start(
server,
port: port,
token: token,
);
});
Future<void> release(LocalMcpBridge bridge) => _run(() async {
if (!identical(_current, bridge)) return;
await bridge.close();
_current = null;
});
Future<void> close() {
_closed = true;
return _run(() async {
await _current?.close();
_current = null;
});
}
Future<void> stop() => _run(() async {
await _current?.close();
_current = null;
});
}