11import { DraftFilters , DraftResponse , DraftSort , Pager , StateStore , StreamChat } from 'stream-chat' ;
2+ import { WithSubscriptions } from './WithSubscription' ;
23
34export type QueryDraftOptions = Pager & {
45 filter ?: DraftFilters ;
@@ -34,7 +35,7 @@ export type DraftManagerPagination = {
3435 nextCursor : string | null ;
3536} ;
3637
37- export class DraftsManager {
38+ export class DraftsManager extends WithSubscriptions {
3839 public readonly state : StateStore < DraftManagerState > ;
3940 private client : StreamChat ;
4041 private draftsByIdGetterCache : {
@@ -43,6 +44,7 @@ export class DraftsManager {
4344 } ;
4445
4546 constructor ( { client } : { client : StreamChat } ) {
47+ super ( ) ;
4648 this . client = client ;
4749 this . state = new StateStore < DraftManagerState > ( DRAFT_MANAGER_INITIAL_STATE ) ;
4850 this . draftsByIdGetterCache = { drafts : [ ] , draftsById : { } } ;
@@ -78,6 +80,99 @@ export class DraftsManager {
7880 this . state . partialNext ( { active : false } ) ;
7981 } ;
8082
83+ private subscribeDraftUpdated = ( ) =>
84+ this . client . on ( 'draft.updated' , ( event ) => {
85+ if ( ! event . draft ) {
86+ return ;
87+ }
88+
89+ const draftData = event . draft ;
90+
91+ const { drafts } = this . state . getLatestValue ( ) ;
92+
93+ const newDrafts = [ ...drafts ] ;
94+
95+ let existingDraftIndex = - 1 ;
96+
97+ if ( draftData . parent_id ) {
98+ existingDraftIndex = drafts . findIndex (
99+ ( draft ) =>
100+ draft . parent_id === draftData . parent_id &&
101+ draft . channel ?. cid === draftData . channel ?. cid ,
102+ ) ;
103+ } else {
104+ existingDraftIndex = drafts . findIndex (
105+ ( draft ) => draft . channel ?. cid === draftData . channel ?. cid ,
106+ ) ;
107+ }
108+
109+ if ( existingDraftIndex !== - 1 ) {
110+ newDrafts [ existingDraftIndex ] = draftData ;
111+ } else {
112+ newDrafts . push ( draftData ) ;
113+ }
114+
115+ this . state . partialNext ( { drafts : newDrafts } ) ;
116+ } ) . unsubscribe ;
117+
118+ private subscribeDraftDeleted = ( ) =>
119+ this . client . on ( 'draft.deleted' , ( event ) => {
120+ if ( ! event . draft ) {
121+ return ;
122+ }
123+
124+ const { drafts } = this . state . getLatestValue ( ) ;
125+
126+ const newDrafts = [ ...drafts ] ;
127+
128+ const draftData = event . draft ;
129+
130+ let existingDraftIndex = - 1 ;
131+
132+ if ( draftData . parent_id ) {
133+ existingDraftIndex = drafts . findIndex (
134+ ( draft ) =>
135+ draft . parent_id === draftData . parent_id &&
136+ draft . channel ?. cid === draftData . channel ?. cid ,
137+ ) ;
138+ } else {
139+ existingDraftIndex = drafts . findIndex (
140+ ( draft ) => draft . channel ?. cid === draftData . channel ?. cid ,
141+ ) ;
142+ }
143+
144+ if ( existingDraftIndex !== - 1 ) {
145+ newDrafts . splice ( existingDraftIndex , 1 ) ;
146+ }
147+
148+ this . state . partialNext ( {
149+ drafts : newDrafts ,
150+ } ) ;
151+ } ) . unsubscribe ;
152+
153+ private subscribeReloadOnActivation = ( ) =>
154+ this . state . subscribeWithSelector (
155+ ( nextValue ) => ( { active : nextValue . active } ) ,
156+ ( { active } ) => {
157+ if ( active ) {
158+ this . reload ( ) ;
159+ }
160+ } ,
161+ ) ;
162+
163+ public registerSubscriptions = ( ) => {
164+ if ( this . hasSubscriptions ) {
165+ return ;
166+ }
167+ this . addUnsubscribeFunction ( this . subscribeReloadOnActivation ( ) ) ;
168+ this . addUnsubscribeFunction ( this . subscribeDraftUpdated ( ) ) ;
169+ this . addUnsubscribeFunction ( this . subscribeDraftDeleted ( ) ) ;
170+ } ;
171+
172+ public unregisterSubscriptions = ( ) => {
173+ return super . unregisterSubscriptions ( ) ;
174+ } ;
175+
81176 public reload = async ( { force = false } = { } ) => {
82177 const { drafts, isDraftsOrderStale, pagination, ready } = this . state . getLatestValue ( ) ;
83178 if ( pagination . isLoading ) {
0 commit comments