@coasys/ad4m 0.8.0-fix → 0.8.1

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 (161) hide show
  1. package/README.md +229 -229
  2. package/jest.config.js +5 -5
  3. package/jsdoc.json +27 -27
  4. package/lib/index.cjs +4522 -4571
  5. package/lib/index.js +4522 -4571
  6. package/lib/shims/type-graphql.d.ts +32 -32
  7. package/lib/shims/type-graphql.js +70 -70
  8. package/lib/src/Ad4mClient.d.ts +26 -26
  9. package/lib/src/Ad4mClient.js +66 -66
  10. package/lib/src/Address.d.ts +1 -1
  11. package/lib/src/Address.js +1 -1
  12. package/lib/src/DID.d.ts +1 -1
  13. package/lib/src/DID.js +1 -1
  14. package/lib/src/Exception.d.ts +6 -6
  15. package/lib/src/Exception.js +7 -7
  16. package/lib/src/Literal.d.ts +7 -7
  17. package/lib/src/Literal.js +76 -76
  18. package/lib/src/PubSub.d.ts +14 -14
  19. package/lib/src/PubSub.js +14 -14
  20. package/lib/src/SmartLiteral.d.ts +12 -12
  21. package/lib/src/SmartLiteral.js +74 -74
  22. package/lib/src/agent/Agent.d.ts +104 -104
  23. package/lib/src/agent/Agent.js +337 -337
  24. package/lib/src/agent/AgentClient.d.ts +57 -57
  25. package/lib/src/agent/AgentClient.js +431 -431
  26. package/lib/src/agent/AgentResolver.d.ts +30 -30
  27. package/lib/src/agent/AgentResolver.js +325 -325
  28. package/lib/src/agent/AgentStatus.d.ts +8 -8
  29. package/lib/src/agent/AgentStatus.js +61 -61
  30. package/lib/src/buildSchema.d.ts +1 -1
  31. package/lib/src/buildSchema.js +36 -36
  32. package/lib/src/expression/Expression.d.ts +28 -28
  33. package/lib/src/expression/Expression.js +143 -143
  34. package/lib/src/expression/ExpressionClient.d.ts +13 -13
  35. package/lib/src/expression/ExpressionClient.js +102 -102
  36. package/lib/src/expression/ExpressionRef.d.ts +9 -9
  37. package/lib/src/expression/ExpressionRef.js +67 -67
  38. package/lib/src/expression/ExpressionResolver.d.ts +10 -10
  39. package/lib/src/expression/ExpressionResolver.js +106 -106
  40. package/lib/src/index.d.ts +30 -30
  41. package/lib/src/index.js +30 -30
  42. package/lib/src/jestSetup.d.ts +1 -1
  43. package/lib/src/jestSetup.js +1 -1
  44. package/lib/src/language/Icon.d.ts +4 -4
  45. package/lib/src/language/Icon.js +24 -24
  46. package/lib/src/language/Language.d.ts +195 -195
  47. package/lib/src/language/Language.js +79 -79
  48. package/lib/src/language/LanguageClient.d.ts +17 -17
  49. package/lib/src/language/LanguageClient.js +137 -137
  50. package/lib/src/language/LanguageContext.d.ts +37 -37
  51. package/lib/src/language/LanguageContext.js +2 -2
  52. package/lib/src/language/LanguageHandle.d.ts +9 -9
  53. package/lib/src/language/LanguageHandle.js +41 -41
  54. package/lib/src/language/LanguageMeta.d.ts +35 -35
  55. package/lib/src/language/LanguageMeta.js +89 -89
  56. package/lib/src/language/LanguageRef.d.ts +6 -6
  57. package/lib/src/language/LanguageRef.js +30 -30
  58. package/lib/src/language/LanguageResolver.d.ts +19 -19
  59. package/lib/src/language/LanguageResolver.js +143 -143
  60. package/lib/src/links/Links.d.ts +39 -39
  61. package/lib/src/links/Links.js +148 -148
  62. package/lib/src/links/Links.test.d.ts +1 -1
  63. package/lib/src/links/Links.test.js +33 -33
  64. package/lib/src/neighbourhood/Neighbourhood.d.ts +10 -10
  65. package/lib/src/neighbourhood/Neighbourhood.js +38 -38
  66. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +25 -25
  67. package/lib/src/neighbourhood/NeighbourhoodClient.js +256 -256
  68. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +19 -19
  69. package/lib/src/neighbourhood/NeighbourhoodProxy.js +54 -54
  70. package/lib/src/neighbourhood/NeighbourhoodResolver.d.ts +24 -24
  71. package/lib/src/neighbourhood/NeighbourhoodResolver.js +180 -180
  72. package/lib/src/perspectives/LinkQuery.d.ts +11 -11
  73. package/lib/src/perspectives/LinkQuery.js +80 -80
  74. package/lib/src/perspectives/Perspective.d.ts +38 -38
  75. package/lib/src/perspectives/Perspective.js +144 -144
  76. package/lib/src/perspectives/Perspective.test.d.ts +1 -1
  77. package/lib/src/perspectives/Perspective.test.js +20 -20
  78. package/lib/src/perspectives/PerspectiveClient.d.ts +55 -54
  79. package/lib/src/perspectives/PerspectiveClient.js +418 -410
  80. package/lib/src/perspectives/PerspectiveDiff.d.ts +9 -9
  81. package/lib/src/perspectives/PerspectiveDiff.js +33 -33
  82. package/lib/src/perspectives/PerspectiveHandle.d.ts +16 -16
  83. package/lib/src/perspectives/PerspectiveHandle.js +59 -59
  84. package/lib/src/perspectives/PerspectiveProxy.d.ts +178 -178
  85. package/lib/src/perspectives/PerspectiveProxy.js +501 -540
  86. package/lib/src/perspectives/PerspectiveResolver.d.ts +38 -37
  87. package/lib/src/perspectives/PerspectiveResolver.js +376 -362
  88. package/lib/src/runtime/RuntimeClient.d.ts +33 -33
  89. package/lib/src/runtime/RuntimeClient.js +263 -263
  90. package/lib/src/runtime/RuntimeResolver.d.ts +47 -47
  91. package/lib/src/runtime/RuntimeResolver.js +328 -328
  92. package/lib/src/schema.gql +1 -0
  93. package/lib/src/subject/SDNADecorators.d.ts +45 -45
  94. package/lib/src/subject/SDNADecorators.js +250 -250
  95. package/lib/src/subject/Subject.d.ts +7 -7
  96. package/lib/src/subject/Subject.js +160 -160
  97. package/lib/src/subject/SubjectEntity.d.ts +31 -31
  98. package/lib/src/subject/SubjectEntity.js +250 -250
  99. package/lib/src/subject/util.d.ts +12 -12
  100. package/lib/src/subject/util.js +73 -73
  101. package/lib/src/typeDefs.d.ts +1 -1
  102. package/lib/src/typeDefs.js +1 -0
  103. package/lib/src/unwrapApolloResult.d.ts +2 -2
  104. package/lib/src/unwrapApolloResult.js +14 -12
  105. package/lib/src/utils.d.ts +2 -2
  106. package/lib/src/utils.js +21 -21
  107. package/package.json +61 -61
  108. package/rollup.config.js +26 -26
  109. package/shims/type-graphql.ts +75 -75
  110. package/src/Ad4mClient.test.ts +1089 -1084
  111. package/src/Ad4mClient.ts +62 -62
  112. package/src/Address.ts +1 -1
  113. package/src/Exception.ts +6 -6
  114. package/src/Literal.test.ts +30 -30
  115. package/src/Literal.ts +80 -80
  116. package/src/PubSub.ts +13 -13
  117. package/src/SmartLiteral.ts +74 -74
  118. package/src/agent/Agent.ts +293 -293
  119. package/src/agent/AgentClient.ts +531 -531
  120. package/src/agent/AgentResolver.ts +263 -263
  121. package/src/agent/AgentStatus.ts +43 -43
  122. package/src/buildSchema.ts +41 -41
  123. package/src/expression/Expression.ts +99 -99
  124. package/src/expression/ExpressionClient.ts +98 -98
  125. package/src/expression/ExpressionRef.ts +62 -62
  126. package/src/expression/ExpressionResolver.ts +63 -63
  127. package/src/index.ts +30 -30
  128. package/src/language/Icon.ts +10 -10
  129. package/src/language/Language.ts +266 -266
  130. package/src/language/LanguageClient.ts +159 -159
  131. package/src/language/LanguageContext.ts +37 -37
  132. package/src/language/LanguageHandle.ts +23 -23
  133. package/src/language/LanguageMeta.ts +69 -69
  134. package/src/language/LanguageRef.ts +18 -18
  135. package/src/language/LanguageResolver.ts +98 -98
  136. package/src/links/Links.test.ts +39 -39
  137. package/src/links/Links.ts +107 -107
  138. package/src/neighbourhood/Neighbourhood.ts +22 -22
  139. package/src/neighbourhood/NeighbourhoodClient.ts +275 -275
  140. package/src/neighbourhood/NeighbourhoodProxy.ts +58 -58
  141. package/src/neighbourhood/NeighbourhoodResolver.ts +99 -99
  142. package/src/perspectives/LinkQuery.ts +65 -65
  143. package/src/perspectives/Perspective.test.ts +22 -22
  144. package/src/perspectives/Perspective.ts +118 -118
  145. package/src/perspectives/PerspectiveClient.ts +452 -443
  146. package/src/perspectives/PerspectiveDiff.ts +15 -15
  147. package/src/perspectives/PerspectiveHandle.ts +39 -39
  148. package/src/perspectives/PerspectiveProxy.ts +576 -610
  149. package/src/perspectives/PerspectiveResolver.ts +224 -219
  150. package/src/runtime/RuntimeClient.ts +282 -282
  151. package/src/runtime/RuntimeResolver.ts +192 -192
  152. package/src/subject/SDNADecorators.ts +333 -333
  153. package/src/subject/Subject.ts +152 -152
  154. package/src/subject/SubjectEntity.ts +291 -291
  155. package/src/subject/util.ts +84 -84
  156. package/src/typeDefs.ts +5 -5
  157. package/src/unwrapApolloResult.ts +15 -13
  158. package/src/utils.ts +25 -25
  159. package/tsconfig-browser.json +82 -82
  160. package/tsconfig.json +80 -80
  161. package/yarn-error.log +0 -22814
@@ -1,444 +1,453 @@
1
- import { ApolloClient, gql } from "@apollo/client/core";
2
- import { ExpressionRendered } from "../expression/Expression";
3
- import { ExpressionClient } from "../expression/ExpressionClient";
4
- import { Link, LinkExpressionInput, LinkExpression, LinkInput, LinkMutations, LinkExpressionMutations } from "../links/Links";
5
- import { NeighbourhoodClient } from "../neighbourhood/NeighbourhoodClient";
6
- import { NeighbourhoodProxy } from "../neighbourhood/NeighbourhoodProxy";
7
- import unwrapApolloResult from "../unwrapApolloResult";
8
- import { LinkQuery } from "./LinkQuery";
9
- import { Perspective } from "./Perspective";
10
- import { PerspectiveHandle, PerspectiveState } from "./PerspectiveHandle";
11
- import { LinkStatus, PerspectiveProxy } from './PerspectiveProxy';
12
-
13
- const LINK_EXPRESSION_FIELDS = `
14
- author
15
- timestamp
16
- status
17
- data { source, predicate, target }
18
- proof { valid, invalid, signature, key }
19
- `
20
-
21
- const PERSPECTIVE_HANDLE_FIELDS = `
22
- uuid
23
- name
24
- sharedUrl
25
- state
26
- neighbourhood {
27
- data {
28
- linkLanguage
29
- meta {
30
- links
31
- {
32
- author
33
- timestamp
34
- data { source, predicate, target }
35
- proof { valid, invalid, signature, key }
36
- }
37
- }
38
- }
39
- author
40
- }
41
- `
42
-
43
- export type PerspectiveHandleCallback = (perspective: PerspectiveHandle) => null
44
- export type UuidCallback = (uuid: string) => null
45
- export type LinkCallback = (link: LinkExpression) => null
46
- export type SyncStateChangeCallback = (state: PerspectiveState) => null
47
-
48
- export class PerspectiveClient {
49
- #apolloClient: ApolloClient<any>
50
- #perspectiveAddedCallbacks: PerspectiveHandleCallback[]
51
- #perspectiveUpdatedCallbacks: PerspectiveHandleCallback[]
52
- #perspectiveRemovedCallbacks: UuidCallback[]
53
- #perspectiveSyncStateChangeCallbacks: SyncStateChangeCallback[]
54
- #expressionClient?: ExpressionClient
55
- #neighbourhoodClient?: NeighbourhoodClient
56
-
57
- constructor(client: ApolloClient<any>, subscribe: boolean = true) {
58
- this.#apolloClient = client
59
- this.#perspectiveAddedCallbacks = []
60
- this.#perspectiveUpdatedCallbacks = []
61
- this.#perspectiveRemovedCallbacks = []
62
- this.#perspectiveSyncStateChangeCallbacks = []
63
-
64
- if(subscribe) {
65
- this.subscribePerspectiveAdded()
66
- this.subscribePerspectiveUpdated()
67
- this.subscribePerspectiveRemoved()
68
- }
69
- }
70
-
71
- setExpressionClient(client: ExpressionClient) {
72
- this.#expressionClient = client
73
- }
74
-
75
- setNeighbourhoodClient(client: NeighbourhoodClient) {
76
- this.#neighbourhoodClient = client
77
- }
78
-
79
- async all(): Promise<PerspectiveProxy[]> {
80
- const { perspectives } = unwrapApolloResult(await this.#apolloClient.query({
81
- query: gql`query perspectives {
82
- perspectives {
83
- ${PERSPECTIVE_HANDLE_FIELDS}
84
- }
85
- }`
86
- }))
87
- return perspectives.map(handle => new PerspectiveProxy(handle, this))
88
- }
89
-
90
- async byUUID(uuid: string): Promise<PerspectiveProxy|null> {
91
- const { perspective } = unwrapApolloResult(await this.#apolloClient.query({
92
- query: gql`query perspective($uuid: String!) {
93
- perspective(uuid: $uuid) {
94
- ${PERSPECTIVE_HANDLE_FIELDS}
95
- }
96
- }`,
97
- variables: { uuid }
98
- }))
99
- if(!perspective) return null
100
- return new PerspectiveProxy(perspective, this)
101
- }
102
-
103
- async snapshotByUUID(uuid: string): Promise<Perspective|null> {
104
- const { perspectiveSnapshot } = unwrapApolloResult(await this.#apolloClient.query({
105
- query: gql`query perspectiveSnapshot($uuid: String!) {
106
- perspectiveSnapshot(uuid: $uuid) {
107
- links { ${LINK_EXPRESSION_FIELDS} }
108
- }
109
- }`,
110
- variables: { uuid }
111
- }))
112
- return perspectiveSnapshot
113
- }
114
- async publishSnapshotByUUID(uuid: string): Promise<string|null> {
115
- const { perspectivePublishSnapshot } = unwrapApolloResult(await this.#apolloClient.mutate({
116
- mutation: gql`mutation perspectivePublishSnapshot($uuid: String!) {
117
- perspectivePublishSnapshot(uuid: $uuid)
118
- }`,
119
- variables: { uuid }
120
- }))
121
- return perspectivePublishSnapshot
122
- }
123
-
124
- async queryLinks(uuid: string, query: LinkQuery): Promise<LinkExpression[]> {
125
- const { perspectiveQueryLinks } = unwrapApolloResult(await this.#apolloClient.query({
126
- query: gql`query perspectiveQueryLinks($uuid: String!, $query: LinkQuery!) {
127
- perspectiveQueryLinks(query: $query, uuid: $uuid) {
128
- ${LINK_EXPRESSION_FIELDS}
129
- }
130
- }`,
131
- variables: { uuid, query }
132
- }))
133
- return perspectiveQueryLinks
134
- }
135
-
136
- async queryProlog(uuid: string, query: string): Promise<any> {
137
- const { perspectiveQueryProlog } = unwrapApolloResult(await this.#apolloClient.query({
138
- query: gql`query perspectiveQueryProlog($uuid: String!, $query: String!) {
139
- perspectiveQueryProlog(uuid: $uuid, query: $query)
140
- }`,
141
- variables: { uuid, query }
142
- }))
143
-
144
- return JSON.parse(perspectiveQueryProlog)
145
- }
146
-
147
- async add(name: string): Promise<PerspectiveProxy> {
148
- const { perspectiveAdd } = unwrapApolloResult(await this.#apolloClient.mutate({
149
- mutation: gql`mutation perspectiveAdd($name: String!) {
150
- perspectiveAdd(name: $name) {
151
- ${PERSPECTIVE_HANDLE_FIELDS}
152
- }
153
- }`,
154
- variables: { name }
155
- }))
156
- return new PerspectiveProxy(perspectiveAdd, this)
157
- }
158
-
159
- async update(uuid: string, name: string): Promise<PerspectiveProxy> {
160
- const { perspectiveUpdate } = unwrapApolloResult(await this.#apolloClient.mutate({
161
- mutation: gql`mutation perspectiveUpdate($uuid: String!, $name: String!) {
162
- perspectiveUpdate(uuid: $uuid, name: $name) {
163
- ${PERSPECTIVE_HANDLE_FIELDS}
164
- }
165
- }`,
166
- variables: { uuid, name }
167
- }))
168
- return new PerspectiveProxy(perspectiveUpdate, this)
169
- }
170
-
171
- async remove(uuid: string): Promise<{perspectiveRemove: boolean}> {
172
- return unwrapApolloResult(await this.#apolloClient.mutate({
173
- mutation: gql`mutation perspectiveRemove($uuid: String!) {
174
- perspectiveRemove(uuid: $uuid)
175
- }`,
176
- variables: { uuid }
177
- }))
178
- }
179
-
180
- async addLink(uuid: string, link: Link, status?: LinkStatus): Promise<LinkExpression> {
181
- const { perspectiveAddLink } = unwrapApolloResult(await this.#apolloClient.mutate({
182
- mutation: gql`mutation perspectiveAddLink($uuid: String!, $link: LinkInput!, $status: String){
183
- perspectiveAddLink(link: $link, uuid: $uuid, status: $status) {
184
- ${LINK_EXPRESSION_FIELDS}
185
- }
186
- }`,
187
- variables: { uuid, link, status }
188
- }))
189
- return perspectiveAddLink
190
- }
191
-
192
- async addLinks(uuid: string, links: Link[], status?: LinkStatus): Promise<LinkExpression[]> {
193
- const { perspectiveAddLinks } = unwrapApolloResult(await this.#apolloClient.mutate({
194
- mutation: gql`mutation perspectiveAddLinks($uuid: String!, $links: [LinkInput!]!, $status: String){
195
- perspectiveAddLinks(links: $links, uuid: $uuid, status: $status) {
196
- ${LINK_EXPRESSION_FIELDS}
197
- }
198
- }`,
199
- variables: { uuid, links, status }
200
- }))
201
- return perspectiveAddLinks
202
- }
203
-
204
- async removeLinks(uuid: string, links: LinkExpressionInput[]): Promise<LinkExpression[]> {
205
- const { perspectiveRemoveLinks } = unwrapApolloResult(await this.#apolloClient.mutate({
206
- mutation: gql`mutation perspectiveRemoveLinks($uuid: String!, $links: [LinkExpressionInput!]!){
207
- perspectiveRemoveLinks(links: $links, uuid: $uuid) {
208
- ${LINK_EXPRESSION_FIELDS}
209
- }
210
- }`,
211
- variables: { uuid, links }
212
- }))
213
- return perspectiveRemoveLinks
214
- }
215
-
216
- async linkMutations(uuid: string, mutations: LinkMutations, status?: LinkStatus): Promise<LinkExpressionMutations> {
217
- const { perspectiveLinkMutations } = unwrapApolloResult(await this.#apolloClient.mutate({
218
- mutation: gql`mutation perspectiveLinkMutations($uuid: String!, $mutations: LinkMutations!, $status: String){
219
- perspectiveLinkMutations(mutations: $mutations, uuid: $uuid, status: $status) {
220
- additions {
221
- ${LINK_EXPRESSION_FIELDS}
222
- }
223
- removals {
224
- ${LINK_EXPRESSION_FIELDS}
225
- }
226
- }
227
- }`,
228
- variables: { uuid, mutations, status }
229
- }))
230
- return perspectiveLinkMutations
231
- }
232
-
233
- async addLinkExpression(uuid: string, link: LinkExpressionInput, status?: LinkStatus): Promise<LinkExpression> {
234
- const { perspectiveAddLinkExpression } = unwrapApolloResult(await this.#apolloClient.mutate({
235
- mutation: gql`mutation perspectiveAddLinkExpression($uuid: String!, $link: LinkExpressionInput!, $status: String){
236
- perspectiveAddLinkExpression(link: $link, uuid: $uuid, status: $status) {
237
- ${LINK_EXPRESSION_FIELDS}
238
- }
239
- }`,
240
- variables: { uuid, link }
241
- }))
242
- return perspectiveAddLinkExpression
243
- }
244
-
245
- async updateLink(uuid: string, oldLink: LinkExpressionInput, newLink: LinkInput): Promise<LinkExpression> {
246
- delete oldLink.__typename
247
- delete oldLink.data.__typename
248
- delete oldLink.proof.__typename
249
- const { perspectiveUpdateLink } = unwrapApolloResult(await this.#apolloClient.mutate({
250
- mutation: gql`mutation perspectiveUpdateLink(
251
- $uuid: String!,
252
- $newLink: LinkInput!
253
- $oldLink: LinkExpressionInput!
254
- ){
255
- perspectiveUpdateLink(
256
- newLink: $newLink,
257
- oldLink: $oldLink,
258
- uuid: $uuid
259
- ) {
260
- ${LINK_EXPRESSION_FIELDS}
261
- }
262
- }`,
263
- variables: { uuid, oldLink, newLink }
264
- }))
265
-
266
- if (!perspectiveUpdateLink.status) {
267
- delete perspectiveUpdateLink.status
268
- }
269
-
270
- return perspectiveUpdateLink
271
- }
272
-
273
- async removeLink(uuid: string, link: LinkExpressionInput): Promise<{perspectiveRemoveLink: boolean}> {
274
- delete link.__typename
275
- delete link.data.__typename
276
- delete link.proof.__typename
277
- delete link.status
278
- return unwrapApolloResult(await this.#apolloClient.mutate({
279
- mutation: gql`mutation perspectiveRemoveLink($link: LinkExpressionInput!, $uuid: String!) {
280
- perspectiveRemoveLink(link: $link, uuid: $uuid)
281
- }`,
282
- variables: { uuid, link }
283
- }))
284
- }
285
-
286
- // ExpressionClient functions, needed for Subjects:
287
- async getExpression(expressionURI: string): Promise<ExpressionRendered> {
288
- return await this.#expressionClient.get(expressionURI)
289
- }
290
-
291
- async createExpression(content: any, languageAddress: string): Promise<string> {
292
- return await this.#expressionClient.create(content, languageAddress)
293
- }
294
-
295
- // Subscriptions:
296
- addPerspectiveAddedListener(cb: PerspectiveHandleCallback) {
297
- this.#perspectiveAddedCallbacks.push(cb)
298
- }
299
-
300
- subscribePerspectiveAdded() {
301
- this.#apolloClient.subscribe({
302
- query: gql` subscription {
303
- perspectiveAdded { ${PERSPECTIVE_HANDLE_FIELDS} }
304
- }
305
- `}).subscribe({
306
- next: result => {
307
- this.#perspectiveAddedCallbacks.forEach(cb => {
308
- cb(result.data.perspectiveAdded)
309
- })
310
- },
311
- error: (e) => console.error(e)
312
- })
313
- }
314
-
315
- addPerspectiveUpdatedListener(cb: PerspectiveHandleCallback) {
316
- this.#perspectiveUpdatedCallbacks.push(cb)
317
- }
318
-
319
- subscribePerspectiveUpdated() {
320
- this.#apolloClient.subscribe({
321
- query: gql` subscription {
322
- perspectiveUpdated { ${PERSPECTIVE_HANDLE_FIELDS} }
323
- }
324
- `}).subscribe({
325
- next: result => {
326
- this.#perspectiveUpdatedCallbacks.forEach(cb => {
327
- cb(result.data.perspectiveUpdated)
328
- })
329
- },
330
- error: (e) => console.error(e)
331
- })
332
- }
333
-
334
- addPerspectiveSyncedListener(cb: SyncStateChangeCallback) {
335
- this.#perspectiveSyncStateChangeCallbacks.push(cb)
336
- }
337
-
338
- async addPerspectiveSyncStateChangeListener(uuid: String, cb: SyncStateChangeCallback[]): Promise<void> {
339
- this.#apolloClient.subscribe({
340
- query: gql` subscription {
341
- perspectiveSyncStateChange(uuid: "${uuid}")
342
- }
343
- `}).subscribe({
344
- next: result => {
345
- cb.forEach(c => {
346
- c(result.data.perspectiveSyncStateChange)
347
- })
348
- },
349
- error: (e) => console.error(e)
350
- })
351
-
352
- await new Promise<void>(resolve => setTimeout(resolve, 500))
353
- }
354
-
355
- addPerspectiveRemovedListener(cb: UuidCallback) {
356
- this.#perspectiveRemovedCallbacks.push(cb)
357
- }
358
-
359
- subscribePerspectiveRemoved() {
360
- this.#apolloClient.subscribe({
361
- query: gql` subscription {
362
- perspectiveRemoved
363
- }
364
- `}).subscribe({
365
- next: result => {
366
- this.#perspectiveRemovedCallbacks.forEach(cb => {
367
- cb(result.data.perspectiveRemoved)
368
- })
369
- },
370
- error: (e) => console.error(e)
371
- })
372
- }
373
-
374
- async addPerspectiveLinkAddedListener(uuid: String, cb: LinkCallback[]): Promise<void> {
375
- this.#apolloClient.subscribe({
376
- query: gql` subscription {
377
- perspectiveLinkAdded(uuid: "${uuid}") { ${LINK_EXPRESSION_FIELDS} }
378
- }
379
- `}).subscribe({
380
- next: result => {
381
- cb.forEach(c => {
382
- c(result.data.perspectiveLinkAdded)
383
- })
384
- },
385
- error: (e) => console.error(e)
386
- })
387
-
388
- await new Promise<void>(resolve => setTimeout(resolve, 500))
389
- }
390
-
391
- async addPerspectiveLinkRemovedListener(uuid: String, cb: LinkCallback[]): Promise<void> {
392
- this.#apolloClient.subscribe({
393
- query: gql` subscription {
394
- perspectiveLinkRemoved(uuid: "${uuid}") { ${LINK_EXPRESSION_FIELDS} }
395
- }
396
- `}).subscribe({
397
- next: result => {
398
- cb.forEach(c => {
399
- if (!result.data.perspectiveLinkRemoved.status) {
400
- delete result.data.perspectiveLinkRemoved.status
401
- }
402
- c(result.data.perspectiveLinkRemoved)
403
- })
404
- },
405
- error: (e) => console.error(e)
406
- })
407
-
408
- await new Promise<void>(resolve => setTimeout(resolve, 500))
409
- }
410
-
411
- async addPerspectiveLinkUpdatedListener(uuid: String, cb: LinkCallback[]): Promise<void> {
412
- this.#apolloClient.subscribe({
413
- query: gql` subscription {
414
- perspectiveLinkUpdated(uuid: "${uuid}") {
415
- oldLink {
416
- ${LINK_EXPRESSION_FIELDS}
417
- }
418
- newLink {
419
- ${LINK_EXPRESSION_FIELDS}
420
- }
421
- }
422
- }
423
- `}).subscribe({
424
- next: result => {
425
- cb.forEach(c => {
426
- if (!result.data.perspectiveLinkUpdated.newLink.status) {
427
- delete result.data.perspectiveLinkUpdated.newLink.status
428
- }
429
- if (!result.data.perspectiveLinkUpdated.oldLink.status) {
430
- delete result.data.perspectiveLinkUpdated.oldLink.status
431
- }
432
- c(result.data.perspectiveLinkUpdated)
433
- })
434
- },
435
- error: (e) => console.error(e)
436
- })
437
-
438
- await new Promise<void>(resolve => setTimeout(resolve, 500))
439
- }
440
-
441
- getNeighbourhoodProxy(uuid: string): NeighbourhoodProxy {
442
- return new NeighbourhoodProxy(this.#neighbourhoodClient, uuid)
443
- }
1
+ import { ApolloClient, gql } from "@apollo/client/core";
2
+ import { ExpressionRendered } from "../expression/Expression";
3
+ import { ExpressionClient } from "../expression/ExpressionClient";
4
+ import { Link, LinkExpressionInput, LinkExpression, LinkInput, LinkMutations, LinkExpressionMutations } from "../links/Links";
5
+ import { NeighbourhoodClient } from "../neighbourhood/NeighbourhoodClient";
6
+ import { NeighbourhoodProxy } from "../neighbourhood/NeighbourhoodProxy";
7
+ import unwrapApolloResult from "../unwrapApolloResult";
8
+ import { LinkQuery } from "./LinkQuery";
9
+ import { Perspective } from "./Perspective";
10
+ import { PerspectiveHandle, PerspectiveState } from "./PerspectiveHandle";
11
+ import { LinkStatus, PerspectiveProxy } from './PerspectiveProxy';
12
+
13
+ const LINK_EXPRESSION_FIELDS = `
14
+ author
15
+ timestamp
16
+ status
17
+ data { source, predicate, target }
18
+ proof { valid, invalid, signature, key }
19
+ `
20
+
21
+ const PERSPECTIVE_HANDLE_FIELDS = `
22
+ uuid
23
+ name
24
+ sharedUrl
25
+ state
26
+ neighbourhood {
27
+ data {
28
+ linkLanguage
29
+ meta {
30
+ links
31
+ {
32
+ author
33
+ timestamp
34
+ data { source, predicate, target }
35
+ proof { valid, invalid, signature, key }
36
+ }
37
+ }
38
+ }
39
+ author
40
+ }
41
+ `
42
+
43
+ export type PerspectiveHandleCallback = (perspective: PerspectiveHandle) => null
44
+ export type UuidCallback = (uuid: string) => null
45
+ export type LinkCallback = (link: LinkExpression) => null
46
+ export type SyncStateChangeCallback = (state: PerspectiveState) => null
47
+
48
+ export class PerspectiveClient {
49
+ #apolloClient: ApolloClient<any>
50
+ #perspectiveAddedCallbacks: PerspectiveHandleCallback[]
51
+ #perspectiveUpdatedCallbacks: PerspectiveHandleCallback[]
52
+ #perspectiveRemovedCallbacks: UuidCallback[]
53
+ #perspectiveSyncStateChangeCallbacks: SyncStateChangeCallback[]
54
+ #expressionClient?: ExpressionClient
55
+ #neighbourhoodClient?: NeighbourhoodClient
56
+
57
+ constructor(client: ApolloClient<any>, subscribe: boolean = true) {
58
+ this.#apolloClient = client
59
+ this.#perspectiveAddedCallbacks = []
60
+ this.#perspectiveUpdatedCallbacks = []
61
+ this.#perspectiveRemovedCallbacks = []
62
+ this.#perspectiveSyncStateChangeCallbacks = []
63
+
64
+ if(subscribe) {
65
+ this.subscribePerspectiveAdded()
66
+ this.subscribePerspectiveUpdated()
67
+ this.subscribePerspectiveRemoved()
68
+ }
69
+ }
70
+
71
+ setExpressionClient(client: ExpressionClient) {
72
+ this.#expressionClient = client
73
+ }
74
+
75
+ setNeighbourhoodClient(client: NeighbourhoodClient) {
76
+ this.#neighbourhoodClient = client
77
+ }
78
+
79
+ async all(): Promise<PerspectiveProxy[]> {
80
+ const { perspectives } = unwrapApolloResult(await this.#apolloClient.query({
81
+ query: gql`query perspectives {
82
+ perspectives {
83
+ ${PERSPECTIVE_HANDLE_FIELDS}
84
+ }
85
+ }`
86
+ }))
87
+ return perspectives.map(handle => new PerspectiveProxy(handle, this))
88
+ }
89
+
90
+ async byUUID(uuid: string): Promise<PerspectiveProxy|null> {
91
+ const { perspective } = unwrapApolloResult(await this.#apolloClient.query({
92
+ query: gql`query perspective($uuid: String!) {
93
+ perspective(uuid: $uuid) {
94
+ ${PERSPECTIVE_HANDLE_FIELDS}
95
+ }
96
+ }`,
97
+ variables: { uuid }
98
+ }))
99
+ if(!perspective) return null
100
+ return new PerspectiveProxy(perspective, this)
101
+ }
102
+
103
+ async snapshotByUUID(uuid: string): Promise<Perspective|null> {
104
+ const { perspectiveSnapshot } = unwrapApolloResult(await this.#apolloClient.query({
105
+ query: gql`query perspectiveSnapshot($uuid: String!) {
106
+ perspectiveSnapshot(uuid: $uuid) {
107
+ links { ${LINK_EXPRESSION_FIELDS} }
108
+ }
109
+ }`,
110
+ variables: { uuid }
111
+ }))
112
+ return perspectiveSnapshot
113
+ }
114
+ async publishSnapshotByUUID(uuid: string): Promise<string|null> {
115
+ const { perspectivePublishSnapshot } = unwrapApolloResult(await this.#apolloClient.mutate({
116
+ mutation: gql`mutation perspectivePublishSnapshot($uuid: String!) {
117
+ perspectivePublishSnapshot(uuid: $uuid)
118
+ }`,
119
+ variables: { uuid }
120
+ }))
121
+ return perspectivePublishSnapshot
122
+ }
123
+
124
+ async queryLinks(uuid: string, query: LinkQuery): Promise<LinkExpression[]> {
125
+ const { perspectiveQueryLinks } = unwrapApolloResult(await this.#apolloClient.query({
126
+ query: gql`query perspectiveQueryLinks($uuid: String!, $query: LinkQuery!) {
127
+ perspectiveQueryLinks(query: $query, uuid: $uuid) {
128
+ ${LINK_EXPRESSION_FIELDS}
129
+ }
130
+ }`,
131
+ variables: { uuid, query }
132
+ }))
133
+ return perspectiveQueryLinks
134
+ }
135
+
136
+ async queryProlog(uuid: string, query: string): Promise<any> {
137
+ const { perspectiveQueryProlog } = unwrapApolloResult(await this.#apolloClient.query({
138
+ query: gql`query perspectiveQueryProlog($uuid: String!, $query: String!) {
139
+ perspectiveQueryProlog(uuid: $uuid, query: $query)
140
+ }`,
141
+ variables: { uuid, query }
142
+ }))
143
+
144
+ return JSON.parse(perspectiveQueryProlog)
145
+ }
146
+
147
+ async add(name: string): Promise<PerspectiveProxy> {
148
+ const { perspectiveAdd } = unwrapApolloResult(await this.#apolloClient.mutate({
149
+ mutation: gql`mutation perspectiveAdd($name: String!) {
150
+ perspectiveAdd(name: $name) {
151
+ ${PERSPECTIVE_HANDLE_FIELDS}
152
+ }
153
+ }`,
154
+ variables: { name }
155
+ }))
156
+ return new PerspectiveProxy(perspectiveAdd, this)
157
+ }
158
+
159
+ async update(uuid: string, name: string): Promise<PerspectiveProxy> {
160
+ const { perspectiveUpdate } = unwrapApolloResult(await this.#apolloClient.mutate({
161
+ mutation: gql`mutation perspectiveUpdate($uuid: String!, $name: String!) {
162
+ perspectiveUpdate(uuid: $uuid, name: $name) {
163
+ ${PERSPECTIVE_HANDLE_FIELDS}
164
+ }
165
+ }`,
166
+ variables: { uuid, name }
167
+ }))
168
+ return new PerspectiveProxy(perspectiveUpdate, this)
169
+ }
170
+
171
+ async remove(uuid: string): Promise<{perspectiveRemove: boolean}> {
172
+ return unwrapApolloResult(await this.#apolloClient.mutate({
173
+ mutation: gql`mutation perspectiveRemove($uuid: String!) {
174
+ perspectiveRemove(uuid: $uuid)
175
+ }`,
176
+ variables: { uuid }
177
+ }))
178
+ }
179
+
180
+ async addLink(uuid: string, link: Link, status?: LinkStatus): Promise<LinkExpression> {
181
+ const { perspectiveAddLink } = unwrapApolloResult(await this.#apolloClient.mutate({
182
+ mutation: gql`mutation perspectiveAddLink($uuid: String!, $link: LinkInput!, $status: String){
183
+ perspectiveAddLink(link: $link, uuid: $uuid, status: $status) {
184
+ ${LINK_EXPRESSION_FIELDS}
185
+ }
186
+ }`,
187
+ variables: { uuid, link, status }
188
+ }))
189
+ return perspectiveAddLink
190
+ }
191
+
192
+ async addLinks(uuid: string, links: Link[], status?: LinkStatus): Promise<LinkExpression[]> {
193
+ const { perspectiveAddLinks } = unwrapApolloResult(await this.#apolloClient.mutate({
194
+ mutation: gql`mutation perspectiveAddLinks($uuid: String!, $links: [LinkInput!]!, $status: String){
195
+ perspectiveAddLinks(links: $links, uuid: $uuid, status: $status) {
196
+ ${LINK_EXPRESSION_FIELDS}
197
+ }
198
+ }`,
199
+ variables: { uuid, links, status }
200
+ }))
201
+ return perspectiveAddLinks
202
+ }
203
+
204
+ async removeLinks(uuid: string, links: LinkExpressionInput[]): Promise<LinkExpression[]> {
205
+ const { perspectiveRemoveLinks } = unwrapApolloResult(await this.#apolloClient.mutate({
206
+ mutation: gql`mutation perspectiveRemoveLinks($uuid: String!, $links: [LinkExpressionInput!]!){
207
+ perspectiveRemoveLinks(links: $links, uuid: $uuid) {
208
+ ${LINK_EXPRESSION_FIELDS}
209
+ }
210
+ }`,
211
+ variables: { uuid, links }
212
+ }))
213
+ return perspectiveRemoveLinks
214
+ }
215
+
216
+ async linkMutations(uuid: string, mutations: LinkMutations, status?: LinkStatus): Promise<LinkExpressionMutations> {
217
+ const { perspectiveLinkMutations } = unwrapApolloResult(await this.#apolloClient.mutate({
218
+ mutation: gql`mutation perspectiveLinkMutations($uuid: String!, $mutations: LinkMutations!, $status: String){
219
+ perspectiveLinkMutations(mutations: $mutations, uuid: $uuid, status: $status) {
220
+ additions {
221
+ ${LINK_EXPRESSION_FIELDS}
222
+ }
223
+ removals {
224
+ ${LINK_EXPRESSION_FIELDS}
225
+ }
226
+ }
227
+ }`,
228
+ variables: { uuid, mutations, status }
229
+ }))
230
+ return perspectiveLinkMutations
231
+ }
232
+
233
+ async addLinkExpression(uuid: string, link: LinkExpressionInput, status?: LinkStatus): Promise<LinkExpression> {
234
+ const { perspectiveAddLinkExpression } = unwrapApolloResult(await this.#apolloClient.mutate({
235
+ mutation: gql`mutation perspectiveAddLinkExpression($uuid: String!, $link: LinkExpressionInput!, $status: String){
236
+ perspectiveAddLinkExpression(link: $link, uuid: $uuid, status: $status) {
237
+ ${LINK_EXPRESSION_FIELDS}
238
+ }
239
+ }`,
240
+ variables: { uuid, link }
241
+ }))
242
+ return perspectiveAddLinkExpression
243
+ }
244
+
245
+ async updateLink(uuid: string, oldLink: LinkExpressionInput, newLink: LinkInput): Promise<LinkExpression> {
246
+ delete oldLink.__typename
247
+ delete oldLink.data.__typename
248
+ delete oldLink.proof.__typename
249
+ const { perspectiveUpdateLink } = unwrapApolloResult(await this.#apolloClient.mutate({
250
+ mutation: gql`mutation perspectiveUpdateLink(
251
+ $uuid: String!,
252
+ $newLink: LinkInput!
253
+ $oldLink: LinkExpressionInput!
254
+ ){
255
+ perspectiveUpdateLink(
256
+ newLink: $newLink,
257
+ oldLink: $oldLink,
258
+ uuid: $uuid
259
+ ) {
260
+ ${LINK_EXPRESSION_FIELDS}
261
+ }
262
+ }`,
263
+ variables: { uuid, oldLink, newLink }
264
+ }))
265
+
266
+ if (!perspectiveUpdateLink.status) {
267
+ delete perspectiveUpdateLink.status
268
+ }
269
+
270
+ return perspectiveUpdateLink
271
+ }
272
+
273
+ async removeLink(uuid: string, link: LinkExpressionInput): Promise<{perspectiveRemoveLink: boolean}> {
274
+ delete link.__typename
275
+ delete link.data.__typename
276
+ delete link.proof.__typename
277
+ delete link.status
278
+ return unwrapApolloResult(await this.#apolloClient.mutate({
279
+ mutation: gql`mutation perspectiveRemoveLink($link: LinkExpressionInput!, $uuid: String!) {
280
+ perspectiveRemoveLink(link: $link, uuid: $uuid)
281
+ }`,
282
+ variables: { uuid, link }
283
+ }))
284
+ }
285
+
286
+ async addSdna(uuid: string, name: string, sdnaCode: string, sdnaType: "subject_class" | "flow" | "custom"): Promise<boolean> {
287
+ return unwrapApolloResult(await this.#apolloClient.mutate({
288
+ mutation: gql`mutation perspectiveAddSdna($uuid: String!, $name: String!, $sdnaCode: String!, $sdnaType: String!) {
289
+ perspectiveAddSdna(uuid: $uuid, name: $name, sdnaCode: $sdnaCode, sdnaType: $sdnaType)
290
+ }`,
291
+ variables: { uuid, name, sdnaCode, sdnaType }
292
+ })).perspectiveAddSdna
293
+ }
294
+
295
+ // ExpressionClient functions, needed for Subjects:
296
+ async getExpression(expressionURI: string): Promise<ExpressionRendered> {
297
+ return await this.#expressionClient.get(expressionURI)
298
+ }
299
+
300
+ async createExpression(content: any, languageAddress: string): Promise<string> {
301
+ return await this.#expressionClient.create(content, languageAddress)
302
+ }
303
+
304
+ // Subscriptions:
305
+ addPerspectiveAddedListener(cb: PerspectiveHandleCallback) {
306
+ this.#perspectiveAddedCallbacks.push(cb)
307
+ }
308
+
309
+ subscribePerspectiveAdded() {
310
+ this.#apolloClient.subscribe({
311
+ query: gql` subscription {
312
+ perspectiveAdded { ${PERSPECTIVE_HANDLE_FIELDS} }
313
+ }
314
+ `}).subscribe({
315
+ next: result => {
316
+ this.#perspectiveAddedCallbacks.forEach(cb => {
317
+ cb(result.data.perspectiveAdded)
318
+ })
319
+ },
320
+ error: (e) => console.error(e)
321
+ })
322
+ }
323
+
324
+ addPerspectiveUpdatedListener(cb: PerspectiveHandleCallback) {
325
+ this.#perspectiveUpdatedCallbacks.push(cb)
326
+ }
327
+
328
+ subscribePerspectiveUpdated() {
329
+ this.#apolloClient.subscribe({
330
+ query: gql` subscription {
331
+ perspectiveUpdated { ${PERSPECTIVE_HANDLE_FIELDS} }
332
+ }
333
+ `}).subscribe({
334
+ next: result => {
335
+ this.#perspectiveUpdatedCallbacks.forEach(cb => {
336
+ cb(result.data.perspectiveUpdated)
337
+ })
338
+ },
339
+ error: (e) => console.error(e)
340
+ })
341
+ }
342
+
343
+ addPerspectiveSyncedListener(cb: SyncStateChangeCallback) {
344
+ this.#perspectiveSyncStateChangeCallbacks.push(cb)
345
+ }
346
+
347
+ async addPerspectiveSyncStateChangeListener(uuid: String, cb: SyncStateChangeCallback[]): Promise<void> {
348
+ this.#apolloClient.subscribe({
349
+ query: gql` subscription {
350
+ perspectiveSyncStateChange(uuid: "${uuid}")
351
+ }
352
+ `}).subscribe({
353
+ next: result => {
354
+ cb.forEach(c => {
355
+ c(result.data.perspectiveSyncStateChange)
356
+ })
357
+ },
358
+ error: (e) => console.error(e)
359
+ })
360
+
361
+ await new Promise<void>(resolve => setTimeout(resolve, 500))
362
+ }
363
+
364
+ addPerspectiveRemovedListener(cb: UuidCallback) {
365
+ this.#perspectiveRemovedCallbacks.push(cb)
366
+ }
367
+
368
+ subscribePerspectiveRemoved() {
369
+ this.#apolloClient.subscribe({
370
+ query: gql` subscription {
371
+ perspectiveRemoved
372
+ }
373
+ `}).subscribe({
374
+ next: result => {
375
+ this.#perspectiveRemovedCallbacks.forEach(cb => {
376
+ cb(result.data.perspectiveRemoved)
377
+ })
378
+ },
379
+ error: (e) => console.error(e)
380
+ })
381
+ }
382
+
383
+ async addPerspectiveLinkAddedListener(uuid: String, cb: LinkCallback[]): Promise<void> {
384
+ this.#apolloClient.subscribe({
385
+ query: gql` subscription {
386
+ perspectiveLinkAdded(uuid: "${uuid}") { ${LINK_EXPRESSION_FIELDS} }
387
+ }
388
+ `}).subscribe({
389
+ next: result => {
390
+ cb.forEach(c => {
391
+ c(result.data.perspectiveLinkAdded)
392
+ })
393
+ },
394
+ error: (e) => console.error(e)
395
+ })
396
+
397
+ await new Promise<void>(resolve => setTimeout(resolve, 500))
398
+ }
399
+
400
+ async addPerspectiveLinkRemovedListener(uuid: String, cb: LinkCallback[]): Promise<void> {
401
+ this.#apolloClient.subscribe({
402
+ query: gql` subscription {
403
+ perspectiveLinkRemoved(uuid: "${uuid}") { ${LINK_EXPRESSION_FIELDS} }
404
+ }
405
+ `}).subscribe({
406
+ next: result => {
407
+ cb.forEach(c => {
408
+ if (!result.data.perspectiveLinkRemoved.status) {
409
+ delete result.data.perspectiveLinkRemoved.status
410
+ }
411
+ c(result.data.perspectiveLinkRemoved)
412
+ })
413
+ },
414
+ error: (e) => console.error(e)
415
+ })
416
+
417
+ await new Promise<void>(resolve => setTimeout(resolve, 500))
418
+ }
419
+
420
+ async addPerspectiveLinkUpdatedListener(uuid: String, cb: LinkCallback[]): Promise<void> {
421
+ this.#apolloClient.subscribe({
422
+ query: gql` subscription {
423
+ perspectiveLinkUpdated(uuid: "${uuid}") {
424
+ oldLink {
425
+ ${LINK_EXPRESSION_FIELDS}
426
+ }
427
+ newLink {
428
+ ${LINK_EXPRESSION_FIELDS}
429
+ }
430
+ }
431
+ }
432
+ `}).subscribe({
433
+ next: result => {
434
+ cb.forEach(c => {
435
+ if (!result.data.perspectiveLinkUpdated.newLink.status) {
436
+ delete result.data.perspectiveLinkUpdated.newLink.status
437
+ }
438
+ if (!result.data.perspectiveLinkUpdated.oldLink.status) {
439
+ delete result.data.perspectiveLinkUpdated.oldLink.status
440
+ }
441
+ c(result.data.perspectiveLinkUpdated)
442
+ })
443
+ },
444
+ error: (e) => console.error(e)
445
+ })
446
+
447
+ await new Promise<void>(resolve => setTimeout(resolve, 500))
448
+ }
449
+
450
+ getNeighbourhoodProxy(uuid: string): NeighbourhoodProxy {
451
+ return new NeighbourhoodProxy(this.#neighbourhoodClient, uuid)
452
+ }
444
453
  }