@coasys/ad4m 0.12.0-rc1-dev.1 → 0.12.0-rc1-dev.2

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 (128) hide show
  1. package/jest.config.js +4 -4
  2. package/lib/index.cjs +5999 -6046
  3. package/lib/index.js +5977 -6040
  4. package/lib/src/Ad4mClient.d.ts +16 -9
  5. package/lib/src/Ad4mClient.js +25 -46
  6. package/lib/src/Ad4mClient.test.d.ts +1 -0
  7. package/lib/src/Ad4mClient.test.js +1204 -0
  8. package/lib/src/Literal.d.ts +2 -1
  9. package/lib/src/Literal.js +35 -38
  10. package/lib/src/Literal.test.d.ts +1 -0
  11. package/lib/src/Literal.test.js +41 -0
  12. package/lib/src/SmartLiteral.d.ts +2 -1
  13. package/lib/src/SmartLiteral.js +15 -30
  14. package/lib/src/agent/AgentClient.d.ts +4 -1
  15. package/lib/src/agent/AgentClient.js +99 -79
  16. package/lib/src/ai/AIClient.d.ts +2 -1
  17. package/lib/src/ai/AIClient.js +220 -224
  18. package/lib/src/expression/ExpressionClient.d.ts +1 -1
  19. package/lib/src/expression/ExpressionClient.js +86 -73
  20. package/lib/src/index.d.ts +1 -1
  21. package/lib/src/index.js +1 -1
  22. package/lib/src/language/LanguageClient.d.ts +1 -1
  23. package/lib/src/language/LanguageClient.js +52 -52
  24. package/lib/src/model/Ad4mModel.d.ts +157 -937
  25. package/lib/src/model/Ad4mModel.js +212 -2799
  26. package/lib/src/model/Ad4mModel.test.js +1002 -561
  27. package/lib/src/model/decorators.d.ts +94 -426
  28. package/lib/src/model/decorators.js +333 -741
  29. package/lib/src/model/mutation.d.ts +81 -0
  30. package/lib/src/model/mutation.js +317 -0
  31. package/lib/src/model/parentUtils.d.ts +32 -0
  32. package/lib/src/model/parentUtils.js +64 -0
  33. package/lib/src/model/prolog/generatePrologFacts.d.ts +36 -0
  34. package/lib/src/model/prolog/generatePrologFacts.js +139 -0
  35. package/lib/src/model/prolog/index.d.ts +1 -0
  36. package/lib/src/model/prolog/index.js +1 -0
  37. package/lib/src/model/query/ModelQueryBuilder.d.ts +82 -0
  38. package/lib/src/model/query/ModelQueryBuilder.js +115 -0
  39. package/lib/src/model/query/QueryBuilder.d.ts +101 -0
  40. package/lib/src/model/query/QueryBuilder.js +193 -0
  41. package/lib/src/model/query/SurrealQueryBuilder.d.ts +48 -0
  42. package/lib/src/model/query/SurrealQueryBuilder.js +371 -0
  43. package/lib/src/model/query/fetchInstance.d.ts +24 -0
  44. package/lib/src/model/query/fetchInstance.js +144 -0
  45. package/lib/src/model/query/hydration.d.ts +54 -0
  46. package/lib/src/model/query/hydration.js +214 -0
  47. package/lib/src/model/query/operations.d.ts +59 -0
  48. package/lib/src/model/query/operations.js +384 -0
  49. package/lib/src/model/query/snapshot.d.ts +59 -0
  50. package/lib/src/model/query/snapshot.js +106 -0
  51. package/lib/src/model/query/surrealCompiler.d.ts +42 -0
  52. package/lib/src/model/query/surrealCompiler.js +304 -0
  53. package/lib/src/model/schema/fromJSONSchema.d.ts +67 -0
  54. package/lib/src/model/schema/fromJSONSchema.js +264 -0
  55. package/lib/src/model/schema/metadata.d.ts +15 -0
  56. package/lib/src/model/schema/metadata.js +112 -0
  57. package/lib/src/model/subscription.d.ts +47 -0
  58. package/lib/src/model/subscription.js +231 -0
  59. package/lib/src/model/transaction.d.ts +27 -0
  60. package/lib/src/model/transaction.js +32 -0
  61. package/lib/src/model/types.d.ts +195 -0
  62. package/lib/src/model/types.js +8 -0
  63. package/lib/src/model/util.js +7 -7
  64. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +5 -1
  65. package/lib/src/neighbourhood/NeighbourhoodClient.js +263 -181
  66. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +2 -1
  67. package/lib/src/neighbourhood/NeighbourhoodProxy.js +13 -28
  68. package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +1 -5
  69. package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
  70. package/lib/src/perspectives/PerspectiveClient.js +316 -240
  71. package/lib/src/perspectives/PerspectiveProxy.d.ts +54 -92
  72. package/lib/src/perspectives/PerspectiveProxy.js +803 -444
  73. package/lib/src/runtime/RuntimeClient.d.ts +5 -1
  74. package/lib/src/runtime/RuntimeClient.js +412 -238
  75. package/lib/src/shacl/SHACLFlow.test.js +164 -105
  76. package/lib/src/shacl/SHACLShape.d.ts +17 -3
  77. package/lib/src/shacl/SHACLShape.js +127 -90
  78. package/lib/src/shacl/SHACLShape.test.js +244 -176
  79. package/lib/src/utils.d.ts +8 -0
  80. package/lib/src/utils.js +21 -7
  81. package/package.json +8 -13
  82. package/src/Ad4mClient.test.ts +1564 -1315
  83. package/src/Ad4mClient.ts +55 -54
  84. package/src/Literal.test.ts +45 -26
  85. package/src/Literal.ts +82 -66
  86. package/src/SmartLiteral.ts +74 -58
  87. package/src/agent/AgentClient.ts +144 -99
  88. package/src/ai/AIClient.ts +409 -359
  89. package/src/expression/ExpressionClient.ts +142 -96
  90. package/src/index.ts +1 -1
  91. package/src/language/LanguageClient.ts +137 -113
  92. package/src/model/Ad4mModel.test.ts +1128 -647
  93. package/src/model/Ad4mModel.ts +485 -3249
  94. package/src/model/IMPROVEMENTS.md +482 -0
  95. package/src/model/PREDICATE_COLLISION_FIX.md +651 -0
  96. package/src/model/README.md +317 -0
  97. package/src/model/SHACL_MUTATION_DECOUPLING.md +356 -0
  98. package/src/model/SUBSCRIPTION_STRATEGY.md +317 -0
  99. package/src/model/TC39_STAGE_3_DECORATORS.md +609 -0
  100. package/src/model/decorators.ts +689 -1046
  101. package/src/model/mutation.ts +461 -0
  102. package/src/model/parentUtils.ts +91 -0
  103. package/src/model/prolog/generatePrologFacts.ts +176 -0
  104. package/src/model/prolog/index.ts +1 -0
  105. package/src/model/query/ModelQueryBuilder.ts +173 -0
  106. package/src/model/query/fetchInstance.ts +181 -0
  107. package/src/model/query/hydration.ts +273 -0
  108. package/src/model/query/operations.ts +510 -0
  109. package/src/model/query/snapshot.ts +126 -0
  110. package/src/model/query/surrealCompiler.ts +376 -0
  111. package/src/model/schema/fromJSONSchema.ts +411 -0
  112. package/src/model/schema/metadata.ts +148 -0
  113. package/src/model/subscription.ts +307 -0
  114. package/src/model/transaction.ts +60 -0
  115. package/src/model/types.ts +207 -0
  116. package/src/model/util.ts +47 -48
  117. package/src/neighbourhood/NeighbourhoodClient.ts +393 -251
  118. package/src/neighbourhood/NeighbourhoodProxy.test.ts +3 -7
  119. package/src/neighbourhood/NeighbourhoodProxy.ts +58 -40
  120. package/src/perspectives/PerspectiveClient.ts +779 -540
  121. package/src/perspectives/PerspectiveProxy.ts +2860 -2053
  122. package/src/runtime/RuntimeClient.ts +775 -476
  123. package/src/shacl/SHACLFlow.test.ts +196 -129
  124. package/src/shacl/SHACLShape.test.ts +284 -190
  125. package/src/shacl/SHACLShape.ts +225 -172
  126. package/src/utils.ts +31 -17
  127. package/tsconfig.json +9 -10
  128. package/src/model/Subject.ts +0 -159
@@ -1,279 +1,421 @@
1
- import { ApolloClient, ApolloQueryResult, gql } from "@apollo/client/core"
2
- import { Address } from "../Address"
3
- import { DID } from "../DID"
4
- import { OnlineAgent, TelepresenceSignalCallback } from "../language/Language"
5
- import { Perspective, PerspectiveUnsignedInput } from "../perspectives/Perspective"
6
- import { PerspectiveHandle } from "../perspectives/PerspectiveHandle"
7
- import unwrapApolloResult from "../unwrapApolloResult"
8
- import { NeighbourhoodProxy } from "./NeighbourhoodProxy"
1
+ import { ApolloClient, gql, FetchResult } from "@apollo/client/core";
2
+ import { Address } from "../Address";
3
+ import { DID } from "../DID";
4
+ import { OnlineAgent, TelepresenceSignalCallback } from "../language/Language";
5
+ import {
6
+ Perspective,
7
+ PerspectiveUnsignedInput,
8
+ } from "../perspectives/Perspective";
9
+ import { PerspectiveHandle } from "../perspectives/PerspectiveHandle";
10
+ import unwrapApolloResult from "../unwrapApolloResult";
11
+ import { isSocketCloseError } from "../utils";
9
12
 
10
13
  export class NeighbourhoodClient {
11
- #apolloClient: ApolloClient<any>
12
- #signalHandlers: Map<string, TelepresenceSignalCallback[]> = new Map()
14
+ private _apolloClient: ApolloClient<any>;
15
+ private _signalHandlers: Map<string, TelepresenceSignalCallback[]> =
16
+ new Map();
17
+ private _signalSubscriptions: Map<string, { unsubscribe(): void }> =
18
+ new Map();
13
19
 
14
- constructor(client: ApolloClient<any>) {
15
- this.#apolloClient = client
16
- }
20
+ constructor(client: ApolloClient<any>) {
21
+ this._apolloClient = client;
22
+ }
17
23
 
18
- async publishFromPerspective(
19
- perspectiveUUID: string,
20
- linkLanguage: Address,
21
- meta: Perspective
22
- ): Promise<string> {
23
- const { neighbourhoodPublishFromPerspective } = unwrapApolloResult(await this.#apolloClient.mutate({
24
- mutation: gql`mutation neighbourhoodPublishFromPerspective(
25
- $linkLanguage: String!,
26
- $meta: PerspectiveInput!,
27
- $perspectiveUUID: String!
28
- ) {
29
- neighbourhoodPublishFromPerspective(
30
- linkLanguage: $linkLanguage,
31
- meta: $meta,
32
- perspectiveUUID: $perspectiveUUID
33
- )
34
- }`,
35
- variables: { perspectiveUUID, linkLanguage, meta: meta}
36
- }))
37
- return neighbourhoodPublishFromPerspective
38
- }
24
+ async publishFromPerspective(
25
+ perspectiveUUID: string,
26
+ linkLanguage: Address,
27
+ meta: Perspective,
28
+ ): Promise<string> {
29
+ const { neighbourhoodPublishFromPerspective } = unwrapApolloResult(
30
+ await this._apolloClient.mutate({
31
+ mutation: gql`
32
+ mutation neighbourhoodPublishFromPerspective(
33
+ $linkLanguage: String!
34
+ $meta: PerspectiveInput!
35
+ $perspectiveUUID: String!
36
+ ) {
37
+ neighbourhoodPublishFromPerspective(
38
+ linkLanguage: $linkLanguage
39
+ meta: $meta
40
+ perspectiveUUID: $perspectiveUUID
41
+ )
42
+ }
43
+ `,
44
+ variables: { perspectiveUUID, linkLanguage, meta: meta },
45
+ }),
46
+ );
47
+ return neighbourhoodPublishFromPerspective;
48
+ }
39
49
 
40
- async joinFromUrl(url: string): Promise<PerspectiveHandle> {
41
- const { neighbourhoodJoinFromUrl } = unwrapApolloResult(await this.#apolloClient.mutate({
42
- mutation: gql`mutation neighbourhoodJoinFromUrl($url: String!) {
43
- neighbourhoodJoinFromUrl(url: $url) {
44
- uuid
45
- name
46
- sharedUrl
47
- state
48
- neighbourhood {
49
- data {
50
- linkLanguage
51
- meta {
52
- links
53
- {
54
- author
55
- timestamp
56
- data { source, predicate, target }
57
- proof { valid, invalid, signature, key }
58
- }
59
- }
60
- }
61
- author
50
+ async joinFromUrl(url: string): Promise<PerspectiveHandle> {
51
+ const { neighbourhoodJoinFromUrl } = unwrapApolloResult(
52
+ await this._apolloClient.mutate({
53
+ mutation: gql`
54
+ mutation neighbourhoodJoinFromUrl($url: String!) {
55
+ neighbourhoodJoinFromUrl(url: $url) {
56
+ uuid
57
+ name
58
+ sharedUrl
59
+ state
60
+ neighbourhood {
61
+ data {
62
+ linkLanguage
63
+ meta {
64
+ links {
65
+ author
66
+ timestamp
67
+ data {
68
+ source
69
+ predicate
70
+ target
71
+ }
72
+ proof {
73
+ valid
74
+ invalid
75
+ signature
76
+ key
77
+ }
62
78
  }
79
+ }
63
80
  }
64
- }`,
65
- variables: { url }
66
- }))
67
- return neighbourhoodJoinFromUrl
68
- }
81
+ author
82
+ }
83
+ }
84
+ }
85
+ `,
86
+ variables: { url },
87
+ }),
88
+ );
89
+ return neighbourhoodJoinFromUrl;
90
+ }
69
91
 
70
- async otherAgents(perspectiveUUID: string): Promise<DID[]> {
71
- const { neighbourhoodOtherAgents } = unwrapApolloResult(await this.#apolloClient.query({
72
- query: gql`query neighbourhoodOtherAgents($perspectiveUUID: String!) {
73
- neighbourhoodOtherAgents(perspectiveUUID: $perspectiveUUID)
74
- }`,
75
- variables: { perspectiveUUID }
76
- }))
77
- return neighbourhoodOtherAgents
78
- }
92
+ async otherAgents(perspectiveUUID: string): Promise<DID[]> {
93
+ const { neighbourhoodOtherAgents } = unwrapApolloResult(
94
+ await this._apolloClient.query({
95
+ query: gql`
96
+ query neighbourhoodOtherAgents($perspectiveUUID: String!) {
97
+ neighbourhoodOtherAgents(perspectiveUUID: $perspectiveUUID)
98
+ }
99
+ `,
100
+ variables: { perspectiveUUID },
101
+ }),
102
+ );
103
+ return neighbourhoodOtherAgents;
104
+ }
79
105
 
80
- async hasTelepresenceAdapter(perspectiveUUID: string): Promise<boolean> {
81
- const { neighbourhoodHasTelepresenceAdapter } = unwrapApolloResult(await this.#apolloClient.query({
82
- query: gql`query neighbourhoodHasTelepresenceAdapter($perspectiveUUID: String!) {
83
- neighbourhoodHasTelepresenceAdapter(perspectiveUUID: $perspectiveUUID)
84
- }`,
85
- variables: { perspectiveUUID }
86
- }))
87
- return neighbourhoodHasTelepresenceAdapter
88
- }
106
+ async hasTelepresenceAdapter(perspectiveUUID: string): Promise<boolean> {
107
+ const { neighbourhoodHasTelepresenceAdapter } = unwrapApolloResult(
108
+ await this._apolloClient.query({
109
+ query: gql`
110
+ query neighbourhoodHasTelepresenceAdapter($perspectiveUUID: String!) {
111
+ neighbourhoodHasTelepresenceAdapter(
112
+ perspectiveUUID: $perspectiveUUID
113
+ )
114
+ }
115
+ `,
116
+ variables: { perspectiveUUID },
117
+ }),
118
+ );
119
+ return neighbourhoodHasTelepresenceAdapter;
120
+ }
89
121
 
90
- async onlineAgents(perspectiveUUID: string): Promise<OnlineAgent[]> {
91
- const { neighbourhoodOnlineAgents } = unwrapApolloResult(await this.#apolloClient.query({
92
- query: gql`query neighbourhoodOnlineAgents($perspectiveUUID: String!) {
93
- neighbourhoodOnlineAgents(perspectiveUUID: $perspectiveUUID) {
94
- did
95
- status {
96
- author
97
- timestamp
98
- data {
99
- links {
100
- author
101
- timestamp
102
- data { source, predicate, target }
103
- proof { valid, invalid, signature, key }
104
- }
105
- }
106
- proof { valid, invalid, signature, key }
122
+ async onlineAgents(perspectiveUUID: string): Promise<OnlineAgent[]> {
123
+ const { neighbourhoodOnlineAgents } = unwrapApolloResult(
124
+ await this._apolloClient.query({
125
+ query: gql`
126
+ query neighbourhoodOnlineAgents($perspectiveUUID: String!) {
127
+ neighbourhoodOnlineAgents(perspectiveUUID: $perspectiveUUID) {
128
+ did
129
+ status {
130
+ author
131
+ timestamp
132
+ data {
133
+ links {
134
+ author
135
+ timestamp
136
+ data {
137
+ source
138
+ predicate
139
+ target
107
140
  }
141
+ proof {
142
+ valid
143
+ invalid
144
+ signature
145
+ key
146
+ }
147
+ }
108
148
  }
109
- }`,
110
- variables: { perspectiveUUID }
111
- }))
112
- return neighbourhoodOnlineAgents
113
- }
114
-
115
- async setOnlineStatus(perspectiveUUID: string, status: Perspective): Promise<boolean> {
116
- const { neighbourhoodSetOnlineStatus } = unwrapApolloResult(await this.#apolloClient.mutate({
117
- mutation: gql`mutation neighbourhoodSetOnlineStatus(
118
- $perspectiveUUID: String!,
119
- $status: PerspectiveInput!
120
- ) {
121
- neighbourhoodSetOnlineStatus(
122
- perspectiveUUID: $perspectiveUUID,
123
- status: $status
124
- )
125
- }`,
126
- variables: { perspectiveUUID, status }
127
- }))
128
-
129
- return neighbourhoodSetOnlineStatus
130
- }
131
-
132
- async setOnlineStatusU(perspectiveUUID: string, status: PerspectiveUnsignedInput): Promise<boolean> {
133
- const { neighbourhoodSetOnlineStatusU } = unwrapApolloResult(await this.#apolloClient.mutate({
134
- mutation: gql`mutation neighbourhoodSetOnlineStatusU(
135
- $perspectiveUUID: String!,
136
- $status: PerspectiveUnsignedInput!
137
- ) {
138
- neighbourhoodSetOnlineStatusU(
139
- perspectiveUUID: $perspectiveUUID,
140
- status: $status
141
- )
142
- }`,
143
- variables: { perspectiveUUID, status }
144
- }))
145
-
146
- return neighbourhoodSetOnlineStatusU
147
- }
149
+ proof {
150
+ valid
151
+ invalid
152
+ signature
153
+ key
154
+ }
155
+ }
156
+ }
157
+ }
158
+ `,
159
+ variables: { perspectiveUUID },
160
+ }),
161
+ );
162
+ return neighbourhoodOnlineAgents;
163
+ }
148
164
 
149
- async sendSignal(perspectiveUUID: string, remoteAgentDid: string, payload: Perspective): Promise<boolean> {
150
- const { neighbourhoodSendSignal } = unwrapApolloResult(await this.#apolloClient.mutate({
151
- mutation: gql`mutation neighbourhoodSendSignal(
152
- $perspectiveUUID: String!,
153
- $remoteAgentDid: String!,
154
- $payload: PerspectiveInput!
155
- ) {
156
- neighbourhoodSendSignal(
157
- perspectiveUUID: $perspectiveUUID,
158
- remoteAgentDid: $remoteAgentDid,
159
- payload: $payload
160
- )
161
- }`,
162
- variables: { perspectiveUUID, remoteAgentDid, payload }
163
- }))
165
+ async setOnlineStatus(
166
+ perspectiveUUID: string,
167
+ status: Perspective,
168
+ ): Promise<boolean> {
169
+ const { neighbourhoodSetOnlineStatus } = unwrapApolloResult(
170
+ await this._apolloClient.mutate({
171
+ mutation: gql`
172
+ mutation neighbourhoodSetOnlineStatus(
173
+ $perspectiveUUID: String!
174
+ $status: PerspectiveInput!
175
+ ) {
176
+ neighbourhoodSetOnlineStatus(
177
+ perspectiveUUID: $perspectiveUUID
178
+ status: $status
179
+ )
180
+ }
181
+ `,
182
+ variables: { perspectiveUUID, status },
183
+ }),
184
+ );
185
+ return neighbourhoodSetOnlineStatus;
186
+ }
164
187
 
165
- return neighbourhoodSendSignal
166
- }
188
+ async setOnlineStatusU(
189
+ perspectiveUUID: string,
190
+ status: PerspectiveUnsignedInput,
191
+ ): Promise<boolean> {
192
+ const { neighbourhoodSetOnlineStatusU } = unwrapApolloResult(
193
+ await this._apolloClient.mutate({
194
+ mutation: gql`
195
+ mutation neighbourhoodSetOnlineStatusU(
196
+ $perspectiveUUID: String!
197
+ $status: PerspectiveUnsignedInput!
198
+ ) {
199
+ neighbourhoodSetOnlineStatusU(
200
+ perspectiveUUID: $perspectiveUUID
201
+ status: $status
202
+ )
203
+ }
204
+ `,
205
+ variables: { perspectiveUUID, status },
206
+ }),
207
+ );
208
+ return neighbourhoodSetOnlineStatusU;
209
+ }
167
210
 
168
- async sendSignalU(perspectiveUUID: string, remoteAgentDid: string, payload: PerspectiveUnsignedInput): Promise<boolean> {
169
- const { neighbourhoodSendSignalU } = unwrapApolloResult(await this.#apolloClient.mutate({
170
- mutation: gql`mutation neighbourhoodSendSignalU(
171
- $perspectiveUUID: String!,
172
- $remoteAgentDid: String!,
173
- $payload: PerspectiveUnsignedInput!
174
- ) {
175
- neighbourhoodSendSignalU(
176
- perspectiveUUID: $perspectiveUUID,
177
- remoteAgentDid: $remoteAgentDid,
178
- payload: $payload
179
- )
180
- }`,
181
- variables: { perspectiveUUID, remoteAgentDid, payload }
182
- }))
211
+ async sendSignal(
212
+ perspectiveUUID: string,
213
+ remoteAgentDid: string,
214
+ payload: Perspective,
215
+ ): Promise<boolean> {
216
+ const { neighbourhoodSendSignal } = unwrapApolloResult(
217
+ await this._apolloClient.mutate({
218
+ mutation: gql`
219
+ mutation neighbourhoodSendSignal(
220
+ $perspectiveUUID: String!
221
+ $remoteAgentDid: String!
222
+ $payload: PerspectiveInput!
223
+ ) {
224
+ neighbourhoodSendSignal(
225
+ perspectiveUUID: $perspectiveUUID
226
+ remoteAgentDid: $remoteAgentDid
227
+ payload: $payload
228
+ )
229
+ }
230
+ `,
231
+ variables: { perspectiveUUID, remoteAgentDid, payload },
232
+ }),
233
+ );
234
+ return neighbourhoodSendSignal;
235
+ }
183
236
 
184
- return neighbourhoodSendSignalU
185
- }
237
+ async sendSignalU(
238
+ perspectiveUUID: string,
239
+ remoteAgentDid: string,
240
+ payload: PerspectiveUnsignedInput,
241
+ ): Promise<boolean> {
242
+ const { neighbourhoodSendSignalU } = unwrapApolloResult(
243
+ await this._apolloClient.mutate({
244
+ mutation: gql`
245
+ mutation neighbourhoodSendSignalU(
246
+ $perspectiveUUID: String!
247
+ $remoteAgentDid: String!
248
+ $payload: PerspectiveUnsignedInput!
249
+ ) {
250
+ neighbourhoodSendSignalU(
251
+ perspectiveUUID: $perspectiveUUID
252
+ remoteAgentDid: $remoteAgentDid
253
+ payload: $payload
254
+ )
255
+ }
256
+ `,
257
+ variables: { perspectiveUUID, remoteAgentDid, payload },
258
+ }),
259
+ );
260
+ return neighbourhoodSendSignalU;
261
+ }
186
262
 
187
- async sendBroadcast(perspectiveUUID: string, payload: Perspective, loopback: boolean = false): Promise<boolean> {
188
- const { neighbourhoodSendBroadcast } = unwrapApolloResult(await this.#apolloClient.mutate({
189
- mutation: gql`mutation neighbourhoodSendBroadcast(
190
- $perspectiveUUID: String!,
191
- $payload: PerspectiveInput!,
192
- $loopback: Boolean
193
- ) {
194
- neighbourhoodSendBroadcast(
195
- perspectiveUUID: $perspectiveUUID,
196
- payload: $payload,
197
- loopback: $loopback
198
- )
199
- }`,
200
- variables: { perspectiveUUID, payload, loopback }
201
- }))
263
+ async sendBroadcast(
264
+ perspectiveUUID: string,
265
+ payload: Perspective,
266
+ loopback: boolean = false,
267
+ ): Promise<boolean> {
268
+ const { neighbourhoodSendBroadcast } = unwrapApolloResult(
269
+ await this._apolloClient.mutate({
270
+ mutation: gql`
271
+ mutation neighbourhoodSendBroadcast(
272
+ $perspectiveUUID: String!
273
+ $payload: PerspectiveInput!
274
+ $loopback: Boolean
275
+ ) {
276
+ neighbourhoodSendBroadcast(
277
+ perspectiveUUID: $perspectiveUUID
278
+ payload: $payload
279
+ loopback: $loopback
280
+ )
281
+ }
282
+ `,
283
+ variables: { perspectiveUUID, payload, loopback },
284
+ }),
285
+ );
286
+ return neighbourhoodSendBroadcast;
287
+ }
202
288
 
203
- return neighbourhoodSendBroadcast
204
- }
289
+ async sendBroadcastU(
290
+ perspectiveUUID: string,
291
+ payload: PerspectiveUnsignedInput,
292
+ loopback: boolean = false,
293
+ ): Promise<boolean> {
294
+ const { neighbourhoodSendBroadcastU } = unwrapApolloResult(
295
+ await this._apolloClient.mutate({
296
+ mutation: gql`
297
+ mutation neighbourhoodSendBroadcastU(
298
+ $perspectiveUUID: String!
299
+ $payload: PerspectiveUnsignedInput!
300
+ $loopback: Boolean
301
+ ) {
302
+ neighbourhoodSendBroadcastU(
303
+ perspectiveUUID: $perspectiveUUID
304
+ payload: $payload
305
+ loopback: $loopback
306
+ )
307
+ }
308
+ `,
309
+ variables: { perspectiveUUID, payload, loopback },
310
+ }),
311
+ );
312
+ return neighbourhoodSendBroadcastU;
313
+ }
205
314
 
206
- async sendBroadcastU(perspectiveUUID: string, payload: PerspectiveUnsignedInput, loopback: boolean = false): Promise<boolean> {
207
- const { neighbourhoodSendBroadcastU } = unwrapApolloResult(await this.#apolloClient.mutate({
208
- mutation: gql`mutation neighbourhoodSendBroadcastU(
209
- $perspectiveUUID: String!,
210
- $payload: PerspectiveUnsignedInput!,
211
- $loopback: Boolean
212
- ) {
213
- neighbourhoodSendBroadcastU(
214
- perspectiveUUID: $perspectiveUUID,
215
- payload: $payload,
216
- loopback: $loopback
217
- )
218
- }`,
219
- variables: { perspectiveUUID, payload, loopback }
220
- }))
221
-
222
- return neighbourhoodSendBroadcastU
223
- }
224
-
225
- dispatchSignal(perspectiveUUID:string, signal: any) {
226
- const handlers = this.#signalHandlers.get(perspectiveUUID)
227
- if (handlers) {
228
- handlers.forEach(handler => handler(signal))
315
+ dispatchSignal(perspectiveUUID: string, signal: any) {
316
+ const handlers = this._signalHandlers.get(perspectiveUUID);
317
+ if (handlers) {
318
+ for (const handler of handlers) {
319
+ try {
320
+ handler(signal);
321
+ } catch (e) {
322
+ console.error("Error in signal handler:", e);
229
323
  }
324
+ }
230
325
  }
326
+ }
231
327
 
232
- async subscribeToSignals(perspectiveUUID: string): Promise<void> {
233
- const that = this
234
- await this.#apolloClient.subscribe({
235
- query: gql`subscription neighbourhoodSignal($perspectiveUUID: String!) {
236
- neighbourhoodSignal(perspectiveUUID: $perspectiveUUID) {
237
- author
238
- timestamp
239
- data {
240
- links
241
- {
242
- author
243
- timestamp
244
- data { source, predicate, target }
245
- proof { valid, invalid, signature, key }
246
- }
247
- }
248
- proof { valid, invalid, signature, key }
328
+ async subscribeToSignals(perspectiveUUID: string): Promise<void> {
329
+ const that = this;
330
+ const sub = this._apolloClient
331
+ .subscribe({
332
+ query: gql`
333
+ subscription neighbourhoodSignal($perspectiveUUID: String!) {
334
+ neighbourhoodSignal(perspectiveUUID: $perspectiveUUID) {
335
+ author
336
+ timestamp
337
+ data {
338
+ links {
339
+ author
340
+ timestamp
341
+ data {
342
+ source
343
+ predicate
344
+ target
345
+ }
346
+ proof {
347
+ valid
348
+ invalid
349
+ signature
350
+ key
351
+ }
249
352
  }
250
- }`,
251
- variables: { perspectiveUUID }
252
- }).subscribe({
253
- next: (result: ApolloQueryResult<any>) => {
254
- const { neighbourhoodSignal } = unwrapApolloResult(result)
255
- that.dispatchSignal(perspectiveUUID, neighbourhoodSignal)
353
+ }
354
+ proof {
355
+ valid
356
+ invalid
357
+ signature
358
+ key
359
+ }
256
360
  }
257
- })
258
- }
361
+ }
362
+ `,
363
+ variables: { perspectiveUUID },
364
+ })
365
+ .subscribe({
366
+ next: (result: FetchResult<any>) => {
367
+ try {
368
+ const { neighbourhoodSignal } = unwrapApolloResult(result);
369
+ that.dispatchSignal(perspectiveUUID, neighbourhoodSignal);
370
+ } catch (e) {
371
+ console.error("Error in signal subscription:", e);
372
+ }
373
+ },
374
+ error: (e) => {
375
+ if (!isSocketCloseError(e))
376
+ console.error("neighbourhoodSignal subscription error:", e);
377
+ },
378
+ });
379
+ this._signalSubscriptions.set(perspectiveUUID, sub);
380
+ }
259
381
 
260
- async addSignalHandler(perspectiveUUID: string, handler: TelepresenceSignalCallback): Promise<void> {
261
- let handlersForPerspective = this.#signalHandlers.get(perspectiveUUID)
262
- if (!handlersForPerspective) {
263
- handlersForPerspective = []
264
- this.#signalHandlers.set(perspectiveUUID, handlersForPerspective)
265
- await this.subscribeToSignals(perspectiveUUID)
266
- }
267
- handlersForPerspective.push(handler)
382
+ async addSignalHandler(
383
+ perspectiveUUID: string,
384
+ handler: TelepresenceSignalCallback,
385
+ ): Promise<void> {
386
+ let handlersForPerspective = this._signalHandlers.get(perspectiveUUID);
387
+ if (!handlersForPerspective) {
388
+ handlersForPerspective = [];
389
+ this._signalHandlers.set(perspectiveUUID, handlersForPerspective);
390
+ // Push handler BEFORE subscribing so it's available when signals arrive
391
+ handlersForPerspective.push(handler);
392
+ await this.subscribeToSignals(perspectiveUUID);
393
+ } else {
394
+ handlersForPerspective.push(handler);
268
395
  }
396
+ }
269
397
 
270
- removeSignalHandler(perspectiveUUID: string, handler: TelepresenceSignalCallback): void {
271
- const handlersForPerspective = this.#signalHandlers.get(perspectiveUUID)
272
- if (handlersForPerspective) {
273
- const index = handlersForPerspective.indexOf(handler)
274
- if (index > -1) {
275
- handlersForPerspective.splice(index, 1)
276
- }
277
- }
398
+ removeSignalHandler(
399
+ perspectiveUUID: string,
400
+ handler: TelepresenceSignalCallback,
401
+ ): void {
402
+ const handlersForPerspective = this._signalHandlers.get(perspectiveUUID);
403
+ if (handlersForPerspective) {
404
+ const index = handlersForPerspective.indexOf(handler);
405
+ if (index > -1) {
406
+ handlersForPerspective.splice(index, 1);
407
+ }
408
+ // Intentionally keep the Apollo subscription and the handlers-map entry alive
409
+ // even when the array becomes empty. This prevents an unnecessary re-subscription
410
+ // if a new handler is added shortly after (the empty array makes addSignalHandler
411
+ // skip subscribeToSignals). For explicit teardown use unsubscribeFromPerspective().
278
412
  }
279
- }
413
+ }
414
+
415
+ /** Fully tears down the Apollo subscription for a perspective UUID. */
416
+ unsubscribeFromPerspective(perspectiveUUID: string): void {
417
+ this._signalSubscriptions.get(perspectiveUUID)?.unsubscribe();
418
+ this._signalSubscriptions.delete(perspectiveUUID);
419
+ this._signalHandlers.delete(perspectiveUUID);
420
+ }
421
+ }