Mid-rewrite

This commit is contained in:
2026-02-06 13:14:24 +00:00
parent 601c3db9d0
commit eb1bf9020e
12 changed files with 1300 additions and 103 deletions

99
src/services/app.ts Normal file
View File

@@ -0,0 +1,99 @@
import { Engine, type XOEngineOptions } from '@xo-cash/engine';
import type { XOInvitation } from '@xo-cash/types';
import { Invitation } from './invitation.js';
import { Storage } from './storage.js';
import { SyncServer } from '../utils/sync-server.js';
import { EventEmitter } from '../utils/event-emitter.js';
export type AppEventMap = {
'invitation-added': Invitation;
'invitation-removed': Invitation;
}
export interface AppConfig {
syncServerUrl: string;
engineConfig: XOEngineOptions;
invitationStoragePath: string;
}
export class AppService extends EventEmitter<AppEventMap> {
public engine: Engine;
public storage: Storage;
public config: AppConfig;
public invitations: Invitation[] = [];
static async create(seed: string, config: AppConfig): Promise<AppService> {
// Create the engine
const engine = await Engine.create(seed, config.engineConfig);
// Create our own storage for the invitations
const storage = await Storage.create(config.invitationStoragePath);
// Create the app service
return new AppService(engine, storage, config);
}
constructor(engine: Engine, storage: Storage, config: AppConfig) {
super();
this.engine = engine;
this.storage = storage;
this.config = config;
}
async createInvitation(invitation: XOInvitation | string): Promise<Invitation> {
// Make sure the engine has the template imported
const invitationStorage = this.storage.child('invitations')
const invitationSyncServer = new SyncServer(this.config.syncServerUrl, typeof invitation === 'string' ? invitation : invitation.invitationIdentifier);
const deps = {
engine: this.engine,
syncServer: invitationSyncServer,
storage: invitationStorage,
};
// Create the invitation
const invitationInstance = await Invitation.create(invitation, deps);
// Add the invitation to the invitations array
await this.addInvitation(invitationInstance);
return invitationInstance;
}
async addInvitation(invitation: Invitation): Promise<void> {
// Add the invitation to the invitations array
this.invitations.push(invitation);
// Make sure the invitation is started
await invitation.start();
// Emit the invitation-added event
this.emit('invitation-added', invitation);
}
async removeInvitation(invitation: Invitation): Promise<void> {
// Remove the invitation from the invitations array
this.invitations = this.invitations.filter(i => i !== invitation);
// Emit the invitation-removed event
this.emit('invitation-removed', invitation);
}
async start(): Promise<void> {
// Get the invitations db
const invitationsDb = this.storage.child('invitations');
// Load invitations from storage
const invitations = await invitationsDb.all() as { key: string; value: XOInvitation }[];
// Start the invitations
for (const { key } of invitations) {
// TODO: This is doing some double work of grabbing the invitation data. We can probably skip it, but who knows.
await this.createInvitation(key);
}
}
}