UNPKG

@river-build/sdk

Version:

For more details, visit the following resources:

108 lines 4.89 kB
import { RiverConnection } from './river-connection/riverConnection'; import { RiverRegistry, SpaceDapp } from '@river-build/web3'; import { Store } from '../store/store'; import { userIdFromAddress } from '../id'; import { User } from './user/user'; import { makeBaseProvider, makeRiverProvider } from './utils/providers'; import { RiverDbManager } from '../riverDbManager'; import { Entitlements } from './entitlements/entitlements'; import { DB_MODELS, DB_VERSION } from './db'; import { Spaces } from './spaces/spaces'; import { Gdms } from './gdms/gdms'; import { Dms } from './dms/dms'; import { dlog, shortenHexString } from '@river-build/dlog'; export class SyncAgent { log; userId; config; riverConnection; store; user; spaces; gdms; dms; stopped = false; // flattened observables - just pointers to the observable objects in the models observables; constructor(config) { this.userId = userIdFromAddress(config.context.creatorAddress); const logId = config.logId ?? shortenHexString(this.userId); this.log = dlog('csb:syncAgent', { defaultEnabled: true }).extend(logId); this.config = config; const base = config.riverConfig.base; const river = config.riverConfig.river; const baseProvider = config.baseProvider ?? makeBaseProvider(config.riverConfig); const riverProvider = config.riverProvider ?? makeRiverProvider(config.riverConfig); this.store = new Store(this.syncAgentDbName(), DB_VERSION, DB_MODELS); this.store.newTransactionGroup('SyncAgent::initialization'); const spaceDapp = new SpaceDapp(base.chainConfig, baseProvider); const riverRegistryDapp = new RiverRegistry(river.chainConfig, riverProvider); this.riverConnection = new RiverConnection(this.store, spaceDapp, riverRegistryDapp, { signerContext: config.context, cryptoStore: RiverDbManager.getCryptoDb(this.userId, this.cryptoDbName()), entitlementsDelegate: new Entitlements(this.config.riverConfig, spaceDapp), opts: { persistenceStoreName: config.disablePersistenceStore !== true ? this.persistenceDbName() : undefined, logNamespaceFilter: undefined, highPriorityStreamIds: this.config.highPriorityStreamIds, unpackEnvelopeOpts: config.unpackEnvelopeOpts, logId: config.logId, }, rpcRetryParams: config.retryParams, encryptionDevice: config.encryptionDevice, onTokenExpired: config.onTokenExpired, }); this.user = new User(this.userId, this.store, this.riverConnection); this.spaces = new Spaces(this.store, this.riverConnection, this.user.memberships, spaceDapp); this.gdms = new Gdms(this.store, this.riverConnection, this.user.memberships); this.dms = new Dms(this.store, this.riverConnection, this.user.memberships); // flatten out the observables this.observables = { riverAuthStatus: this.riverConnection.authStatus, riverConnection: this.riverConnection, riverChain: this.riverConnection.riverChain, spaces: this.spaces, user: this.user, userMemberships: this.user.memberships, userInbox: this.user.inbox, userMetadata: this.user.deviceKeys, userSettings: this.user.settings, gdms: this.gdms, dms: this.dms, }; } async start() { if (this.stopped) { throw new Error('SyncAgent is stopped, please instantiate a new sync agent'); } // commit the initialization transaction, which triggers onLoaded on the models await this.store.commitTransaction(); this.log('SyncAgent::start: starting river connection'); // start thie river connection, this will log us in if the user is already signed up // it will leave us in a connected state otherwise, see riverConnection.authStatus await this.riverConnection.start(); this.log('SyncAgent::start: river connection started'); } async stop() { this.stopped = true; await this.riverConnection.stop(); } syncAgentDbName() { return this.dbName('syncAgent'); } persistenceDbName() { return this.dbName('persistence'); } cryptoDbName() { return this.dbName('database'); } dbName(db) { const envSuffix = this.config.riverConfig.environmentId === 'gamma' ? '' : `-${this.config.riverConfig.environmentId}`; const postfix = this.config.deviceId !== undefined ? `-${this.config.deviceId}` : ''; const dbName = `${db}-${this.userId}${envSuffix}${postfix}`; return dbName; } } //# sourceMappingURL=syncAgent.js.map