Skip to content

Commit

Permalink
isSubscribed should work fine now
Browse files Browse the repository at this point in the history
  • Loading branch information
tobicrain committed Oct 17, 2022
1 parent ffb99fa commit 8632529
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
60 changes: 38 additions & 22 deletions src/context/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { recordsAction } from '../store/actions';
import { StorageService } from '../service/Storage';

type SubscribeType = (collectionName: string) => Promise<void>;
type UnsubscribeType = (collectionName?: string) => void;
type UnsubscribeType = (collectionName?: string) => Promise<void>;
type FetchType = (collectionName: string) => Promise<void>;
type CreateType = (collectionName: string, record: {}) => Promise<void>;
type UpdateType = (collectionName: string, recordId: string, record: {}) => Promise<void>;
Expand Down Expand Up @@ -41,11 +41,12 @@ interface MessageData {
export const ContentProvider = (props: ContentProviderProps) => {
const client = useClientContext();
const dispatch = store.useAppDispatch;
const [collections, _] = React.useState<string[]>(props.collections || []);


const actions: ContentActions = {
subscribe: async (collectionName: string) => {
const subscribedCollectionsString = await StorageService.get(StorageService.Constants.SUBSCRIBED) ?? JSON.stringify([]);
var subscribedCollections = JSON.parse(subscribedCollectionsString) as string[];

await client?.realtime.subscribe(collectionName, (event: MessageData) => {
switch (event.action) {
case 'create':
Expand All @@ -61,27 +62,38 @@ export const ContentProvider = (props: ContentProviderProps) => {
break;
}
})
.then(async () => {
const subscribed = JSON.parse(await StorageService.get("subscribed") ?? JSON.stringify([])) as string[];
await StorageService.set("subscribed", JSON.stringify([...subscribed, collectionName]));
.then(() => {
if (!subscribedCollections.includes(collectionName)) {
subscribedCollections.push(collectionName);
}
})
.catch((_error) => {});
.catch((_error) => {
subscribedCollections = subscribedCollections.filter((collection) => collection !== collectionName);
});

await StorageService.set(StorageService.Constants.SUBSCRIBED, JSON.stringify(subscribedCollections));
},
unsubscribe: (collectionName?: string) => {
unsubscribe: async (collectionName?: string) => {
const subscribedCollectionsString = await StorageService.get(StorageService.Constants.SUBSCRIBED) ?? JSON.stringify([]);
var subscribedCollections = JSON.parse(subscribedCollectionsString) as string[];

if (collectionName) {
client?.realtime.unsubscribe(collectionName)
.then(async () => {
const subscribed = JSON.parse(await StorageService.get("subscribed") ?? JSON.stringify([])) as string[];
await StorageService.set("subscribed", JSON.stringify(subscribed.filter((name) => name !== collectionName)));
await client?.realtime.unsubscribe(collectionName)
.then(() => {
subscribedCollections = subscribedCollections.filter((collection) => collection !== collectionName);
})
.catch((_error) => {});
.catch((_error) => {
subscribedCollections = subscribedCollections.filter((collection) => collection !== collectionName);
});
} else {
client?.realtime.unsubscribe()
.then(async () => {
await StorageService.set("subscribed", JSON.stringify([]));
await client?.realtime.unsubscribe()
.then(() => {
subscribedCollections = [];
})
.catch((_error) => {});
}

await StorageService.set(StorageService.Constants.SUBSCRIBED, JSON.stringify(subscribedCollections));
},
fetch: async (collectionName: string) => {
const records = await client?.records.getFullList(collectionName, 200).catch((_error) => {});
Expand All @@ -99,14 +111,18 @@ export const ContentProvider = (props: ContentProviderProps) => {
};

useEffect(() => {
if (collections) {
collections.forEach((collectionName) => {
actions.fetch(collectionName);
actions.subscribe(collectionName);
if (props.collections) {
props.collections.forEach(async (collectionName) => {
await actions.fetch(collectionName);
await actions.subscribe(collectionName);
});
}
return () => actions.unsubscribe();
}, [collections]);
return () => {
(async () => {
await actions.unsubscribe();
})();
};
}, [props.collections]);

return (
<ContentContext.Provider value={{
Expand Down
11 changes: 6 additions & 5 deletions src/hooks/useAppContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Record } from '../interfaces/Record';
import { StorageService } from '../service/Storage';

export type SubscribeType = () => Promise<void | undefined>
export type UnsubscribeType = () => void | undefined;
export type UnsubscribeType = () => Promise<void | undefined>;
export type FetchType = () => Promise<void | undefined>
export type CreateType = (record: {}) => Promise<void | undefined>
export type UpdateType = (id: string, record: {}) => Promise<void | undefined>
Expand Down Expand Up @@ -36,8 +36,9 @@ export function useAppContent<T extends Record>(
const [isSubscribed, setIsSubscribed] = useState(false);

async function refetchSubscribeState() {
const isSubscribed = JSON.parse(await StorageService.get("subscribed") ?? JSON.stringify([])) as string[];
setIsSubscribed(isSubscribed.includes(collectionName));
const subscribedCollectionsString = await StorageService.get(StorageService.Constants.SUBSCRIBED) ?? JSON.stringify([]);
var subscribedCollections = JSON.parse(subscribedCollectionsString) as string[];
setIsSubscribed(subscribedCollections.includes(collectionName));
}

useEffect(() => {
Expand All @@ -52,9 +53,9 @@ export function useAppContent<T extends Record>(
await context.actions.subscribe(collectionName)
await refetchSubscribeState()
},
unsubscribe: () => {
unsubscribe: async () => {
context.actions.unsubscribe(collectionName)
refetchSubscribeState()
await refetchSubscribeState()
},
fetch: async () => await context.actions.fetch(collectionName),
create: async (record: {}) => await context.actions.create(collectionName, record),
Expand Down
5 changes: 5 additions & 0 deletions src/service/Storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import AsyncStorage from "@react-native-async-storage/async-storage";

export class StorageService {

static Constants = {
SUBSCRIBED: "subscribed",
}

static async get(key: string): Promise<string | null> {
return typeof document !== 'undefined' ? localStorage.getItem(key) : await AsyncStorage.getItem(key);
}
Expand Down

0 comments on commit 8632529

Please sign in to comment.