@@ -30,17 +30,14 @@ const EVENT_LIST_MAX_SIZE = 50;
30
30
export class EventStore {
31
31
notifications = xmpp . message . filter ( msg => msg . notification ) ;
32
32
@observable loading : boolean = false ;
33
- processedEvents : number = 0 ;
34
33
35
34
constructor ( ) {
36
35
this . notifications . onValue ( this . onNotification ) ;
37
36
}
38
37
39
38
async start ( ) {
40
39
this . loading = true ;
41
- if ( ! model . events . version ) {
42
- await this . accumulateItems ( ) ;
43
- }
40
+ await this . accumulateItems ( model . events . version ) ;
44
41
autorun ( ( ) => {
45
42
if ( model . connected ) this . request ( ) ;
46
43
} ) ;
@@ -49,7 +46,7 @@ export class EventStore {
49
46
}
50
47
51
48
request ( ) {
52
- home . request ( model . events . version ) ;
49
+ home . request ( model . events . nextVersion || model . events . version ) ;
53
50
}
54
51
55
52
@action
@@ -104,8 +101,7 @@ export class EventStore {
104
101
}
105
102
}
106
103
107
- const eventMessage = bot ? new EventBotShare ( id , botFactory . create ( { id : bot . id , server : bot . server } ) , time , msg ) : new EventMessage ( id , msg . from , msg ) ;
108
- return eventMessage ;
104
+ return bot ? new EventBotShare ( id , botFactory . create ( { id : bot . id , server : bot . server } ) , time , msg ) : new EventMessage ( id , msg . from , msg ) ;
109
105
} else {
110
106
log . log ( '& UNSUPPORTED ITEM!' , item , { level : log . levels . WARNING } ) ;
111
107
}
@@ -124,22 +120,29 @@ export class EventStore {
124
120
nextVersion : ?string = null ;
125
121
126
122
async onNotification ( { notification, delay} ) {
127
- log . log ( 'Notification ' , notification ) ;
123
+ log . log ( 'onNotification ' , notification ) ;
128
124
let item ;
129
125
if ( notification [ 'reference-changed' ] ) {
130
126
const { id , server } = notification [ 'reference-changed' ] . bot ;
131
127
const bot = botFactory . create ( { id, server} ) ;
132
128
// don't download posts for the bot (it will be loaded later)
133
129
await botStore . download ( bot , false ) ;
134
130
} else if ( notification . item ) {
135
- item = notification . item ;
131
+ const { item } = notification ;
136
132
const newItem = this . processItem ( item , delay ) ;
137
133
if ( ! newItem ) {
138
134
return ;
139
135
}
140
- model . events . listToAdd . push ( newItem ) ;
141
136
if ( newItem . bot ) {
142
- await botStore . download ( newItem . bot ) ;
137
+ if ( ! model . eventBots . get ( newItem . bot . id ) ) {
138
+ botStore . download ( newItem . bot , false ) . then ( ( ) => {
139
+ model . eventBots . add ( newItem . bot ) ;
140
+ model . events . listToAdd . push ( newItem ) ;
141
+ } ) ;
142
+ } else {
143
+ log . log ( `Bot ${ newItem . bot . id } already exists!` ) ;
144
+ model . events . listToAdd . push ( newItem ) ;
145
+ }
143
146
}
144
147
if ( item . version ) model . events . nextVersion = item . version ;
145
148
else log . log ( 'item has no version!' , item ) ;
@@ -154,7 +157,6 @@ export class EventStore {
154
157
}
155
158
156
159
finish ( ) {
157
- this . processedEvents = 0 ;
158
160
this . loading = false ;
159
161
}
160
162
@@ -172,28 +174,37 @@ export class EventStore {
172
174
}
173
175
174
176
@action
175
- async accumulateItems ( count : number = EVENT_PAGE_SIZE , current : number = 0 ) : Promise < void > {
177
+ async accumulateItems ( version , count : number = EVENT_PAGE_SIZE , current : number = 0 ) : Promise < void > {
176
178
const { earliestId} = model . events ;
177
- const data = await home . items ( earliestId , count , true ) ;
178
- if ( ! data . items . length ) {
179
+ const data = version ? await home . items ( null , count , true , version ) : await home . items ( earliestId , count , true ) ;
180
+ if ( ! version && ! data . items . length ) {
179
181
model . events . finished = true ;
180
182
return ;
181
183
}
182
184
data . bots . forEach ( bot => model . eventBots . add ( botFactory . create ( bot ) ) ) ;
183
185
let newEventCount = 0 ;
184
186
data . items . map ( this . processItem ) . forEach ( ( p ) => {
185
187
if ( p ) {
186
- model . events . add ( p ) ;
188
+ if ( version ) {
189
+ model . events . listToAdd . push ( p ) ;
190
+ } else {
191
+ model . events . add ( p ) ;
192
+ }
187
193
newEventCount += 1 ;
188
194
}
189
195
} ) ;
190
- const latest = data . version ;
191
- if ( latest ) model . events . version = latest ;
196
+ if ( data . version ) {
197
+ if ( version ) {
198
+ model . events . nextVersion = data . version ;
199
+ } else if ( ! model . events . version ) {
200
+ model . events . version = data . version ;
201
+ }
202
+ }
192
203
193
- if ( newEventCount + current < count && this . processedEvents < data . count ) {
204
+ if ( newEventCount + current < count && data . count ) {
194
205
// account for the case where none are processed and earliestId remains the same
195
206
if ( newEventCount === 0 && model . events . earliestId === earliestId ) count += EVENT_PAGE_SIZE ;
196
- await this . accumulateItems ( count , newEventCount + current ) ;
207
+ await this . accumulateItems ( version , count , newEventCount + current ) ;
197
208
}
198
209
}
199
210
@@ -222,10 +233,6 @@ export class EventStore {
222
233
listToAdd . forEach ( ( e ) => {
223
234
try {
224
235
model . events . add ( e ) ;
225
- // we must add referenced bot to 'eventBots' for serialization (HS contains only references!)
226
- if ( e . bot ) {
227
- model . eventBots . add ( e . bot ) ;
228
- }
229
236
} catch ( err ) {
230
237
log . log ( 'Incorporate updates error, could not add' , e , err ) ;
231
238
}
0 commit comments