@@ -27,6 +27,8 @@ export default class App extends React.Component {
27
27
stationChannel : "" ,
28
28
messages : [ ] ,
29
29
lastUpdate : null ,
30
+ firstItem : - 1 ,
31
+ refreshing : false ,
30
32
} ;
31
33
32
34
urbit = null
@@ -108,19 +110,41 @@ export default class App extends React.Component {
108
110
}
109
111
110
112
async doLeave ( ) {
111
- res = await this . urbitAnon . unsubscribe ( this . state . stationShip , 'talk' , '/afx/' + this . state . stationChannel )
113
+ res = await this . urbitAnon . unsubscribe ( this . state . stationShip , '/' , ' talk', '/afx/' + this . state . stationChannel )
112
114
if ( ! res ) {
113
115
console . log ( "Failed to unsubscribe" )
114
116
}
115
117
116
- this . setState ( { inChannel : false , messages : [ ] } )
118
+ this . setState ( {
119
+ inChannel : false ,
120
+ messages : [ ] ,
121
+ refreshing : false ,
122
+ firstItem : - 1
123
+ } )
117
124
}
118
125
119
- handleMessages ( data ) {
120
- var newMessages = this . state . messages . slice ( )
126
+ handleMessages ( wire , data ) {
127
+ var isRefresh = wire . startsWith ( '/refresh' )
121
128
122
- if ( data . grams ) {
123
- this . setState ( { loading : false } )
129
+ if ( data == null ) {
130
+ // got %quit
131
+ console . log ( "got %quit for: " + wire )
132
+ if ( isRefresh ) {
133
+ this . setState ( { refreshing : false } )
134
+
135
+ } else {
136
+ //TODO resubscribe
137
+ }
138
+
139
+ } else if ( data . grams ) {
140
+ var newMessages = [ ]
141
+ if ( ! isRefresh ) {
142
+ newMessages = this . state . messages . slice ( )
143
+ }
144
+
145
+ if ( this . state . firstItem == - 1 || data . grams . num < this . state . firstItem ) {
146
+ this . setState ( { firstItem : data . grams . num } )
147
+ }
124
148
data . grams . tele . forEach ( t => {
125
149
var speech = t . thought . statement . speech
126
150
var messages = this . processSpeech (
@@ -134,9 +158,20 @@ export default class App extends React.Component {
134
158
}
135
159
} )
136
160
137
- this . setState ( {
138
- messages : newMessages
139
- } )
161
+ if ( isRefresh ) {
162
+ newMessages = newMessages . concat ( this . state . messages . slice ( ) )
163
+
164
+ var path = wire . substring ( '/refresh' . length )
165
+ this . urbitAnon . unsubscribe ( this . state . stationShip , wire , 'talk' , path )
166
+ }
167
+
168
+ this . setState ( { messages : newMessages } )
169
+ if ( isRefresh ) {
170
+ this . setState ( { refreshing : false } )
171
+
172
+ } else {
173
+ this . setState ( { loading : false } )
174
+ }
140
175
}
141
176
}
142
177
@@ -347,6 +382,28 @@ export default class App extends React.Component {
347
382
) ;
348
383
}
349
384
385
+ async refresh ( ) {
386
+ if ( this . state . firstItem == 0 || this . state . refreshing ) {
387
+ return
388
+ }
389
+
390
+ this . setState ( { refreshing : true } )
391
+
392
+ var maxFetchItems = 32
393
+ var end = this . state . firstItem + 1
394
+ var start = Math . max ( 0 , end - maxFetchItems )
395
+ var path = '/afx/' + this . state . stationChannel
396
+ + '/' + this . urbit . formatNumber ( start )
397
+ + '/' + this . urbit . formatNumber ( end )
398
+
399
+ var res = await this . urbitAnon . subscribe ( this . state . stationShip , '/refresh' + path ,
400
+ 'talk' , path , this . handleMessages . bind ( this ) )
401
+
402
+ if ( ! res ) {
403
+ console . log ( "refresh failed" )
404
+ this . setState ( { refreshing : false } )
405
+ }
406
+ }
350
407
351
408
render ( ) {
352
409
if ( this . state . loading ) {
@@ -392,6 +449,8 @@ export default class App extends React.Component {
392
449
data = { this . state . messages }
393
450
renderItem = { this . renderItem . bind ( this ) }
394
451
ListFooterComponent = { this . listFooter . bind ( this ) }
452
+ refreshing = { this . state . refreshing }
453
+ onRefresh = { this . refresh . bind ( this ) }
395
454
/>
396
455
397
456
< KeyboardAvoidingView behavior = "padding" >
0 commit comments