feat(StatusQ): Added ModelChangeGuard utility to prevent altering models

This commit is contained in:
Michał Cieślak 2023-02-27 12:31:59 +01:00 committed by Michał
parent aff97d741e
commit bb2e03bd88
4 changed files with 36 additions and 4 deletions

View File

@ -61,15 +61,36 @@ QtObject {
] ]
readonly property ListModel permissionsModel: ListModel { readonly property ListModel permissionsModel: ListModel {
Component.onCompleted: append(permissionsModelData) readonly property ModelChangeGuard guard: ModelChangeGuard {
model: root.permissionsModel
}
Component.onCompleted: {
append(permissionsModelData)
guard.enabled = true
}
} }
readonly property var shortPermissionsModel: ListModel { readonly property var shortPermissionsModel: ListModel {
Component.onCompleted: append(shortPermissionsModelData) readonly property ModelChangeGuard guard: ModelChangeGuard {
model: root.shortPermissionsModel
}
Component.onCompleted: {
append(shortPermissionsModelData)
guard.enabled = true
}
} }
readonly property var longPermissionsModel: ListModel { readonly property var longPermissionsModel: ListModel {
Component.onCompleted: append(longPermissionsModelData) readonly property ModelChangeGuard guard: ModelChangeGuard {
model: root.longPermissionsModel
}
Component.onCompleted: {
append(longPermissionsModelData)
guard.enabled = true
}
} }
function createHoldingsModel1() { function createHoldingsModel1() {

View File

@ -0,0 +1,9 @@
import QtQml 2.15
ModelChangeTracker {
enabled: false
onRevisionChanged: {
throw new Error("The model is assumed to be immutable.")
}
}

View File

@ -1,9 +1,10 @@
import QtQml 2.14 import QtQml 2.15
QtObject { QtObject {
property var model property var model
readonly property alias revision: d.revision readonly property alias revision: d.revision
property alias enabled: d.enabled
function reset() { function reset() {
d.revision = 0 d.revision = 0

View File

@ -2,6 +2,7 @@ module StatusQ.Core.Utils
EmojiJSON 1.0 emojiList.js EmojiJSON 1.0 emojiList.js
JSONListModel 0.1 JSONListModel.qml JSONListModel 0.1 JSONListModel.qml
ModelChangeGuard 0.1 ModelChangeGuard.qml
ModelChangeTracker 0.1 ModelChangeTracker.qml ModelChangeTracker 0.1 ModelChangeTracker.qml
ModelsComparator 0.1 ModelsComparator.qml ModelsComparator 0.1 ModelsComparator.qml
XSS 1.0 xss.js XSS 1.0 xss.js