@amityco/ts-sdk-react-native
Version:
Amity Social Cloud Typescript SDK
57 lines (49 loc) • 1.79 kB
text/typescript
import { getActiveClient } from '~/client/api/activeClient';
import { dropFromCache } from '~/cache/api/dropFromCache';
import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
import { UserFeedLiveCollectionController } from './getUserFeed/LiveCollectionController';
/* begin_public_function
id: feed.query.user_feed
*/
/**
* ```js
* import { FeedRepository } from '@amityco/ts-sdk'
*
* let posts = []
* const unsubscribe = FeedRepository.getUserFeed({
* userId: string,
* feedSources: ['user', 'community'],
* }, response => response => processResponse(response))
* ```
*
* Observe all mutations on a list of {@link Amity.Post} for a given user and feedSources
*
* @param params.userId the ID of the user
* @param params.feedSources the sources of the feed
* @param callback the function to call when new data are available
* @param config
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
*
* @category Posts Live Collection
*/
export const getUserFeed = (
params: Amity.UserFeedLiveCollection,
callback: Amity.LiveCollectionCallback<Amity.Post>,
config?: Amity.LiveCollectionConfig,
): Amity.Unsubscriber => {
const { log, cache } = getActiveClient();
if (!cache) {
console.log(ENABLE_CACHE_MESSAGE);
}
const timestamp = Date.now();
log(`getUserFeed(tmpid: ${timestamp}) > listen`);
const userFeedLiveCollection = new UserFeedLiveCollectionController(params, callback);
const disposers = userFeedLiveCollection.startSubscription();
const cacheKey = userFeedLiveCollection.getCacheKey();
disposers.push(() => dropFromCache(cacheKey));
return () => {
log(`getUserFeed(tmpid: ${timestamp}) > dispose`);
disposers.forEach(fn => fn());
};
};
/* end_public_function */