@amityco/ts-sdk-react-native
Version:
Amity Social Cloud Typescript SDK
57 lines (48 loc) • 1.73 kB
text/typescript
import { getActiveClient } from '~/client/api';
import { dropFromCache } from '~/cache/api';
import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
import { SearchPostLiveCollectionController } from './getPostsByHashtag/SearchPostLiveCollectionController';
/* begin_public_function
id: post.query
*/
/**
* ```js
* import { PostRepository } from '@amityco/ts-sdk'
*
* let posts = []
* const unsub = PostRepository.searchPostsByHashtag({
* hashtags: ['amity'],
* limit: 10,
* }, response => merge(posts, response.data))
* ```
*
* Observe all mutations on a list of {@link Amity.Post} for a given target object
*
* @param params.hashtags the hashtags to search for
* @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 searchPostsByHashtag = (
params: Amity.SearchPostWithHashtagLiveCollection,
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(`searchPostsByHashtag(tmpid: ${timestamp}) > listen`);
const postsLiveCollection = new SearchPostLiveCollectionController(params, callback);
const disposers = postsLiveCollection.startSubscription();
const cacheKey = postsLiveCollection.getCacheKey();
disposers.push(() => dropFromCache(cacheKey));
return () => {
log(`searchPostsByHashtag(tmpid: ${timestamp}) > dispose`);
disposers.forEach(fn => fn());
};
};
/* end_public_function */