mirror of
https://github.com/balzack/databag.git
synced 2025-04-22 09:35:16 +00:00
emit when content loaded
This commit is contained in:
parent
9d3b3fe74f
commit
b2600b9879
@ -20,6 +20,10 @@ export function useSession() {
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const setContentState = (loaded: boolean) => {
|
||||
console.log('content loaded: ', loaded);
|
||||
updateState({ loaded });
|
||||
}
|
||||
const setSdkState = (state: string) => {
|
||||
updateState({ sdkState: state === 'connected' });
|
||||
}
|
||||
@ -28,10 +32,13 @@ export function useSession() {
|
||||
}
|
||||
const session = app.state.session;
|
||||
if (session) {
|
||||
const content = session.getContent();
|
||||
content.addLoadedListener(setContentState);
|
||||
session.addStatusListener(setSdkState);
|
||||
const sub = AppState.addEventListener('change', setAppState);
|
||||
return () => {
|
||||
session.removeStatusListener();
|
||||
session.removeStatusListener(setSdkState);
|
||||
content.removeLoadedListener(setContentState);
|
||||
sub.remove();
|
||||
}
|
||||
}
|
||||
|
@ -109,6 +109,9 @@ export interface Content {
|
||||
|
||||
addChannelListener(ev: (arg: { channels: Channel[]; cardId: string | null }) => void): void;
|
||||
removeChannelListener(ev: (arg: { channels: Channel[]; cardId: string | null }) => void): void;
|
||||
|
||||
addLoadedListener(ev: (loaded: boolean) => void): void;
|
||||
removeLoadedListener(ev: (loaded: boolean) => void): void;
|
||||
}
|
||||
|
||||
export interface Alias {
|
||||
|
@ -47,6 +47,7 @@ export class ContactModule implements Contact {
|
||||
private token: string;
|
||||
private node: string;
|
||||
private secure: boolean;
|
||||
private loaded: boolean;
|
||||
private emitter: EventEmitter;
|
||||
private articleTypes: string[];
|
||||
private channelTypes: string[];
|
||||
@ -93,6 +94,7 @@ export class ContactModule implements Contact {
|
||||
this.articleTypes = articleTypes;
|
||||
this.channelTypes = channelTypes;
|
||||
this.unsealAll = false;
|
||||
this.loaded = false;
|
||||
this.focus = null;
|
||||
this.seal = null;
|
||||
|
||||
@ -172,6 +174,9 @@ export class ContactModule implements Contact {
|
||||
this.unsealAll = true;
|
||||
this.syncing = false;
|
||||
await this.sync();
|
||||
|
||||
this.loaded = true;
|
||||
this.emitLoaded();
|
||||
}
|
||||
|
||||
public async setRevision(rev: number): Promise<void> {
|
||||
@ -684,6 +689,19 @@ export class ContactModule implements Contact {
|
||||
this.emitter.emit('channel', { cardId, channels });
|
||||
}
|
||||
|
||||
public addLoadedListener(ev: (loaded: boolean) => void): void {
|
||||
this.emitter.on('loaded', ev);
|
||||
ev(this.loaded);
|
||||
}
|
||||
|
||||
public removeLoadedListener(ev: (loaded: boolean) => void): void {
|
||||
this.emitter.off('loaded', ev);
|
||||
}
|
||||
|
||||
private emitLoaded() {
|
||||
this.emitter.emit('loaded', this.loaded);
|
||||
}
|
||||
|
||||
public async setFocus(cardId: string, channelId: string): Promise<Focus> {
|
||||
if (this.focus) {
|
||||
this.focus.close();
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import type { Content } from './api';
|
||||
import type { Channel } from './types';
|
||||
import { ContactModule } from './contact';
|
||||
@ -8,14 +9,30 @@ import { Crypto } from './crypto';
|
||||
export class ContentModule implements Content {
|
||||
private crypto: Crypto | null;
|
||||
private log: Logging;
|
||||
private emitter: EventEmitter;
|
||||
private contact: ContactModule;
|
||||
private stream: StreamModule;
|
||||
private streamLoaded: boolean;
|
||||
private contactLoaded: boolean;
|
||||
|
||||
constructor(log: Logging, crypto: Crypto | null, contact: ContactModule, stream: StreamModule) {
|
||||
this.contact = contact;
|
||||
this.stream = stream;
|
||||
this.log = log;
|
||||
this.crypto = crypto;
|
||||
this.emitter = new EventEmitter();
|
||||
this.streamLoaded = false;
|
||||
this.contactLoaded = false;
|
||||
|
||||
this.stream.addLoadedListener((loaded: boolean) => {
|
||||
this.streamLoaded = loaded;
|
||||
this.emitLoaded();
|
||||
});
|
||||
|
||||
this.contact.addLoadedListener((loaded: boolean) => {
|
||||
this.contactLoaded = loaded;
|
||||
this.emitLoaded();
|
||||
});
|
||||
}
|
||||
|
||||
public async addChannel(sealed: boolean, type: string, subject: any, cardIds: string[]): Promise<string> {
|
||||
@ -115,4 +132,17 @@ export class ContentModule implements Content {
|
||||
this.stream.removeChannelListener(ev);
|
||||
this.contact.removeChannelListener(ev);
|
||||
}
|
||||
|
||||
public addLoadedListener(ev: (loaded: boolean) => void): void {
|
||||
this.emitter.on('loaded', ev);
|
||||
ev(this.streamLoaded && this.contactLoaded);
|
||||
}
|
||||
|
||||
public removeLoadedListener(ev: (loaded: boolean) => void): void {
|
||||
this.emitter.off('loaded', ev);
|
||||
}
|
||||
|
||||
private emitLoaded() {
|
||||
this.emitter.emit('loaded', this.streamLoaded && this.contactLoaded);
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,7 @@ export class StreamModule {
|
||||
private nextRevision: number | null;
|
||||
private seal: { privateKey: string; publicKey: string } | null;
|
||||
private unsealAll: boolean;
|
||||
private loaded: boolean;
|
||||
private blocked: Set<string>;
|
||||
private read: Map<string, number>
|
||||
private channelTypes: string[];
|
||||
@ -61,6 +62,7 @@ export class StreamModule {
|
||||
this.focus = null;
|
||||
this.seal = null;
|
||||
this.unsealAll = false;
|
||||
this.loaded = false;
|
||||
this.channelTypes = channelTypes;
|
||||
this.emitter = new EventEmitter();
|
||||
|
||||
@ -102,6 +104,9 @@ export class StreamModule {
|
||||
this.unsealAll = true;
|
||||
this.syncing = false;
|
||||
await this.sync();
|
||||
|
||||
this.loaded = true;
|
||||
this.emitLoaded();
|
||||
}
|
||||
|
||||
private parse(data: string | null): any {
|
||||
@ -269,6 +274,19 @@ export class StreamModule {
|
||||
this.emitter.emit('channel', { channels, cardId: null });
|
||||
}
|
||||
|
||||
public addLoadedListener(ev: (loaded: boolean) => void): void {
|
||||
this.emitter.on('loaded', ev);
|
||||
ev(this.loaded);
|
||||
}
|
||||
|
||||
public removeLoadedListener(ev: (loaded: boolean) => void): void {
|
||||
this.emitter.off('loaded', ev);
|
||||
}
|
||||
|
||||
private emitLoaded() {
|
||||
this.emitter.emit('loaded', this.loaded);
|
||||
}
|
||||
|
||||
public async close(): Promise<void> {
|
||||
this.closing = true;
|
||||
if (this.focus) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user