remove resync interface on subclasses

This commit is contained in:
balzack 2024-07-07 13:10:03 -07:00
parent 3ef40a532f
commit a22e412214
8 changed files with 63 additions and 61 deletions

View File

@ -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');
});

View File

@ -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<void> {
}
public async resync(): Promise<void> {
console.log('set account revision:', rev);
}
public async enableNotifications(): Promise<void> {

View File

@ -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<void> {
}
public async resync(): Promise<void> {
console.log('set alias revision:', rev);
}
public async addGroup(sealed: boolean, dataType: string, subject: string, cardIds: string[]): Promise<string> {

View File

@ -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<void> {
}
public async resync(): Promise<void> {
console.log('set attribute revision:', rev);
}
public async addArticle(sealed: boolean, type: string, subject: string, cardIds: string[], groupIds: string[]): Promise<string> {

View File

@ -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<void> {
}
public async resync(): Promise<void> {
console.log('set contact revision:', rev);
}
public async addCard(server: string, guid: string): Promise<string> {

View File

@ -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<void> {
}
public async resync(): Promise<void> {
console.log('set content revision:', rev);
}
public async addChannel(sealed: boolean, type: string, subject: string, cardIds: string[], groupIds: string[]): Promise<string> {

View File

@ -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<void> {
}
public async resync(): Promise<void> {
console.log('set identity revision:', rev);
}
public async setProfileData(name: string, location: string, description: string): Promise<void> {

View File

@ -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() {
}