From a22e412214e553001e0dc07a8198b796db3d2c2d Mon Sep 17 00:00:00 2001 From: balzack Date: Sun, 7 Jul 2024 13:10:03 -0700 Subject: [PATCH] remove resync interface on subclasses --- app/sdk/__tests__/session.tests.ts | 1 + app/sdk/src/account.ts | 8 +--- app/sdk/src/alias.ts | 8 +--- app/sdk/src/attribute.ts | 8 +--- app/sdk/src/contact.ts | 8 +--- app/sdk/src/content.ts | 8 +--- app/sdk/src/identity.ts | 8 +--- app/sdk/src/session.ts | 75 ++++++++++++++++++++---------- 8 files changed, 63 insertions(+), 61 deletions(-) diff --git a/app/sdk/__tests__/session.tests.ts b/app/sdk/__tests__/session.tests.ts index 1a24ae39..d8460439 100644 --- a/app/sdk/__tests__/session.tests.ts +++ b/app/sdk/__tests__/session.tests.ts @@ -22,5 +22,6 @@ test('allocates session correctly', async () => { const account = session.getAccount(); account.enableNotifications(); mock.emitStatus('connected'); + mock.emitRevision({ account: 0, profile: 0, article: 0, group: 0, channel: 0, card: 0}); expect(status).toBe('connected'); }); diff --git a/app/sdk/src/account.ts b/app/sdk/src/account.ts index 96d238f6..7ad330d9 100644 --- a/app/sdk/src/account.ts +++ b/app/sdk/src/account.ts @@ -5,15 +5,13 @@ import type { AccountStatus } from './types'; export class AccountModule implements Account { private emitter: EventEmitter; - private sync: (flag: boolean) => void; private token: string; private url: string; - constructor(token: string, url: string, sync: (flag: boolean) => void) { + constructor(token: string, url: string) { this.emitter = new EventEmitter(); this.token = token; this.url = url; - this.sync = sync; } public addStatusListener(ev: (status: AccountStatus) => void): void { @@ -28,9 +26,7 @@ export class AccountModule implements Account { } public async setRevision(rev: number): Promise { - } - - public async resync(): Promise { + console.log('set account revision:', rev); } public async enableNotifications(): Promise { diff --git a/app/sdk/src/alias.ts b/app/sdk/src/alias.ts index 0317db89..0eec70fe 100644 --- a/app/sdk/src/alias.ts +++ b/app/sdk/src/alias.ts @@ -6,14 +6,12 @@ export class AliasModule implements Alias { private token: string; private url: string; - private sync: (flag: boolean) => void; private account: Account; private emitter: EventEmitter; - constructor(token: string, url: string, sync: (flag: boolean) => void, account: Account) { + constructor(token: string, url: string, account: Account) { this.token = token; this.url = url; - this.sync = sync; this.account = account; this.emitter = new EventEmitter(); } @@ -30,9 +28,7 @@ export class AliasModule implements Alias { } public async setRevision(rev: number): Promise { - } - - public async resync(): Promise { + console.log('set alias revision:', rev); } public async addGroup(sealed: boolean, dataType: string, subject: string, cardIds: string[]): Promise { diff --git a/app/sdk/src/attribute.ts b/app/sdk/src/attribute.ts index 86ee8132..e1dec422 100644 --- a/app/sdk/src/attribute.ts +++ b/app/sdk/src/attribute.ts @@ -6,14 +6,12 @@ export class AttributeModule implements Attribute { private token: string; private url: string; - private sync: (flag: boolean) => void; private account: Account; private emitter: EventEmitter; - constructor(token: string, url: string, sync: (flag: boolean) => void, account: Account) { + constructor(token: string, url: string, account: Account) { this.token = token; this.url = url; - this.sync = sync; this.account = account; this.emitter = new EventEmitter(); } @@ -30,9 +28,7 @@ export class AttributeModule implements Attribute { } public async setRevision(rev: number): Promise { - } - - public async resync(): Promise { + console.log('set attribute revision:', rev); } public async addArticle(sealed: boolean, type: string, subject: string, cardIds: string[], groupIds: string[]): Promise { diff --git a/app/sdk/src/contact.ts b/app/sdk/src/contact.ts index 887d6d5a..77f17f72 100644 --- a/app/sdk/src/contact.ts +++ b/app/sdk/src/contact.ts @@ -6,13 +6,11 @@ export class ContactModule implements Contact { private token: string; private url: string; - private sync: (flag: boolean) => void; private emitter: EventEmitter; - constructor(token: string, url: string, sync: (flag: boolean) => void) { + constructor(token: string, url: string) { this.token = token; this.url = url; - this.sync = sync; this.emitter = new EventEmitter(); } @@ -28,9 +26,7 @@ export class ContactModule implements Contact { } public async setRevision(rev: number): Promise { - } - - public async resync(): Promise { + console.log('set contact revision:', rev); } public async addCard(server: string, guid: string): Promise { diff --git a/app/sdk/src/content.ts b/app/sdk/src/content.ts index 96b5e1b7..2d5ca5fc 100644 --- a/app/sdk/src/content.ts +++ b/app/sdk/src/content.ts @@ -6,14 +6,12 @@ export class ContentModule implements Content { private token: string; private url: string; - private sync: (flag: boolean) => void; private account: Account; private emitter: EventEmitter; - constructor(token: string, url: string, sync: (flag: boolean) => void, account: Account) { + constructor(token: string, url: string, account: Account) { this.token = token; this.url = url; - this.sync = sync; this.account = account; this.emitter = new EventEmitter(); } @@ -30,9 +28,7 @@ export class ContentModule implements Content { } public async setRevision(rev: number): Promise { - } - - public async resync(): Promise { + console.log('set content revision:', rev); } public async addChannel(sealed: boolean, type: string, subject: string, cardIds: string[], groupIds: string[]): Promise { diff --git a/app/sdk/src/identity.ts b/app/sdk/src/identity.ts index d4774377..8bcf4091 100644 --- a/app/sdk/src/identity.ts +++ b/app/sdk/src/identity.ts @@ -6,13 +6,11 @@ export class IdentityModule implements Identity { private token: string; private url: string; - private sync: (flag: boolean) => void; private emitter: EventEmitter; - constructor(token: string, url: string, sync: (flag: boolean) => void) { + constructor(token: string, url: string) { this.token = token; this.url = url; - this.sync = sync; this.emitter = new EventEmitter(); } @@ -28,9 +26,7 @@ export class IdentityModule implements Identity { } public async setRevision(rev: number): Promise { - } - - public async resync(): Promise { + console.log('set identity revision:', rev); } public async setProfileData(name: string, location: string, description: string): Promise { diff --git a/app/sdk/src/session.ts b/app/sdk/src/session.ts index f05f6866..6859cf85 100644 --- a/app/sdk/src/session.ts +++ b/app/sdk/src/session.ts @@ -22,7 +22,8 @@ export class SessionModule implements Session { private crypto: Crypto | null; private token: string; private url: string; - private sync: boolean; + private syncRevision: Revision | null; + private status: string; private account: AccountModule; private identity: IdentityModule; private contact: ContactModule; @@ -32,35 +33,57 @@ export class SessionModule implements Session { private stream: StreamModule; private ring: RingModule; private connection: Connection; - + constructor(store: SqlStore | WebStore | null, crypto: Crypto | null, token: string, url: string) { + this.store = store; this.crypto = crypto; this.token = token; this.url = url; - this.sync = true; + this.syncRevision = null; + this.status = 'connecting' this.emitter = new EventEmitter(); - this.account = new AccountModule(token, url, this.setSync); - this.identity = new IdentityModule(token, url, this.setSync); - this.contact = new ContactModule(token, url, this.setSync); - this.alias = new AliasModule(token, url, this.setSync, this.account); - this.attribute = new AttributeModule(token, url, this.setSync, this.account); - this.content = new ContentModule(token, url, this.setSync, this.account); + + this.account = new AccountModule(token, url); + this.identity = new IdentityModule(token, url); + this.contact = new ContactModule(token, url); + this.alias = new AliasModule(token, url, this.account); + this.attribute = new AttributeModule(token, url, this.account); + this.content = new ContentModule(token, url, this.account); this.stream = new StreamModule(this.contact, this.content); this.ring = new RingModule(); + + const onStatus = (ev: string) => { + this.status = ev; + this.emitter.emit('status', this.getStatus()); + } + + const onRevision = async (ev: Revision) => { + try { + await this.identity.setRevision(ev.profile); + await this.account.setRevision(ev.account); + await this.contact.setRevision(ev.card); + await this.attribute.setRevision(ev.article); + await this.alias.setRevision(ev.group); + await this.content.setRevision(ev.channel); + if (this.syncRevision) { + this.syncRevision = null + this.emitter.emit('status', this.getStatus()); + } + } + catch(err) { + this.syncRevision = ev; + this.emitter.emit('status', this.getStatus()); + } + } + + const onRing = (ev: Ringing) => { + } + this.connection = new Connection(token, url); - - this.connection.addStatusListener((ev: string) => { - this.emitter.emit('status', ev); - }); - - this.connection.addRevisionListener((ev: Revision) => { - // handle revision - }); - - this.connection.addRingListener((ev: Ringing) => { - // handle ringing - }); + this.connection.addStatusListener(onStatus); + this.connection.addRevisionListener(onRevision); + this.connection.addRingListener(onRing); } public addStatusListener(ev: (status: string) => void): void { @@ -71,11 +94,13 @@ export class SessionModule implements Session { this.emitter.off('status', ev); } - public setSync(sync: boolean) { - this.sync = sync; - // update status + private getStatus(): string { + if (this.status === 'connected' && this.syncRevision) { + return 'offsync'; + } + return this.status; } - + public resync() { }