@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,259 +1,341 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _NeighbourhoodClient_apolloClient, _NeighbourhoodClient_signalHandlers;
13
1
  import { gql } from "@apollo/client/core";
14
2
  import unwrapApolloResult from "../unwrapApolloResult";
3
+ import { isSocketCloseError } from "../utils";
15
4
  export class NeighbourhoodClient {
16
5
  constructor(client) {
17
- _NeighbourhoodClient_apolloClient.set(this, void 0);
18
- _NeighbourhoodClient_signalHandlers.set(this, new Map());
19
- __classPrivateFieldSet(this, _NeighbourhoodClient_apolloClient, client, "f");
6
+ this._signalHandlers = new Map();
7
+ this._signalSubscriptions = new Map();
8
+ this._apolloClient = client;
20
9
  }
21
10
  async publishFromPerspective(perspectiveUUID, linkLanguage, meta) {
22
- const { neighbourhoodPublishFromPerspective } = unwrapApolloResult(await __classPrivateFieldGet(this, _NeighbourhoodClient_apolloClient, "f").mutate({
23
- mutation: gql `mutation neighbourhoodPublishFromPerspective(
24
- $linkLanguage: String!,
25
- $meta: PerspectiveInput!,
26
- $perspectiveUUID: String!
27
- ) {
28
- neighbourhoodPublishFromPerspective(
29
- linkLanguage: $linkLanguage,
30
- meta: $meta,
31
- perspectiveUUID: $perspectiveUUID
32
- )
33
- }`,
34
- variables: { perspectiveUUID, linkLanguage, meta: meta }
11
+ const { neighbourhoodPublishFromPerspective } = unwrapApolloResult(await this._apolloClient.mutate({
12
+ mutation: gql `
13
+ mutation neighbourhoodPublishFromPerspective(
14
+ $linkLanguage: String!
15
+ $meta: PerspectiveInput!
16
+ $perspectiveUUID: String!
17
+ ) {
18
+ neighbourhoodPublishFromPerspective(
19
+ linkLanguage: $linkLanguage
20
+ meta: $meta
21
+ perspectiveUUID: $perspectiveUUID
22
+ )
23
+ }
24
+ `,
25
+ variables: { perspectiveUUID, linkLanguage, meta: meta },
35
26
  }));
36
27
  return neighbourhoodPublishFromPerspective;
37
28
  }
38
29
  async joinFromUrl(url) {
39
- const { neighbourhoodJoinFromUrl } = unwrapApolloResult(await __classPrivateFieldGet(this, _NeighbourhoodClient_apolloClient, "f").mutate({
40
- mutation: gql `mutation neighbourhoodJoinFromUrl($url: String!) {
41
- neighbourhoodJoinFromUrl(url: $url) {
42
- uuid
43
- name
44
- sharedUrl
45
- state
46
- neighbourhood {
47
- data {
48
- linkLanguage
49
- meta {
50
- links
51
- {
52
- author
53
- timestamp
54
- data { source, predicate, target }
55
- proof { valid, invalid, signature, key }
56
- }
57
- }
58
- }
59
- author
30
+ const { neighbourhoodJoinFromUrl } = unwrapApolloResult(await this._apolloClient.mutate({
31
+ mutation: gql `
32
+ mutation neighbourhoodJoinFromUrl($url: String!) {
33
+ neighbourhoodJoinFromUrl(url: $url) {
34
+ uuid
35
+ name
36
+ sharedUrl
37
+ state
38
+ neighbourhood {
39
+ data {
40
+ linkLanguage
41
+ meta {
42
+ links {
43
+ author
44
+ timestamp
45
+ data {
46
+ source
47
+ predicate
48
+ target
49
+ }
50
+ proof {
51
+ valid
52
+ invalid
53
+ signature
54
+ key
55
+ }
60
56
  }
57
+ }
61
58
  }
62
- }`,
63
- variables: { url }
59
+ author
60
+ }
61
+ }
62
+ }
63
+ `,
64
+ variables: { url },
64
65
  }));
65
66
  return neighbourhoodJoinFromUrl;
66
67
  }
67
68
  async otherAgents(perspectiveUUID) {
68
- const { neighbourhoodOtherAgents } = unwrapApolloResult(await __classPrivateFieldGet(this, _NeighbourhoodClient_apolloClient, "f").query({
69
- query: gql `query neighbourhoodOtherAgents($perspectiveUUID: String!) {
70
- neighbourhoodOtherAgents(perspectiveUUID: $perspectiveUUID)
71
- }`,
72
- variables: { perspectiveUUID }
69
+ const { neighbourhoodOtherAgents } = unwrapApolloResult(await this._apolloClient.query({
70
+ query: gql `
71
+ query neighbourhoodOtherAgents($perspectiveUUID: String!) {
72
+ neighbourhoodOtherAgents(perspectiveUUID: $perspectiveUUID)
73
+ }
74
+ `,
75
+ variables: { perspectiveUUID },
73
76
  }));
74
77
  return neighbourhoodOtherAgents;
75
78
  }
76
79
  async hasTelepresenceAdapter(perspectiveUUID) {
77
- const { neighbourhoodHasTelepresenceAdapter } = unwrapApolloResult(await __classPrivateFieldGet(this, _NeighbourhoodClient_apolloClient, "f").query({
78
- query: gql `query neighbourhoodHasTelepresenceAdapter($perspectiveUUID: String!) {
79
- neighbourhoodHasTelepresenceAdapter(perspectiveUUID: $perspectiveUUID)
80
- }`,
81
- variables: { perspectiveUUID }
80
+ const { neighbourhoodHasTelepresenceAdapter } = unwrapApolloResult(await this._apolloClient.query({
81
+ query: gql `
82
+ query neighbourhoodHasTelepresenceAdapter($perspectiveUUID: String!) {
83
+ neighbourhoodHasTelepresenceAdapter(
84
+ perspectiveUUID: $perspectiveUUID
85
+ )
86
+ }
87
+ `,
88
+ variables: { perspectiveUUID },
82
89
  }));
83
90
  return neighbourhoodHasTelepresenceAdapter;
84
91
  }
85
92
  async onlineAgents(perspectiveUUID) {
86
- const { neighbourhoodOnlineAgents } = unwrapApolloResult(await __classPrivateFieldGet(this, _NeighbourhoodClient_apolloClient, "f").query({
87
- query: gql `query neighbourhoodOnlineAgents($perspectiveUUID: String!) {
88
- neighbourhoodOnlineAgents(perspectiveUUID: $perspectiveUUID) {
89
- did
90
- status {
91
- author
92
- timestamp
93
- data {
94
- links {
95
- author
96
- timestamp
97
- data { source, predicate, target }
98
- proof { valid, invalid, signature, key }
99
- }
100
- }
101
- proof { valid, invalid, signature, key }
93
+ const { neighbourhoodOnlineAgents } = unwrapApolloResult(await this._apolloClient.query({
94
+ query: gql `
95
+ query neighbourhoodOnlineAgents($perspectiveUUID: String!) {
96
+ neighbourhoodOnlineAgents(perspectiveUUID: $perspectiveUUID) {
97
+ did
98
+ status {
99
+ author
100
+ timestamp
101
+ data {
102
+ links {
103
+ author
104
+ timestamp
105
+ data {
106
+ source
107
+ predicate
108
+ target
109
+ }
110
+ proof {
111
+ valid
112
+ invalid
113
+ signature
114
+ key
102
115
  }
116
+ }
103
117
  }
104
- }`,
105
- variables: { perspectiveUUID }
118
+ proof {
119
+ valid
120
+ invalid
121
+ signature
122
+ key
123
+ }
124
+ }
125
+ }
126
+ }
127
+ `,
128
+ variables: { perspectiveUUID },
106
129
  }));
107
130
  return neighbourhoodOnlineAgents;
108
131
  }
109
132
  async setOnlineStatus(perspectiveUUID, status) {
110
- const { neighbourhoodSetOnlineStatus } = unwrapApolloResult(await __classPrivateFieldGet(this, _NeighbourhoodClient_apolloClient, "f").mutate({
111
- mutation: gql `mutation neighbourhoodSetOnlineStatus(
112
- $perspectiveUUID: String!,
113
- $status: PerspectiveInput!
114
- ) {
115
- neighbourhoodSetOnlineStatus(
116
- perspectiveUUID: $perspectiveUUID,
117
- status: $status
118
- )
119
- }`,
120
- variables: { perspectiveUUID, status }
133
+ const { neighbourhoodSetOnlineStatus } = unwrapApolloResult(await this._apolloClient.mutate({
134
+ mutation: gql `
135
+ mutation neighbourhoodSetOnlineStatus(
136
+ $perspectiveUUID: String!
137
+ $status: PerspectiveInput!
138
+ ) {
139
+ neighbourhoodSetOnlineStatus(
140
+ perspectiveUUID: $perspectiveUUID
141
+ status: $status
142
+ )
143
+ }
144
+ `,
145
+ variables: { perspectiveUUID, status },
121
146
  }));
122
147
  return neighbourhoodSetOnlineStatus;
123
148
  }
124
149
  async setOnlineStatusU(perspectiveUUID, status) {
125
- const { neighbourhoodSetOnlineStatusU } = unwrapApolloResult(await __classPrivateFieldGet(this, _NeighbourhoodClient_apolloClient, "f").mutate({
126
- mutation: gql `mutation neighbourhoodSetOnlineStatusU(
127
- $perspectiveUUID: String!,
128
- $status: PerspectiveUnsignedInput!
129
- ) {
130
- neighbourhoodSetOnlineStatusU(
131
- perspectiveUUID: $perspectiveUUID,
132
- status: $status
133
- )
134
- }`,
135
- variables: { perspectiveUUID, status }
150
+ const { neighbourhoodSetOnlineStatusU } = unwrapApolloResult(await this._apolloClient.mutate({
151
+ mutation: gql `
152
+ mutation neighbourhoodSetOnlineStatusU(
153
+ $perspectiveUUID: String!
154
+ $status: PerspectiveUnsignedInput!
155
+ ) {
156
+ neighbourhoodSetOnlineStatusU(
157
+ perspectiveUUID: $perspectiveUUID
158
+ status: $status
159
+ )
160
+ }
161
+ `,
162
+ variables: { perspectiveUUID, status },
136
163
  }));
137
164
  return neighbourhoodSetOnlineStatusU;
138
165
  }
139
166
  async sendSignal(perspectiveUUID, remoteAgentDid, payload) {
140
- const { neighbourhoodSendSignal } = unwrapApolloResult(await __classPrivateFieldGet(this, _NeighbourhoodClient_apolloClient, "f").mutate({
141
- mutation: gql `mutation neighbourhoodSendSignal(
142
- $perspectiveUUID: String!,
143
- $remoteAgentDid: String!,
144
- $payload: PerspectiveInput!
145
- ) {
146
- neighbourhoodSendSignal(
147
- perspectiveUUID: $perspectiveUUID,
148
- remoteAgentDid: $remoteAgentDid,
149
- payload: $payload
150
- )
151
- }`,
152
- variables: { perspectiveUUID, remoteAgentDid, payload }
167
+ const { neighbourhoodSendSignal } = unwrapApolloResult(await this._apolloClient.mutate({
168
+ mutation: gql `
169
+ mutation neighbourhoodSendSignal(
170
+ $perspectiveUUID: String!
171
+ $remoteAgentDid: String!
172
+ $payload: PerspectiveInput!
173
+ ) {
174
+ neighbourhoodSendSignal(
175
+ perspectiveUUID: $perspectiveUUID
176
+ remoteAgentDid: $remoteAgentDid
177
+ payload: $payload
178
+ )
179
+ }
180
+ `,
181
+ variables: { perspectiveUUID, remoteAgentDid, payload },
153
182
  }));
154
183
  return neighbourhoodSendSignal;
155
184
  }
156
185
  async sendSignalU(perspectiveUUID, remoteAgentDid, payload) {
157
- const { neighbourhoodSendSignalU } = unwrapApolloResult(await __classPrivateFieldGet(this, _NeighbourhoodClient_apolloClient, "f").mutate({
158
- mutation: gql `mutation neighbourhoodSendSignalU(
159
- $perspectiveUUID: String!,
160
- $remoteAgentDid: String!,
161
- $payload: PerspectiveUnsignedInput!
162
- ) {
163
- neighbourhoodSendSignalU(
164
- perspectiveUUID: $perspectiveUUID,
165
- remoteAgentDid: $remoteAgentDid,
166
- payload: $payload
167
- )
168
- }`,
169
- variables: { perspectiveUUID, remoteAgentDid, payload }
186
+ const { neighbourhoodSendSignalU } = unwrapApolloResult(await this._apolloClient.mutate({
187
+ mutation: gql `
188
+ mutation neighbourhoodSendSignalU(
189
+ $perspectiveUUID: String!
190
+ $remoteAgentDid: String!
191
+ $payload: PerspectiveUnsignedInput!
192
+ ) {
193
+ neighbourhoodSendSignalU(
194
+ perspectiveUUID: $perspectiveUUID
195
+ remoteAgentDid: $remoteAgentDid
196
+ payload: $payload
197
+ )
198
+ }
199
+ `,
200
+ variables: { perspectiveUUID, remoteAgentDid, payload },
170
201
  }));
171
202
  return neighbourhoodSendSignalU;
172
203
  }
173
204
  async sendBroadcast(perspectiveUUID, payload, loopback = false) {
174
- const { neighbourhoodSendBroadcast } = unwrapApolloResult(await __classPrivateFieldGet(this, _NeighbourhoodClient_apolloClient, "f").mutate({
175
- mutation: gql `mutation neighbourhoodSendBroadcast(
176
- $perspectiveUUID: String!,
177
- $payload: PerspectiveInput!,
178
- $loopback: Boolean
179
- ) {
180
- neighbourhoodSendBroadcast(
181
- perspectiveUUID: $perspectiveUUID,
182
- payload: $payload,
183
- loopback: $loopback
184
- )
185
- }`,
186
- variables: { perspectiveUUID, payload, loopback }
205
+ const { neighbourhoodSendBroadcast } = unwrapApolloResult(await this._apolloClient.mutate({
206
+ mutation: gql `
207
+ mutation neighbourhoodSendBroadcast(
208
+ $perspectiveUUID: String!
209
+ $payload: PerspectiveInput!
210
+ $loopback: Boolean
211
+ ) {
212
+ neighbourhoodSendBroadcast(
213
+ perspectiveUUID: $perspectiveUUID
214
+ payload: $payload
215
+ loopback: $loopback
216
+ )
217
+ }
218
+ `,
219
+ variables: { perspectiveUUID, payload, loopback },
187
220
  }));
188
221
  return neighbourhoodSendBroadcast;
189
222
  }
190
223
  async sendBroadcastU(perspectiveUUID, payload, loopback = false) {
191
- const { neighbourhoodSendBroadcastU } = unwrapApolloResult(await __classPrivateFieldGet(this, _NeighbourhoodClient_apolloClient, "f").mutate({
192
- mutation: gql `mutation neighbourhoodSendBroadcastU(
193
- $perspectiveUUID: String!,
194
- $payload: PerspectiveUnsignedInput!,
195
- $loopback: Boolean
196
- ) {
197
- neighbourhoodSendBroadcastU(
198
- perspectiveUUID: $perspectiveUUID,
199
- payload: $payload,
200
- loopback: $loopback
201
- )
202
- }`,
203
- variables: { perspectiveUUID, payload, loopback }
224
+ const { neighbourhoodSendBroadcastU } = unwrapApolloResult(await this._apolloClient.mutate({
225
+ mutation: gql `
226
+ mutation neighbourhoodSendBroadcastU(
227
+ $perspectiveUUID: String!
228
+ $payload: PerspectiveUnsignedInput!
229
+ $loopback: Boolean
230
+ ) {
231
+ neighbourhoodSendBroadcastU(
232
+ perspectiveUUID: $perspectiveUUID
233
+ payload: $payload
234
+ loopback: $loopback
235
+ )
236
+ }
237
+ `,
238
+ variables: { perspectiveUUID, payload, loopback },
204
239
  }));
205
240
  return neighbourhoodSendBroadcastU;
206
241
  }
207
242
  dispatchSignal(perspectiveUUID, signal) {
208
- const handlers = __classPrivateFieldGet(this, _NeighbourhoodClient_signalHandlers, "f").get(perspectiveUUID);
243
+ const handlers = this._signalHandlers.get(perspectiveUUID);
209
244
  if (handlers) {
210
- handlers.forEach(handler => handler(signal));
245
+ for (const handler of handlers) {
246
+ try {
247
+ handler(signal);
248
+ }
249
+ catch (e) {
250
+ console.error("Error in signal handler:", e);
251
+ }
252
+ }
211
253
  }
212
254
  }
213
255
  async subscribeToSignals(perspectiveUUID) {
214
256
  const that = this;
215
- await __classPrivateFieldGet(this, _NeighbourhoodClient_apolloClient, "f").subscribe({
216
- query: gql `subscription neighbourhoodSignal($perspectiveUUID: String!) {
217
- neighbourhoodSignal(perspectiveUUID: $perspectiveUUID) {
218
- author
219
- timestamp
220
- data {
221
- links
222
- {
223
- author
224
- timestamp
225
- data { source, predicate, target }
226
- proof { valid, invalid, signature, key }
227
- }
228
- }
229
- proof { valid, invalid, signature, key }
257
+ const sub = this._apolloClient
258
+ .subscribe({
259
+ query: gql `
260
+ subscription neighbourhoodSignal($perspectiveUUID: String!) {
261
+ neighbourhoodSignal(perspectiveUUID: $perspectiveUUID) {
262
+ author
263
+ timestamp
264
+ data {
265
+ links {
266
+ author
267
+ timestamp
268
+ data {
269
+ source
270
+ predicate
271
+ target
272
+ }
273
+ proof {
274
+ valid
275
+ invalid
276
+ signature
277
+ key
278
+ }
230
279
  }
231
- }`,
232
- variables: { perspectiveUUID }
233
- }).subscribe({
234
- next: (result) => {
235
- const { neighbourhoodSignal } = unwrapApolloResult(result);
236
- that.dispatchSignal(perspectiveUUID, neighbourhoodSignal);
280
+ }
281
+ proof {
282
+ valid
283
+ invalid
284
+ signature
285
+ key
286
+ }
237
287
  }
288
+ }
289
+ `,
290
+ variables: { perspectiveUUID },
291
+ })
292
+ .subscribe({
293
+ next: (result) => {
294
+ try {
295
+ const { neighbourhoodSignal } = unwrapApolloResult(result);
296
+ that.dispatchSignal(perspectiveUUID, neighbourhoodSignal);
297
+ }
298
+ catch (e) {
299
+ console.error("Error in signal subscription:", e);
300
+ }
301
+ },
302
+ error: (e) => {
303
+ if (!isSocketCloseError(e))
304
+ console.error("neighbourhoodSignal subscription error:", e);
305
+ },
238
306
  });
307
+ this._signalSubscriptions.set(perspectiveUUID, sub);
239
308
  }
240
309
  async addSignalHandler(perspectiveUUID, handler) {
241
- let handlersForPerspective = __classPrivateFieldGet(this, _NeighbourhoodClient_signalHandlers, "f").get(perspectiveUUID);
310
+ let handlersForPerspective = this._signalHandlers.get(perspectiveUUID);
242
311
  if (!handlersForPerspective) {
243
312
  handlersForPerspective = [];
244
- __classPrivateFieldGet(this, _NeighbourhoodClient_signalHandlers, "f").set(perspectiveUUID, handlersForPerspective);
313
+ this._signalHandlers.set(perspectiveUUID, handlersForPerspective);
314
+ // Push handler BEFORE subscribing so it's available when signals arrive
315
+ handlersForPerspective.push(handler);
245
316
  await this.subscribeToSignals(perspectiveUUID);
246
317
  }
247
- handlersForPerspective.push(handler);
318
+ else {
319
+ handlersForPerspective.push(handler);
320
+ }
248
321
  }
249
322
  removeSignalHandler(perspectiveUUID, handler) {
250
- const handlersForPerspective = __classPrivateFieldGet(this, _NeighbourhoodClient_signalHandlers, "f").get(perspectiveUUID);
323
+ const handlersForPerspective = this._signalHandlers.get(perspectiveUUID);
251
324
  if (handlersForPerspective) {
252
325
  const index = handlersForPerspective.indexOf(handler);
253
326
  if (index > -1) {
254
327
  handlersForPerspective.splice(index, 1);
255
328
  }
329
+ // Intentionally keep the Apollo subscription and the handlers-map entry alive
330
+ // even when the array becomes empty. This prevents an unnecessary re-subscription
331
+ // if a new handler is added shortly after (the empty array makes addSignalHandler
332
+ // skip subscribeToSignals). For explicit teardown use unsubscribeFromPerspective().
256
333
  }
257
334
  }
335
+ /** Fully tears down the Apollo subscription for a perspective UUID. */
336
+ unsubscribeFromPerspective(perspectiveUUID) {
337
+ this._signalSubscriptions.get(perspectiveUUID)?.unsubscribe();
338
+ this._signalSubscriptions.delete(perspectiveUUID);
339
+ this._signalHandlers.delete(perspectiveUUID);
340
+ }
258
341
  }
259
- _NeighbourhoodClient_apolloClient = new WeakMap(), _NeighbourhoodClient_signalHandlers = new WeakMap();
@@ -3,7 +3,8 @@ import { OnlineAgent } from "../language/Language";
3
3
  import { Perspective, PerspectiveExpression, PerspectiveUnsignedInput } from "../perspectives/Perspective";
4
4
  import { NeighbourhoodClient } from "./NeighbourhoodClient";
5
5
  export declare class NeighbourhoodProxy {
6
- #private;
6
+ private _client;
7
+ private _pID;
7
8
  constructor(client: NeighbourhoodClient, pID: string);
8
9
  otherAgents(): Promise<DID[]>;
9
10
  hasTelepresenceAdapter(): Promise<boolean>;
@@ -1,54 +1,39 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _NeighbourhoodProxy_client, _NeighbourhoodProxy_pID;
13
1
  export class NeighbourhoodProxy {
14
2
  constructor(client, pID) {
15
- _NeighbourhoodProxy_client.set(this, void 0);
16
- _NeighbourhoodProxy_pID.set(this, void 0);
17
- __classPrivateFieldSet(this, _NeighbourhoodProxy_client, client, "f");
18
- __classPrivateFieldSet(this, _NeighbourhoodProxy_pID, pID, "f");
3
+ this._client = client;
4
+ this._pID = pID;
19
5
  }
20
6
  async otherAgents() {
21
- return await __classPrivateFieldGet(this, _NeighbourhoodProxy_client, "f").otherAgents(__classPrivateFieldGet(this, _NeighbourhoodProxy_pID, "f"));
7
+ return await this._client.otherAgents(this._pID);
22
8
  }
23
9
  async hasTelepresenceAdapter() {
24
- return await __classPrivateFieldGet(this, _NeighbourhoodProxy_client, "f").hasTelepresenceAdapter(__classPrivateFieldGet(this, _NeighbourhoodProxy_pID, "f"));
10
+ return await this._client.hasTelepresenceAdapter(this._pID);
25
11
  }
26
12
  async onlineAgents() {
27
- return await __classPrivateFieldGet(this, _NeighbourhoodProxy_client, "f").onlineAgents(__classPrivateFieldGet(this, _NeighbourhoodProxy_pID, "f"));
13
+ return await this._client.onlineAgents(this._pID);
28
14
  }
29
15
  async setOnlineStatus(status) {
30
- return await __classPrivateFieldGet(this, _NeighbourhoodProxy_client, "f").setOnlineStatus(__classPrivateFieldGet(this, _NeighbourhoodProxy_pID, "f"), status);
16
+ return await this._client.setOnlineStatus(this._pID, status);
31
17
  }
32
18
  async setOnlineStatusU(status) {
33
- return await __classPrivateFieldGet(this, _NeighbourhoodProxy_client, "f").setOnlineStatusU(__classPrivateFieldGet(this, _NeighbourhoodProxy_pID, "f"), status);
19
+ return await this._client.setOnlineStatusU(this._pID, status);
34
20
  }
35
21
  async sendSignal(remoteAgentDid, payload) {
36
- return await __classPrivateFieldGet(this, _NeighbourhoodProxy_client, "f").sendSignal(__classPrivateFieldGet(this, _NeighbourhoodProxy_pID, "f"), remoteAgentDid, payload);
22
+ return await this._client.sendSignal(this._pID, remoteAgentDid, payload);
37
23
  }
38
24
  async sendSignalU(remoteAgentDid, payload) {
39
- return await __classPrivateFieldGet(this, _NeighbourhoodProxy_client, "f").sendSignalU(__classPrivateFieldGet(this, _NeighbourhoodProxy_pID, "f"), remoteAgentDid, payload);
25
+ return await this._client.sendSignalU(this._pID, remoteAgentDid, payload);
40
26
  }
41
27
  async sendBroadcast(payload, loopback = false) {
42
- return await __classPrivateFieldGet(this, _NeighbourhoodProxy_client, "f").sendBroadcast(__classPrivateFieldGet(this, _NeighbourhoodProxy_pID, "f"), payload, loopback);
28
+ return await this._client.sendBroadcast(this._pID, payload, loopback);
43
29
  }
44
30
  async sendBroadcastU(payload, loopback = false) {
45
- return await __classPrivateFieldGet(this, _NeighbourhoodProxy_client, "f").sendBroadcastU(__classPrivateFieldGet(this, _NeighbourhoodProxy_pID, "f"), payload, loopback);
31
+ return await this._client.sendBroadcastU(this._pID, payload, loopback);
46
32
  }
47
33
  async addSignalHandler(handler) {
48
- await __classPrivateFieldGet(this, _NeighbourhoodProxy_client, "f").addSignalHandler(__classPrivateFieldGet(this, _NeighbourhoodProxy_pID, "f"), handler);
34
+ await this._client.addSignalHandler(this._pID, handler);
49
35
  }
50
36
  removeSignalHandler(handler) {
51
- __classPrivateFieldGet(this, _NeighbourhoodProxy_client, "f").removeSignalHandler(__classPrivateFieldGet(this, _NeighbourhoodProxy_pID, "f"), handler);
37
+ this._client.removeSignalHandler(this._pID, handler);
52
38
  }
53
39
  }
54
- _NeighbourhoodProxy_client = new WeakMap(), _NeighbourhoodProxy_pID = new WeakMap();
@@ -33,11 +33,7 @@ describe("NeighbourhoodProxy", () => {
33
33
  const subscriptions = [];
34
34
  const subscribe = (args) => {
35
35
  subscriptions.push(args);
36
- new Promise((resolve) => {
37
- setTimeout(() => {
38
- resolve({});
39
- }, 10);
40
- });
36
+ return { unsubscribe: () => { } };
41
37
  };
42
38
  const mockApolloClient = {
43
39
  subscribe: () => ({