@hydranium/protocol 1.0.0-next.22 → 1.0.0-next.220

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (246) hide show
  1. package/README.md +39 -3
  2. package/lib/abstract-logger.d.ts +5 -0
  3. package/lib/abstract-logger.d.ts.map +1 -1
  4. package/lib/abstract-logger.js +7 -0
  5. package/lib/abstract-logger.js.map +1 -1
  6. package/lib/client/data-connection.d.ts +157 -0
  7. package/lib/client/data-connection.d.ts.map +1 -0
  8. package/lib/client/data-connection.js +237 -0
  9. package/lib/client/data-connection.js.map +1 -0
  10. package/lib/client/data-events.d.ts +13 -1
  11. package/lib/client/data-events.d.ts.map +1 -1
  12. package/lib/client/data-events.js +21 -0
  13. package/lib/client/data-events.js.map +1 -1
  14. package/lib/client/data-port.d.ts +24 -22
  15. package/lib/client/data-port.d.ts.map +1 -1
  16. package/lib/client/data-session.d.ts +471 -81
  17. package/lib/client/data-session.d.ts.map +1 -1
  18. package/lib/client/data-session.js +738 -108
  19. package/lib/client/data-session.js.map +1 -1
  20. package/lib/client/index.d.ts +14 -9
  21. package/lib/client/index.d.ts.map +1 -1
  22. package/lib/client/index.js +14 -9
  23. package/lib/client/index.js.map +1 -1
  24. package/lib/client/message-relay.d.ts +9 -3
  25. package/lib/client/message-relay.d.ts.map +1 -1
  26. package/lib/client/message-relay.js +11 -5
  27. package/lib/client/message-relay.js.map +1 -1
  28. package/lib/client/post-message-transport.d.ts +64 -3
  29. package/lib/client/post-message-transport.d.ts.map +1 -1
  30. package/lib/client/post-message-transport.js +175 -1
  31. package/lib/client/post-message-transport.js.map +1 -1
  32. package/lib/client/rpc-connection.d.ts +149 -0
  33. package/lib/client/rpc-connection.d.ts.map +1 -0
  34. package/lib/client/rpc-connection.js +202 -0
  35. package/lib/client/rpc-connection.js.map +1 -0
  36. package/lib/client-ids.d.ts +45 -0
  37. package/lib/client-ids.d.ts.map +1 -0
  38. package/lib/client-ids.js +48 -0
  39. package/lib/client-ids.js.map +1 -0
  40. package/lib/clock.d.ts +38 -0
  41. package/lib/clock.d.ts.map +1 -1
  42. package/lib/clock.js +36 -1
  43. package/lib/clock.js.map +1 -1
  44. package/lib/console-logger.d.ts +23 -0
  45. package/lib/console-logger.d.ts.map +1 -0
  46. package/lib/console-logger.js +39 -0
  47. package/lib/console-logger.js.map +1 -0
  48. package/lib/data/data-protocol-methods.d.ts +4 -4
  49. package/lib/data/data-protocol-methods.d.ts.map +1 -1
  50. package/lib/data/data-protocol-methods.js +12 -1
  51. package/lib/data/data-protocol-methods.js.map +1 -1
  52. package/lib/data/data-server-protocol.d.ts +132 -41
  53. package/lib/data/data-server-protocol.d.ts.map +1 -1
  54. package/lib/data/events.d.ts +117 -21
  55. package/lib/data/events.d.ts.map +1 -1
  56. package/lib/data/requests.d.ts +69 -11
  57. package/lib/data/requests.d.ts.map +1 -1
  58. package/lib/debouncer.d.ts.map +1 -1
  59. package/lib/debouncer.js.map +1 -1
  60. package/lib/errors.d.ts +187 -29
  61. package/lib/errors.d.ts.map +1 -1
  62. package/lib/errors.js +270 -29
  63. package/lib/errors.js.map +1 -1
  64. package/lib/glsp-request-model-args.d.ts +16 -0
  65. package/lib/glsp-request-model-args.d.ts.map +1 -0
  66. package/lib/glsp-request-model-args.js +19 -0
  67. package/lib/glsp-request-model-args.js.map +1 -0
  68. package/lib/glsp-save-model-actions.d.ts +50 -0
  69. package/lib/glsp-save-model-actions.d.ts.map +1 -0
  70. package/lib/glsp-save-model-actions.js +28 -0
  71. package/lib/glsp-save-model-actions.js.map +1 -0
  72. package/lib/index.d.ts +7 -0
  73. package/lib/index.d.ts.map +1 -1
  74. package/lib/index.js +10 -0
  75. package/lib/index.js.map +1 -1
  76. package/lib/latency-collector.d.ts +8 -4
  77. package/lib/latency-collector.d.ts.map +1 -1
  78. package/lib/latency-collector.js.map +1 -1
  79. package/lib/logger.d.ts +22 -1
  80. package/lib/logger.d.ts.map +1 -1
  81. package/lib/logger.js +31 -3
  82. package/lib/logger.js.map +1 -1
  83. package/lib/messages/index.d.ts +29 -0
  84. package/lib/messages/index.d.ts.map +1 -0
  85. package/lib/messages/index.js +59 -0
  86. package/lib/messages/index.js.map +1 -0
  87. package/lib/messages/primitives.d.ts +188 -0
  88. package/lib/messages/primitives.d.ts.map +1 -0
  89. package/lib/messages/primitives.js +161 -0
  90. package/lib/messages/primitives.js.map +1 -0
  91. package/lib/model-server.d.ts +60 -13
  92. package/lib/model-server.d.ts.map +1 -1
  93. package/lib/model-server.js +4 -2
  94. package/lib/model-server.js.map +1 -1
  95. package/lib/model-service/base-version.d.ts +64 -0
  96. package/lib/model-service/base-version.d.ts.map +1 -0
  97. package/lib/model-service/base-version.js +43 -0
  98. package/lib/model-service/base-version.js.map +1 -0
  99. package/lib/model-service/index.d.ts +1 -1
  100. package/lib/model-service/index.d.ts.map +1 -1
  101. package/lib/model-service/index.js +4 -5
  102. package/lib/model-service/index.js.map +1 -1
  103. package/lib/model-service/reference-candidate.d.ts +5 -3
  104. package/lib/model-service/reference-candidate.d.ts.map +1 -1
  105. package/lib/{model-service/args.js → node/index.d.ts} +2 -3
  106. package/lib/node/index.d.ts.map +1 -0
  107. package/lib/node/index.js +29 -0
  108. package/lib/node/index.js.map +1 -0
  109. package/lib/node/process-memory.d.ts +66 -0
  110. package/lib/node/process-memory.d.ts.map +1 -0
  111. package/lib/node/process-memory.js +291 -0
  112. package/lib/node/process-memory.js.map +1 -0
  113. package/lib/noop-logger.d.ts.map +1 -1
  114. package/lib/noop-logger.js.map +1 -1
  115. package/lib/observable-value.js.map +1 -1
  116. package/lib/patch-merge.d.ts +35 -32
  117. package/lib/patch-merge.d.ts.map +1 -1
  118. package/lib/patch-merge.js +67 -23
  119. package/lib/patch-merge.js.map +1 -1
  120. package/lib/profile-session.d.ts +8 -4
  121. package/lib/profile-session.d.ts.map +1 -1
  122. package/lib/profile-session.js.map +1 -1
  123. package/lib/random-uuid.d.ts +14 -0
  124. package/lib/random-uuid.d.ts.map +1 -0
  125. package/lib/random-uuid.js +24 -0
  126. package/lib/random-uuid.js.map +1 -0
  127. package/lib/reconcile-write.d.ts +65 -0
  128. package/lib/reconcile-write.d.ts.map +1 -0
  129. package/lib/reconcile-write.js +67 -0
  130. package/lib/reconcile-write.js.map +1 -0
  131. package/lib/rpc/bind-rpc-methods.d.ts +33 -3
  132. package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
  133. package/lib/rpc/bind-rpc-methods.js +32 -3
  134. package/lib/rpc/bind-rpc-methods.js.map +1 -1
  135. package/lib/rpc/create-rpc-proxy.d.ts +10 -0
  136. package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
  137. package/lib/rpc/create-rpc-proxy.js +12 -2
  138. package/lib/rpc/create-rpc-proxy.js.map +1 -1
  139. package/lib/rpc/index.d.ts +1 -0
  140. package/lib/rpc/index.d.ts.map +1 -1
  141. package/lib/rpc/index.js +1 -0
  142. package/lib/rpc/index.js.map +1 -1
  143. package/lib/rpc/send-by-method-name.d.ts +76 -0
  144. package/lib/rpc/send-by-method-name.d.ts.map +1 -0
  145. package/lib/rpc/send-by-method-name.js +120 -0
  146. package/lib/rpc/send-by-method-name.js.map +1 -0
  147. package/lib/rpc/wire-prefix.js.map +1 -1
  148. package/lib/testing/catalogue-audit.d.ts +80 -0
  149. package/lib/testing/catalogue-audit.d.ts.map +1 -0
  150. package/lib/testing/catalogue-audit.js +94 -0
  151. package/lib/testing/catalogue-audit.js.map +1 -0
  152. package/lib/testing/data-doubles.d.ts +39 -15
  153. package/lib/testing/data-doubles.d.ts.map +1 -1
  154. package/lib/testing/data-doubles.js +57 -10
  155. package/lib/testing/data-doubles.js.map +1 -1
  156. package/lib/testing/fake-clock.d.ts +9 -1
  157. package/lib/testing/fake-clock.d.ts.map +1 -1
  158. package/lib/testing/fake-clock.js +54 -45
  159. package/lib/testing/fake-clock.js.map +1 -1
  160. package/lib/testing/index.d.ts +1 -0
  161. package/lib/testing/index.d.ts.map +1 -1
  162. package/lib/testing/index.js +5 -2
  163. package/lib/testing/index.js.map +1 -1
  164. package/lib/testing/node/duplex-connection.d.ts.map +1 -1
  165. package/lib/testing/node/duplex-connection.js +3 -2
  166. package/lib/testing/node/duplex-connection.js.map +1 -1
  167. package/lib/testing/node/duplex-stream.js.map +1 -1
  168. package/lib/testing/node/index.d.ts +1 -0
  169. package/lib/testing/node/index.d.ts.map +1 -1
  170. package/lib/testing/node/index.js +2 -2
  171. package/lib/testing/node/index.js.map +1 -1
  172. package/lib/testing/node/message-port-pair.d.ts +25 -0
  173. package/lib/testing/node/message-port-pair.d.ts.map +1 -0
  174. package/lib/testing/node/message-port-pair.js +26 -0
  175. package/lib/testing/node/message-port-pair.js.map +1 -0
  176. package/lib/testing/wait-for.js.map +1 -1
  177. package/lib/tracer.d.ts.map +1 -1
  178. package/lib/tracer.js.map +1 -1
  179. package/lib/transfer-diagnostic.d.ts +33 -0
  180. package/lib/transfer-diagnostic.d.ts.map +1 -1
  181. package/lib/transfer-diagnostic.js +23 -0
  182. package/lib/transfer-diagnostic.js.map +1 -1
  183. package/lib/transfer-document.d.ts +70 -32
  184. package/lib/transfer-document.d.ts.map +1 -1
  185. package/lib/transfer-document.js +17 -9
  186. package/lib/transfer-document.js.map +1 -1
  187. package/lib/uri.d.ts.map +1 -1
  188. package/lib/uri.js.map +1 -1
  189. package/lib/util.d.ts +8 -0
  190. package/lib/util.d.ts.map +1 -1
  191. package/lib/util.js +32 -0
  192. package/lib/util.js.map +1 -1
  193. package/package.json +29 -37
  194. package/src/abstract-logger.ts +8 -0
  195. package/src/client/data-connection.ts +315 -0
  196. package/src/client/data-events.ts +33 -1
  197. package/src/client/data-port.ts +25 -23
  198. package/src/client/data-session.ts +946 -126
  199. package/src/client/index.ts +14 -9
  200. package/src/client/message-relay.ts +29 -7
  201. package/src/client/post-message-transport.ts +219 -4
  202. package/src/client/rpc-connection.ts +268 -0
  203. package/src/client-ids.ts +49 -0
  204. package/src/clock.ts +56 -0
  205. package/src/console-logger.ts +39 -0
  206. package/src/data/data-protocol-methods.ts +13 -4
  207. package/src/data/data-server-protocol.ts +157 -41
  208. package/src/data/events.ts +123 -21
  209. package/src/data/requests.ts +74 -11
  210. package/src/errors.ts +322 -36
  211. package/src/glsp-request-model-args.ts +16 -0
  212. package/src/glsp-save-model-actions.ts +59 -0
  213. package/src/index.ts +10 -0
  214. package/src/latency-collector.ts +8 -3
  215. package/src/logger.ts +28 -2
  216. package/src/messages/index.ts +36 -0
  217. package/src/messages/primitives.ts +271 -0
  218. package/src/model-server.ts +63 -18
  219. package/src/model-service/base-version.ts +72 -0
  220. package/src/model-service/index.ts +4 -5
  221. package/src/model-service/reference-candidate.ts +5 -3
  222. package/src/node/index.ts +14 -0
  223. package/src/node/process-memory.ts +299 -0
  224. package/src/patch-merge.ts +97 -42
  225. package/src/profile-session.ts +9 -4
  226. package/src/random-uuid.ts +21 -0
  227. package/src/reconcile-write.ts +124 -0
  228. package/src/rpc/README.md +2 -3
  229. package/src/rpc/bind-rpc-methods.ts +59 -4
  230. package/src/rpc/create-rpc-proxy.ts +20 -2
  231. package/src/rpc/index.ts +1 -0
  232. package/src/rpc/send-by-method-name.ts +140 -0
  233. package/src/testing/catalogue-audit.ts +111 -0
  234. package/src/testing/data-doubles.ts +141 -25
  235. package/src/testing/fake-clock.ts +62 -47
  236. package/src/testing/index.ts +5 -2
  237. package/src/testing/node/duplex-connection.ts +3 -2
  238. package/src/testing/node/index.ts +2 -2
  239. package/src/testing/node/message-port-pair.ts +40 -0
  240. package/src/transfer-diagnostic.ts +40 -0
  241. package/src/transfer-document.ts +87 -34
  242. package/src/util.ts +33 -0
  243. package/lib/model-service/args.d.ts +0 -64
  244. package/lib/model-service/args.d.ts.map +0 -1
  245. package/lib/model-service/args.js.map +0 -1
  246. package/src/model-service/args.ts +0 -67
@@ -0,0 +1,315 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2026 CrossBreeze, EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the MIT License which is available in the project root.
6
+ *
7
+ * SPDX-License-Identifier: MIT
8
+ ********************************************************************************/
9
+
10
+ import { Emitter, type Event } from 'vscode-jsonrpc';
11
+ import { FRAMEWORK_CLIENT_IDS } from '../client-ids';
12
+ import {
13
+ DATA_CLIENT_PROTOCOL_METHODS,
14
+ DATA_SERVER_WIRE_PREFIX,
15
+ type DataClientProtocol,
16
+ type DataServerProtocol,
17
+ type DiagnosticOf,
18
+ type ProjectOf,
19
+ type TransferDocumentDirtyChangedEvent,
20
+ type TransferDocumentUpdatedEvent
21
+ } from '../data';
22
+ import { DuplicateClientIdError, ReservedClientIdError } from '../errors';
23
+ import { randomUuid } from '../random-uuid';
24
+ import type { TransferElement } from '../transfer-element';
25
+ import { DataEvents } from './data-events';
26
+ import type { DataPort } from './data-port';
27
+ import { DataSession, type DataSessionFactory } from './data-session';
28
+ import { RpcConnection, type RpcConnectionLifecycle } from './rpc-connection';
29
+
30
+ /** Options for {@link DataConnection}. */
31
+ export interface DataConnectionOptions<
32
+ TTransfer extends TransferElement = TransferElement,
33
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
34
+ > extends RpcConnectionLifecycle {
35
+ /**
36
+ * Wire namespace the server is addressed under. Defaults to the
37
+ * framework's {@link DATA_SERVER_WIRE_PREFIX}, which is what an unmodified
38
+ * `DataServer` binds. Override only alongside the server's own
39
+ * `methodNamespace` option — a mismatch turns every request into
40
+ * "Unhandled method" rather than failing at wire-up.
41
+ */
42
+ readonly methodNamespace?: string;
43
+ /** Builds the sessions `createSession` hands out. Defaults to a plain {@link DataSession}. */
44
+ readonly sessionFactory?: DataSessionFactory<TTransfer, TServer>;
45
+ }
46
+
47
+ /** {@link DataConnectionOptions} for a client that does not speak {@link DataClientProtocol}. */
48
+ export interface DataConnectionOptionsWithMethods<
49
+ TClient extends object,
50
+ TTransfer extends TransferElement = TransferElement,
51
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
52
+ > extends DataConnectionOptions<TTransfer, TServer> {
53
+ /**
54
+ * Method names of the client to bind as inbound handlers. Declare it
55
+ * `as const satisfies ReadonlyArray<keyof YourClient & string>` so the list
56
+ * cannot drift from the interface.
57
+ */
58
+ readonly clientMethods: readonly (keyof TClient & string)[];
59
+ }
60
+
61
+ /**
62
+ * Trailing constructor arguments, required only when the client cannot take
63
+ * the framework's default method list.
64
+ *
65
+ * `bindRpcMethods` throws for a name the target does not implement, so a
66
+ * request/response-only client binding the default list fails at wire-up. The
67
+ * conditional turns that into a compile error.
68
+ */
69
+ export type DataConnectionArgs<
70
+ TTransfer extends TransferElement,
71
+ TClient extends object,
72
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
73
+ > =
74
+ TClient extends DataClientProtocol<TTransfer>
75
+ ? [options?: DataConnectionOptions<TTransfer, TServer> & Partial<DataConnectionOptionsWithMethods<TClient, TTransfer, TServer>>]
76
+ : [options: DataConnectionOptionsWithMethods<TClient, TTransfer, TServer>];
77
+
78
+ /**
79
+ * A {@link RpcConnection} to the data head, carrying as many participants as
80
+ * the host has interested parties.
81
+ *
82
+ * Document operations live on the participants rather than here: they carry a
83
+ * `clientId`, which identifies a participant rather than a wire, and the server
84
+ * keys its opens and watches per `(uri, clientId)`. Two parties sharing one
85
+ * identity cannot tell each other's writes from their own echoes.
86
+ *
87
+ * Generic over the transfer root so this file names no grammar. An adopter
88
+ * binds the concrete root (or the union of them, for a multi-grammar head) at
89
+ * its own edge.
90
+ */
91
+ export class DataConnection<
92
+ TTransfer extends TransferElement,
93
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>,
94
+ TClient extends object = DataClientProtocol<TTransfer>
95
+ > extends RpcConnection<TServer, TClient> {
96
+ protected readonly sessions = new Set<DataSession<TTransfer, TServer>>();
97
+ protected readonly sessionFactory: DataSessionFactory<TTransfer, TServer>;
98
+ protected readonly createSessionEmitter = new Emitter<DataSession<TTransfer, TServer>>();
99
+ /**
100
+ * Fires with each session {@link createSession} starts, before it returns,
101
+ * so a listener sees every session a caller can. A session started before
102
+ * the listener subscribed is in {@link liveSessions}.
103
+ */
104
+ readonly onDidCreateSession: Event<DataSession<TTransfer, TServer>> = this.createSessionEmitter.event;
105
+ /**
106
+ * Per URI, the `dirty` the client was last told since a session of this
107
+ * connection last opened the URI. A restore tells the client its answer
108
+ * only where it differs, so the client hears changes and nothing else; see
109
+ * {@link DataSessionHost.restoreDirty}.
110
+ */
111
+ protected readonly dirtyStates = new Map<string, boolean>();
112
+ /** Backs each session's {@link DataSessionHost.onDidChangeDirty}. */
113
+ protected readonly dirtyChangedEmitter = new Emitter<TransferDocumentDirtyChangedEvent>();
114
+ /** Backs each session's {@link DataSessionHost.onDidUpdateDocument}. */
115
+ protected readonly documentUpdatedEmitter = new Emitter<TransferDocumentUpdatedEvent<TTransfer, DiagnosticOf<TServer>>>();
116
+
117
+ constructor(port: DataPort, client: TClient, ...rest: DataConnectionArgs<TTransfer, TClient, TServer>) {
118
+ const [options = {}] = rest as [
119
+ (DataConnectionOptions<TTransfer, TServer> & Partial<DataConnectionOptionsWithMethods<TClient, TTransfer, TServer>>)?
120
+ ];
121
+ super(port, client, {
122
+ methodNamespace: options.methodNamespace ?? DATA_SERVER_WIRE_PREFIX,
123
+ // The default is reachable only where `TClient` satisfies
124
+ // `DataClientProtocol`, which the constructor's conditional enforces;
125
+ // the compiler cannot carry that through to the generic parameter.
126
+ clientMethods: options.clientMethods ?? (DATA_CLIENT_PROTOCOL_METHODS as unknown as readonly (keyof TClient & string)[]),
127
+ lifecycle: options
128
+ });
129
+ this.sessionFactory =
130
+ options.sessionFactory ?? ((clientId, host, label) => new DataSession<TTransfer, TServer>(clientId, host, label));
131
+ }
132
+
133
+ /**
134
+ * The sessions of this connection that have not ended, in the order they
135
+ * started. A session is listed until its `onDidDispose` fires, or until
136
+ * {@link dispose} clears the list.
137
+ */
138
+ get liveSessions(): readonly DataSession<TTransfer, TServer>[] {
139
+ return [...this.sessions];
140
+ }
141
+
142
+ /**
143
+ * Start a participant on this connection, registered with the server under a
144
+ * fresh id, `label` plus `#` plus a random UUID, or under `clientId` when
145
+ * given. Pass a `label` naming the participant; without one it is
146
+ * `session`. Synchronous: the registration is sent at once, and the
147
+ * session's calls wait for it. The server refuses an id live anywhere in
148
+ * its process, and every call of that session then rejects with an error
149
+ * `isDuplicateClientIdError` recognises; an id the server reserves beyond
150
+ * {@link FRAMEWORK_CLIENT_IDS}, such as the integrity author, with one
151
+ * `isReservedClientIdError` recognises. Only the code crosses the wire, so
152
+ * test with those guards rather than `instanceof`.
153
+ *
154
+ * Throws a {@link ReservedClientIdError} for an id in
155
+ * {@link FRAMEWORK_CLIENT_IDS} — those are authors the SERVER emits rather
156
+ * than participants, so a session holding one would read the framework's
157
+ * own broadcasts as its own echoes and drop them — and a
158
+ * {@link DuplicateClientIdError} for an id a live session on this
159
+ * connection already holds.
160
+ */
161
+ createSession(label = 'session', clientId?: string): DataSession<TTransfer, TServer> {
162
+ this.assertLive();
163
+ const id = clientId ?? `${label}#${randomUuid()}`;
164
+ if (FRAMEWORK_CLIENT_IDS.includes(id)) {
165
+ throw new ReservedClientIdError(id);
166
+ }
167
+ if ([...this.sessions].some(session => session.clientId === id)) {
168
+ throw new DuplicateClientIdError(id);
169
+ }
170
+ const session = this.sessionFactory(
171
+ id,
172
+ {
173
+ connected: () => this.connected(),
174
+ reportError: (error, reported) => this.reportError(error, reported),
175
+ restoreDirty: event => {
176
+ if (this.dirtyStates.get(event.uri) !== (event.text?.dirty ?? false)) {
177
+ this.deliverDirty(event);
178
+ }
179
+ },
180
+ forgetDirty: uri => this.dirtyStates.delete(uri),
181
+ onDidChangeDirty: this.dirtyChangedEmitter.event,
182
+ onDidUpdateDocument: this.documentUpdatedEmitter.event
183
+ },
184
+ label
185
+ );
186
+ this.sessions.add(session);
187
+ // Subscribed before the session is handed out, so the id is free again
188
+ // on this connection by the time any caller's listener runs.
189
+ session.onDidDispose(() => this.sessions.delete(session));
190
+ // A failure reaches the session's own calls, which wait for the same
191
+ // registration; caught here only so it is not also reported unhandled.
192
+ session.connected().catch(() => undefined);
193
+ this.createSessionEmitter.fire(session);
194
+ return session;
195
+ }
196
+
197
+ /**
198
+ * The client, with its `onDocumentDirtyChanged` passing through
199
+ * {@link deliverDirty} first, so {@link dirtyStates} holds what the server
200
+ * told it, and every session hears it through {@link dirtyChangedEmitter};
201
+ * and with its `onDocumentUpdated` heard by every session through
202
+ * {@link documentUpdatedEmitter} first. Every other bound method forwards
203
+ * to the client unchanged, and one the client lacks stays absent, so the
204
+ * binding still refuses it.
205
+ */
206
+ protected override localTarget(): TClient {
207
+ const client = this.client as unknown as Record<string, unknown>;
208
+ const hearsDirty = typeof client.onDocumentDirtyChanged === 'function';
209
+ const hearsUpdates = typeof client.onDocumentUpdated === 'function';
210
+ if (!hearsDirty && !hearsUpdates) {
211
+ return this.client;
212
+ }
213
+ const target: Record<string, unknown> = {};
214
+ for (const name of this.clientMethods) {
215
+ const method: unknown = client[name];
216
+ if (typeof method === 'function') {
217
+ target[name] = (params: unknown): unknown => (method as (params: unknown) => unknown).call(client, params);
218
+ }
219
+ }
220
+ if (hearsDirty) {
221
+ target.onDocumentDirtyChanged = (event: TransferDocumentDirtyChangedEvent): void => {
222
+ this.dirtyChangedEmitter.fire(event);
223
+ this.deliverDirty(event);
224
+ };
225
+ }
226
+ if (hearsUpdates) {
227
+ const forward = target.onDocumentUpdated as (event: TransferDocumentUpdatedEvent<TTransfer, DiagnosticOf<TServer>>) => void;
228
+ target.onDocumentUpdated = (event: TransferDocumentUpdatedEvent<TTransfer, DiagnosticOf<TServer>>): void => {
229
+ this.documentUpdatedEmitter.fire(event);
230
+ forward(event);
231
+ };
232
+ }
233
+ return target as unknown as TClient;
234
+ }
235
+
236
+ /** Record `event` in {@link dirtyStates} and hand it to the client. */
237
+ protected deliverDirty(event: TransferDocumentDirtyChangedEvent): void {
238
+ this.dirtyStates.set(event.uri, event.text?.dirty ?? false);
239
+ const client = this.client as unknown as Partial<Pick<DataClientProtocol<TTransfer>, 'onDocumentDirtyChanged'>>;
240
+ client.onDocumentDirtyChanged?.(event);
241
+ }
242
+
243
+ /**
244
+ * After the transport dropped, reconnect on the next macrotask for the
245
+ * sessions with documents open, so they re-watch and follow their documents
246
+ * again without waiting for a call of their own; a session with nothing
247
+ * open restores on its next call. Nothing is scheduled once this connection
248
+ * is disposed, which drops its generation too.
249
+ */
250
+ protected override dropGeneration(): void {
251
+ const dropped = this.generation !== undefined;
252
+ super.dropGeneration();
253
+ if (dropped && !this.disposed) {
254
+ setTimeout(() => {
255
+ if (!this.disposed) {
256
+ this.sessions.forEach(session => session.reconnect());
257
+ }
258
+ }, 0);
259
+ }
260
+ }
261
+
262
+ /**
263
+ * Sessions are detached rather than disposed: the server ends every session
264
+ * on a connection it sees close, so ending each one first sends requests
265
+ * over a connection this call is about to dispose.
266
+ */
267
+ override dispose(): void {
268
+ for (const session of [...this.sessions]) {
269
+ session.detach();
270
+ }
271
+ // For a factory's session whose `detach` does not fire.
272
+ this.sessions.clear();
273
+ this.createSessionEmitter.dispose();
274
+ this.dirtyChangedEmitter.dispose();
275
+ this.documentUpdatedEmitter.dispose();
276
+ super.dispose();
277
+ }
278
+ }
279
+
280
+ /**
281
+ * A {@link DataConnection} that brings its own {@link DataEvents}, so a host
282
+ * with several interested parties does not have to supply one.
283
+ *
284
+ * **The client slot holds exactly one object, and that is why this exists.**
285
+ * `createRpcProxy` binds a single `localTarget`, and underneath a method name
286
+ * maps to one handler — a second registration replaces the first silently. So a
287
+ * properties panel and a tree cannot both be the client; one fan-out sits in the
288
+ * slot and both subscribe to it.
289
+ *
290
+ * Use {@link DataConnection} directly instead when the client is yours: an
291
+ * adopter service that implements the protocol plus its own methods, a single
292
+ * consumer that IS the client, or a request/response-only client that binds
293
+ * nothing.
294
+ */
295
+ export class DataConnectionWithEvents<
296
+ TTransfer extends TransferElement,
297
+ TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
298
+ > extends DataConnection<TTransfer, TServer, DataEvents<TTransfer, DiagnosticOf<TServer>, ProjectOf<TServer>>> {
299
+ /** Server pushes, fanned out to as many local listeners as the host has. */
300
+ readonly events: DataEvents<TTransfer, DiagnosticOf<TServer>, ProjectOf<TServer>>;
301
+
302
+ constructor(port: DataPort, options?: DataConnectionOptions<TTransfer, TServer>) {
303
+ // Built as a local because `this` is unavailable before `super`, then
304
+ // read back onto the field.
305
+ const events = new DataEvents<TTransfer, DiagnosticOf<TServer>, ProjectOf<TServer>>();
306
+ super(port, events, options);
307
+ this.events = events;
308
+ }
309
+
310
+ /** Disposes the fan-out it created, which no caller else holds. */
311
+ override dispose(): void {
312
+ super.dispose();
313
+ this.events.dispose();
314
+ }
315
+ }
@@ -8,7 +8,15 @@
8
8
  ********************************************************************************/
9
9
 
10
10
  import { Emitter, type Event } from 'vscode-jsonrpc';
11
- import type { DataClientProtocol, ProjectsChangedEvent, TransferDocumentSavedEvent, TransferDocumentUpdatedEvent } from '../data';
11
+ import type {
12
+ DataClientProtocol,
13
+ ProjectsChangedEvent,
14
+ TransferDocumentDeletedEvent,
15
+ TransferDocumentDirtyChangedEvent,
16
+ TransferDocumentSavedEvent,
17
+ TransferDocumentsBuiltEvent,
18
+ TransferDocumentUpdatedEvent
19
+ } from '../data';
12
20
  import type { Project } from '../project';
13
21
  import type { TransferDiagnostic } from '../transfer-diagnostic';
14
22
  import type { TransferElement } from '../transfer-element';
@@ -40,12 +48,21 @@ export class DataEvents<
40
48
  > implements DataClientProtocol<TTransfer, TDiagnostic, TProject> {
41
49
  protected readonly documentUpdatedEmitter = new Emitter<TransferDocumentUpdatedEvent<TTransfer, TDiagnostic>>();
42
50
  protected readonly documentSavedEmitter = new Emitter<TransferDocumentSavedEvent<TTransfer, TDiagnostic>>();
51
+ protected readonly documentDirtyChangedEmitter = new Emitter<TransferDocumentDirtyChangedEvent>();
52
+ protected readonly documentDeletedEmitter = new Emitter<TransferDocumentDeletedEvent>();
53
+ protected readonly documentsBuiltEmitter = new Emitter<TransferDocumentsBuiltEvent>();
43
54
  protected readonly projectsChangedEmitter = new Emitter<ProjectsChangedEvent<TProject>>();
44
55
 
45
56
  /** A build-phase event for a watched document. Carries the originating `sourceClientId`. */
46
57
  readonly onDidUpdateDocument: Event<TransferDocumentUpdatedEvent<TTransfer, TDiagnostic>> = this.documentUpdatedEmitter.event;
47
58
  /** A watched document was persisted to disk. */
48
59
  readonly onDidSaveDocument: Event<TransferDocumentSavedEvent<TTransfer, TDiagnostic>> = this.documentSavedEmitter.event;
60
+ /** A watched document's text started or stopped differing from its file. */
61
+ readonly onDidChangeDocumentDirty: Event<TransferDocumentDirtyChangedEvent> = this.documentDirtyChangedEmitter.event;
62
+ /** A document's backing file was removed, watched or not. Any watch survives. */
63
+ readonly onDidDeleteDocument: Event<TransferDocumentDeletedEvent> = this.documentDeletedEmitter.event;
64
+ /** Documents built that nobody watches — re-read anything derived from them. */
65
+ readonly onDidBuildDocuments: Event<TransferDocumentsBuiltEvent> = this.documentsBuiltEmitter.event;
49
66
  /** The project set changed. */
50
67
  readonly onDidChangeProjects: Event<ProjectsChangedEvent<TProject>> = this.projectsChangedEmitter.event;
51
68
 
@@ -59,6 +76,18 @@ export class DataEvents<
59
76
  this.documentSavedEmitter.fire(event);
60
77
  }
61
78
 
79
+ onDocumentDirtyChanged(event: TransferDocumentDirtyChangedEvent): void {
80
+ this.documentDirtyChangedEmitter.fire(event);
81
+ }
82
+
83
+ onDocumentDeleted(event: TransferDocumentDeletedEvent): void {
84
+ this.documentDeletedEmitter.fire(event);
85
+ }
86
+
87
+ onDocumentsBuilt(event: TransferDocumentsBuiltEvent): void {
88
+ this.documentsBuiltEmitter.fire(event);
89
+ }
90
+
62
91
  onProjectsChanged(event: ProjectsChangedEvent<TProject>): void {
63
92
  this.projectsChangedEmitter.fire(event);
64
93
  }
@@ -66,6 +95,9 @@ export class DataEvents<
66
95
  dispose(): void {
67
96
  this.documentUpdatedEmitter.dispose();
68
97
  this.documentSavedEmitter.dispose();
98
+ this.documentDirtyChangedEmitter.dispose();
99
+ this.documentDeletedEmitter.dispose();
100
+ this.documentsBuiltEmitter.dispose();
69
101
  this.projectsChangedEmitter.dispose();
70
102
  }
71
103
  }
@@ -8,15 +8,20 @@
8
8
  ********************************************************************************/
9
9
 
10
10
  import type { Event, MessageConnection } from 'vscode-jsonrpc';
11
+ import type { ResolvedMessage } from '../messages/primitives';
12
+ import type { RpcConnectionLifecycle } from './rpc-connection';
11
13
 
12
14
  /**
13
15
  * The one thing a host has to supply for the data head: a live JSON-RPC
14
- * connection to the data server, plus the identity and failure sink that go
15
- * with it.
16
+ * connection to the data server, plus the failure sink that goes with it.
16
17
  *
17
- * "Port" in the hexagonal sense — the host implements it, `DataSession`
18
+ * "Port" in the hexagonal sense — the host implements it, `DataConnection`
18
19
  * consumes it, and nothing on either side of the boundary imports the other.
19
20
  *
21
+ * Carries no identity. A `clientId` names a participant, and one transport
22
+ * serves as many as the host has; binding an identity here is what makes two
23
+ * of them share one, so it lives on `DataSession` instead.
24
+ *
20
25
  * **This deliberately does NOT wrap the protocol methods.** `createRpcProxy`
21
26
  * already takes a promise of a connection and produces the whole typed
22
27
  * `DataServerProtocol` surface, so wrapping it would re-derive the framework's
@@ -24,7 +29,7 @@ import type { Event, MessageConnection } from 'vscode-jsonrpc';
24
29
  * method allowlists, which cannot drift. Everything a form or a tree actually
25
30
  * does — the wire contract, the open/watch/update/close sequence, `baseVersion`
26
31
  * conflict handling, echo filtering by `sourceClientId` — is host-invariant and
27
- * lives above this interface. What varies between hosts is exactly the four
32
+ * lives above this interface. What varies between hosts is exactly the
28
33
  * members below.
29
34
  *
30
35
  * **Why the transport hop and not merely the protocol.** In a Theia frontend
@@ -45,22 +50,6 @@ import type { Event, MessageConnection } from 'vscode-jsonrpc';
45
50
  * calls on the promise but never calls `listen` itself.
46
51
  */
47
52
  export interface DataPort {
48
- /**
49
- * Stable identity of this client on the data server, passed as `clientId`
50
- * on every document request.
51
- *
52
- * It has to be stable for the session because it is the echo key: an
53
- * inbound `onDocumentUpdated` carries the originating mutation's
54
- * `clientId` as `sourceClientId`, and a client that cannot recognise its
55
- * own echo treats its own write as a concurrent third-party one. It also
56
- * has to be distinct per client, since it keys the server's per-
57
- * `(uri, clientId)` watch bucket.
58
- *
59
- * Avoid the three values the framework itself uses as sentinels —
60
- * `'language-client'`, `'unknown'` and `'revert-on-close'`.
61
- */
62
- readonly clientId: string;
63
-
64
53
  /**
65
54
  * Open the transport and hand back a listening `MessageConnection`.
66
55
  *
@@ -77,10 +66,16 @@ export interface DataPort {
77
66
  *
78
67
  * It exists because the alternative is worse in both directions: this tier
79
68
  * cannot import a host's UI, and swallowing the error makes a dead
80
- * connection look like an empty model. `context` names what was being
81
- * attempted, not where in the code it happened.
69
+ * connection look like an empty model.
70
+ *
71
+ * `reported` is a complete sentence plus the identity needed to render it in
72
+ * another language. It carries a value rather than using a protocol field
73
+ * because this tier does not know whether a process hop intervenes — in a
74
+ * webview host the render happens across one — and a `ResolvedMessage` is
75
+ * structured-clone safe either way. Render it with `renderFrameworkMessage`;
76
+ * passing no translation map yields the English.
82
77
  */
83
- reportError(error: unknown, context: string): void;
78
+ reportError(error: unknown, reported: ResolvedMessage): void;
84
79
 
85
80
  /**
86
81
  * Fires when the host tears the transport down and the current connection
@@ -96,4 +91,11 @@ export interface DataPort {
96
91
  * `Disposable` on the *wire* would not be.
97
92
  */
98
93
  readonly onDispose: Event<void>;
94
+
95
+ /**
96
+ * Called for each connection generation, beside the lifecycle the
97
+ * connection's options pass, so a host reporting its connections itself
98
+ * does so without every connection built over it passing this on.
99
+ */
100
+ readonly connectionLifecycle?: RpcConnectionLifecycle;
99
101
  }