Skip to content

Commit fb8df38

Browse files
author
assaf
committed
get history on refresh
1 parent 82306f9 commit fb8df38

File tree

4 files changed

+98
-23
lines changed

4 files changed

+98
-23
lines changed

App.js

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export default class App extends React.Component {
2727
stationChannel: "",
2828
messages: [],
2929
lastUpdate: null,
30+
firstItem: -1,
31+
refreshing: false,
3032
};
3133

3234
urbit = null
@@ -108,19 +110,41 @@ export default class App extends React.Component {
108110
}
109111

110112
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)
112114
if (!res) {
113115
console.log("Failed to unsubscribe")
114116
}
115117

116-
this.setState({ inChannel: false, messages: [] })
118+
this.setState({
119+
inChannel: false,
120+
messages: [],
121+
refreshing: false,
122+
firstItem: -1
123+
})
117124
}
118125

119-
handleMessages(data) {
120-
var newMessages = this.state.messages.slice()
126+
handleMessages(wire, data) {
127+
var isRefresh = wire.startsWith('/refresh')
121128

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+
}
124148
data.grams.tele.forEach(t => {
125149
var speech = t.thought.statement.speech
126150
var messages = this.processSpeech(
@@ -134,9 +158,20 @@ export default class App extends React.Component {
134158
}
135159
})
136160

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+
}
140175
}
141176
}
142177

@@ -347,6 +382,28 @@ export default class App extends React.Component {
347382
);
348383
}
349384

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+
}
350407

351408
render() {
352409
if (this.state.loading) {
@@ -392,6 +449,8 @@ export default class App extends React.Component {
392449
data={this.state.messages}
393450
renderItem={this.renderItem.bind(this)}
394451
ListFooterComponent={this.listFooter.bind(this)}
452+
refreshing={this.state.refreshing}
453+
onRefresh={this.refresh.bind(this)}
395454
/>
396455

397456
<KeyboardAvoidingView behavior="padding">

JoinStation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default class JoinStation extends React.Component {
3131
await this.urbitAnon.isAuthenticated()
3232

3333
res = await this.urbitAnon.subscribe(
34-
this.state.stationShip,
34+
this.state.stationShip, "/",
3535
'talk', '/afx/' + this.state.stationChannel,
3636
this.props.onMessages, this.props.onPoll)
3737

Urbit.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default class Urbit {
55
this.oryx = null;
66
this.ixor = null;
77
this.event = -1
8+
this.subscriptions = 0
89
}
910

1011
async isAuthenticated() {
@@ -128,7 +129,7 @@ export default class Urbit {
128129
}
129130
}
130131

131-
async subscribe(ship, app, path, callback, pollback) {
132+
async subscribe(ship, wire, app, path, callback, pollback) {
132133
try {
133134
var url = this.server + "/~/is/~" + ship + "/" + app + path + "/.json?PUT"
134135
let response = await fetch(url, {
@@ -139,7 +140,7 @@ export default class Urbit {
139140
method: 'POST',
140141
body: JSON.stringify({
141142
oryx: this.oryx,
142-
wire: path,
143+
wire: wire,
143144
appl: app,
144145
mark: 'json',
145146
ship: ship
@@ -153,10 +154,12 @@ export default class Urbit {
153154
}
154155

155156
var responseJson = await response.json()
156-
this.event = 1;
157-
158-
console.log("Subscribed successfully")
159-
this.poll(callback, pollback);
157+
console.log("Subscribed successfully: " + wire)
158+
this.subscriptions++
159+
if (this.event == -1) {
160+
this.event = 1;
161+
this.poll(callback, pollback);
162+
}
160163
return true
161164

162165
} catch (error) {
@@ -165,7 +168,7 @@ export default class Urbit {
165168
}
166169
}
167170

168-
async unsubscribe(ship, app, path) {
171+
async unsubscribe(ship, wire, app, path) {
169172
try {
170173
var url = this.server + "/~/is/~" + ship + "/" + app + path + "/.json?DELETE"
171174
let response = await fetch(url, {
@@ -176,7 +179,7 @@ export default class Urbit {
176179
method: 'POST',
177180
body: JSON.stringify({
178181
oryx: this.oryx,
179-
wire: path,
182+
wire: wire,
180183
appl: app,
181184
mark: 'json',
182185
ship: ship
@@ -189,9 +192,12 @@ export default class Urbit {
189192
return false
190193
}
191194

192-
// cancel polling
193-
this.event = -1
194-
console.log("Unsubscribed successfully")
195+
this.subscriptions--
196+
if (this.subscriptions == 0) {
197+
// cancel polling
198+
this.event = -1
199+
}
200+
console.log("Unsubscribed successfully: " + wire)
195201

196202
return true
197203

@@ -226,7 +232,10 @@ export default class Urbit {
226232
if (!responseJson.beat) {
227233
// got a change
228234
if (responseJson.type == 'rush') {
229-
callback(responseJson.data.json)
235+
callback(responseJson.from.path, responseJson.data.json)
236+
237+
} else if (responseJson.type == 'quit') {
238+
callback(responseJson.from.path, null)
230239
}
231240
this.event++
232241
}
@@ -250,6 +259,13 @@ export default class Urbit {
250259
return str.slice(0, -1);
251260
}
252261

262+
/**
263+
* format a number the urbit way (with dots)
264+
*/
265+
formatNumber(num) {
266+
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
267+
}
268+
253269
formatStation(stationShip, stationChannel, short) {
254270
return "~" + this.formatShip(stationShip, short) + "/" + stationChannel
255271
}

app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"expo": {
33
"name": "Urbit Mobile Talk",
44
"icon": "./icon.png",
5-
"version": "0.4.0",
5+
"version": "0.5.0",
66
"slug": "urbit-mobile-talk",
77
"sdkVersion": "20.0.0",
88
"ios": {

0 commit comments

Comments
 (0)