Skip to content

Instantly share code, notes, and snippets.

@tchak
Created March 8, 2020 12:06
Show Gist options
  • Save tchak/04394c5fa87e3e8eccf44c8332a2589d to your computer and use it in GitHub Desktop.
Save tchak/04394c5fa87e3e8eccf44c8332a2589d to your computer and use it in GitHub Desktop.
import { RecordIdentity } from '@orbit/data';
import { SyncLiveQuery } from '@orbit/record-cache';
import { consume, dirty } from './tracked';
import { ReadonlyArray } from './readonly-array';
export class LiveQueryArray<M extends RecordIdentity> extends ReadonlyArray<M> {
private readonly _liveQuery: SyncLiveQuery;
private get _content(): M[] {
return this._liveQuery.query() as M[];
}
readonly unsubscribe: () => void;
[Symbol.iterator]() {
consume(this);
return this._content[Symbol.iterator]();
}
getAt(i: number): M | undefined {
consume(this);
return this._content[i];
}
constructor(liveQuery: SyncLiveQuery) {
super();
this._liveQuery = liveQuery;
this.unsubscribe = this._liveQuery.subscribe(() => {
dirty(this);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment