refactor useAvatarETag
This commit is contained in:
parent
9c073fa6dc
commit
22230e0584
|
@ -1,5 +1,5 @@
|
||||||
import { Q } from '@nozbe/watermelondb';
|
import { Q } from '@nozbe/watermelondb';
|
||||||
import { useEffect, useRef, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Observable, Subscription } from 'rxjs';
|
import { Observable, Subscription } from 'rxjs';
|
||||||
|
|
||||||
import { TSubscriptionModel, TUserModel } from '../../definitions';
|
import { TSubscriptionModel, TUserModel } from '../../definitions';
|
||||||
|
@ -16,19 +16,14 @@ export const useAvatarETag = ({
|
||||||
text: string;
|
text: string;
|
||||||
rid?: string;
|
rid?: string;
|
||||||
}) => {
|
}) => {
|
||||||
const subscription = useRef<Subscription>();
|
|
||||||
const [avatarETag, setAvatarETag] = useState<string | undefined>('');
|
const [avatarETag, setAvatarETag] = useState<string | undefined>('');
|
||||||
|
|
||||||
const isDirect = () => type === 'd';
|
const isDirect = () => type === 'd';
|
||||||
|
|
||||||
const unsubscribeQuery = () => {
|
useEffect(() => {
|
||||||
if (subscription?.current?.unsubscribe) {
|
let subscription: Subscription;
|
||||||
subscription.current.unsubscribe();
|
if (!avatarETag) {
|
||||||
}
|
(async () => {
|
||||||
};
|
|
||||||
|
|
||||||
const init = async () => {
|
|
||||||
unsubscribeQuery();
|
|
||||||
const db = database.active;
|
const db = database.active;
|
||||||
const usersCollection = db.get('users');
|
const usersCollection = db.get('users');
|
||||||
const subsCollection = db.get('subscriptions');
|
const subsCollection = db.get('subscriptions');
|
||||||
|
@ -47,19 +42,19 @@ export const useAvatarETag = ({
|
||||||
|
|
||||||
if (record) {
|
if (record) {
|
||||||
const observable = record.observe() as Observable<TSubscriptionModel | TUserModel>;
|
const observable = record.observe() as Observable<TSubscriptionModel | TUserModel>;
|
||||||
subscription.current = observable.subscribe(r => {
|
subscription = observable.subscribe(r => {
|
||||||
setAvatarETag(r.avatarETag);
|
setAvatarETag(r.avatarETag);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
})();
|
||||||
|
|
||||||
useEffect(() => {
|
return () => {
|
||||||
if (!avatarETag) {
|
if (subscription?.unsubscribe) {
|
||||||
init();
|
subscription.unsubscribe();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}, [text]);
|
}, [text]);
|
||||||
|
|
||||||
useEffect(() => () => unsubscribeQuery(), []);
|
|
||||||
|
|
||||||
return { avatarETag };
|
return { avatarETag };
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue