@amityco/ts-sdk-react-native 6.24.2-7ddc891.0 → 6.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/.env +26 -26
  2. package/dist/@types/core/events.d.ts +2 -0
  3. package/dist/@types/core/events.d.ts.map +1 -1
  4. package/dist/@types/core/model.d.ts +4 -2
  5. package/dist/@types/core/model.d.ts.map +1 -1
  6. package/dist/@types/core/payload.d.ts +2 -1
  7. package/dist/@types/core/payload.d.ts.map +1 -1
  8. package/dist/@types/domains/stream.d.ts +19 -3
  9. package/dist/@types/domains/stream.d.ts.map +1 -1
  10. package/dist/core/events.d.ts +3 -3
  11. package/dist/core/events.d.ts.map +1 -1
  12. package/dist/core/model/idResolvers.d.ts.map +1 -1
  13. package/dist/core/model/index.d.ts.map +1 -1
  14. package/dist/index.cjs.js +92 -25
  15. package/dist/index.esm.js +92 -25
  16. package/dist/index.umd.js +2 -2
  17. package/dist/reactionRepository/events/onReactorAdded.d.ts.map +1 -1
  18. package/dist/reactionRepository/events/onReactorRemoved.d.ts.map +1 -1
  19. package/dist/streamRepository/api/createStream.d.ts +4 -4
  20. package/dist/streamRepository/api/createStream.d.ts.map +1 -1
  21. package/dist/streamRepository/api/deleteStream.d.ts +4 -4
  22. package/dist/streamRepository/api/deleteStream.d.ts.map +1 -1
  23. package/dist/streamRepository/api/disposeStream.d.ts +4 -4
  24. package/dist/streamRepository/api/disposeStream.d.ts.map +1 -1
  25. package/dist/streamRepository/api/queryStreams.d.ts +4 -4
  26. package/dist/streamRepository/api/queryStreams.d.ts.map +1 -1
  27. package/dist/streamRepository/api/updateStream.d.ts.map +1 -1
  28. package/dist/streamRepository/events/index.d.ts +2 -0
  29. package/dist/streamRepository/events/index.d.ts.map +1 -1
  30. package/dist/streamRepository/events/onStreamFlagged.d.ts +17 -0
  31. package/dist/streamRepository/events/onStreamFlagged.d.ts.map +1 -0
  32. package/dist/streamRepository/events/onStreamRecorded.d.ts +2 -2
  33. package/dist/streamRepository/events/onStreamRecorded.d.ts.map +1 -1
  34. package/dist/streamRepository/events/onStreamStarted.d.ts +2 -2
  35. package/dist/streamRepository/events/onStreamStarted.d.ts.map +1 -1
  36. package/dist/streamRepository/events/onStreamStopped.d.ts +2 -2
  37. package/dist/streamRepository/events/onStreamStopped.d.ts.map +1 -1
  38. package/dist/streamRepository/events/onStreamTerminated.d.ts +17 -0
  39. package/dist/streamRepository/events/onStreamTerminated.d.ts.map +1 -0
  40. package/dist/streamRepository/internalApi/queryStreams.d.ts +3 -3
  41. package/dist/streamRepository/internalApi/queryStreams.d.ts.map +1 -1
  42. package/dist/streamRepository/observers/getStreamById.d.ts +1 -0
  43. package/dist/streamRepository/observers/getStreamById.d.ts.map +1 -1
  44. package/dist/streamRepository/observers/getStreams/GetStreamsLiveCollectionController.d.ts +14 -0
  45. package/dist/streamRepository/observers/getStreams/GetStreamsLiveCollectionController.d.ts.map +1 -0
  46. package/dist/streamRepository/observers/getStreams/GetStreamsPageController.d.ts +14 -0
  47. package/dist/streamRepository/observers/getStreams/GetStreamsPageController.d.ts.map +1 -0
  48. package/dist/streamRepository/observers/getStreams/GetStreamsQueryStreamController.d.ts +15 -0
  49. package/dist/streamRepository/observers/getStreams/GetStreamsQueryStreamController.d.ts.map +1 -0
  50. package/dist/streamRepository/observers/getStreams/getStreams.d.ts +2 -0
  51. package/dist/streamRepository/observers/getStreams/getStreams.d.ts.map +1 -0
  52. package/dist/streamRepository/observers/getStreams/index.d.ts +2 -0
  53. package/dist/streamRepository/observers/getStreams/index.d.ts.map +1 -0
  54. package/dist/utils/linkedObject/index.d.ts +1 -0
  55. package/dist/utils/linkedObject/index.d.ts.map +1 -1
  56. package/dist/utils/linkedObject/streamLinkedObject.d.ts +2 -0
  57. package/dist/utils/linkedObject/streamLinkedObject.d.ts.map +1 -0
  58. package/package.json +1 -1
  59. package/src/@types/core/events.ts +5 -1
  60. package/src/@types/core/model.ts +4 -2
  61. package/src/@types/core/payload.ts +2 -1
  62. package/src/@types/domains/stream.ts +23 -3
  63. package/src/core/model/idResolvers.ts +1 -0
  64. package/src/core/model/index.ts +1 -0
  65. package/src/reactionRepository/events/onReactorAdded.ts +6 -1
  66. package/src/reactionRepository/events/onReactorRemoved.ts +6 -1
  67. package/src/streamRepository/api/createStream.ts +8 -5
  68. package/src/streamRepository/api/deleteStream.ts +6 -4
  69. package/src/streamRepository/api/disposeStream.ts +5 -5
  70. package/src/streamRepository/api/queryStreams.ts +4 -4
  71. package/src/streamRepository/api/updateStream.ts +2 -1
  72. package/src/streamRepository/events/index.ts +2 -0
  73. package/src/streamRepository/events/onStreamFlagged.ts +37 -0
  74. package/src/streamRepository/events/onStreamRecorded.ts +4 -2
  75. package/src/streamRepository/events/onStreamStarted.ts +4 -2
  76. package/src/streamRepository/events/onStreamStopped.ts +4 -2
  77. package/src/streamRepository/events/onStreamTerminated.ts +37 -0
  78. package/src/streamRepository/internalApi/queryStreams.ts +3 -3
  79. package/src/streamRepository/observers/getStreamById.ts +18 -5
  80. package/src/streamRepository/observers/getStreams/GetStreamsLiveCollectionController.ts +114 -0
  81. package/src/streamRepository/observers/getStreams/GetStreamsPageController.ts +23 -0
  82. package/src/streamRepository/observers/getStreams/GetStreamsQueryStreamController.ts +83 -0
  83. package/src/streamRepository/observers/getStreams/getStreams.ts +32 -0
  84. package/src/streamRepository/observers/getStreams/index.ts +1 -0
  85. package/src/streamRepository/observers/tests/getStreamById.test.ts +1 -1
  86. package/src/utils/linkedObject/index.ts +2 -0
  87. package/src/utils/linkedObject/streamLinkedObject.ts +11 -0
@@ -8,18 +8,18 @@ import { ingestInCache } from '~/cache/api/ingestInCache';
8
8
  * const stream = await disposeStream(streamId)
9
9
  * ```
10
10
  *
11
- * Dispose a {@link Amity.Stream}.
11
+ * Dispose a {@link Amity.InternalStream}.
12
12
  * Streaming status will be updated to "ended" and streaming url will be invalidated
13
13
  *
14
- * @param streamId The {@link Amity.Stream} ID to dispose
15
- * @returns the associated {@link Amity.Stream} object
14
+ * @param streamId The {@link Amity.InternalStream} ID to dispose
15
+ * @returns the associated {@link Amity.InternalStream} object
16
16
  *
17
17
  * @category Stream API
18
18
  * @async
19
19
  */
20
20
  export const disposeStream = async (
21
- streamId: Amity.Stream['streamId'],
22
- ): Promise<Amity.Cached<Amity.Stream>> => {
21
+ streamId: Amity.InternalStream['streamId'],
22
+ ): Promise<Amity.Cached<Amity.InternalStream>> => {
23
23
  const client = getActiveClient();
24
24
  client.log('stream/disposeStream', streamId);
25
25
 
@@ -14,10 +14,10 @@ import { ingestInCache } from '~/cache/api/ingestInCache';
14
14
  * const streams = await getStreams()
15
15
  * ```
16
16
  *
17
- * Queries a paginable list of {@link Amity.Stream} objects
17
+ * Queries a paginable list of {@link Amity.InternalStream} objects
18
18
  *
19
19
  * @param query The query parameters
20
- * @returns A page of {@link Amity.Stream} objects
20
+ * @returns A page of {@link Amity.InternalStream} objects
21
21
  *
22
22
  * @category Stream API
23
23
  * @async
@@ -27,9 +27,9 @@ export const queryStreams = async (query?: {
27
27
  statuses?: Amity.StreamStatus[];
28
28
  userPublicIds?: Amity.InternalUser['userId'][];
29
29
  sortBy?: 'lastCreated' | 'firstCreated';
30
- isDeleted?: Amity.Stream['isDeleted'];
30
+ isDeleted?: Amity.InternalStream['isDeleted'];
31
31
  page?: Amity.Page;
32
- }): Promise<Amity.Cached<Amity.Paged<Amity.Stream>>> => {
32
+ }): Promise<Amity.Cached<Amity.Paged<Amity.InternalStream>>> => {
33
33
  const client = getActiveClient();
34
34
  client.log('stream/queryStreams', query);
35
35
 
@@ -1,6 +1,7 @@
1
1
  import { getActiveClient } from '~/client/api';
2
2
 
3
3
  import { ingestInCache } from '~/cache/api/ingestInCache';
4
+ import { LinkedObject } from '~/utils/linkedObject';
4
5
 
5
6
  /* begin_public_function
6
7
  id: stream.update
@@ -38,7 +39,7 @@ export const updateStream = async (
38
39
  const { videoStreamings } = data;
39
40
 
40
41
  return {
41
- data: videoStreamings.find(stream => stream.streamId === streamId)!,
42
+ data: LinkedObject.stream(videoStreamings.find(stream => stream.streamId === streamId)!),
42
43
  cachedAt,
43
44
  };
44
45
  };
@@ -1,3 +1,5 @@
1
1
  export { onStreamStarted } from './onStreamStarted';
2
2
  export { onStreamStopped } from './onStreamStopped';
3
3
  export { onStreamRecorded } from './onStreamRecorded';
4
+ export { onStreamFlagged } from './onStreamFlagged';
5
+ export { onStreamTerminated } from './onStreamTerminated';
@@ -0,0 +1,37 @@
1
+ import { getActiveClient } from '~/client/api';
2
+ import { createEventSubscriber } from '~/core/events';
3
+
4
+ import { ingestInCache } from '~/cache/api/ingestInCache';
5
+
6
+ /**
7
+ * ```js
8
+ * import { onStreamFlagged } from '@amityco/ts-sdk-react-native'
9
+ * const dispose = onStreamFlagged(stream => {
10
+ * // ...
11
+ * })
12
+ * ```
13
+ *
14
+ * Fired when a {@link Amity.InternalStream} has started airing
15
+ *
16
+ * @param callback The function to call when the event was fired
17
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
18
+ *
19
+ * @category Stream Events
20
+ */
21
+ export const onStreamFlagged = (
22
+ callback: Amity.Listener<Amity.InternalStream>,
23
+ ): Amity.Unsubscriber => {
24
+ const client = getActiveClient();
25
+
26
+ const filter = (payload: Amity.StreamPayload) => {
27
+ ingestInCache(payload);
28
+ callback(payload.videoStreamings[0]);
29
+ };
30
+
31
+ return createEventSubscriber(
32
+ client,
33
+ 'stream/onStreamFlagged',
34
+ 'v3.video-streaming.didFlag',
35
+ filter,
36
+ );
37
+ };
@@ -11,14 +11,16 @@ import { ingestInCache } from '~/cache/api/ingestInCache';
11
11
  * })
12
12
  * ```
13
13
  *
14
- * Fired when the recordings of a {@link Amity.Stream} are available
14
+ * Fired when the recordings of a {@link Amity.InternalStream} are available
15
15
  *
16
16
  * @param callback The function to call when the event was fired
17
17
  * @returns an {@link Amity.Unsubscriber} function to stop listening
18
18
  *
19
19
  * @category Stream Events
20
20
  */
21
- export const onStreamRecorded = (callback: Amity.Listener<Amity.Stream>): Amity.Unsubscriber => {
21
+ export const onStreamRecorded = (
22
+ callback: Amity.Listener<Amity.InternalStream>,
23
+ ): Amity.Unsubscriber => {
22
24
  const client = getActiveClient();
23
25
 
24
26
  const filter = (payload: Amity.StreamPayload) => {
@@ -11,14 +11,16 @@ import { ingestInCache } from '~/cache/api/ingestInCache';
11
11
  * })
12
12
  * ```
13
13
  *
14
- * Fired when a {@link Amity.Stream} has started airing
14
+ * Fired when a {@link Amity.InternalStream} has started airing
15
15
  *
16
16
  * @param callback The function to call when the event was fired
17
17
  * @returns an {@link Amity.Unsubscriber} function to stop listening
18
18
  *
19
19
  * @category Stream Events
20
20
  */
21
- export const onStreamStarted = (callback: Amity.Listener<Amity.Stream>): Amity.Unsubscriber => {
21
+ export const onStreamStarted = (
22
+ callback: Amity.Listener<Amity.InternalStream>,
23
+ ): Amity.Unsubscriber => {
22
24
  const client = getActiveClient();
23
25
 
24
26
  const filter = (payload: Amity.StreamPayload) => {
@@ -11,14 +11,16 @@ import { ingestInCache } from '~/cache/api/ingestInCache';
11
11
  * })
12
12
  * ```
13
13
  *
14
- * Fired when a {@link Amity.Stream} has stopped airing
14
+ * Fired when a {@link Amity.InternalStream} has stopped airing
15
15
  *
16
16
  * @param callback The function to call when the event was fired
17
17
  * @returns an {@link Amity.Unsubscriber} function to stop listening
18
18
  *
19
19
  * @category Stream Events
20
20
  */
21
- export const onStreamStopped = (callback: Amity.Listener<Amity.Stream>): Amity.Unsubscriber => {
21
+ export const onStreamStopped = (
22
+ callback: Amity.Listener<Amity.InternalStream>,
23
+ ): Amity.Unsubscriber => {
22
24
  const client = getActiveClient();
23
25
 
24
26
  const filter = (payload: Amity.StreamPayload) => {
@@ -0,0 +1,37 @@
1
+ import { getActiveClient } from '~/client/api';
2
+ import { createEventSubscriber } from '~/core/events';
3
+
4
+ import { ingestInCache } from '~/cache/api/ingestInCache';
5
+
6
+ /**
7
+ * ```js
8
+ * import { onStreamTerminated } from '@amityco/ts-sdk-react-native'
9
+ * const dispose = onStreamTerminated(stream => {
10
+ * // ...
11
+ * })
12
+ * ```
13
+ *
14
+ * Fired when a {@link Amity.InternalStream} has started airing
15
+ *
16
+ * @param callback The function to call when the event was fired
17
+ * @returns an {@link Amity.Unsubscriber} function to stop listening
18
+ *
19
+ * @category Stream Events
20
+ */
21
+ export const onStreamTerminated = (
22
+ callback: Amity.Listener<Amity.InternalStream>,
23
+ ): Amity.Unsubscriber => {
24
+ const client = getActiveClient();
25
+
26
+ const filter = (payload: Amity.StreamPayload) => {
27
+ ingestInCache(payload);
28
+ callback(payload.videoStreamings[0]);
29
+ };
30
+
31
+ return createEventSubscriber(
32
+ client,
33
+ 'stream/onStreamTerminated',
34
+ 'v3.video-streaming.didTerminate',
35
+ filter,
36
+ );
37
+ };
@@ -11,17 +11,17 @@ import { ingestInCache } from '~/cache/api/ingestInCache';
11
11
  * const streams = await getStreams()
12
12
  * ```
13
13
  *
14
- * Queries a paginable list of {@link Amity.Stream} objects
14
+ * Queries a paginable list of {@link Amity.InternalStream} objects
15
15
  *
16
16
  * @param query The query parameters
17
- * @returns A page of {@link Amity.Stream} objects
17
+ * @returns A page of {@link Amity.InternalStream} objects
18
18
  *
19
19
  * @category Stream API
20
20
  * @async
21
21
  */
22
22
  export const queryStreams = async (
23
23
  query?: Amity.QueryStreams,
24
- ): Promise<Amity.Cached<Amity.PageToken<Amity.Stream>>> => {
24
+ ): Promise<Amity.Cached<Amity.PageToken<Amity.InternalStream>>> => {
25
25
  const client = getActiveClient();
26
26
  client.log('stream/queryStreams', query);
27
27
 
@@ -1,10 +1,13 @@
1
1
  /* eslint-disable no-use-before-define */
2
2
 
3
- import { onStreamRecorded, onStreamStarted, onStreamStopped } from '../events';
4
- import { getStream as _getStream } from '../api/getStream';
5
3
  import { liveObject } from '~/utils/liveObject';
6
4
  import { getActiveClient } from '~/client';
7
5
  import { pullFromCache } from '~/cache/api';
6
+ import { onStreamFlagged } from '~/streamRepository/events/onStreamFlagged';
7
+ import { onStreamTerminated } from '~/streamRepository/events/onStreamTerminated';
8
+ import { LinkedObject } from '~/utils/linkedObject';
9
+ import { getStream as _getStream } from '../api/getStream';
10
+ import { onStreamRecorded, onStreamStarted, onStreamStopped } from '../events';
8
11
 
9
12
  /* begin_public_function
10
13
  id: stream.get
@@ -19,6 +22,7 @@ import { pullFromCache } from '~/cache/api';
19
22
  * Fetches a {@link Amity.Stream} object
20
23
  *
21
24
  * @param streamId the ID of the {@link Amity.Stream} to get
25
+ * @param callback
22
26
  * @returns the associated {@link Amity.Stream} object
23
27
  *
24
28
  * @category Stream Live Object
@@ -28,10 +32,19 @@ export const getStreamById = (
28
32
  streamId: Amity.Stream['streamId'],
29
33
  callback: Amity.LiveObjectCallback<Amity.Stream>,
30
34
  ): Amity.Unsubscriber => {
31
- return liveObject(streamId, callback, 'streamId', _getStream, [
35
+ const reactor: Amity.LiveObjectCallback<Amity.InternalStream> = (
36
+ snapshot: Amity.LiveObject<Amity.InternalStream>,
37
+ ) => {
38
+ const { data } = snapshot;
39
+ callback({ ...snapshot, data: data ? LinkedObject.stream(snapshot.data) : data });
40
+ };
41
+
42
+ return liveObject(streamId, reactor, 'streamId', _getStream, [
32
43
  onStreamRecorded,
33
44
  onStreamStarted,
34
45
  onStreamStopped,
46
+ onStreamFlagged,
47
+ onStreamTerminated,
35
48
  ]);
36
49
  };
37
50
  /* end_public_function */
@@ -57,12 +70,12 @@ getStreamById.locally = (
57
70
 
58
71
  if (!client.cache) return;
59
72
 
60
- const cached = pullFromCache<Amity.Stream>(['stream', 'get', streamId]);
73
+ const cached = pullFromCache<Amity.InternalStream>(['stream', 'get', streamId]);
61
74
 
62
75
  if (!cached) return;
63
76
 
64
77
  return {
65
- data: cached.data,
78
+ data: LinkedObject.stream(cached.data),
66
79
  cachedAt: cached.cachedAt,
67
80
  };
68
81
  };
@@ -0,0 +1,114 @@
1
+ import hash from 'object-hash';
2
+ import { LiveCollectionController } from '~/core/liveCollection/LiveCollectionController';
3
+ import { pullFromCache, pushToCache } from '~/cache/api';
4
+ import { LinkedObject } from '~/utils/linkedObject';
5
+ import { filterByPropEquality, sortByFirstCreated, sortByLastCreated } from '~/core/query';
6
+ import {
7
+ onStreamFlagged,
8
+ onStreamRecorded,
9
+ onStreamStarted,
10
+ onStreamStopped,
11
+ onStreamTerminated,
12
+ } from '~/streamRepository/events';
13
+ import { GetStreamsPageController } from '~/streamRepository/observers/getStreams/GetStreamsPageController';
14
+ import { GetStreamsQueryStreamController } from '~/streamRepository/observers/getStreams/GetStreamsQueryStreamController';
15
+
16
+ export class GetStreamsLiveCollectionController extends LiveCollectionController<
17
+ 'stream',
18
+ Amity.StreamLiveCollection,
19
+ Amity.Stream,
20
+ GetStreamsPageController
21
+ > {
22
+ private queryStreamController: GetStreamsQueryStreamController;
23
+
24
+ private query: Amity.StreamLiveCollection;
25
+
26
+ constructor(
27
+ query: Amity.StreamLiveCollection,
28
+ callback: Amity.LiveCollectionCallback<Amity.Stream>,
29
+ ) {
30
+ const queryStreamId = hash(query);
31
+ const cacheKey = ['streams', 'collection', queryStreamId];
32
+ const paginationController = new GetStreamsPageController(query);
33
+
34
+ super(paginationController, queryStreamId, cacheKey, callback);
35
+
36
+ this.query = query;
37
+
38
+ this.queryStreamController = new GetStreamsQueryStreamController(
39
+ this.query,
40
+ this.cacheKey,
41
+ this.notifyChange.bind(this),
42
+ paginationController,
43
+ );
44
+
45
+ this.paginationController = paginationController;
46
+ this.callback = callback.bind(this);
47
+ this.loadPage({ initial: true });
48
+ }
49
+
50
+ notifyChange({ origin, loading, error }: Amity.LiveCollectionNotifyParams) {
51
+ const collection = pullFromCache<Amity.StreamLiveCollectionCache>(this.cacheKey)?.data;
52
+ if (!collection) return;
53
+
54
+ let data = collection.data
55
+ .map(streamId => pullFromCache<Amity.InternalStream>(['stream', 'get', streamId])!)
56
+ .filter(Boolean)
57
+ .map(stream => LinkedObject.stream(stream.data));
58
+
59
+ if (!this.shouldNotify(data) && origin === 'event') return;
60
+
61
+ data = this.applyFilter(data);
62
+
63
+ this.callback({
64
+ onNextPage: () =>
65
+ this.loadPage({ initial: false, direction: Amity.LiveCollectionPageDirection.NEXT }),
66
+ data,
67
+ hasNextPage: !!this.paginationController.getNextToken(),
68
+ loading,
69
+ error,
70
+ });
71
+ }
72
+
73
+ startSubscription() {
74
+ return this.queryStreamController.subscribeRTE([
75
+ { fn: onStreamRecorded, action: 'onStreamRecorded' },
76
+ { fn: onStreamStarted, action: 'onStreamStarted' },
77
+ { fn: onStreamStopped, action: 'onStreamStopped' },
78
+ { fn: onStreamFlagged, action: 'onStreamFlagged' },
79
+ { fn: onStreamTerminated, action: 'onStreamTerminated' },
80
+ ]);
81
+ }
82
+
83
+ protected setup() {
84
+ const collection = pullFromCache<Amity.StreamLiveCollectionCache>(this.cacheKey)?.data;
85
+ if (!collection) {
86
+ pushToCache(this.cacheKey, {
87
+ data: [],
88
+ params: {},
89
+ });
90
+ }
91
+ }
92
+
93
+ protected persistModel(response: Amity.StreamPayload) {
94
+ this.queryStreamController.saveToMainDB(response);
95
+ }
96
+
97
+ protected persistQueryStream({
98
+ response,
99
+ direction,
100
+ refresh,
101
+ }: Amity.LiveCollectionPersistQueryStreamParams<'stream'>) {
102
+ this.queryStreamController.appendToQueryStream(response, direction, refresh);
103
+ }
104
+
105
+ private applyFilter = (data: Amity.InternalStream[]): Amity.InternalStream[] => {
106
+ let streams = filterByPropEquality(data, 'isDeleted', this.query.isDeleted);
107
+
108
+ streams = streams.sort(
109
+ this.query.sortBy === 'lastCreated' ? sortByLastCreated : sortByFirstCreated,
110
+ );
111
+
112
+ return streams;
113
+ };
114
+ }
@@ -0,0 +1,23 @@
1
+ import { PaginationController } from '~/core/liveCollection/PaginationController';
2
+ import { COLLECTION_DEFAULT_PAGINATION_LIMIT } from '~/utils/constants';
3
+
4
+ export class GetStreamsPageController extends PaginationController<
5
+ 'stream',
6
+ Amity.StreamLiveCollection
7
+ > {
8
+ async getRequest(queryParams: Amity.StreamLiveCollection, token: string | undefined) {
9
+ const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT, ...params } = queryParams;
10
+ const options = token ? { token } : { limit };
11
+
12
+ const { data: queryResponse } = await this.http.get<
13
+ { results: Amity.StreamPayload } & Amity.Pagination
14
+ >(`/api/v3/video-streaming`, {
15
+ params: {
16
+ ...params,
17
+ options,
18
+ },
19
+ });
20
+
21
+ return { ...queryResponse.results, paging: queryResponse.paging };
22
+ }
23
+ }
@@ -0,0 +1,83 @@
1
+ import { QueryStreamController } from '~/core/liveCollection/QueryStreamController';
2
+ import { getActiveClient } from '~/client';
3
+ import { ingestInCache } from '~/cache/api/ingestInCache';
4
+ import { GetStreamsPageController } from '~/streamRepository/observers/getStreams/GetStreamsPageController';
5
+ import { getResolver } from '~/core/model';
6
+ import { pullFromCache, pushToCache } from '~/cache/api';
7
+
8
+ export class GetStreamsQueryStreamController extends QueryStreamController<
9
+ Amity.StreamPayload,
10
+ Amity.StreamLiveCollection
11
+ > {
12
+ private notifyChange: (params: Amity.LiveCollectionNotifyParams) => void;
13
+
14
+ private paginationController: GetStreamsPageController;
15
+
16
+ constructor(
17
+ query: Amity.StreamLiveCollection,
18
+ cacheKey: string[],
19
+ notifyChange: (params: Amity.LiveCollectionNotifyParams) => void,
20
+ paginationController: GetStreamsPageController,
21
+ ) {
22
+ super(query, cacheKey);
23
+ this.notifyChange = notifyChange;
24
+ this.paginationController = paginationController;
25
+ }
26
+
27
+ // eslint-disable-next-line class-methods-use-this
28
+ saveToMainDB(response: Amity.StreamPayload) {
29
+ const client = getActiveClient();
30
+ const cachedAt = client.cache && Date.now();
31
+
32
+ if (client.cache) {
33
+ ingestInCache(response, { cachedAt });
34
+ }
35
+ }
36
+
37
+ appendToQueryStream(
38
+ response: Amity.StreamPayload & Partial<Amity.Pagination>,
39
+ direction: Amity.LiveCollectionPageDirection,
40
+ refresh = false,
41
+ ) {
42
+ if (refresh) {
43
+ pushToCache(this.cacheKey, {
44
+ data: response.videoStreamings.map(getResolver('stream')),
45
+ query: this.query,
46
+ });
47
+ } else {
48
+ const collection = pullFromCache<Amity.StreamLiveCollectionCache>(this.cacheKey)?.data;
49
+
50
+ const messages = collection?.data ?? [];
51
+
52
+ pushToCache(this.cacheKey, {
53
+ ...collection,
54
+ data:
55
+ direction === 'next'
56
+ ? [...new Set([...messages, ...response.videoStreamings.map(getResolver('stream'))])]
57
+ : [...new Set([...response.videoStreamings.map(getResolver('stream')), ...messages])],
58
+ });
59
+ }
60
+ }
61
+
62
+ reactor(action: string) {
63
+ return (payload: Amity.InternalStream) => {
64
+ const collection = pullFromCache<Amity.StreamLiveCollectionCache>(this.cacheKey)?.data;
65
+ if (!collection) return;
66
+
67
+ collection.data = [...new Set([payload.streamId, ...collection.data])];
68
+
69
+ pushToCache(this.cacheKey, collection);
70
+
71
+ this.notifyChange({ origin: Amity.LiveDataOrigin.EVENT, loading: false });
72
+ };
73
+ }
74
+
75
+ subscribeRTE(
76
+ createSubscriber: {
77
+ fn: (reactor: Amity.Listener<Amity.InternalStream>) => Amity.Unsubscriber;
78
+ action: string;
79
+ }[],
80
+ ) {
81
+ return createSubscriber.map(subscriber => subscriber.fn(this.reactor(subscriber.action)));
82
+ }
83
+ }
@@ -0,0 +1,32 @@
1
+ import { getActiveClient } from '~/client';
2
+ import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
3
+ import { dropFromCache } from '~/cache/api';
4
+ import { GetStreamsLiveCollectionController } from '~/streamRepository/observers/getStreams/GetStreamsLiveCollectionController';
5
+
6
+ export const getStreams = (
7
+ params: Amity.StreamLiveCollection,
8
+ callback: Amity.LiveCollectionCallback<Amity.Stream>,
9
+ config?: Amity.LiveCollectionConfig,
10
+ ) => {
11
+ const { log, cache, userId } = getActiveClient();
12
+
13
+ if (!cache) {
14
+ console.log(ENABLE_CACHE_MESSAGE);
15
+ }
16
+
17
+ const timestamp = Date.now();
18
+ log(`getStreams(tmpid: ${timestamp}) > listen`);
19
+
20
+ const liveCollection = new GetStreamsLiveCollectionController(params, callback);
21
+ const disposers = liveCollection.startSubscription();
22
+ const cacheKey = liveCollection.getCacheKey();
23
+
24
+ disposers.push(() => {
25
+ dropFromCache(cacheKey);
26
+ });
27
+
28
+ return () => {
29
+ log(`getStreams(tmpid: ${timestamp}) > dispose`);
30
+ disposers.forEach(fn => fn());
31
+ };
32
+ };
@@ -0,0 +1 @@
1
+ export * from './getStreams';
@@ -6,7 +6,7 @@ const STREAM_ID = streamResponse.data.videoStreamings[0].streamId;
6
6
 
7
7
  const getSnapshot = (params?: Record<string, any>) => {
8
8
  return {
9
- data: undefined as undefined | Amity.Stream,
9
+ data: undefined as undefined | Amity.InternalStream,
10
10
  loading: true,
11
11
  error: undefined as any,
12
12
  ...params,
@@ -1,4 +1,5 @@
1
1
  import { storyLinkedObject } from '~/utils/linkedObject/storyLinkedObject';
2
+ import { streamLinkedObject } from '~/utils/linkedObject/streamLinkedObject';
2
3
  import { categoryLinkedObject } from './categoryLinkedObject';
3
4
  import { commentLinkedObject } from './commentLinkedObject';
4
5
  import { userLinkedObject } from './userLinkedObject';
@@ -13,6 +14,7 @@ export const LinkedObject = {
13
14
  post: postLinkedObject,
14
15
  user: userLinkedObject,
15
16
  category: categoryLinkedObject,
17
+ stream: streamLinkedObject,
16
18
  story: storyLinkedObject,
17
19
  storyTarget: storyTargetLinkedObject,
18
20
  message: messageLinkedObject,
@@ -0,0 +1,11 @@
1
+ import { pullFromCache } from '~/cache/api';
2
+
3
+ export const streamLinkedObject = (stream: Amity.InternalStream): Amity.Stream => {
4
+ return {
5
+ ...stream,
6
+ get moderation() {
7
+ return pullFromCache<Amity.StreamModeration>(['streamModeration', 'get', stream.streamId])
8
+ ?.data;
9
+ },
10
+ };
11
+ };