Support token system in Qml app to reduce polling

Main window keep track centrally of all valid tokens, a Timer coupled
  with it's own instance of LibresapiLocalClient refresh periodically
  valid tokens, registerToken and isTokenValid are offered to child
  components to operate without polling
Main window coreStateCheckTimer is stopped after login
This commit is contained in:
Gioacchino Mazzurco 2017-04-03 21:19:14 +02:00
parent 286783c72e
commit 620e5225f2

View File

@ -28,6 +28,33 @@ ApplicationWindow
width: 400
height: 400
property var tokens: ({})
function registerToken(token, callback)
{
if (Array.isArray(tokens[token])) tokens[token].push(callback)
else tokens[token] = [callback]
}
function tokenExpire(token)
{
if(Array.isArray(tokens[token]))
{
var arrLen = tokens[token].length
for(var i=0; i<arrLen; ++i)
{
var tokCallback = tokens[token][i]
if (typeof tokCallback == 'function')
{
console.log("event token", token, tokCallback)
tokCallback()
}
}
}
delete tokens[token]
}
function isTokenValid(token) { return Array.isArray(tokens[token]) }
header: ToolBar
{
id: toolBar
@ -74,15 +101,14 @@ ApplicationWindow
{
text: qsTr("Trusted Nodes")
//iconSource: "qrc:/qml/icons/document-share.png"
onTriggered:
stackView.push("qrc:/qml/TrustedNodesView.qml")
onTriggered: stackView.push("qrc:/qml/TrustedNodesView.qml")
}
MenuItem
{
text: qsTr("Search Contacts")
onTriggered:
stackView.push(
"qrc:/qml/Contacts.qml", {'searching': true} )
stackView.push("qrc:/qml/Contacts.qml",
{'searching': true} )
}
MenuItem
{
@ -104,37 +130,6 @@ ApplicationWindow
event.accepted = true;
}
function checkCoreStatus()
{
function runStateCallback(par)
{
var jsonReponse = JSON.parse(par.response)
var runState = jsonReponse.data.runstate
if(typeof(runState) === 'string') stackView.state = runState
else
{
stackView.state = "core_down"
console.log("runStateCallback(...) core is down")
}
}
var ret = rsApi.request("/control/runstate/", "", runStateCallback)
if ( ret < 1 )
{
console.log("checkCoreStatus() core is down")
stackView.state = "core_down"
}
}
Timer
{
id: refreshTimer
interval: 800
repeat: true
triggeredOnStart: true
onTriggered: if(stackView.visible) stackView.checkCoreStatus()
Component.onCompleted: start()
}
state: "core_down"
states: [
State
@ -164,7 +159,8 @@ ApplicationWindow
{
script:
{
console.log("StateChangeScript waiting_account_select")
console.log("StateChangeScript running_ok")
coreStateCheckTimer.stop()
stackView.clear()
stackView.push("qrc:/qml/Contacts.qml")
}
@ -179,11 +175,79 @@ ApplicationWindow
initialItem: Rectangle
{
anchors.fill: parent
color: "green"
border.color: "black"
Text
{
text: "Connecting to core..."
anchors.centerIn: parent
}
}
}
Text { text: "Connecting to core..." }
LibresapiLocalClient
{
id: refreshTokensApi
onResponseReceived:
{
var jsonData = JSON.parse(msg).data
var arrayLength = jsonData.length;
for (var i = 0; i < arrayLength; i++)
mainWindow.tokenExpire(jsonData[i])
}
Component.onCompleted:
{
if(QT_DEBUG) debug = false
openConnection(apiSocketPath)
refreshTokensTimer.start()
}
function refreshTokens()
{
var ret = request("/statetokenservice/*",
'['+Object.keys(mainWindow.tokens)+']')
if(ret < 1) stackView.state = "core_down"
}
}
Timer
{
id: refreshTokensTimer
interval: 1500
repeat: true
triggeredOnStart: true
onTriggered: refreshTokensApi.refreshTokens()
}
Timer
{
id: coreStateCheckTimer
interval: 1000
repeat: true
triggeredOnStart: true
onTriggered:
{
var ret = rsApi.request("/control/runstate/", "", runStateCallback)
if ( ret < 1 )
{
console.log("checkCoreStatus() core is down")
stackView.state = "core_down"
}
}
Component.onCompleted: start()
function runStateCallback(par)
{
var jsonReponse = JSON.parse(par.response)
var runState = jsonReponse.data.runstate
if(typeof(runState) === 'string') stackView.state = runState
else
{
stackView.state = "core_down"
console.log("runStateCallback(...) core is down")
}
}
}
}