Skip to content

Commit

Permalink
Optimize live queries for useLiveQuery with dexie 3 (#1728)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfahlander authored May 30, 2023
1 parent c0ba24b commit f288637
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/classes/observable/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const symbolObservable: typeof Symbol.observable =

export class Observable<T> implements IObservable<T> {
private _subscribe: (observer: Observer<T>) => Subscription;
hasValue?: ()=>boolean;
getValue?: ()=>T;

constructor(subscribe: (observer: Observer<T>) => Subscription) {
this._subscribe = subscribe;
}
Expand Down
10 changes: 9 additions & 1 deletion src/live-query/live-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import { extendObservabilitySet } from "./extend-observability-set";
import { rangesOverlap } from "../helpers/rangeset";

export function liveQuery<T>(querier: () => T | Promise<T>): IObservable<T> {
return new Observable<T>((observer) => {
let hasValue = false;
let currentValue: T = undefined as any;
const observable = new Observable<T>((observer) => {
const scopeFuncIsAsync = isAsyncFunction(querier);
function execute(subscr: ObservabilitySet) {
if (scopeFuncIsAsync) {
Expand Down Expand Up @@ -83,6 +85,8 @@ export function liveQuery<T>(querier: () => T | Promise<T>): IObservable<T> {
querying = true;
Promise.resolve(ret).then(
(result) => {
hasValue = true;
currentValue = result;
querying = false;
if (closed) return;
if (shouldNotify()) {
Expand All @@ -97,6 +101,7 @@ export function liveQuery<T>(querier: () => T | Promise<T>): IObservable<T> {
},
(err) => {
querying = false;
hasValue = false;
observer.error && observer.error(err);
subscription.unsubscribe();
}
Expand All @@ -106,4 +111,7 @@ export function liveQuery<T>(querier: () => T | Promise<T>): IObservable<T> {
doQuery();
return subscription;
});
observable.hasValue = () => hasValue;
observable.getValue = () => currentValue;
return observable;
}
22 changes: 15 additions & 7 deletions src/public/types/observable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ declare global {
readonly observable: symbol;
}
}

interface Subscribable<T> {
subscribe(observer: Partial<Observer<T>>): Unsubscribable;
}

interface Unsubscribable {
unsubscribe(): void;
}

export interface Observable<T = any> {
subscribe(
onNext?: ((value: T) => void) | null,
onError?: ((error: any) => void) | null,
onComplete?: (() => void) | null
): Subscription;
subscribe(observer?: Observer<T> | null): Subscription;
[Symbol.observable]: () => Observable<T>;
subscribe(observerOrNext?: Observer<T> | ((value: T) => void)): Subscription;
subscribe(next?: ((value: T) => void) | null, error?: ((error: any) => void) | null, complete?: (() => void) | null): Subscription;
getValue?(): T;
hasValue?(): boolean;

[Symbol.observable]: () => Subscribable<T>;
}

export interface Subscription {
Expand Down

0 comments on commit f288637

Please sign in to comment.