159 lines
5.1 KiB
TypeScript
159 lines
5.1 KiB
TypeScript
/**
|
|
* rSchedule Local-First Client
|
|
*
|
|
* Wraps the shared local-first stack for collaborative schedule management.
|
|
* Jobs, reminders, workflows, and execution logs sync in real-time.
|
|
*/
|
|
|
|
import { DocumentManager } from '../../shared/local-first/document';
|
|
import type { DocumentId } from '../../shared/local-first/document';
|
|
import { EncryptedDocStore } from '../../shared/local-first/storage';
|
|
import { DocSyncManager } from '../../shared/local-first/sync';
|
|
import { DocCrypto } from '../../shared/local-first/crypto';
|
|
import { scheduleSchema, scheduleDocId, MAX_LOG_ENTRIES } from './schemas';
|
|
import type { ScheduleDoc, ScheduleJob, Reminder, Workflow, ExecutionLogEntry } from './schemas';
|
|
|
|
export class ScheduleLocalFirstClient {
|
|
#space: string;
|
|
#documents: DocumentManager;
|
|
#store: EncryptedDocStore;
|
|
#sync: DocSyncManager;
|
|
#initialized = false;
|
|
|
|
constructor(space: string, docCrypto?: DocCrypto) {
|
|
this.#space = space;
|
|
this.#documents = new DocumentManager();
|
|
this.#store = new EncryptedDocStore(space, docCrypto);
|
|
this.#sync = new DocSyncManager({
|
|
documents: this.#documents,
|
|
store: this.#store,
|
|
});
|
|
this.#documents.registerSchema(scheduleSchema);
|
|
}
|
|
|
|
get isConnected(): boolean { return this.#sync.isConnected; }
|
|
|
|
async init(): Promise<void> {
|
|
if (this.#initialized) return;
|
|
await this.#store.open();
|
|
const cachedIds = await this.#store.listByModule('schedule', 'jobs');
|
|
const cached = await this.#store.loadMany(cachedIds);
|
|
for (const [docId, binary] of cached) {
|
|
this.#documents.open<ScheduleDoc>(docId, scheduleSchema, binary);
|
|
}
|
|
await this.#sync.preloadSyncStates(cachedIds);
|
|
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
const wsUrl = `${proto}//${location.host}/ws/${this.#space}`;
|
|
try { await this.#sync.connect(wsUrl, this.#space); } catch { console.warn('[ScheduleClient] Working offline'); }
|
|
this.#initialized = true;
|
|
}
|
|
|
|
async subscribe(): Promise<ScheduleDoc | null> {
|
|
const docId = scheduleDocId(this.#space) as DocumentId;
|
|
let doc = this.#documents.get<ScheduleDoc>(docId);
|
|
if (!doc) {
|
|
const binary = await this.#store.load(docId);
|
|
doc = binary
|
|
? this.#documents.open<ScheduleDoc>(docId, scheduleSchema, binary)
|
|
: this.#documents.open<ScheduleDoc>(docId, scheduleSchema);
|
|
}
|
|
await this.#sync.subscribe([docId]);
|
|
return doc ?? null;
|
|
}
|
|
|
|
getDoc(): ScheduleDoc | undefined {
|
|
return this.#documents.get<ScheduleDoc>(scheduleDocId(this.#space) as DocumentId);
|
|
}
|
|
|
|
onChange(cb: (doc: ScheduleDoc) => void): () => void {
|
|
return this.#sync.onChange(scheduleDocId(this.#space) as DocumentId, cb as (doc: any) => void);
|
|
}
|
|
|
|
onConnect(cb: () => void): () => void { return this.#sync.onConnect(cb); }
|
|
|
|
// ── Job CRUD ──
|
|
|
|
saveJob(job: ScheduleJob): void {
|
|
const docId = scheduleDocId(this.#space) as DocumentId;
|
|
this.#sync.change<ScheduleDoc>(docId, `Save job ${job.name}`, (d) => {
|
|
d.jobs[job.id] = job;
|
|
});
|
|
}
|
|
|
|
deleteJob(jobId: string): void {
|
|
const docId = scheduleDocId(this.#space) as DocumentId;
|
|
this.#sync.change<ScheduleDoc>(docId, `Delete job`, (d) => {
|
|
delete d.jobs[jobId];
|
|
});
|
|
}
|
|
|
|
toggleJob(jobId: string, enabled: boolean): void {
|
|
const docId = scheduleDocId(this.#space) as DocumentId;
|
|
this.#sync.change<ScheduleDoc>(docId, `Toggle job`, (d) => {
|
|
if (d.jobs[jobId]) {
|
|
d.jobs[jobId].enabled = enabled;
|
|
d.jobs[jobId].updatedAt = Date.now();
|
|
}
|
|
});
|
|
}
|
|
|
|
// ── Reminder CRUD ──
|
|
|
|
saveReminder(reminder: Reminder): void {
|
|
const docId = scheduleDocId(this.#space) as DocumentId;
|
|
this.#sync.change<ScheduleDoc>(docId, `Save reminder ${reminder.title}`, (d) => {
|
|
d.reminders[reminder.id] = reminder;
|
|
});
|
|
}
|
|
|
|
deleteReminder(reminderId: string): void {
|
|
const docId = scheduleDocId(this.#space) as DocumentId;
|
|
this.#sync.change<ScheduleDoc>(docId, `Delete reminder`, (d) => {
|
|
delete d.reminders[reminderId];
|
|
});
|
|
}
|
|
|
|
completeReminder(reminderId: string): void {
|
|
const docId = scheduleDocId(this.#space) as DocumentId;
|
|
this.#sync.change<ScheduleDoc>(docId, `Complete reminder`, (d) => {
|
|
if (d.reminders[reminderId]) {
|
|
d.reminders[reminderId].completed = true;
|
|
d.reminders[reminderId].updatedAt = Date.now();
|
|
}
|
|
});
|
|
}
|
|
|
|
// ── Workflow CRUD ──
|
|
|
|
saveWorkflow(workflow: Workflow): void {
|
|
const docId = scheduleDocId(this.#space) as DocumentId;
|
|
this.#sync.change<ScheduleDoc>(docId, `Save workflow ${workflow.name}`, (d) => {
|
|
d.workflows[workflow.id] = workflow;
|
|
});
|
|
}
|
|
|
|
deleteWorkflow(workflowId: string): void {
|
|
const docId = scheduleDocId(this.#space) as DocumentId;
|
|
this.#sync.change<ScheduleDoc>(docId, `Delete workflow`, (d) => {
|
|
delete d.workflows[workflowId];
|
|
});
|
|
}
|
|
|
|
// ── Execution Log ──
|
|
|
|
appendLogEntry(entry: ExecutionLogEntry): void {
|
|
const docId = scheduleDocId(this.#space) as DocumentId;
|
|
this.#sync.change<ScheduleDoc>(docId, `Log execution`, (d) => {
|
|
if (!d.log) d.log = [] as any;
|
|
d.log.push(entry);
|
|
// Trim to keep doc size manageable
|
|
while (d.log.length > MAX_LOG_ENTRIES) d.log.splice(0, 1);
|
|
});
|
|
}
|
|
|
|
async disconnect(): Promise<void> {
|
|
await this.#sync.flush();
|
|
this.#sync.disconnect();
|
|
}
|
|
}
|