/**
 * Micro-observable implementation following TC-39 Observable protocol.
 * ~25 lines of code for minimal footprint while maintaining spec compliance.
 */
if (typeof Symbol.observable === "undefined") {
    Symbol.observable = Symbol("observable");
}
export class LixObservable {
    subscriber;
    constructor(subscriber) {
        this.subscriber = subscriber;
    }
    subscribe(observer) {
        let closed = false;
        const safeObserver = {
            next: (value) => !closed && observer.next?.(value),
            error: (err) => {
                if (!closed) {
                    closed = true;
                    observer.error?.(err);
                }
            },
            complete: () => {
                if (!closed) {
                    closed = true;
                    observer.complete?.();
                }
            },
        };
        const cleanup = this.subscriber(safeObserver);
        return {
            unsubscribe() {
                if (!closed) {
                    closed = true;
                    cleanup?.();
                }
            },
        };
    }
    [Symbol.observable]() {
        return this;
    }
    subscribeTakeFirst(observer) {
        return this.subscribe({
            next: (rows) => observer.next?.(rows[0]),
            error: observer.error,
            complete: observer.complete,
        });
    }
    subscribeTakeFirstOrThrow(observer) {
        return this.subscribe({
            next: (rows) => {
                if (rows.length === 0) {
                    observer.error?.(new Error("Query returned no rows"));
                }
                else {
                    observer.next?.(rows[0]);
                }
            },
            error: observer.error,
            complete: observer.complete,
        });
    }
}
//# sourceMappingURL=lix-observable.js.map