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
53 lines
1.2 KiB
Dart
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;
|
|
});
|
|
}
|