2020-09-23 19:57:54 -04:00
|
|
|
// Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
|
2019-12-19 07:46:16 -04:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2019-07-13 05:39:01 -04:00
|
|
|
import QtQuick 2.12
|
2019-06-27 02:31:03 -04:00
|
|
|
import io.thp.pyotherside 1.5
|
2019-12-27 08:58:24 -04:00
|
|
|
import CppUtils 0.1
|
2020-05-15 03:03:47 -04:00
|
|
|
import "."
|
2019-06-27 02:31:03 -04:00
|
|
|
|
|
|
|
Python {
|
|
|
|
id: py
|
2019-12-07 18:33:33 -04:00
|
|
|
|
2020-05-15 03:06:59 -04:00
|
|
|
readonly property var pendingCoroutines: Globals.pendingCoroutines
|
|
|
|
|
2019-12-06 08:44:45 -04:00
|
|
|
function setattr(obj, attr, value, callback=null) {
|
|
|
|
py.call(py.getattr(obj, "__setattr__"), [attr, value], callback)
|
|
|
|
}
|
|
|
|
|
2019-10-28 06:26:02 -04:00
|
|
|
function callCoro(name, args=[], onSuccess=null, onError=null) {
|
2020-09-28 23:06:50 -04:00
|
|
|
const uuid = name + "." + CppUtils.uuid()
|
2019-12-07 18:33:33 -04:00
|
|
|
|
2020-09-28 23:06:50 -04:00
|
|
|
Globals.pendingCoroutines[uuid] = {onSuccess, onError}
|
2020-05-06 13:39:53 -04:00
|
|
|
Globals.pendingCoroutinesChanged()
|
2019-12-07 18:33:33 -04:00
|
|
|
|
2020-09-28 23:06:50 -04:00
|
|
|
call("BRIDGE.call_backend_coro", [name, uuid, args])
|
|
|
|
return uuid
|
2019-06-27 02:31:03 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 06:26:02 -04:00
|
|
|
function callClientCoro(
|
|
|
|
accountId, name, args=[], onSuccess=null, onError=null
|
|
|
|
) {
|
2020-09-28 23:06:50 -04:00
|
|
|
const uuid = accountId + "." + name + "." + CppUtils.uuid()
|
2019-06-28 18:12:45 -04:00
|
|
|
|
2020-09-28 23:06:50 -04:00
|
|
|
Globals.pendingCoroutines[uuid] = {onSuccess, onError}
|
|
|
|
Globals.pendingCoroutinesChanged()
|
2019-12-07 18:33:33 -04:00
|
|
|
|
2020-09-28 23:06:50 -04:00
|
|
|
// Ensure the client exists or wait for it to exist
|
|
|
|
callCoro("get_client", [accountId, [name, args]], () => {
|
|
|
|
// Now that we're sure it won't error, run that client's function
|
|
|
|
call("BRIDGE.call_client_coro", [accountId, name, uuid, args])
|
2019-07-21 08:38:49 -04:00
|
|
|
})
|
2019-12-07 18:33:33 -04:00
|
|
|
|
2020-09-28 23:06:50 -04:00
|
|
|
return uuid
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancelCoro(uuid) {
|
2020-10-29 04:40:15 -04:00
|
|
|
delete Globals.pendingCoroutines[uuid]
|
2020-09-28 23:06:50 -04:00
|
|
|
call("BRIDGE.cancel_coro", [uuid])
|
2019-06-28 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
2020-10-05 03:06:07 -04:00
|
|
|
function saveConfig(backend_attribute, data) {
|
|
|
|
if (! py.ready) { return } // config not done loading yet
|
|
|
|
callCoro(backend_attribute + ".set_data", [data])
|
2019-07-24 17:26:40 -04:00
|
|
|
}
|
2020-08-03 01:19:08 -04:00
|
|
|
|
|
|
|
function showError(type, traceback, sourceIndication="", message="") {
|
|
|
|
console.error(`python: ${sourceIndication}\n${traceback}`)
|
|
|
|
|
|
|
|
if (Globals.hideErrorTypes.has(type)) {
|
|
|
|
console.info("Not showing popup for ignored error type " + type)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:15:51 -04:00
|
|
|
const popup = window.mainUI.unexpectedErrorPopup
|
|
|
|
popup.errors.unshift({type, message, traceback})
|
|
|
|
popup.errorsChanged()
|
2020-08-03 01:19:08 -04:00
|
|
|
}
|
2019-06-27 02:31:03 -04:00
|
|
|
}
|