Skip to content

Commit

Permalink
Merge pull request #909 from eisbuk/enhancement/react-redux-firebase-…
Browse files Browse the repository at this point in the history
…firestore-updates

Enhancement/react redux firebase firestore updates
  • Loading branch information
ikusteu authored Jan 16, 2024
2 parents ae2b3c3 + 7e24a5c commit aa992f7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";

import { CollectionSubscription } from "../types";

import { getFirestoreListeners } from "../selectors";

import { updateSubscription } from "../thunks/subscribe";

import { getCollectionPath, getConstraintForColl } from "../utils/utils";

const useUpdateSubscription = (
subscription: CollectionSubscription,
deps: any[]
): void => {
const dispatch = useDispatch();
const collection = subscription.collection;

const listeners = useSelector(getFirestoreListeners);

useEffect(() => {
const listener = listeners[collection];

if (!subscription.meta || !listener) return;

const meta = { ...listener.meta, ...subscription.meta };

dispatch(
updateSubscription({
storeAs: collection,
collPath: getCollectionPath(subscription.collection, meta),
constraint: getConstraintForColl(subscription.collection, meta),
meta,
})
);
}, deps);
};

export default useUpdateSubscription;
8 changes: 4 additions & 4 deletions packages/react-redux-firebase-firestore/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createFirestoreReducer } from "./reducer";
import useFirestoreSubscribe from "./hooks/useFirestoreSubscribe";
import usePaginateFirestore from "./hooks/usePaginateFirestore";
export { createFirestoreReducer } from "./reducer";

export { createFirestoreReducer, useFirestoreSubscribe, usePaginateFirestore };
export { default as useFirestoreSubscribe } from "./hooks/useFirestoreSubscribe";
export { default as usePaginateFirestore } from "./hooks/usePaginateFirestore";
export { default as useUpdateSubscription } from "./hooks/useUpdateSubscription";

export * from "./types";
export * from "./actions";
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ export const updateSubscription: SubscribeFunction =
if (rangeInStore && rangeProperty === rangeInStore[0]) {
const [, storeRangeStart, storeRangeEnd] = rangeInStore;
switch (true) {
// exit early if ranges are the same
case rangeStart >= storeRangeStart && rangeEnd <= storeRangeEnd:
return;

// extend range in both directions: should happed rarely (or never) in production
case rangeStart < storeRangeStart && rangeEnd > storeRangeEnd:
// unsubscribe from old range
Expand Down

0 comments on commit aa992f7

Please sign in to comment.