From 86e76fa11552eda73fac9c3dd967a7f73de9e3b2 Mon Sep 17 00:00:00 2001 From: fayeed Date: Wed, 12 Jan 2022 11:12:53 +0530 Subject: [PATCH 1/3] Added a new query to PerspectiveClient to get perspective from sharedUrl --- src/perspectives/PerspectiveClient.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/perspectives/PerspectiveClient.ts b/src/perspectives/PerspectiveClient.ts index 6fa248f..c316fe5 100644 --- a/src/perspectives/PerspectiveClient.ts +++ b/src/perspectives/PerspectiveClient.ts @@ -111,6 +111,19 @@ export default class PerspectiveClient { return new PerspectiveProxy(perspective, this) } + async fromUrl(url: string): Promise { + const { fromUrl } = unwrapApolloResult(await this.#apolloClient.query({ + query: gql`query fromUrl($url: String!) { + fromUrl(url: $url) { + ${PERSPECTIVE_HANDLE_FIELDS} + } + }`, + variables: { url } + })) + if(!fromUrl) return null + return new PerspectiveProxy(fromUrl, this) + } + async snapshotByUUID(uuid: string): Promise { const { perspectiveSnapshot } = unwrapApolloResult(await this.#apolloClient.query({ query: gql`query perspectiveSnapshot($uuid: String!) { From 33e39ad9681ef3da66d75e1f2e8ad94c0e57b378 Mon Sep 17 00:00:00 2001 From: fayeed Date: Wed, 12 Jan 2022 11:13:16 +0530 Subject: [PATCH 2/3] Added test for fromUrl --- src/Ad4mClient.test.ts | 11 ++++++++++- src/perspectives/PerspectiveResolver.ts | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Ad4mClient.test.ts b/src/Ad4mClient.test.ts index 0725998..62e9bee 100644 --- a/src/Ad4mClient.test.ts +++ b/src/Ad4mClient.test.ts @@ -41,7 +41,8 @@ describe('Ad4mClient', () => { }) - const apolloClient = new ApolloClient({ + const apolloClient = new ApolloClient({ + // @ts-ignore link: ApolloLink.from([errorLink, new HttpLink({ uri: url, fetch})]), cache: new InMemoryCache(), defaultOptions: { @@ -319,6 +320,14 @@ describe('Ad4mClient', () => { expect(p.name).toBe('test-perspective-1') }) + it('fromUrl() smoke test', async () => { + const p1 = await ad4mClient.perspective.fromUrl('neighbourhood://Qm12345') + expect(p1.sharedUrl).toBe('neighbourhood://Qm12345') + expect(p1.name).toBe('test-perspective-2') + const p2 = await ad4mClient.perspective.fromUrl('neighbourhood://Qm67891') + expect(p2).toBeNull() + }) + it('snapshotByUUID() smoke test', async () => { const ps = await ad4mClient.perspective.snapshotByUUID('00004') expect(ps.links.length).toBe(1) diff --git a/src/perspectives/PerspectiveResolver.ts b/src/perspectives/PerspectiveResolver.ts index f45da63..5cc7665 100644 --- a/src/perspectives/PerspectiveResolver.ts +++ b/src/perspectives/PerspectiveResolver.ts @@ -44,6 +44,17 @@ export default class PerspectiveResolver { return new PerspectiveHandle(uuid, 'test-perspective-1') } + @Query(returns => PerspectiveHandle, {nullable: true}) + fromUrl(@Arg('url') url: string): PerspectiveHandle|null { + if (url === 'neighbourhood://Qm12345') { + const perspective = new PerspectiveHandle(url, 'test-perspective-2'); + perspective.sharedUrl = "neighbourhood://Qm12345"; + return perspective; + } + + return null; + } + @Query(returns => Perspective, {nullable: true}) perspectiveSnapshot(@Arg('uuid') uuid: string): Perspective|null { return new Perspective([testLink]) From 8afd9f0b2f012697c1c0e37fb823fc17d3ec16f3 Mon Sep 17 00:00:00 2001 From: fayeed Date: Thu, 20 Jan 2022 10:41:25 +0530 Subject: [PATCH 3/3] Only registers one listener and uses a callback list to handle listeners --- src/perspectives/PerspectiveClient.ts | 14 +++++++------ src/perspectives/PerspectiveProxy.ts | 30 ++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/perspectives/PerspectiveClient.ts b/src/perspectives/PerspectiveClient.ts index c316fe5..ac8434a 100644 --- a/src/perspectives/PerspectiveClient.ts +++ b/src/perspectives/PerspectiveClient.ts @@ -247,15 +247,16 @@ export default class PerspectiveClient { this.#perspectiveRemovedCallbacks.push(cb) } - async addPerspectiveLinkAddedListener(uuid: String, cb: LinkCallback): Promise { + async addPerspectiveLinkAddedListener(uuid: String, cb: LinkCallback[]): Promise { this.#apolloClient.subscribe({ query: gql` subscription { perspectiveLinkAdded(uuid: "${uuid}") { ${LINK_EXPRESSION_FIELDS} } } `}).subscribe({ next: result => { - //@ts-ignore - cb(result.data.perspectiveLinkAdded) + cb.forEach(c => { + c(result.data.perspectiveLinkAdded) + }) }, error: (e) => console.error(e) }) @@ -263,15 +264,16 @@ export default class PerspectiveClient { await new Promise(resolve => setTimeout(resolve, 500)) } - async addPerspectiveLinkRemovedListener(uuid: String, cb: LinkCallback): Promise { + async addPerspectiveLinkRemovedListener(uuid: String, cb: LinkCallback[]): Promise { this.#apolloClient.subscribe({ query: gql` subscription { perspectiveLinkRemoved(uuid: "${uuid}") { ${LINK_EXPRESSION_FIELDS} } } `}).subscribe({ next: result => { - //@ts-ignore - cb(result.data.perspectiveLinkRemoved) + cb.forEach(c => { + c(result.data.perspectiveLinkRemoved) + }) }, error: (e) => console.error(e) }) diff --git a/src/perspectives/PerspectiveProxy.ts b/src/perspectives/PerspectiveProxy.ts index d3ea036..102d387 100644 --- a/src/perspectives/PerspectiveProxy.ts +++ b/src/perspectives/PerspectiveProxy.ts @@ -1,17 +1,25 @@ -import PerspectiveClient, { LinkCallback } from "./PerspectiveClient"; +import PerspectiveClient, { LinkCallback, PerspectiveHandleCallback } from "./PerspectiveClient"; import { Link, LinkExpression } from "../links/Links"; import { LinkQuery } from "./LinkQuery"; import { Neighbourhood } from "../neighbourhood/Neighbourhood"; import { PerspectiveHandle } from './PerspectiveHandle' import { Perspective } from "./Perspective"; +type PerspectiveListenerTypes = "link-added" | "link-removed" + export class PerspectiveProxy { #handle: PerspectiveHandle #client: PerspectiveClient + #perspectiveLinkAddedCallbacks: PerspectiveHandleCallback[] + #perspectiveLinkRemovedCallbacks: PerspectiveHandleCallback[] constructor(handle: PerspectiveHandle, ad4m: PerspectiveClient) { + this.#perspectiveLinkAddedCallbacks = [] + this.#perspectiveLinkRemovedCallbacks = [] this.#handle = handle this.#client = ad4m + this.#client.addPerspectiveLinkAddedListener(this.#handle.uuid, this.#perspectiveLinkAddedCallbacks) + this.#client.addPerspectiveLinkRemovedListener(this.#handle.uuid, this.#perspectiveLinkRemovedCallbacks) } get uuid(): string { @@ -50,12 +58,24 @@ export class PerspectiveProxy { return await this.#client.removeLink(this.#handle.uuid, link) } - async addListener(cb: LinkCallback) { - this.#client.addPerspectiveLinkAddedListener(this.#handle.uuid, cb) + async addListener(type: PerspectiveListenerTypes, cb: LinkCallback) { + if (type === 'link-added') { + this.#perspectiveLinkAddedCallbacks.push(cb); + } else if (type === 'link-removed') { + this.#perspectiveLinkRemovedCallbacks.push(cb); + } } - async removeListener(cb: LinkCallback) { - this.#client.addPerspectiveLinkRemovedListener(this.#handle.uuid, cb) + async removeListener(type: PerspectiveListenerTypes, cb: LinkCallback) { + if (type === 'link-added') { + const index = this.#perspectiveLinkAddedCallbacks.indexOf(cb); + + this.#perspectiveLinkAddedCallbacks.splice(index, 1); + } else if (type === 'link-removed') { + const index = this.#perspectiveLinkRemovedCallbacks.indexOf(cb); + + this.#perspectiveLinkRemovedCallbacks.splice(index, 1); + } } async snapshot() {