Verified Commit 20d90352 authored by Camil Staps's avatar Camil Staps 🚀

Let the server close windows; this allows a beforeClose task on the server

parent 7bd7b876
......@@ -34,9 +34,10 @@ from iTasks.WF.Definition import :: Task, class iTask
}
:: Connection = E.stoc ctos: Connection (Connection` stoc ctos)
:: Connection` stoc ctos =
:: Connection` stoc ctos = E.a:
{ match :: !Dynamic -> Maybe ctos
, messageHandler :: !ServerMessageHandler ctos
, beforeClose :: !Task a
}
derive JSONEncode TransportMethod
......@@ -49,6 +50,8 @@ serveElectron ::
!(String ElectronProcess UniversalConnectionDetails -> Task Connection)
!*World -> *World
closeAllWindows :: Task ()
runInElectron :: !ElectronProcess !(JSVal *JSWorld -> *JSWorld) -> Task ()
/**
......
......@@ -232,8 +232,12 @@ where
throw "failed to deserialize"
| otherwise ->
task_with_cont (sendSerializedTCPMsg processId "response" msg.extra)
"close" ->
c.beforeClose @! () >-|
set Nothing (tcpConnection processId) >-|
sendTCPMsg processId {type="close",val="",extra=[]}
_ ->
throw "invalid message type"
throw ("invalid message type "+++msg.TCPMsg.type)
connectionDetails :: !ElectronProcess -> UniversalConnectionDetails
connectionDetails processId = ConnectionDetails
......@@ -262,6 +266,13 @@ parseBaseOptions ["--port":p:rest] opts =
parseBaseOptions rest {opts & serverPort=toInt p}
parseBaseOptions cli opts = (cli,opts)
closeAllWindows :: Task ()
closeAllWindows =
upd
(\shr -> {shr & tcpTx=flip (++) [{type="close",val="",extra=[]}] <$> shr.tcpTx})
tcpShare @!
()
runInElectron :: !ElectronProcess !(JSVal *JSWorld -> *JSWorld) -> Task ()
runInElectron processId f = sendSerializedTCPMsg processId "run" [] (wrapInitFunction f)
......
......@@ -69,6 +69,10 @@ class Component {
console.log.apply (null,arguments);
}
tearDown () {
this.send ('close','');
}
onData (data) {
data=this.buffer+data.toString();
const lines=data.split ('\n');
......@@ -88,6 +92,10 @@ class Component {
case 'response':
this.abc.interpret (this.response_handlers.get (data.id),[data.val]);
break;
case 'close':
if (typeof window!='undefined')
window.close();
break;
default:
throw new Error ('unknown message type "'+data.type+'"');
}
......@@ -188,7 +196,18 @@ function connect (_opts) {
const opts=DEFAULTS;
Object.assign (opts,_opts);
return Component.instantiate (opts);
return Component.instantiate (opts).then (component => {
if (typeof window!='undefined'){
const onbeforeunload=ev => {
component.tearDown();
ev.preventDefault();
ev.returnValue='';
};
window.addEventListener ('beforeunload',onbeforeunload,{once: true});
}
return component;
});
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment