chore(dapps) move responsibility of tracking sessions from WC SDK

Updates: #14615
This commit is contained in:
Stefan 2024-05-16 20:57:37 +03:00 committed by Stefan Dunca
parent 0cf41a60cb
commit ea2b6b2ed7
4 changed files with 84 additions and 84 deletions

View File

@ -0,0 +1,79 @@
import QtQuick 2.15
import utils 1.0
QtObject {
id: root
required property WalletConnectSDK sdk
readonly property alias pairingsModel: d.pairingsModel
readonly property alias sessionsModel: d.sessionsModel
function updatePairings() {
d.resetPairingsModel()
}
function updateSessions() {
d.resetSessionsModel()
}
readonly property QtObject _d: QtObject {
id: d
property ListModel pairingsModel: ListModel {
id: pairings
}
property ListModel sessionsModel: ListModel {
id: sessions
}
function resetPairingsModel(entryCallback)
{
pairings.clear();
// We have to postpone `getPairings` call, cause otherwise:
// - the last made pairing will always have `active` prop set to false
// - expiration date won't be the correct one, but one used in session proposal
// - the list of pairings will display succesfully made pairing as inactive
Backpressure.debounce(this, 250, () => {
sdk.getPairings((pairList) => {
for (let i = 0; i < pairList.length; i++) {
pairings.append(pairList[i]);
if (entryCallback) {
entryCallback(pairList[i])
}
}
});
})();
}
function resetSessionsModel() {
sessions.clear();
Backpressure.debounce(this, 250, () => {
sdk.getActiveSessions((sessionList) => {
for (var topic of Object.keys(sessionList)) {
sessions.append(sessionList[topic]);
}
});
})();
}
function getPairingTopicFromPairingUrl(url)
{
if (!url.startsWith("wc:"))
{
return null;
}
const atIndex = url.indexOf("@");
if (atIndex < 0)
{
return null;
}
return url.slice(3, atIndex);
}
}
}

View File

@ -8,15 +8,11 @@ import QtWebChannel 1.15
import StatusQ.Core.Utils 0.1 as SQUtils
import StatusQ.Components 0.1
import utils 1.0
Item {
id: root
required property string projectId
readonly property alias sdkReady: d.sdkReady
readonly property alias pairingsModel: d.pairingsModel
readonly property alias sessionsModel: d.sessionsModel
readonly property alias webEngineLoader: loader
property alias active: loader.active
@ -108,67 +104,7 @@ Item {
id: d
property bool sdkReady: false
property ListModel pairingsModel: pairings
property ListModel sessionsModel: sessions
property WebEngineView engine: loader.instance
onSdkReadyChanged: {
if (sdkReady)
{
d.resetPairingsModel()
d.resetSessionsModel()
}
}
function resetPairingsModel(entryCallback)
{
pairings.clear();
// We have to postpone `getPairings` call, cause otherwise:
// - the last made pairing will always have `active` prop set to false
// - expiration date won't be the correct one, but one used in session proposal
// - the list of pairings will display succesfully made pairing as inactive
Backpressure.debounce(this, 250, () => {
wcCalls.getPairings((pairList) => {
for (let i = 0; i < pairList.length; i++) {
pairings.append(pairList[i]);
if (entryCallback) {
entryCallback(pairList[i])
}
}
});
})();
}
function resetSessionsModel() {
sessions.clear();
Backpressure.debounce(this, 250, () => {
wcCalls.getActiveSessions((sessionList) => {
for (var topic of Object.keys(sessionList)) {
sessions.append(sessionList[topic]);
}
});
})();
}
function getPairingTopicFromPairingUrl(url)
{
if (!url.startsWith("wc:"))
{
return null;
}
const atIndex = url.indexOf("@");
if (atIndex < 0)
{
return null;
}
return url.slice(3, atIndex);
}
}
QtObject {
@ -445,13 +381,11 @@ Item {
function onDisconnectSessionResponse(topic, error) {
console.debug(`WC WalletConnectSDK.onDisconnectSessionResponse; topic: ${topic}, error: ${error}`)
d.resetSessionsModel()
root.sessionDelete(topic, error)
}
function onDisconnectPairingResponse(topic, error) {
console.debug(`WC WalletConnectSDK.onDisconnectPairingResponse; topic: ${topic}, error: ${error}`)
d.resetPairingsModel()
}
function onBuildApprovedNamespacesResponse(approvedNamespaces, error) {
@ -461,30 +395,22 @@ Item {
function onApproveSessionResponse(session, error) {
console.debug(`WC WalletConnectSDK.onApproveSessionResponse; sessionTopic: ${JSON.stringify(session, null, 2)}, error: ${error}`)
d.resetPairingsModel()
d.resetSessionsModel()
root.approveSessionResult(session, error)
}
function onRejectSessionResponse(error) {
console.debug(`WC WalletConnectSDK.onRejectSessionResponse; error: ${error}`)
root.rejectSessionResult(error)
d.resetPairingsModel()
d.resetSessionsModel()
}
function onRespondSessionRequestResponse(error) {
console.debug(`WC WalletConnectSDK.onRespondSessionRequestResponse; error: ${error}`)
root.sessionRequestUserAnswerResult(true, error)
d.resetPairingsModel()
d.resetSessionsModel()
}
function onRejectSessionRequestResponse(error) {
console.debug(`WC WalletConnectSDK.onRejectSessionRequestResponse; error: ${error}`)
root.sessionRequestUserAnswerResult(false, error)
d.resetPairingsModel()
d.resetSessionsModel()
}
function onSessionProposal(details) {
@ -507,8 +433,6 @@ Item {
function onSessionDelete(details) {
console.debug(`WC WalletConnectSDK.onSessionDelete; details: ${JSON.stringify(details, null, 2)}`)
root.sessionDelete(details.topic, "")
d.resetPairingsModel()
d.resetSessionsModel()
}
function onSessionExpire(details) {
@ -553,14 +477,6 @@ Item {
}
}
ListModel {
id: pairings
}
ListModel {
id: sessions
}
WebEngineLoader {
id: loader

View File

@ -103,5 +103,9 @@ QtObject {
readonly property QtObject _d: QtObject {
property var currentSessionProposal: null
property var acceptedSessionProposal: null
readonly property DAppsListProvider dappsProvider: DAppsListProvider {
sdk: root.wcSDK
}
}
}

View File

@ -1,4 +1,5 @@
WalletConnectSDK 1.0 WalletConnectSDK.qml
WalletConnectService 1.0 WalletConnectService.qml
DAppsListProvider 1.0 DAppsListProvider.qml
Helpers 1.0 helpers.js