@amityco/ts-sdk-react-native
Version:
Amity Social Cloud Typescript SDK
120 lines (98 loc) • 3.9 kB
text/typescript
import { getResolver } from '~/core/model';
import { getActiveClient } from '~/client';
import { pullFromCache, pushToCache } from '~/cache/api';
import { ingestInCache } from '~/cache/api/ingestInCache';
import { EnumPostActions } from '~/postRepository/observers/enums';
import { QueryStreamController } from '~/core/liveCollection/QueryStreamController';
export class UserFeedQueryStreamController extends QueryStreamController<
Amity.PostPayload,
Amity.UserFeedLiveCollection
> {
private notifyChange: (params: Amity.LiveCollectionNotifyParams) => void;
private preparePayload: (response: Amity.PostPayload) => Amity.ProcessedPostPayload;
constructor(
query: Amity.UserFeedLiveCollection,
cacheKey: string[],
notifyChange: (params: Amity.LiveCollectionNotifyParams) => void,
preparePayload: (response: Amity.PostPayload) => Amity.ProcessedPostPayload,
) {
super(query, cacheKey);
this.notifyChange = notifyChange;
this.preparePayload = preparePayload;
}
async saveToMainDB(response: Amity.PostPayload) {
const processedPayload = await this.preparePayload(response);
const client = getActiveClient();
const cachedAt = client.cache && Date.now();
if (client.cache) {
ingestInCache(processedPayload, { cachedAt });
}
}
appendToQueryStream(
response: Amity.PostPayload & Partial<Amity.Pagination>,
direction: Amity.LiveCollectionPageDirection,
refresh = false,
) {
if (refresh) {
pushToCache(this.cacheKey, {
data: response.posts.map(getResolver('post')),
});
} else {
const collection = pullFromCache<Amity.UserFeedLiveCollectionCache>(this.cacheKey)?.data;
const posts = collection?.data ?? [];
pushToCache(this.cacheKey, {
...collection,
data: [...new Set([...posts, ...response.posts.map(getResolver('post'))])],
});
}
}
reactor(action: EnumPostActions) {
return (post: Amity.InternalPost) => {
const collection = pullFromCache<Amity.UserFeedLiveCollectionCache>(this.cacheKey)?.data;
if (!collection) return;
// if the collection is parent post collection and
// post is not included in the collection or post is child post
if (
(!this.query.dataTypes || this.query.dataTypes.length === 0) &&
!collection.data.includes(post.parentPostId ? post.parentPostId : post.postId)
)
return;
if (action === EnumPostActions.OnPostDeleted) {
collection.data = collection.data.filter(postId => postId !== post.postId);
}
if (post.parentPostId && post.isDeleted) {
const parentPost = pullFromCache<Amity.InternalPost>([
'post',
'get',
post.parentPostId,
])?.data;
if (!parentPost) return;
parentPost.children = parentPost.children.filter(childId => childId !== post.postId);
pushToCache(['post', 'get', parentPost.postId], parentPost);
}
if (action === EnumPostActions.OnPostDeclined) {
collection.data = collection.data.filter(postId => postId !== post.postId);
}
if (action === EnumPostActions.OnPostCreated || action === EnumPostActions.OnPostApproved) {
if (
this.query.dataTypes &&
this.query.dataTypes.length > 0 &&
!this.query.dataTypes.includes(post.dataType)
) {
return;
}
collection.data = [...new Set([post.postId, ...collection.data])];
}
pushToCache(this.cacheKey, collection);
this.notifyChange({ origin: Amity.LiveDataOrigin.EVENT, loading: false });
};
}
subscribeRTE(
createSubscriber: {
fn: (reactor: (post: Amity.InternalPost) => void) => Amity.Unsubscriber;
action: EnumPostActions;
}[],
) {
return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
}
}