// // Copyright 2025 DXOS.org // import { type Obj, type Relation } from '@dxos/echo'; import { ObjectId, type BaseEchoObject, type HasId } from '@dxos/echo-schema'; import { compositeRuntime } from '@dxos/echo-signals/runtime'; import { invariant } from '@dxos/invariant'; import { DXN, SpaceId } from '@dxos/keys'; import { type Queue } from './types'; export type MemoryQueueOptions = { spaceId?: SpaceId; queueId?: string; dxn?: DXN; objects?: T[]; }; /** * In-memory queue. * @deprecated Use the actual queue with a mock service. */ export class MemoryQueue implements Queue { static make({ spaceId, queueId, dxn, objects, }: MemoryQueueOptions): MemoryQueue { if (!dxn) { dxn = new DXN(DXN.kind.QUEUE, [spaceId ?? SpaceId.random(), queueId ?? ObjectId.random()]); } else { invariant(spaceId == null && queueId == null); } const queue = new MemoryQueue(dxn); if (objects?.length) { void queue.append(objects); } return queue; } private readonly _signal = compositeRuntime.createSignal(); private _objects: T[] = []; constructor(private readonly _dxn: DXN) {} toJSON() { return { dxn: this._dxn.toString(), objects: this._objects.length, }; } get dxn() { return this._dxn; } get isLoading(): boolean { return false; } get error(): Error | null { return null; } get objects(): T[] { this._signal.notifyRead(); return [...this._objects]; } /** * Insert into queue with optimistic update. */ async append(objects: T[]): Promise { this._objects = [...this._objects, ...objects]; this._signal.notifyWrite(); } async queryObjects(): Promise { return this._objects; } async getObjectsById(ids: ObjectId[]): Promise<(T | null)[]> { return ids.map((id) => this._objects.find((object) => (object as HasId).id === id) ?? null); } async delete(ids: ObjectId[]): Promise { // TODO(dmaretskyi): Restrict types. this._objects = this._objects.filter((object) => !ids.includes((object as HasId).id)); this._signal.notifyWrite(); } async refresh(): Promise { // No-op. } }