Skip to content

Commit

Permalink
DataSync: support custom newRecordBehaviour with useQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Nov 14, 2024
1 parent dcc0f03 commit d1613e6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/IHP/DataSync/ihp-datasync.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ const PREPEND_NEW_RECORD = 1;
export const NewRecordBehaviour = { APPEND_NEW_RECORD, PREPEND_NEW_RECORD };

class DataSubscription {
constructor(query, cache = null) {
constructor(query, options = null, cache = null) {
if (typeof query !== "object" || !('table' in query)) {
throw new Error("Query passed to `new DataSubscription(..)` doesn't look like a query object. If you're using the `query()` functions to costruct the object, make sure you pass the `.query` property, like this: `new DataSubscription(query('my_table').orderBy('createdAt').query)`");
}
Expand All @@ -261,7 +261,7 @@ class DataSubscription {
this.onMessage = this.onMessage.bind(this);

// When a new record is inserted, do we put it at the end or at the beginning?
this.newRecordBehaviour = this.detectNewRecordBehaviour();
this.newRecordBehaviour = (options && 'newRecordBehaviour' in options) ? options.newRecordBehaviour : this.detectNewRecordBehaviour();

this.optimisticCreatedPendingRecordIds = [];
}
Expand Down
16 changes: 8 additions & 8 deletions lib/IHP/DataSync/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const recordsCache = new Map();
* @example
* const messages = useQuery(query('messages').orderBy('createdAt'));
*/
export function useQuery(queryBuilder) {
const dataSubscription = DataSubscriptionStore.get(queryBuilder.query);
export function useQuery(queryBuilder, options = null) {
const dataSubscription = DataSubscriptionStore.get(queryBuilder.query, options);
const isAuthCompleted = useContext(AuthCompletedContext);
const records = useSyncExternalStore(dataSubscription.subscribe, dataSubscription.getRecords)

Expand Down Expand Up @@ -73,19 +73,19 @@ export class DataSubscriptionStore {
// once it has arrived.
static cache = new Map();

static get(query) {
const strinigifiedQuery = JSON.stringify(query);
const existingSubscription = DataSubscriptionStore.queryMap.get(strinigifiedQuery)
static get(query, options = null) {
const key = JSON.stringify(query) + JSON.stringify(options);
const existingSubscription = DataSubscriptionStore.queryMap.get(key)

if (existingSubscription) {
return existingSubscription;
} else {

const subscription = new DataSubscription(query, DataSubscriptionStore.cache);
const subscription = new DataSubscription(query, options, DataSubscriptionStore.cache);
subscription.createOnServer();
subscription.onClose = () => { DataSubscriptionStore.queryMap.delete(strinigifiedQuery); };
subscription.onClose = () => { DataSubscriptionStore.queryMap.delete(key); };

DataSubscriptionStore.queryMap.set(strinigifiedQuery, subscription);
DataSubscriptionStore.queryMap.set(key, subscription);

// If the query changes very rapid in `useQuery` it can happen that the `dataSubscription.subscribe`
// is never called at all. In this case we have a unused DataSubscription laying around. We avoid
Expand Down

0 comments on commit d1613e6

Please sign in to comment.