11import { BaseStore } from '../common/BaseStore' ;
2- import { action , IObservableArray , observable , reaction , runInAction } from 'mobx' ;
2+ import { action , IObservableArray , makeObservable , observable , reaction , runInAction } from 'mobx' ;
33import axios , { AxiosResponse } from 'axios' ;
44import * as config from '../config' ;
55import { createTransformer } from 'mobx-utils' ;
@@ -22,15 +22,33 @@ interface PendingDelete {
2222}
2323
2424export class MessagesStore {
25- @ observable private accessor state : Record < string , MessagesState > = { } ;
26- @ observable private accessor pendingDeletes : Map < number , PendingDelete > = observable . map ( ) ;
25+ private state : Record < string , MessagesState > = { } ;
26+ private pendingDeletes : Map < number , PendingDelete > = observable . map ( ) ;
2727
2828 private loading = false ;
2929
3030 public constructor (
3131 private readonly appStore : BaseStore < IApplication > ,
3232 private readonly snack : SnackReporter
3333 ) {
34+ makeObservable < MessagesStore , 'state' | 'pendingDeletes' | 'removeFromList' | 'clear' > (
35+ this ,
36+ {
37+ state : observable ,
38+ pendingDeletes : observable ,
39+ loadMore : action ,
40+ publishSingleMessage : action ,
41+ removeByApp : action ,
42+ addPendingDelete : action ,
43+ cancelPendingDelete : action ,
44+ executePendingDeletes : action ,
45+ removeSingle : action ,
46+ clearAll : action ,
47+ refreshByApp : action ,
48+ removeFromList : action ,
49+ clear : action ,
50+ }
51+ ) ;
3452 reaction ( ( ) => appStore . getItems ( ) , this . createEmptyStatesForApps ) ;
3553 }
3654
@@ -45,7 +63,6 @@ export class MessagesStore {
4563
4664 public canLoadMore = ( appId : number ) => this . stateOf ( appId , /*create*/ false ) . hasMore ;
4765
48- @action
4966 public loadMore = async ( appId : number ) => {
5067 const state = this . stateOf ( appId ) ;
5168 if ( ! state . hasMore || this . loading ) {
@@ -70,7 +87,6 @@ export class MessagesStore {
7087 return Promise . resolve ( ) ;
7188 } ;
7289
73- @action
7490 public publishSingleMessage = ( message : IMessage ) => {
7591 if ( this . exists ( AllMessages ) ) {
7692 this . stateOf ( AllMessages ) . messages . unshift ( message ) ;
@@ -80,7 +96,6 @@ export class MessagesStore {
8096 }
8197 } ;
8298
83- @action
8499 public removeByApp = async ( appId : number ) => {
85100 if ( appId === AllMessages ) {
86101 await axios . delete ( config . get ( 'url' ) + 'message' ) ;
@@ -95,11 +110,9 @@ export class MessagesStore {
95110 await this . loadMore ( appId ) ;
96111 } ;
97112
98- @action
99113 public addPendingDelete = ( pending : PendingDelete ) =>
100114 this . pendingDeletes . set ( pending . message . id , pending ) ;
101115
102- @action
103116 public cancelPendingDelete = ( message : IMessage ) : boolean => {
104117 const pending = this . pendingDeletes . get ( message . id ) ;
105118 if ( pending ) {
@@ -109,13 +122,11 @@ export class MessagesStore {
109122 return ! ! pending ;
110123 } ;
111124
112- @action
113125 public executePendingDeletes = ( ) =>
114126 Array . from ( this . pendingDeletes . values ( ) ) . forEach ( ( { message} ) => this . removeSingle ( message ) ) ;
115127
116128 public visible = ( message : number ) : boolean => ! this . pendingDeletes . has ( message ) ;
117129
118- @action
119130 public removeSingle = async ( message : IMessage ) => {
120131 if ( ! this . pendingDeletes . has ( message . id ) ) {
121132 return ;
@@ -152,21 +163,18 @@ export class MessagesStore {
152163 this . snack ( `Message sent to ${ app . name } ` ) ;
153164 } ;
154165
155- @action
156166 public clearAll = ( ) => {
157167 this . state = { } ;
158168 this . createEmptyStatesForApps ( this . appStore . getItems ( ) ) ;
159169 } ;
160170
161- @action
162171 public refreshByApp = async ( appId : number ) => {
163172 this . clearAll ( ) ;
164173 this . loadMore ( appId ) ;
165174 } ;
166175
167176 public exists = ( id : number ) => this . stateOf ( id ) . loaded ;
168177
169- @action
170178 private removeFromList ( messages : IMessage [ ] , messageToDelete : IMessage ) : false | number {
171179 if ( messages ) {
172180 const index = messages . findIndex ( ( message ) => message . id === messageToDelete . id ) ;
@@ -178,7 +186,6 @@ export class MessagesStore {
178186 return false ;
179187 }
180188
181- @action
182189 private clear = ( appId : number ) => ( this . state [ appId ] = this . emptyState ( ) ) ;
183190
184191 private fetchMessages = (
0 commit comments