Skip to content

Commit

Permalink
chore(dapps) move responsibility of tracking sessions from WC SDK
Browse files Browse the repository at this point in the history
Updates: #14615
  • Loading branch information
stefandunca committed May 30, 2024
1 parent 0cf41a6 commit ea2b6b2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 84 deletions.
79 changes: 79 additions & 0 deletions ui/app/AppLayouts/Wallet/services/dapps/DAppsListProvider.qml
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
84 changes: 0 additions & 84 deletions ui/app/AppLayouts/Wallet/services/dapps/WalletConnectSDK.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -553,14 +477,6 @@ Item {
}
}

ListModel {
id: pairings
}

ListModel {
id: sessions
}

WebEngineLoader {
id: loader

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
1 change: 1 addition & 0 deletions ui/app/AppLayouts/Wallet/services/dapps/qmldir
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
WalletConnectSDK 1.0 WalletConnectSDK.qml
WalletConnectService 1.0 WalletConnectService.qml
DAppsListProvider 1.0 DAppsListProvider.qml

Helpers 1.0 helpers.js

0 comments on commit ea2b6b2

Please sign in to comment.