Storages: make all methods async (total chaos and destruction)

This commit is contained in:
Alex Gleason 2024-05-14 16:10:50 -05:00
parent 0a79ecb0a3
commit 3c706dc81b
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 47 additions and 38 deletions

View File

@ -1,3 +1,4 @@
// deno-lint-ignore-file require-await
import { NCache } from '@nostrify/nostrify'; import { NCache } from '@nostrify/nostrify';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { db } from '@/db.ts'; import { db } from '@/db.ts';
@ -12,89 +13,97 @@ import { UserStore } from '@/storages/UserStore.ts';
import { Time } from '@/utils/time.ts'; import { Time } from '@/utils/time.ts';
export class Storages { export class Storages {
private static _db: EventsDB | undefined; private static _db: Promise<EventsDB> | undefined;
private static _admin: UserStore | undefined; private static _admin: Promise<UserStore> | undefined;
private static _cache: NCache | undefined; private static _cache: Promise<NCache> | undefined;
private static _client: PoolStore | undefined; private static _client: Promise<PoolStore> | undefined;
private static _optimizer: Optimizer | undefined; private static _optimizer: Promise<Optimizer> | undefined;
private static _reqmeister: Reqmeister | undefined; private static _reqmeister: Promise<Reqmeister> | undefined;
private static _pubsub: InternalRelay | undefined; private static _pubsub: Promise<InternalRelay> | undefined;
private static _search: SearchStore | undefined; private static _search: Promise<SearchStore> | undefined;
/** SQLite database to store events this Ditto server cares about. */ /** SQLite database to store events this Ditto server cares about. */
public static get db(): EventsDB { public static async db(): Promise<EventsDB> {
if (!this._db) { if (!this._db) {
this._db = new EventsDB(db); this._db = Promise.resolve(new EventsDB(db));
} }
return this._db; return this._db;
} }
/** Admin user storage. */ /** Admin user storage. */
public static get admin(): UserStore { public static async admin(): Promise<UserStore> {
if (!this._admin) { if (!this._admin) {
this._admin = new UserStore(Conf.pubkey, this.db); this._admin = Promise.resolve(new UserStore(Conf.pubkey, await this.db()));
} }
return this._admin; return this._admin;
} }
/** Internal pubsub relay between controllers and the pipeline. */ /** Internal pubsub relay between controllers and the pipeline. */
public static get pubsub(): InternalRelay { public static async pubsub(): Promise<InternalRelay> {
if (!this._pubsub) { if (!this._pubsub) {
this._pubsub = new InternalRelay(); this._pubsub = Promise.resolve(new InternalRelay());
} }
return this._pubsub; return this._pubsub;
} }
/** Relay pool storage. */ /** Relay pool storage. */
public static get client(): PoolStore { public static async client(): Promise<PoolStore> {
if (!this._client) { if (!this._client) {
this._client = new PoolStore({ this._client = Promise.resolve(
pool, new PoolStore({
relays: activeRelays, pool,
}); relays: activeRelays,
}),
);
} }
return this._client; return this._client;
} }
/** In-memory data store for cached events. */ /** In-memory data store for cached events. */
public static get cache(): NCache { public static async cache(): Promise<NCache> {
if (!this._cache) { if (!this._cache) {
this._cache = new NCache({ max: 3000 }); this._cache = Promise.resolve(new NCache({ max: 3000 }));
} }
return this._cache; return this._cache;
} }
/** Batches requests for single events. */ /** Batches requests for single events. */
public static get reqmeister(): Reqmeister { public static async reqmeister(): Promise<Reqmeister> {
if (!this._reqmeister) { if (!this._reqmeister) {
this._reqmeister = new Reqmeister({ this._reqmeister = Promise.resolve(
client: this.client, new Reqmeister({
delay: Time.seconds(1), client: await this.client(),
timeout: Time.seconds(1), delay: Time.seconds(1),
}); timeout: Time.seconds(1),
}),
);
} }
return this._reqmeister; return this._reqmeister;
} }
/** Main Ditto storage adapter */ /** Main Ditto storage adapter */
public static get optimizer(): Optimizer { public static async optimizer(): Promise<Optimizer> {
if (!this._optimizer) { if (!this._optimizer) {
this._optimizer = new Optimizer({ this._optimizer = Promise.resolve(
db: this.db, new Optimizer({
cache: this.cache, db: await this.db(),
client: this.reqmeister, cache: await this.cache(),
}); client: await this.reqmeister(),
}),
);
} }
return this._optimizer; return this._optimizer;
} }
/** Storage to use for remote search. */ /** Storage to use for remote search. */
public static get search(): SearchStore { public static async search(): Promise<SearchStore> {
if (!this._search) { if (!this._search) {
this._search = new SearchStore({ this._search = Promise.resolve(
relay: Conf.searchRelay, new SearchStore({
fallback: this.optimizer, relay: Conf.searchRelay,
}); fallback: await this.optimizer(),
}),
);
} }
return this._search; return this._search;
} }