Added ability to quit channel from rver

This commit is contained in:
Logan Allen 2019-06-13 14:53:04 -07:00
parent 15363bbcad
commit 67da73ad77

View File

@ -262,6 +262,9 @@
:: %unsubscribe: unsubscribes from an application path :: %unsubscribe: unsubscribes from an application path
:: ::
[%unsubscribe request-id=@ud subscription-id=@ud] [%unsubscribe request-id=@ud subscription-id=@ud]
:: %delete: kills a channel
::
[%delete ~]
== ==
:: channel-timeout: the delay before a channel should be reaped :: channel-timeout: the delay before a channel should be reaped
:: ::
@ -328,6 +331,8 @@
%. item %. item
%+ pe %unsubscribe %+ pe %unsubscribe
(ot id+ni subscription+ni ~) (ot id+ni subscription+ni ~)
?: =('delete' u.maybe-key)
`[%delete ~]
:: if we reached this, we have an invalid action key. fail parsing. :: if we reached this, we have an invalid action key. fail parsing.
:: ::
~ ~
@ -478,6 +483,7 @@
// the functions will be called, and the outstanding poke will be // the functions will be called, and the outstanding poke will be
// removed after calling the success or failure function. // removed after calling the success or failure function.
// //
this.outstandingPokes = new Map(); this.outstandingPokes = new Map();
// a registry of requestId to subscription functions. // a registry of requestId to subscription functions.
@ -489,6 +495,14 @@
// disconnect function may be called exactly once. // disconnect function may be called exactly once.
// //
this.outstandingSubscriptions = new Map(); this.outstandingSubscriptions = new Map();
this.deleteOnUnload();
}
deleteOnUnload() {
window.addEventListener('beforeunload', (event) => {
this.delete();
});
} }
// sends a poke to an app on an urbit ship // sends a poke to an app on an urbit ship
@ -528,6 +542,16 @@
return id; return id;
} }
// quit the channel
//
delete() {
var id = this.nextId();
this.sendJSONToChannel({
"id": id,
"action": "delete"
}, false);
}
// unsubscribe to a specific subscription // unsubscribe to a specific subscription
// //
unsubscribe(subscriptionId) { unsubscribe(subscriptionId) {
@ -536,12 +560,12 @@
"id": id, "id": id,
"action": "unsubscribe", "action": "unsubscribe",
"subscription": subscriptionId "subscription": subscriptionId
}); });
} }
// sends a JSON command command to the server. // sends a JSON command command to the server.
// //
sendJSONToChannel(j) { sendJSONToChannel(j, connectAgain = true) {
var req = new XMLHttpRequest(); var req = new XMLHttpRequest();
req.open("PUT", this.channelURL()); req.open("PUT", this.channelURL());
req.setRequestHeader("Content-Type", "application/json"); req.setRequestHeader("Content-Type", "application/json");
@ -563,7 +587,9 @@
this.lastEventId = this.lastAcknowledgedEventId; this.lastEventId = this.lastAcknowledgedEventId;
} }
this.connectIfDisconnected(); if (connectAgain) {
this.connectIfDisconnected();
}
} }
// connects to the EventSource if we are not currently connected // connects to the EventSource if we are not currently connected
@ -580,12 +606,13 @@
var obj = JSON.parse(e.data); var obj = JSON.parse(e.data);
if (obj.response == "poke") { if (obj.response == "poke") {
var funcs = this.outstandingPokes.get(obj.id); var funcs = this.outstandingPokes.get(obj.id);
if (obj.hasOwnProperty("ok")) if (obj.hasOwnProperty("ok")) {
funcs["success"]() funcs["success"]()
else if (obj.hasOwnProperty("err")) } else if (obj.hasOwnProperty("err")) {
funcs["fail"](obj.err) funcs["fail"](obj.err)
else } else {
console.log("Invalid poke response: ", obj); console.log("Invalid poke response: ", obj);
}
this.outstandingPokes.delete(obj.id); this.outstandingPokes.delete(obj.id);
} else if (obj.response == "subscribe") { } else if (obj.response == "subscribe") {
@ -1307,6 +1334,27 @@
channel(subscriptions (~(del by subscriptions.channel) channel-wire)) channel(subscriptions (~(del by subscriptions.channel) channel-wire))
:: ::
$(requests t.requests) $(requests t.requests)
::
%delete
=/ session
(~(got by session.channel-state.state) channel-id)
::
=. session.channel-state.state
(~(del by session.channel-state.state) channel-id)
=. gall-moves
%+ weld
gall-moves
::
:: produce a list of moves which cancels every gall subscription
::
%+ turn ~(tap by subscriptions.session)
|= [channel-wire=path ship=@p app=term =path]
^- move
::
[duct %pass channel-wire [%g %deal [our ship] app %pull ~]]
::
$(requests t.requests)
::
== ==
:: +on-gall-response: turns a gall response into an event :: +on-gall-response: turns a gall response into an event
:: ::