UNPKG

@amityco/ts-sdk-react-native

Version:

Amity Social Cloud Typescript SDK

57 lines (48 loc) 1.72 kB
import { getActiveClient } from '~/client/api'; import { dropFromCache } from '~/cache/api'; import { ENABLE_CACHE_MESSAGE } from '~/utils/constants'; import { GlobalFeedLiveCollectionController } from './getGlobalFeed/LiveCollectionController'; /* begin_public_function id: feed.query.global_feed */ /** * ```js * import { FeedRepository } from '@amityco/ts-sdk' * * let posts = [] * const unsub = FeedRepository.getGlobalFeed({ * dataTypes: ['video','image'], * resolveParent: true, * }, response => processResponse(response)) * ``` * * Observe all mutations on a list of {@link Amity.Post} for global feed * @param params.dataTypes array of data types for the posts * @param params.resolveParent * @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 getGlobalFeed = ( params: Amity.GlobalFeedLiveCollection, 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(`getGlobalFeed(tmpid: ${timestamp}) > listen`); const liveCollection = new GlobalFeedLiveCollectionController(params, callback); const disposers = liveCollection.startSubscription(); const cacheKey = liveCollection.getCacheKey(); disposers.push(() => dropFromCache(cacheKey)); return () => { log(`getGlobalFeed(tmpid: ${timestamp}) > dispose`); disposers.forEach(fn => fn()); }; }; /* end_public_function */