Skip to content

Commit

Permalink
Implementing Disposable interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriztiaan committed Oct 17, 2024
1 parent 782b70e commit e2e68e2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
13 changes: 9 additions & 4 deletions packages/react/src/QueryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ export class QueryStore {
if (this.cache.has(key)) {
return this.cache.get(key);
}
const disposer = () => {
this.cache.delete(key);
};
const q = new WatchedQuery(this.db, query, options, disposer);

const q = new WatchedQuery(this.db, query, options);
const disposer = q.registerListener({
disposed: () => {
this.cache.delete(key);
disposer?.();
}
});

this.cache.set(key, q);

return q;
Expand Down
29 changes: 19 additions & 10 deletions packages/react/src/WatchedQuery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbstractPowerSyncDatabase, BaseListener, BaseObserver, CompilableQuery } from '@powersync/common';
import { AbstractPowerSyncDatabase, BaseListener, BaseObserver, CompilableQuery, Disposable } from '@powersync/common';
import { AdditionalOptions } from './hooks/useQuery';

export class Query<T> {
Expand All @@ -9,9 +9,10 @@ export class Query<T> {

export interface WatchedQueryListener extends BaseListener {
onUpdate: () => void;
disposed: () => void;
}

export class WatchedQuery extends BaseObserver<WatchedQueryListener> {
export class WatchedQuery extends BaseObserver<WatchedQueryListener> implements Disposable {
readyPromise: Promise<void>;
isReady: boolean = false;
currentData: any[] | undefined;
Expand All @@ -26,14 +27,12 @@ export class WatchedQuery extends BaseObserver<WatchedQueryListener> {

readonly query: Query<unknown>;
readonly options: AdditionalOptions;
private disposer: () => void;

constructor(db: AbstractPowerSyncDatabase, query: Query<unknown>, options: AdditionalOptions, disposer: () => void) {
constructor(db: AbstractPowerSyncDatabase, query: Query<unknown>, options: AdditionalOptions) {
super();
this.db = db;
this.query = query;
this.options = options;
this.disposer = disposer;

this.readyPromise = new Promise((resolve) => {
this.resolveReady = resolve;
Expand Down Expand Up @@ -89,6 +88,7 @@ export class WatchedQuery extends BaseObserver<WatchedQueryListener> {

async fetchData() {
try {
await new Promise((resolve) => setTimeout(resolve, 18000));
const result =
typeof this.query.rawQuery == 'string'
? await this.db.getAll(this.query.sqlStatement, this.query.queryParameters)
Expand All @@ -106,7 +106,8 @@ export class WatchedQuery extends BaseObserver<WatchedQueryListener> {
if (this.controller != null) {
return;
}
if (this.listeners.size == 0 && this.temporaryHolds.size == 0) {

if (this.onUpdateListenersCount() == 0 && this.temporaryHolds.size == 0) {
return;
}

Expand Down Expand Up @@ -145,7 +146,7 @@ export class WatchedQuery extends BaseObserver<WatchedQueryListener> {
this.currentError = undefined;
this.resolveReady?.();

this.iterateListeners((l) => l?.onUpdate());
this.iterateListeners((l) => l.onUpdate?.());
}

private setError(error: any) {
Expand All @@ -154,21 +155,29 @@ export class WatchedQuery extends BaseObserver<WatchedQueryListener> {
this.currentError = error;
this.resolveReady?.();

this.iterateListeners((l) => l?.onUpdate());
this.iterateListeners((l) => l.onUpdate?.());
}

private onUpdateListenersCount(): number {
return Array.from(this.listeners).filter((listener) => listener.onUpdate !== undefined).length;
}

private maybeDispose() {
if (this.listeners.size == 0 && this.temporaryHolds.size == 0) {
if (this.onUpdateListenersCount() == 0 && this.temporaryHolds.size == 0) {
this.controller?.abort();
this.controller = undefined;
this.isReady = false;
this.currentData = undefined;
this.currentError = undefined;
this.disposer?.();
this.dispose();

this.readyPromise = new Promise((resolve, reject) => {
this.resolveReady = resolve;
});
}
}

async dispose() {
this.iterateAsyncListeners(async (l) => l.disposed?.());
}
}

0 comments on commit e2e68e2

Please sign in to comment.